1

I see the script-loader has one line write

"require(" + JSON.stringify("!!" + path.join(__dirname, "addScript.js")) + ")"

Who can tell me why use the !! here? Thanks in advance.

wuxiandiejia
  • 851
  • 1
  • 10
  • 15

4 Answers4

2

I don't really want to post an "answer" because I don't know for sure... however, ! is a standard webpack loader separator and that call will return something like:

"!!/var/www/project/addScript.js" 

It was not present in the project's initial commits, so it stands to reason that this is a workaround that was added to be compatible with newer versions of webpack (0.9 to be exact). You can see that the first exclamation was added here for webpack 0.9 compatibility. The second one was added without much explanation (commit "Don't crash in environments without window")

Rob M.
  • 35,491
  • 6
  • 51
  • 50
1

It just converts whatever path.join() returns to bool.

Barak
  • 535
  • 6
  • 18
0

I found the answer in the NormalModuleFactory.js in the webpack folder. In line 70:

var noPrePostAutoLoaders = /^!!/.test(request);

When I use require('./file'),the preLoader has effect on the file, and when I use require('!!./file'), the preLoader has no effect on the file.So, I think !! prefix can prevent the preLoader to affect the file or let webpack ignore the preLoader.

I found the doc in the webpack 1.x website https://webpack.github.io/docs/loaders.html#loader-order

wuxiandiejia
  • 851
  • 1
  • 10
  • 15
0

One ! will ignore all post loaders.

Two !! will ignore pre and post loaders.

So if you have no ! or only 1 ! prefix, the input to your loader may not be the actual file because It might have been processed by another loader from your configuration.

Steven Spungin
  • 27,002
  • 5
  • 88
  • 78