为什么 webpack-dev-server 不执行 compiler hook?
Why does webpack-dev-server not execute compiler hook?
考虑以下 webpack.config.js
。当 运行 npx webpack
我对 console.log('Here goes my custom function')
的调用被执行。但是 运行 npx webpack-dev-server
它没有,尽管我可以看到它进行了编译。
为什么会这样,我该如何解决?
module.exports = {
// ...
module: {
// ...
plugins: [
{
apply: (compiler) =>{
compiler.hooks.beforeRun.tap('BeforeRunPlugin', (compilation) => {
console.log('Here goes my custom function')
})
}
},
// ...
],
devServer: {
contentBase: join(__dirname, 'dist'),
publicPath: './assets/',
port: 7700,
open: false,
overlay: true, // show compiler errors as overlay
writeToDisk: true,
}
};
我在这里引用了 sokra,所以感谢他:https://github.com/webpack/webpack/issues/10061
There are two way to run webpack: run and watch. The dev-server uses
watch
.
The beforeRun
hook is only called for run
and not for watch
.
You may want to use the compile
hook instead.
考虑以下 webpack.config.js
。当 运行 npx webpack
我对 console.log('Here goes my custom function')
的调用被执行。但是 运行 npx webpack-dev-server
它没有,尽管我可以看到它进行了编译。
为什么会这样,我该如何解决?
module.exports = {
// ...
module: {
// ...
plugins: [
{
apply: (compiler) =>{
compiler.hooks.beforeRun.tap('BeforeRunPlugin', (compilation) => {
console.log('Here goes my custom function')
})
}
},
// ...
],
devServer: {
contentBase: join(__dirname, 'dist'),
publicPath: './assets/',
port: 7700,
open: false,
overlay: true, // show compiler errors as overlay
writeToDisk: true,
}
};
我在这里引用了 sokra,所以感谢他:https://github.com/webpack/webpack/issues/10061
There are two way to run webpack: run and watch. The dev-server uses
watch
.The
beforeRun
hook is only called forrun
and not forwatch
.You may want to use the
compile
hook instead.