Webpack 4通用库目标 [英] Webpack 4 universal library target

查看:130
本文介绍了Webpack 4通用库目标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

(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_;
});

但是,我得到的是:

(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

而不是这个

(typeof self!=='undefined'?self:this,function()

我得到了这个:

(窗口,function()

(显然)这会导致运行时错误<

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

要明确:
在节点环境中导入时,code>窗口未定义。
我知道窗口不存在应用程序。我的问题不是这个问题,而是 webpack

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

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

Is it a bug or am I missing something?

我的输出配置:

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


推荐答案

这将是错误 Webpack 4 中。

Webpack 3 产生适当的捆绑包。

This would be a bug in Webpack 4.
Webpack 3 produces a proper bundle.

此问题应通过功能,直到完成为止,建议的解决方法是使用 globalObject

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

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

这篇关于Webpack 4通用库目标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆