Webpack react-hot-loader 不工作 [英] Webpack react-hot-loader not working
本文介绍了Webpack react-hot-loader 不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
下面是我的 webpack.config.js 代码
Below is my webpack.config.js code
var webpack = require('webpack');
var path = require('path');
module.exports = {
// context: __dirname + "/app",
entry: [
'webpack-dev-server/client?http://0.0.0.0:8080',
'webpack/hot/only-dev-server',
'./src/main.jsx'],
output: {
path: "./build",
filename: "main.js"
},
module: {
loaders: [
{
test: /\.jsx?$/,
exclude: /(node_modules|bower_components)/,
loaders: ['react-hot', 'babel'],
include: path.join(__dirname, 'build'),
query:
{
presets:['es2015', 'react']
},
plugins: [
new webpack.HotModuleReplacementPlugin()
]
}
]
}
}
这是我的 package.son 的脚本代码
And this is script code of my package.son
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "webpack",
"dev": "webpack-dev-server --devtool eval --progress --colors --hot --content-base build"
},
这是我的 main.js 代码
And this is my main.js code
var React = require('react');
var ReactDOM = require('react-dom');
ReactDOM.render(
<h1>Hello World!</h1>,
document.getElementById('example')
);
当我输入 "npm run dev" 时,出现此错误
When I type "npm run dev" , I got this error
ERROR in ./src/main.jsx
Module parse failed: /Users/testaccount/Documents/React/test-react/src/main.jsx Line 6: Unexpected token <
You may need an appropriate loader to handle this file type.
|
| ReactDOM.render(
| <h1>Hello World!</h1>,
| document.getElementById('example')
| );
@ multi main
然后我转到 localhost:8080 并没有显示任何内容.有谁知道发生了什么?为什么我的 react-hot-loader 不工作?
And I go to localhost:8080 and nothing show. Anyone knows what happened? Why my react-hot-loader is not working?
推荐答案
我使用找到的解决方案 这里.基本上需要做的是将 module.hot.accept()
添加到您将组件渲染到的脚本中.
I made it work using the solution found here. Basically what needs to be done is add module.hot.accept()
to the script where you render your component to.
例如:
import React from 'react';
import ReactDOM from 'react-dom';
import MainLayout from './components/MainLayout.jsx';
ReactDOM.render(
<MainLayout />,
document.getElementById('root')
);
module.hot.accept(); // <-- this one.
这篇关于Webpack react-hot-loader 不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文