当指定 output.globalObject: "this" 时,为什么 webpack 输出全局 `self` 而不是 `this`?

Why would webpack output with global `self` instead of `this` when output.globalObject: "this" is specified?

我正在使用 webpack-cli 4.7.0。我有一个库,我想为两种不同的环境编译,一个用于网络,一个用于节点。根据 this documentation 这很容易实现,我从 webpack.config.js.

导出两个独立的、有效的 webpack 配置

所以我创建了两个有效的 webpack 配置,一个用于服务器,一个用于客户端。 Webpack 按指定输出两个单独的文件,但坚持使用“self”而不是“this”,即使我在配置中指定了 output.globalObject: "this"。如果我理解official documentation,应该不是这样。

webpack.config.js

const path = require( "path" );
// const webpack = require( "webpack" );


const serverConfig  = {
    mode: "production",
    entry: "./src/Check4.js",
    target: "node",
    output: {
        path: path.resolve( __dirname, "dist" ),
        filename: "Check4.node.js",
        globalObject: "this",
        library : {
            type: "commonjs2",
            export: "default"

        }

    }
};

const clientConfig  = {
    mode: "production",
    entry: "./src/Check4.web.js",
    target: "web",
    output: {
        path: path.resolve( __dirname, "dist" ),
        filename: "Check4.js",
        globalObject: "this",
        library : {
            name: "Check4",
            type: "window",
        }
    }
};

module.exports = [serverConfig, clientConfig];

供参考,这里是 webpack 输出的前几个字节,表明它正在使用全局“self”而不是“this”

(()=>{var e={172:e=>{self,e.exports=(()=>{"use strict";var e={502:...

答案在我的依赖项中。我写了一个 class 这个 class 所依赖的。父 class 未编译,因此 webpack 试图同时编译两者。

几个小时后,我偶然发现了这个问题,这让我解决了这个问题。 https://github.com/markdalgleish/static-site-generator-webpack-plugin/issues/79

Hey, update; After looking around a tad, it looks like for me, this error is coming from isomorphic-fetch dependency (https://github.com/matthew-andrews/iso…). Seems similar to document is not defined kind of issues people get, since this pre rendering doesn't happen in a browser context. I don't have any more time to look at it today for why that's the case or for solutions, but that's my hunch. Check your polyfills and dependencies.