Node.js使用依赖数组时的Webpack错误

huangapple go评论123阅读模式
英文:

Node.Js WebPack Error when Using Dependency Array

问题

我在尝试弄清楚为什么在使用多个依赖项时,dependOn WebPack 属性失败时遇到困难。

如果我的入口点是:

./src/EntryA.js

let LibraryA = require("./library/LibraryA.js");
let LibraryB = require("./library/LibraryB.js");

class EntryA {
    constructor() {
        console.log("EntryA");
        let libraryA = new LibraryA();
        let libraryB = new LibraryB();
    }
}

module.exports = EntryA;

其两个依赖项分别是:

./src/library/LibraryA.js

class LibraryA {
    constructor() { console.log("LibraryA"); }
}

module.exports = LibraryA;

./src/library/LibraryB.js

class LibraryB {
    constructor() { console.log("LibraryB"); }
}

module.exports = LibraryB;

然后,使用以下配置,WebPack 似乎没有问题地创建捆绑包:

module.exports = {
  mode: "production",
  entry: {
    EntryA: {
        import: "./src/EntryA.js"
    }
  },
  output: {
    filename: "[name].min.js",
    path: path.resolve(__dirname, "bundle"),
    libraryTarget: "var",
    library: "[name]",
  },
  target: "web",
  module: {
    rules: [
      {
        test: /\.(js)$/,
        exclude: /node_modules/,
        use: {
          loader: "babel-loader",
          options: { presets: ["@babel/preset-env"] }
        }
      }
    ]
  }
};

这是没问题的,但是当单个捆绑包变得太大,我想拆分依赖项时,我遇到了问题,就像使用以下更新的配置一样:

module.exports = {
  mode: "production",
  entry: {
    EntryA: {
        import: "./src/EntryA.js",
        dependOn: ["LibraryA", "LibraryB"]
    },
    LibraryA: "./src/library/LibraryA.js",
    LibraryB: "./src/library/LibraryB.js"
  },
  output: {
    filename: "[name].min.js",
    path: path.resolve(__dirname, "bundle"),
    libraryTarget: "var",
    library: "[name]",
  },
  target: "web",
  module: {
    rules: [
      {
        test: /\.(js)$/,
        exclude: /node_modules/,
        use: {
          loader: "babel-loader",
          options: { presets: ["@babel/preset-env"] }
        }
      }
    ]
  }
};

看起来似乎正常工作,因为我仍然得到了我期望的三个捆绑包:

./bundle/LibraryA.min.js
./bundle/LibraryB.min.js
./bundle/EntryA.min.js

但是尝试在 HTML 文件中运行它们不再起作用(在不拆分依赖项的情况下可以工作):

<html>
  <head>
    <script src="./bundle/LibraryA.min.js"></script>
    <script src="./bundle/LibraryB.min.js"></script>
    <script src="./bundle/EntryA.min.js"></script>
  </head>
  <body>
    <script>
      window.onload = () => { const entryA = new EntryA(); }
    </script>
  </body>
</html>

出现以下错误:

Uncaught TypeError: o[r] is not a function
Uncaught TypeError: EntryA is not a constructor
英文:

I'm having difficulty trying to figure out why the dependOn WebPack property is failing when using multiple dependencies.

If I have an entry point as :

./src/EntryA.js

let LibraryA = require(&quot;./library/LibraryA.js&quot;);
let LibraryB = require(&quot;./library/LibraryB.js&quot;);

class EntryA {
    constructor() {
        console.log(&quot;EntryA&quot;);
        let libraryA = new LibraryA();
        let libraryB = new LibraryB();
    }
}

module.exports = EntryA;

With it's two dependencies as :

./src/library/LibraryA.js

class LibraryA {
    constructor() { console.log(&quot;LibraryA&quot;); }
}

module.exports = LibraryA;

./src/library/LibraryB.js

class LibraryB {
    constructor() { console.log(&quot;LibraryB&quot;); }
}

module.exports = LibraryB;

Then WebPack seems to have no problem creating the bundle with this config :

module.exports = {
  mode: &quot;production&quot;,
  entry: {
    EntryA: {
        import: &quot;./src/EntryA.js&quot;
    }
  },
  output: {
    filename: &quot;[name].min.js&quot;,
    path: path.resolve(__dirname, &quot;bundle&quot;),
    libraryTarget: &quot;var&quot;,
    library: &quot;[name]&quot;,
  },
  target: &quot;web&quot;,
  module: {
    rules: [
      {
        test: /\.(js)$/,
        exclude: /node_modules/,
        use: {
          loader: &quot;babel-loader&quot;,
          options: { presets: [&quot;@babel/preset-env&quot;] }
        }
      }
    ]
  }
};

This is fine but when the single bundle becomes too big and I want to split out the dependencies I run into problems, like with this updated config :

module.exports = {
  mode: &quot;production&quot;,
  entry: {
    EntryA: {
        import: &quot;./src/EntryA.js&quot;,
        dependOn: [&quot;LibraryA&quot;, &quot;LibraryB&quot;]
    },
    LibraryA: &quot;./src/library/LibraryA.js&quot;,
    LibraryB: &quot;./src/library/LibraryB.js&quot;
  },
  output: {
    filename: &quot;[name].min.js&quot;,
    path: path.resolve(__dirname, &quot;bundle&quot;),
    libraryTarget: &quot;var&quot;,
    library: &quot;[name]&quot;,
  },
  target: &quot;web&quot;,
  module: {
    rules: [
      {
        test: /\.(js)$/,
        exclude: /node_modules/,
        use: {
          loader: &quot;babel-loader&quot;,
          options: { presets: [&quot;@babel/preset-env&quot;] }
        }
      }
    ]
  }
};

It looks like it's working because I still get the three bundles I am expecting

./bundle/LibraryA.min.js
./bundle/LibraryB.min.js
./bundle/EntryA.min.js

But trying to run them in the HTML file no longer works (without splitting the dependencies it did work) :

&lt;html&gt;
  &lt;head&gt;
    &lt;script src=&quot;./bundle/LibraryA.min.js&quot;&gt;&lt;/script&gt;
    &lt;script src=&quot;./bundle/LibraryB.min.js&quot;&gt;&lt;/script&gt;
    &lt;script src=&quot;./bundle/EntryA.min.js&quot;&gt;&lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;script&gt;
      window.onload = () =&gt; { const entryA = new EntryA(); }
    &lt;/script&gt;
  &lt;/body&gt;
&lt;/html&gt;

With the errors :

Uncaught TypeError: o[r] is not a function
Uncaught TypeError: EntryA is not a constructor

答案1

得分: 0

以下是翻译好的部分:

唯一使这个工作的方式是将所有模块都改为使用 import 而不是 require,然后这三个捆绑似乎能够良好地协同工作。

我不完全知道为什么这样做能修复它,但依赖似乎不喜欢使用 require

英文:

The only way I was able to get this to work was to change all of the modules to use import instead of require and then the three bundles seem to play nice with each other.

I don't fully know why this fixes it but the dependencies seem to not like using require

huangapple
  • 本文由 发表于 2023年7月20日 19:46:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76729526.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定