我尝试使用热模块更换,但从未成功。
然后我发现了这个repository ,它可以很好地使用热模块更换。它使用了一个 react-hot 加载器,如果我删除这个加载器,我会得到错误:
[HMR] Cannot find update. Need to do a full reload!
我根据上面的存储库调整我的项目,但是我没有使用react,所以我没有使用react-hot loader,因此我总是得到上述错误。
我可以使用 webpack 的热模块替换但不使用 react 吗?或者我只需要一个 xx-hot 加载程序来使其热模块可更换?
我的结构:
src
entry.js
index.html
server.js
webpack.config.js
index.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<script src="/static/bundle.js"></script>
</body>
</html>
entry.js:
document.write('hello');
server.js:
var webpack = require('webpack');
var WebpackDevServer = require('webpack-dev-server');
var config = require('./webpack.config');
new WebpackDevServer(webpack(config), {
publicPath: config.output.publicPath,
hot: true,
historyApiFallback: true
}).listen(3000, 'localhost', function (err, result) {
if (err) {
console.log(err);
}
console.log('Listening at localhost:3000');
});
webpack.config.js:
var path = require("path");
var webpack = require('webpack');
module.exports = {
devtool: 'eval',
entry: [
'webpack-dev-server/client?http://localhost:3000',
'webpack/hot/only-dev-server',
"./src/entry.js"
],
output: {
path: path.join(__dirname, "build"),
publicPath: '/static/',
filename: "bundle.js"
},
plugins: [
new webpack.HotModuleReplacementPlugin()
]
};
最佳答案
这是 HMR 工作的入口点所必需的。它将通过在 prod 中的缩小作为其“无法访问”的代码来删除
if (module.hot) {
module.hot.accept();
}
关于webpack - 我可以使用 webpack 的热模块替换但不使用 react 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32857313/