gpt4 book ai didi

javascript - 当命名导入不存在时,Webpack 静默失败

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:40:53 25 4
gpt4 key购买 nike

当我尝试导入命名导入时,如果名称导入不存在,它会自动失败。有没有办法让 webpack 在构建时找不到导入时大声失败

例如:

// 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 选项或其他一些技术来更快地捕获此错误吗?

最佳答案

较新版本的 webpack 具有此功能。当我尝试从问题中捆绑示例时,使用 "webpack": "^4.16.5" 版本收到以下警告。这正是我要找的。

$ 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'

关于javascript - 当命名导入不存在时,Webpack 静默失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40071942/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com