简体   繁体   English

webpack production build 总是返回 这个页面使用的是 React 的开发 build

[英]Webpack production build always returns This page is using the development build of React

I am trying to use webpack-4 to get the production build of react (The project is not created usinf Create React App), but not successful.我正在尝试使用 webpack-4 来获取 react 的生产版本(该项目不是通过使用 Create React App 创建的),但没有成功。 My project is using typescript and using ts-loader and using the version of React 15.6.2.我的项目正在使用 typescript 和 ts-loader 并使用 React 15.6.2 版本。

The current webpack config file当前的 webpack 配置文件

const path = require("path");

module.exports = {
    entry: ['./lib/tproj/js/index.ts'],
    output: {
        filename: './dist/tproj/js/bundle.js'
    },
    devtool: "source-map",
    resolve: {
        extensions: ['.webpack.js', '.web.js', '.ts', '.tsx', '.js', '.json']
    },
    module: {
        rules: [
            {
                test: /\.tsx?$/,
                exclude: [
                    /lib\/tests/,
                    /lib\/tproj\/elasticsearch/,
                    /lib\/tproj\/expressserver/
                ],
                loader: "ts-loader"
            },
            {
                test: /\.js$/,
                enforce: "pre",
                loader: "source-map-loader"
            }
        ]
    }
};

I have tried modifying it as follows still it's not working.我试过如下修改它仍然不起作用。

const path = require("path");
const webpack = require("webpack");
const TerserPlugin = require('terser-webpack-plugin');

var config = {
    entry: ['./lib/tproj/js/index.ts'],
    output: {
        filename: './dist/tproj/js/bundle.js'
    },
    resolve: {
        extensions: ['.webpack.js', '.web.js', '.ts', '.tsx', '.js', '.json']
    },
    module: {
        rules: [
            {
                 test: /\.tsx?$/,
                exclude: [
                    /lib\/tests/,
                    /lib\/tproj\/elasticsearch/,
                    /lib\/tproj\/expressserver/
                ],
                loader: "ts-loader"
            },
            {
                test: /\.css$/,
                use: ['style-loader', 'css-loader'],
            }
        ]
    },
    mode: 'production',
    optimization: {
        minimize: true,
        minimizer: [new TerserPlugin()],
        splitChunks: {
            chunks: 'all',
        }
    }

};

module.exports = (env, argv) => {

    if (argv.mode === 'development') {
        config.devtool = 'source-map';
    }

    if (argv.mode === 'production') {
        config.devtool = 'false';
    }

    return config;
}
;

Still a newbie to webpack and React.仍然是 webpack 和 React 的新手。 Thanks in advance for any help or suggestions.在此先感谢您的任何帮助或建议。

React contains switch in index.js file between dev and prod versions. React 在 index.js 文件中包含 dev 和 prod 版本之间的切换。 在此处输入图片说明

Check process.env.NODE_ENV value in your code after build.构建后检查代码中的process.env.NODE_ENV值。

In your webpack config, add this to the plugins array.在您的 webpack 配置中,将其添加到 plugins 数组中。

new webpack.DefinePlugin({
  'process.env': {
    NODE_ENV: JSON.stringify('production')
  }
})

This define the proper environment variable for react to run the production build.这定义了反应运行生产构建的适当环境变量。

var config = {
    entry: ['./lib/tproj/js/index.ts'],
    output: {
        filename: './dist/tproj/js/bundle.js'
    },
    resolve: {
        extensions: ['.webpack.js', '.web.js', '.ts', '.tsx', '.js', '.json']
    },
    module: {
        rules: [
            {
                 test: /\.tsx?$/,
                exclude: [
                    /lib\/tests/,
                    /lib\/tproj\/elasticsearch/,
                    /lib\/tproj\/expressserver/
                ],
                loader: "ts-loader"
            },
            {
                test: /\.css$/,
                use: ['style-loader', 'css-loader'],
            }
        ]
    },
    mode: 'production',
    optimization: {
        minimize: true,
        minimizer: [new TerserPlugin()],
        splitChunks: {
            chunks: 'all',
        }
    },
    plugins: [
      new webpack.DefinePlugin({
        'process.env': {
          NODE_ENV: JSON.stringify('production')
        }
      })
    ]
};

Thanks for all the suggestions and help guys, the problem was because the bundle.js file was not getting created, I had recently updated the version of webpack to 4感谢所有的建议和帮助,问题是因为没有创建 bundle.js 文件,我最近将 webpack 的版本更新为 4

So instead of所以代替

output: {
        filename: './dist/tproj/js/bundle.js'
    },

Using this worked for me使用这个对我有用

output: {
        path: path.resolve(__dirname, "./dist/tproj/js/"),
        filename: "bundle.js",
    },

确保在运行 webpack 时 bundle.js 是否被更改

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

相关问题 reactjs的生产Webpack版本-看起来您正在使用React开发版本的缩小版本 - Production webpack build of reactjs - It looks like you're using a minified copy of the development build of React React - 使用 webpack 或 CRA 捆绑生产构建 - React - bundle production build using webpack or CRA Webpack没有给出反应生产版本 - Webpack not giving react production build 为什么 React Webpack 生产版本显示空白页面? - Why is React Webpack production build showing Blank page? 为什么React app的生成版本(使用Webpack和Babel)使用错误的开发环境与HMR,这会导致错误? - Why does production build of React app (with Webpack and Babel) use wrong development env with HMR, which causes errors? 检测React / ReactDOM开发/生产构建 - Detect React/ReactDOM development/production build webpack开发和生产构建模式之间有什么区别? - What are the differences between webpack development and production build modes? React 组件未在生产构建中呈现,但在开发构建中按预期工作 - React component not rendering in production build but works as expected in development build 无法使用 webpack 在 GitHub Actions 中为生产构建 - Cannot build for production in GitHub Actions using webpack 使用 webpack 模式“生产”时,Angular @ngtools/webpack 构建失败 - Angular @ngtools/webpack build fails when using webpack mode 'production'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM