是否将外部库捆绑在webpack中?

问题描述 投票:0回答:1

我正在尝试配置Webpack,以便可以导入库,但它们不会与我的代码捆绑在一起,而是从链接到html文件的CDN提供。我在博客文章中了解了此实现,但忘记了如何执行。

这是一个基于subject-js库的小项目。

webpack.config.js

module.exports = {
  mode: "production",
  entry: "./src/index",
  target: "web",
  output: {
    path: path.resolve(__dirname, "dist"),
    filename: "bundle.js"
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        use: ["babel-loader"]
      },
      {
        test: /\.html$/,
        use: ["html-loader"]
      }
    ]
  },
  plugins: [
    new HtmlWebpackPlugin({
      filename: "index.html",
      template: path.join(__dirname, "./src/index.html"),
      scriptLoading: "defer",
      inject: "body"
    })
  ],
  devServer: {
    contentBase: path.join(__dirname, "dist"),
    compress: false,
    port: 3000,
    hot: true,
    open: true
  }
};

index.html]

<body>
    <!-- Matter JS CDN-->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/matter-js/0.14.2/matter.min.js"></script>

    <!-- The bundle will be injected here-->
  </body>
javascript html webpack bundler
1个回答
0
投票

我已经找到解决方法。

[物质-js属性是指节点模块中的库,而值是指您要从捆绑中排除的全局对象。

module.exports = {
  //...
  externals: {
    "matter-js": "Matter"
  },
  //...
};

© www.soinside.com 2019 - 2024. All rights reserved.