使用Webpack和生产中的Hot Loader 3

时间:2016-06-01 09:27:22

标签: webpack babeljs webpack-dev-server react-hot-loader

我正在使用Webpack和React Hot Loader v3。在开发过程中,它的工作大多与预期一致。但是,我希望在使用

输出静态包文件时禁用加载功能

webpack --progress -p

但我得到的却是这种重复的错误;

enter image description here

这是我的Webpack配置;

var path = require('path');
var webpack = require('webpack');

module.exports = {
    devServer: {
        publicPath: '/js/',
        hot: false,
        historyApiFallback: true,
        port: process.env.PORT || 3000
    },
    devtool: 'eval-source-map',
    entry: [
        'react-hot-loader/patch',
        'webpack-dev-server/client?http://localhost:3000',
        'webpack/hot/only-dev-server',
        './js/app/index'
    ],
    output: {
        path: path.join(__dirname, '/web/js/'),
        filename: 'bundle.js',
        publicPath: '/js/'
    },
    plugins: [
        new webpack.HotModuleReplacementPlugin()
    ],
    module: {
        loaders: [
            {
                test: /\.css$/,
                loader: "style-loader!css-loader?modules",
            },
            {
                test: /\.js$/,
                exclude: /(node_modules|bower_components)/,
                loader: 'babel',
                query: {
                    presets: ['es2015', 'react', 'stage-0'],
                    plugins: ['transform-flow-strip-types'],
                    cacheDirectory: true
                },
                include: path.join(__dirname, '/js/')
            }
        ]
    }
};

我得到了尝试禁用HotModuleReplacementPlugin()的建议,但仍然会出现同样的错误。关于我在这里缺少什么的想法?

2 个答案:

答案 0 :(得分:4)

您需要一个单独的生产配置,该配置在条目中不包含开发服务器或热加载器。请参阅下面的webpack配置的简化版本。如果应用程序使用webpack -p之类的内容运行,则LAUNCH_COMMANDproduction并且productionConfig已使用。

但这只是一种方法。您还可以使用单独的配置文件进行生产。像webpack.prod.config.js这样的东西。然后,您将使用webpack -p指定生产配置,而不是运行webpack -p --config webpack.prod.config.js。同样,您的生产配置不会在条目中包含webpack-dev-serverhot-loader

import webpack from 'webpack'
import path from 'path'
import HtmlWebpackPlugin from 'html-webpack-plugin'

const HtmlWebpackPluginConfig = new HtmlWebpackPlugin({
  template: path.join(__dirname, '/app/index.html'),
  filename: 'index.html',
  inject: 'body',
})

const PATHS = {
  app: path.join(__dirname, 'app'),
  build: path.join(__dirname, 'dist'),
}

const LAUNCH_COMMAND = process.env.npm_lifecycle_event

const isProduction = LAUNCH_COMMAND === 'production'
process.env.BABEL_ENV = LAUNCH_COMMAND

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

const base = {
  output: {
    path: PATHS.build,
    filename: 'index_bundle.js',
  },
}

const developmentConfig = {
  entry: [
    'webpack-dev-server/client?http://localhost:8080',
    'webpack/hot/only-dev-server',
    './app/index',
  ],
}

const productionConfig = {
  entry: [
    './app/index',
  ],
}

export default Object.assign({}, base,
  isProduction === true ? productionConfig : developmentConfig)

答案 1 :(得分:1)

除了摆脱HotModuleReplacementPlugin之外,你还需要摆脱额外的切入点:

// Current
entry: [
    'react-hot-loader/patch',
    'webpack-dev-server/client?http://localhost:3000',
    'webpack/hot/only-dev-server',
    './js/app/index'
],

// Fixed
entry: [
    './js/app/index'
],