从Webpack的require.context中排除文件 [英] Exclude files from require.context of Webpack

查看:502
本文介绍了从Webpack的require.context中排除文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Webpack的 require.context 中包含伊斯坦布尔记者应该涵盖的所有文件。

I'm trying to include in with require.context of Webpack all the files that should be covered by my Istanbul reporter.

我想包含/要求 app 下没有 .test.js extension。

I would like to include/require all the files under app that have not the .test.js extension.

// internals/testing/test-bundler.js
const context = require.context('../../app', true, /^.*(?!(\.test|internals.*))\.js$/);
context.keys().forEach(context);

我的文件结构是:

app/components/
app/containers/
app/decorators/
app/orm/
app/pages/
app/store/
app/tests/
app/utils/
app/app.js
app/reducers.js
app/routes.js
internals/testing/test-bundler.js

显然我的正则表达式不起作用,因为在报道报告中我查看所有 .test.js 文件,甚至 internals / testing / test-bundler.js 文件。

Obviously my regex doesn't work because inside the coverage report I see all the .test.js files and even the internals/testing/test-bundler.js file.

我做错了什么?

推荐答案

你需要注意一下负面的前瞻部分采用了哪种方式拒绝。如果你在第一个正斜杠之后立即执行它可以正常工作。

You need to be aware after what part the negative lookahead employs it's rejection. If you do it right after the first forward slash it works fine.

并且你要拒绝。* test 斜杠之后,而不是直接在它后面的 test

And with that you want to reject .*test after the slash, instead of just test directly behind it.

/^(?!internals).*\/(?!.*test).*\.js$/

或更具体地说,路径名中不允许 internals

也不以<$结尾c $ c> test.js

Or more specific not allowing internals in the path name.
Nor ending with test.js:

^(?!.*(?:internals|test.js$)).*\.js$

这篇关于从Webpack的require.context中排除文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆