我正在运行一个非常简单的mern堆栈代码。我正在使用webpack-dev-middleware和webpack-hot-middleware。问题是当我运行时node server.js
,编译工作没有错误,但我没有看到我对浏览器上的客户端代码所做的任何更改,即使使用热模块,浏览器也不会自行刷新。也许上述两个问题都是因为我在代码中遗漏了一些东西。
编辑:当我使用 webpackdevmiddleware 时,我的程序正在从磁盘中提取包。例如,如果我清空我的 bundle.js,那么即使服务器打开,我的浏览器实际上也会拉出一个空文件,它可以观察文件更改并成功编译它,但浏览器不会反映它们。感觉就像浏览器不是从任何内存中提取它,而是从磁盘中提取它。
Webpack.config.js:
const path = require('path');
const webpack = require("webpack")
module.exports = {
mode: "production",
entry: {
app: [__dirname + "/static/jsx/core-jsx/app3.jsx"],
vendor: ["react", "react-dom", "whatwg-fetch"],
},
plugins: [
//new webpack.optimize.CommonsChunkPlugin({name:"vendor",filename: "vendor.bundle.js"}),
new webpack.NamedModulesPlugin(),
new webpack.HotModuleReplacementPlugin()
],
module: {
rules: [
{
test: /\.jsx$/,
loader: 'babel-loader',
}
]
},
devServer: {
hot:true,
port: 8000,
contentBase: "static",
proxy: {
'/api/*': {
target: "http://localhost:3000/",
changeOrigin: true
},
}
},
devtool: "source-map",
resolve: {
alias: {
jsx: path.resolve(__dirname, 'static/jsx/core-jsx')
},
},
output: {
path: __dirname + '/static/jsx',
filename: 'app.bundle.js',
publicPath: '/',
}
}
服务器.js
if (process.env.NODE_ENV !== 'production') {
const webpack = require('webpack');
const webpackDevMiddleware = require('webpack-dev-middleware');
const webpackHotMiddleware = require('webpack-hot-middleware');
const config = require('./webpack.config');
const bundler = webpack(config);
app.use(webpackDevMiddleware(bundler, {
noInfo: true,
publicPath: config.output.publicPath,
}));
app.use(webpackHotMiddleware(bundler));
}