Generate single physical javascript file using create-react-app

前端 未结 3 1096
眼角桃花
眼角桃花 2021-01-04 06:06

This is perhaps a newbie question. I have created a small reactjs app using create-react-app and I see that the bundle.js file is being served from http://localhost:3000/sta

3条回答
  •  日久生厌
    2021-01-04 06:34

    Yes it can be possible by below webpack configuration:

    create file webpack.config.js in your root directory

    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()],
        }
    

    In package.json

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

    Reference from here

提交回复
热议问题