问题
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