开发一组将在浏览器上下文中运行的脚本,其中某些模块(例如下划线)将作为全局模块使用。但是,我依赖于 node_modules
中直接 require
/import
下划线的模块。是否可以将 WebPack 配置为在编译这些文件时依赖于全局下划线实例,而不是在我编译的脚本中复制该库?
最佳答案
您要查找的是Externals :
externals
configuration in webpack provides a way of not including a dependency in the bundle. Instead the created bundle relies on that dependency to be present in the consumers environment. This typically applies to library developers though application developers can make good use of this feature too.
这甚至适用于 node_modules
中的模块,因为 webpack 会遍历整个依赖关系树来找出要包含在生成的包中的内容。
甚至还有一个 example这是专门针对您的用例的,它看起来像:
externals : {
lodash : {
commonjs: "lodash",
amd: "lodash",
root: "_" // indicates global variable
}
}
This syntax is used to describe all the possible ways that an external library can be available. lodash here is available as lodash under AMD and CommonJS module systems but available as _ in a global variable form.
关于javascript - 网络包2 : Replaced require'd module with global,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40870468/