javascript Webpack 4 通用库目标

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49111086/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 08:25:39  来源:igfitidea点击:

Webpack 4 universal library target

javascriptwebpackbundlerumdwebpack-4

提问by JeB

According to the Webpack 4 documentation, if I specify libraryTarget: 'umd'it should result in the following output:

根据Webpack 4 文档,如果我指定libraryTarget: 'umd'它应该导致以下输出:

(function webpackUniversalModuleDefinition(root, factory) {
  if(typeof exports === 'object' && typeof module === 'object')
    module.exports = factory();
  else if(typeof define === 'function' && define.amd)
    define([], factory);
  else if(typeof exports === 'object')
    exports["MyLibrary"] = factory();
  else
    root["MyLibrary"] = factory();
})(typeof self !== 'undefined' ? self : this, function() {
  return _entry_return_;
});

However, what I get is:

但是,我得到的是:

(function webpackUniversalModuleDefinition(root, factory) {
    if(typeof exports === 'object' && typeof module === 'object')
        module.exports = factory();
    else if(typeof define === 'function' && define.amd)
        define("lib", [], factory);
    else if(typeof exports === 'object')
        exports["lib"] = factory();
    else
        root["lib"] = factory();
})(window, function() {
return

To be more precise, instead of this
(typeof self !== 'undefined' ? self : this, function()
I get this:
(window, function()

更准确地说,
(typeof self !== 'undefined' ? self : this, function()
我得到的不是这个:
(window, function()

This (obviously) causes runtime error window is undefinedwhen importing in node environment.

这(显然)window is undefined在节点环境中导入时会导致运行时错误。

To be clear:
I know that windowdoesn't exist in node applications. My question is not about this, but rather about webpack.

需要明确的是:
我知道window节点应用程序中不存在。我的问题不是关于这个,而是关于webpack

Is it a bug or am I missing something?

这是一个错误还是我错过了什么?

My output config:

我的输出配置:

output: {
    path: resolve(__dirname, 'umd'),
    filename: 'lib.js',
    libraryTarget: 'umd',
    library: 'lib',
    umdNamedDefine: true,
},

回答by JeB

This would be a bugin Webpack 4.
Webpack 3produces a proper bundle.

这将是一个错误的WebPack 4
Webpack 3生成了一个合适的包。

This issue should be fixed with thisfeature, until it's done the suggested workaround is using globalObject:

此问题应使用功能修复,直到完成建议的解决方法使用globalObject

output: {
    path: resolve(__dirname, 'umd'),
    filename: 'lib.js',
    libraryTarget: 'umd',
    library: 'lib',
    umdNamedDefine: true,
    globalObject: `(typeof self !== 'undefined' ? self : this)`
},

回答by v.babak

According to the docsinclude output.globalObjectinto webpack.config.js:

根据包含到webpack.config.js文档output.globalObject

module.exports = {
  output: {
    libraryTarget: 'umd',
    globalObject: 'this'
  }
}

To make UMD build available on both browsers and Node.js, set output.globalObject option to 'this'.

要使 UMD 构建在浏览器和 Node.js 上都可用,请将 output.globalObject 选项设置为“this”。