简体   繁体   English

如何从捆绑 .spec.js 文件中排除 webpack

[英]How to exclude webpack from bundling .spec.js files

my Package.bundle reads我的 Package.bundle 读取

var reqContext = require.context('./', true, /\.js$/);

reqContext.keys().map(reqContext);

Which basically includes all .js files.其中基本上包括所有 .js 文件。

I want the expression to exclude any ***.spec.js files .我希望表达式排除任何 ***.spec.js 文件。 Any regexp here to exclude .spec.js files ?这里有任何正则表达式来排除 .spec.js 文件吗?

Since /\\.js$/ allows all .js files (as it basically matches .js at the end of the string), and you need to allow all .js files with no .spec before them, you need a regex with a negative lookahead:由于/\\.js$/允许所有.js文件(因为它基本上匹配字符串末尾的.js ),并且您需要允许所有.js文件前面没有.spec ,因此您需要一个带有负数的正则表达式前瞻:

/^(?!.*\.spec\.js$).*\.js$/

See this regex demo看到这个正则表达式演示

Details :详情

  • ^ - start of string ^ - 字符串的开始
  • (?!.*\\.spec\\.js$) - the line cannot end with .spec.js , if it does, no match will occur (?!.*\\.spec\\.js$) - 该行不能以.spec.js ,如果是,则不会发生匹配
  • .* - any 0+ chars other than linebreak symbols .* - 除换行符以外的任何 0+ 个字符
  • \\.js - .js sequence \\.js - .js序列
  • $ - end of the string. $ - 字符串的结尾。

Although the accepted answer is technically correct, you shouldn't need to add this regex to your webpack configuration.尽管接受的答案在技术上是正确的,但您不需要将此正则表达式添加到您的 webpack 配置中。

This is because webpack only compiles the files that are referenced via require or import and so your spec.js files won't be included in the bundle as they are not imported anywhere in the actual code.这是因为 webpack 只编译通过requireimport引用的文件,因此您的spec.js文件不会包含在包中,因为它们不会在实际代码中的任何地方导入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Jest 方法在 .spec.js 文件中不可用 - Jest methods not available in .spec.js files 赛普拉斯运行规范选项不适用于 .spec.js 文件 - Cypress run spec option not working with .spec.js files 使WebStorm在“选择声明”下拉列表中忽略.spec.js文件 - Make WebStorm ignore .spec.js files for “Choose Declaration” dropdown 在1个Spec.js文件中测试2个或更多JavaScript文件? - Testing 2 or more JavaScript files in 1 Spec.js file? ReactJS:未定义错误:如何在 spec.js 测试文件中从 authContext 定义 isAuthorized() 和 logonID? - ReactJS: Undefined Error: How to define isAuthorized() and logonID from authContext in spec.js test file? 如何在Protractorjs Spec.js文件中实现IF语句? - How to implement IF statement in Protractorjs Spec.js file? 强制业力从磁盘上获取更新的spec.js文件 - Force Karma to take updated spec.js file from disk Webpack捆绑简单的JS文件 - Webpack bundling simple JS files 这种模式是如何工作的:'test / e2e / ** / * .spec.js'? - How does this pattern work: 'test/e2e/**/*.spec.js'? 在测试用例中编写量角器spec.js文件,从浏览器中获取会话值 - Getting the session values from the browser in test case writing protractor spec.js file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM