配置Webpack生产版本

时间:2018-10-31 11:15:43

标签: reactjs webpack

我需要在Webpack中使用生产版本。如果我现在访问我的网站,则生产图标中的背景为红色,这表示它没有在生产中使用

当我执行npm运行构建时,它说 npm ERR!缺少脚本:构建

如何调整Webpack,以便进行生产构建并加快我的应用程序的速度?

这是我的webpack配置:

 const webpack = require('webpack');

const config = {
  entry: ['babel-polyfill', './src/index.js'],
  output: {
    filename: 'bundle.js',
  },
  devServer: {
    inline: true,
    port: 8080,
    historyApiFallback: true,
  },
  plugins: [
    new webpack.DefinePlugin({
      'process.env': {
        API_KEY: 'API_KEY',
        GOOGLE_MAPS_KEY: 'GOOGLE_MAPS_KEY',
        GOOGLE_GEOLOCATION_KEY: 'GOOGLE_GEOLOCATION_KEY',
      },
    }),
  ],
  module: {
    loaders: [
      {
        test: /\.jsx?$/,
        exclude: /node_modules/,
        loader: 'babel-loader',
        query: {
          presets: ['es2015', 'react'],
        },
      },
      {
        test: /\.css$/,
        loader: 'style!css',
      },
    ],
  },
};

module.exports = config;

1 个答案:

答案 0 :(得分:1)

 plugins: [
    new webpack.DefinePlugin({
      'process.env': {
         API_KEY: 'API_KEY',
         GOOGLE_MAPS_KEY: 'GOOGLE_MAPS_KEY',
         GOOGLE_GEOLOCATION_KEY: 'GOOGLE_GEOLOCATION_KEY',
         NODE_ENV: JSON.stringify('production') // <--- this set everything to use production.
      },
    }),
  ],