当命名导入不存在时,Webpack静默失败 [英] Webpack fails silently when named import doesn't exist
问题描述
当我尝试导入命名的导入时,如果导入的名称不存在,它会静默失败.当在构建时找不到导入时,有没有办法使webpack大声失败
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
例如:
// file1.js
const var1 = 'var1'
export { var1 }
和
// file2.js
import { var2 } from './file1'
// at this point, var2 is undefined at runtime because it was never exported from file1.js
相反,我希望它在构建时失败.是否有webpack选项或其他一些我可以用来更快地捕获此错误的技术?
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?
推荐答案
较新版本的webpack具有此功能.使用版本"webpack":"^ 4.16.5"
尝试捆绑问题中的示例时,收到以下警告.这正是我想要的.
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'
这篇关于当命名导入不存在时,Webpack静默失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!