Webpack 4 - Change a module's type in a loader

天涯浪子 提交于 2020-03-04 04:49:24

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!