在create-react-app应用程序中构建bundle.js

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

进行项目。现在我们需要以bundle.js的形式收集所有内容,而不是以create-react-app的方式收集。

执行eject命令,出现配置。但是我如何获得bundle.js

将项目中的所有内容(样式,图片等)收集到一个文件中非常重要。

##我的解决方案

webpack.config.js

const path = require("path");
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const HtmlWebpackPlugin = require("html-webpack-plugin");

module.exports = {
    context: __dirname,
    entry: "./src/index.js",
    output: {
        path: path.join(__dirname, "/dist"),
        publicPath: '',
        filename: "widget.js"
    },
    module: {
        rules: [
            {
                test: /\.js$/,
                exclude: /node_modules/,
                use: ["babel-loader"]
            },
            {
                test: /\.css$/,
                use: ["style-loader", "css-loader"]
            },
            {
                test: /\.(gif|png|jpe?g|svg|woff(2)?|ttf|eot)$/i,
                use: [
                    {
                        loader: 'file-loader',
                        options: {
                            publicPath: 'img',
                            outputPath: 'img',
                            name: '[name].[ext]',
                        },
                    },
                ],
            }
        ]
    },
    optimization: {
        minimizer: [new UglifyJsPlugin({
            uglifyOptions: {
                warnings: false,
                parse: {},
                compress: {},
                mangle: true,
                output: {
                    comments: false,
                },
                toplevel: false,
                nameCache: null,
                ie8: true,
                keep_fnames: false,
            },
        })],
    },
    plugins: [
        new HtmlWebpackPlugin({
            template: "./public/index.html"
        })
    ]
};
reactjs webpack build create-react-app
1个回答
0
投票

弹出是一个很糟糕的解决方案,请改用重新接线npm install rewire并添加自定义的构建脚本。

请在此处查看我的答案How to build a production version of React without minification?

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