[英]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 只编译通过
require
或import
引用的文件,因此您的spec.js
文件不会包含在包中,因为它们不会在实际代码中的任何地方导入。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.