Webpack fails silently when named import doesn't exist

梦想的初衷 提交于 2021-02-07 18:32:57

问题


When I try to import a named import, it fails silently if the name import does not exist. Is there a way to get webpack to fail loudly when it cannot find the import at build time

For example:

// file1.js
const var1 = 'var1'
export { var1 }

and

// file2.js
import { var2 } from './file1'
// at this point, var2 is undefined at runtime because it was never exported from file1.js

Instead, I want it to fail at build time. Is there a webpack option or some other technique I can use to catch this error sooner?


回答1:


Newer version of webpack have this functionality. Using version "webpack": "^4.16.5" I receive the following warning when I try to bundle the example from the question. This is exactly what I was looking for.

$ npx webpack
Hash: cd3bacb4d03dd9e2e456
Version: webpack 4.16.5
Time: 377ms
Built at: 08/07/2018 9:38:12 AM
  Asset       Size  Chunks             Chunk Names
main.js  971 bytes       0  [emitted]  main
Entrypoint main = main.js
[0] ./src/index.js + 1 modules 201 bytes {0} [built]
    | ./src/index.js 153 bytes [built]
    | ./src/file1.js 48 bytes [built]

WARNING in ./src/index.js 3:12-16
"export 'var2' was not found in './file1'


来源:https://stackoverflow.com/questions/40071942/webpack-fails-silently-when-named-import-doesnt-exist

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