Exclude files from require.context webpack

I am trying to include in require.contextWebpack all the files that should be covered by my reporter from Istanbul.

I would like to include / require all files under appthat have no extension .test.js.

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

The structure of my files:

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

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

What am I doing wrong?

+8
source share
3 answers

You should know after what part the negative lookahead uses this deviation. If you do this right after the first slash, it works great.

.*test test .

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

, internals .
test.js:

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

filter?

:

var paths = [
'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',
'app/blablabla.test.js'
];

var result = paths.filter(function(e,i){
   return (e.startsWith('app/') && !e.endsWith('.test.js'))
});

for(var i = 0; i < result.length; i++)
   console.log(result[i]);
  
Hide result
+2

node_modules require.context, webpack , . .

, node_modules . node_modules wepback module.resolve. .

0
source

All Articles