13

我的 webpack.config.js

var path = require("path")
var webpack = require('webpack')
var BundleTracker = require('webpack-bundle-tracker')

module.exports = {
    context: __dirname,

    entry: [
        'webpack-dev-server/client?http://localhost:3000',
        'webpack/hot/only-dev-server',
        './assets/js/index', // entry point of our app. assets/js/index.js should require other js modules and dependencies it needs
    ],

    output: {
        path: path.resolve('./assets/bundles/'),
        filename: "[name]-[hash].js",
        publicPath: 'http://localhost:3000/assets/bundles/', // Tell django to use this URL to load packages and not use STATIC_URL + bundle_name
    },

    plugins: [
        new webpack.HotModuleReplacementPlugin(),
        new webpack.NoEmitOnErrorsPlugin(), // don't reload if there is an error
        new BundleTracker({filename: './webpack-stats.json'}),
    ],

    module: {
        loaders: [
            {
                test: /\.jsx?$/,
                exclude: /node_modules/,
                loaders: ['react-hot-loader', 'babel-loader?presets[]=react'],
            }, // to transform JSX into JS
        ],
    },

    resolve: {
        modules: ['node_modules', 'bower_components'],
        extensions: ['.js', '.jsx']
    },
} 

错误:

错误:模块 'C:\Workspace\PyCharmProjects\ProjectPearl\node_modules\react-hot-loader\index.js' 不是加载器(必须具有正常或俯仰功能)

看起来有些通过为模块添加 -loader 扩展来工作(https://github.com/webpack/webpack/issues/3180),但是对我来说它仍然无法解决。

请协助。

4

2 回答 2

32

用法是react-hot-loader/webpack

loaders: ['react-hot-loader/webpack', 'babel-loader?presets[]=react'],

在此处查看一些示例用法http://gaearon.github.io/react-hot-loader/getstarted/

于 2017-06-20T04:30:49.457 回答
0

由于 react-hot-loader 依赖库的版本不匹配,可能会出现此问题。为确保您在 package.json 中正确配置了所有与 react-hot-loader 相关的依赖项,请运行以下命令。

  • npm install (如果您已经安装了所有依赖项,那么这不是必需的)
  • npm remove --save-dev react-hot-loader
  • npm install --save react-hot-loader@<specific-version>

在我的情况下,特定版本是 1.3.1

于 2018-11-01T18:28:25.763 回答