0

我正在尝试在 React 中使用 React Hot Loader。我通过运行“npm install --save-dev react-hot-loader”安装了 react hot loader。我试图遵循http://gaearon.github.io/react-hot-loader/getstarted/但无法理解。我附上了我的 webpack.config.js 和 package.json。我按照文档中列出的内容进行了更改。但我无法即时看到我在组件中所做的更改。怎么了?

项目目录结构

webpack.config.js

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

module.exports = {
    devServer: {
        inline: true,
        contentBase: './src',
        port: 3000
    },
    devtool: 'cheap-module-eval-source-map',
    entry: [
        'webpack-dev-server/client?http://0.0.0.0:3000', // WebpackDevServer host and port
        'webpack/hot/only-dev-server', // "only" prevents reload on syntax errors
        './dev/js/index.js' // Your appʼs entry point
    ],
    module: {
        loaders: [
            {
                test: /\.js$/,
                loaders: ['react-hot','babel'],
                exclude: /node_modules/
            },
            {
                test: /\.scss/,
                loader: 'style-loader!css-loader!sass-loader'
            }
        ]
    },
    output: {
        path: 'src',
        filename: 'js/bundle.min.js'
    },
    plugins: [
        new webpack.optimize.OccurrenceOrderPlugin(),
        new webpack.HotModuleReplacementPlugin()
    ]
};

来自 package.json 的脚本

 "scripts": {
    "dev": "webpack",
    "start": "webpack-dev-server"
  }

索引.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>React Webpack</title>
</head>
<body>

    <div id="root"></div>
    <script src="js/bundle.min.js"></script>

</body>
</html>
4

1 回答 1

1

好的,现在您需要在捆绑之前将热加载脚本添加到您的 html 文件中,如下所示:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>React Webpack</title>
</head>
<body>

    <div id="root"></div>

    <script src="http://localhost:3000/webpack-dev-server.js"></script>
    <script src="js/bundle.min.js"></script>
</body>
</html>

它位于 localhost:3000 下,因为我在您的 webpack 配置中看到了它。我通常只是将其保留在:8080 下,但我认为根据您的配置,它需要是这样的。

于 2016-09-01T19:25:15.147 回答