我有一个带有哈巴狗和手写笔的Express应用程序。我已经配置了HMR中间件,它会在手写笔更改时重新加载,但不会对哈巴狗的更改进行重新加载。
我想知道我是否错过了特定的配置。我也尝试添加pug-html-loader
,但这也不起作用。
// server.js
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');
const webpackDevMiddleware = require('./hmr').dev;
const webpackHotMiddleware = require('./hmr').hot;
app.use(webpackDevMiddleware);
app.use(webpackHotMiddleware);
// hmr.js
const webpackDevMiddleware = require('webpack-dev-middleware');
const webpackHotMiddleware = require('webpack-hot-middleware');
const webpack = require('webpack');
const webpackConfig = require('./webpack.config.js');
const compiler = webpack(webpackConfig);
exports.dev = webpackDevMiddleware(compiler, {
noInfo: true,
filename: webpackConfig.output.filename,
publicPath: webpackConfig.output.publicPath,
stats: {
colors: true
}
});
exports.hot = webpackHotMiddleware(compiler, {
log: console.log,
path: '/__webpack_hmr',
heartbeat: 10000
});
// webpack.config.js
const javascriptRule = {
test: /\.js$/,
use: [
{
loader: 'babel-loader',
options: {
presets: ['env']
}
}
]
};
const stylesRule = {
test: /\.styl$/,
use: ['style-loader', 'css-loader', 'stylus-loader']
};
module.exports = {
context: path.join(__dirname, 'javascripts'),
entry: [
'webpack-hot-middleware/client?path=/__webpack_hmr&timeout=20000',
'./index.js'
],
devtool: 'source-map',
output: {
path: path.join(__dirname, 'public', 'dist'),
filename: 'bundle.js',
publicPath: '/dist/',
library: 'aux'
},
module: {
rules: [javascriptRule, stylesRule]
},
plugins: [new webpack.HotModuleReplacementPlugin(), new webpack.NoEmitOnErrorsPlugin()]
}
答案 0 :(得分:1)
您需要安装raw-loader:https://webpack.js.org/loaders/raw-loader/
Webpack 3配置:
module: {
rules: [
{
test: /\.pug$/,
use: [
{loader: 'raw-loader'},
{loader: 'pug-html-loader'}
]
}
]
},
plugins: [
// Templates HTML
new HtmlWebpackPlugin({
filename: 'index.html',
template: './src/templates/index.pug'
}),
new HtmlWebpackPlugin({
filename: 'contact.html',
template: './src/templates/contact.pug'
}),
new webpack.NamedModulesPlugin(),
new webpack.HotModuleReplacementPlugin()
]
app.js
// import all template pug
import 'raw-loader!./templates/index.pug'
import 'raw-loader!./templates/contact.pug'
...
这使得webpack监听pug文件中的更改,但它也将此js代码添加到bundle.js,然后您需要处理app.js来清理bundle.js。