【问题标题】:create-react-app - How to bundle JS filescreate-react-app - 如何捆绑 JS 文件
【发布时间】:2019-10-31 09:15:38
【问题描述】:

我使用 create-react-app 创建了一个 React 应用。 如果我运行npm run build,create-react-app 会在 build/static/js 文件夹中生成 3 个不同的 JS 文件。

但是为了这个特定应用程序的目的,我需要将所有这 3 个 JS 文件捆绑在一个文件中。

这是我可以通过 create-react-app 实现的吗? 如果不是,将这 3 个 JS 文件捆绑到一个文件中的最佳方法是什么?

【问题讨论】:

标签: reactjs


【解决方案1】:

您可以在项目的根目录中构建自己的 webpack.config.js,如下所示:

const path = require('path');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const glob = require('glob');

module.exports = {
  entry: {
    "bundle.js": glob.sync("build/static/?(js|css)/main.*.?(js|css)").map(f => path.resolve(__dirname, f)),
  },
  output: {
    filename: "build/static/js/bundle.min.js",
  },
  module: {
    rules: [
      {
        test: /\.css$/,
        use: ["style-loader", "css-loader"],
      },
    ],
  },
  plugins: [new UglifyJsPlugin()],
}

然后你可以调整脚本以在 package.json 中构建:

"build": "npm run build:react && npm run build:bundle",
"build:bundle": "webpack --config webpack.config.js",

【讨论】:

    【解决方案2】:

    尝试使用 webpack 自定义构建、最小化和更多可用功能

    请关闭此链接https://www.npmjs.com/package/webpack, 文档:https://webpack.js.org/concepts/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-08
      • 2018-02-07
      • 2020-06-21
      • 1970-01-01
      • 1970-01-01
      • 2018-11-07
      • 1970-01-01
      • 2021-12-03
      相关资源
      最近更新 更多