我正在尝试使用 Webpack 和 Babel 为我的 React 应用程序设置环境。我使用 JSX 来实现组件,我想利用实时重载。
这是我的package.json
文件:
{
(...)
"scripts": {
"start": "webpack-dev-server --hot --inline"
},
"dependencies": {
"babel-loader": "^6.2.4",
"babel-preset-es2015": "^6.9.0",
"babel-preset-react": "^6.11.1",
"jsx-loader": "^0.13.2",
"react": "^15.1.0",
"react-dom": "^15.1.0",
"react-hot-loader": "^1.3.0",
"webpack": "^1.13.1",
"webpack-dev-server": "^1.14.1"
}
}
这是我的 Webpack 配置:
var webpack = require('webpack');
var path = require('path');
var BUILD_DIR = path.resolve(__dirname, 'src/client/public');
var APP_DIR = path.resolve(__dirname, 'src/client/app');
var config = {
entry: [
'webpack-dev-server/client?http://0.0.0.0:8080',
'webpack/hot/only-dev-server',
APP_DIR + '/index.jsx'
],
output: {
path: BUILD_DIR,
filename: 'bundle.js'
},
module : {
loaders : [
{
test : /\.jsx?/,
include : APP_DIR,
loaders: ['react-hot', 'babel']
}
]
},
plugins: [
new webpack.HotModuleReplacementPlugin()
]
};
module.exports = config;
执行npm run start
并使用此配置后,我在控制台中看到检测到 JSX 文件中的更新,但浏览器未更新。此外,如果我查看module.js
文件,我的更新不会被考虑在内。
运行命令webpack -d
,它们是...
请参阅此存储库:https ://github.com/templth/react-webpack-hot-reload 。
附加提示。我在浏览器的 JavaScript 控制台中看到以下跟踪:
[HMR] Waiting for update signal from WDS...
Download the React DevTools for a better development experience: https://(...)/react-devtools
XHR finished loading: GET "http://localhost:8080/sockjs-node/info?t=1467375470671".AbstractXHRObject._start @ abstract-xhr.js:132(anonymous function) (...)
[WDS] Hot Module Replacement enabled.
[WDS] App updated. Recompiling...
[WDS] App hot update...
[HMR] Checking for updates on the server...
GET http://localhost:8080/src/client/0cf06dae430007853b6f.hot-update.json 404 (Not Found)hotDownloadManifest @ bootstrap (...)
[HMR] Cannot find update. Need to do a full reload!
only-dev-server.js:28 [HMR](可能是因为重启了 webpack-dev-server)
在启动 webpack 开发服务器的控制台中
Hash: 85771179bfa8706bc10f
Version: webpack 1.13.1
Time: 275ms
Asset Size Chunks Chunk Names
bundle.js 991 kB 0 [emitted] main
0.8073da5d40a8dd413f96.hot-update.js 3.6 kB 0, 0 [emitted] main, main
8073da5d40a8dd413f96.hot-update.json 36 bytes [emitted]
chunk {0} bundle.js, 0.8073da5d40a8dd413f96.hot-update.js, 0.8073da5d40a8dd413f96.hot-update.js (main) 897 kB [rendered]
[76] ./src/client/app/index.jsx 3.58 kB {0} [built]
+ 255 hidden modules
webpack: bundle is now VALID.
非常感谢您的帮助!