【问题标题】:How Webpack bundle in production mode?Webpack 如何在生产模式下捆绑?
【发布时间】:2017-09-27 06:05:06
【问题描述】:

我正在尝试通过 Gulp 将我的 React 应用程序与 Webpack 捆绑在一起进行开发。捆绑后,在 Chrome 中,“React 开发工具”工具栏是红色的,并告诉我“此页面正在使用 React 的开发版本。”。如何使用生产版本进行捆绑?

来自 package.json:

"gulp": "^3.9.1",
"gulp-webpack": "^1.5.0",
"react": "^15.6.1",
"uglifyjs-webpack-plugin": "^0.4.6",
"webpack": "^2.6.1"

这里是 gulp 命令:

var gulp = require('gulp');
var webpack = require('gulp-webpack');
var webpackConfig = require('./webpack.config.js');

gulp.task('webpack', function(){
  return gulp.src('public_html/app/back/js/all.jsx')
    .pipe(webpack(webpackConfig))
    .pipe(gulp.dest('dist/app/back/js'));
});

这是我的 webpack.config.js:

const webpack = require('webpack');

module.exports = {
  entry: './public_html/app/back/js/all.jsx',
  output: {
    path: `${__dirname}dist/app/back/js`,
    filename: 'bundle.js'
  },
  module: {
    loaders: [
      {
        test: /\.jsx?$/,
        loader: 'babel',
        exclude: /node_modules/,
        query: {
          presets: ['react', 'es2015']
        }
      },
      {
        test: /\.scss$/,
        exclude: /node_modules/,
        loader: 'style-loader!css-loader!sass-loader'
      },
      {
        test: /\.css/,
        loader: 'style-loader!css-loader'
      }
    ],
  },
  plugins: [
    new webpack.DefinePlugin({
      'process.env': {
        'NODE_ENV': JSON.stringify('production')
      }
    })
  ],
};

谢谢。

【问题讨论】:

    标签: reactjs webpack gulp


    【解决方案1】:

    在您的 WebPack 代码的 plugins 数组中,您需要添加以下内容

    new webpack.DefinePlugin({ 'process.env.NODE_ENV': JSON.stringify('production') }),
    new webpack.optimize.UglifyJsPlugin({
      beautify: false,
      mangle: {
        screw_ie8: true,
        keep_fnames: true
      },
      compress: {
        screw_ie8: true,
      },
      comments: false,
    }),
    

    【讨论】:

    • 谢谢,但没用。 React 工具栏仍然是红色的,显示相同的消息。控制台说:Warning: It looks like you're using a minified copy of the development build of React. When deploying React apps to production, make sure to use the production build which skips development warnings and is faster. See _facebook-link_ for more details.。随着 gulp 输出的变化(“条件始终为假”、“丢弃无法访问的代码”等),它发生了一些变化。还有其他建议吗?
    • 你在 webpack 脚本中使用-p 标志吗?
    • 我正在通过 gulp 运行 webpack(不是 npm)...我将-p 放在哪里?
    • 在你的 npm scripts "client-development": "node_modules/.bin/webpack --config webpack.dev.config.js --colors -p", 中有这样的东西
    • 正如我在上一条评论中提到的,我没有使用npm,我使用的是gulp。我的gulpfile.js 和webpack.config.js 包含在上面...不确定我是否可以在某处添加该标志?谷歌搜索没有帮助。
    猜你喜欢
    • 2020-09-20
    • 2020-06-29
    • 2016-11-19
    • 2019-10-09
    • 2019-05-01
    • 2022-01-04
    • 1970-01-01
    • 1970-01-01
    • 2019-11-18
    相关资源
    最近更新 更多