简体   繁体   English

使用 webpack 将库作为 ES6 模块输出?

[英]Outputting Library as ES6 module with webpack?

I'm trying to bundle a library right now and Webpack doesn't seem to have an option for outputting as an ES6 module with output.libraryTarget .我现在正在尝试捆绑一个库,而 Webpack 似乎没有使用output.libraryTarget作为 ES6 模块输出的选项。 I see literally every option but ES6 listed in the documentation.除了文档中列出的 ES6,我几乎看到了所有选项。

What's going on here?这里发生了什么? Am I misunderstanding how webpack works?我是否误解了 webpack 的工作原理?

It's not implemented yet.它尚未实施。 See this issue .看到这个问题

You can use Rollup instead, but it expects all modules to be ESM.您可以改用 Rollup,但它希望所有模块都是 ESM。

With the latest Webpack V5 (as of September 2022), you can do this:使用最新的 Webpack V5(截至 2022 年 9 月),您可以这样做:

module.exports = {
  entry: ["./src/index.ts"],


  experiments: {
    outputModule: true,
  },

  output: {
    path: `${__dirname}/dist/myOutputFile.mjs`,
    library: {
      type: "module",
    },
  },
};

I also set devtool: false and used externals to keep things like React out of the bundle.我还设置了 devtool: false 并使用外部组件将 React 之类的东西排除在包之外。

See https://github.com/webpack/webpack/issues/2933 (as mentioned in the other response) and https://webpack.js.org/configuration/output/#outputlibrarytype for more details.有关更多详细信息,请参阅https://github.com/webpack/webpack/issues/2933 (如其他响应中所述)和https://webpack.js.org/configuration/output/#outputlibrarytype

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM