javascript - 网络包2 : Replaced require'd module with global

标签 javascript webpack webpack-2

开发一组将在浏览器上下文中运行的脚本,其中某些模块(例如下划线)将作为全局模块使用。但是,我依赖于 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/

相关文章:

webpack 开发服务器不构建和重新加载 index.html

javascript - Ajax 自动完成不返回正确的值

javascript - Javascript 中 "replace"的不同行为

javascript - 使用 webpack 将多个 ES6 类捆绑到一个文件中,以便在脚本标签中导入

javascript - 将 SVG 作为 React 组件导入不适用于 Next.js

javascript - 使用 webpack v2.2 时,webpack-dev-server 是否已弃用 "--content-base"?

javascript - 执行 && 和 ||运算符将其操作数转换为 bool 值?

隐式全局上的 JavaScript 未定义错误

javascript - 创建纯 css javascript 库/包的最佳实践是什么

javascript - 使用 webpack2 最小化包