问题
Webpack 4 introduces module types, allowing better handling of JSON, CSS, ES6 modules, etc.
Some loaders, like messageformat-loader, take in source of one type (in this case JSON), but output a different type (in this case JS). This currently breaks in Webpack 4.
ERROR in ./src/messages.json
Module parse failed: Unexpected token v in JSON at position 0
You may need an appropriate loader to handle this file type.
SyntaxError: Unexpected token v in JSON at position 0
at JSON.parse (<anonymous>)
How can a loader notify webpack of a module type change?
I tried this._module.type = 'javascript/auto'
in my loader but it had no effect.
As a workaround, the end user can add type: 'javascript/auto'
to the webpack config for the loader, but this seems like a leak of implementation details. Plus, it wouldn't work in the case where the user wants to pipe the output of this loader into another that's expecting JS.
Any loader that changes between source types will be affected, like val-loader, to-string-loader, apply-loader, etc. I think even babel-loader and the like will be affected since they convert from ES6 modules to ES5 code, which are now different module types.
来源:https://stackoverflow.com/questions/49395913/webpack-4-change-a-modules-type-in-a-loader