Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to exclude webpack from bundling .spec.js files

my Package.bundle reads

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

reqContext.keys().map(reqContext);

Which basically includes all .js files.

I want the expression to exclude any ***.spec.js files . Any regexp here to exclude .spec.js files ?

like image 500
looneytunes Avatar asked Sep 28 '16 18:09

looneytunes


2 Answers

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:

/^(?!.*\.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
  • .* - any 0+ chars other than linebreak symbols
  • \.js - .js sequence
  • $ - end of the string.
like image 177
Wiktor Stribiżew Avatar answered Oct 16 '22 19:10

Wiktor Stribiżew


Although the accepted answer is technically correct, you shouldn't need to add this regex to your webpack configuration.

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.

like image 45
cdimitroulas Avatar answered Oct 16 '22 18:10

cdimitroulas