Webpack 开发服务器不自动重新加载
Webpack dev server not auto-reloading
所以我已经设置了 webpack 和 webpack-dev-server
,但是 webpack-dev-server
不会自动重新加载。如果我修改文件并保存它,在我手动刷新之前,浏览器中没有任何变化。
这是我的 webpack 配置和 运行s webpack-dev-server
的脚本文件。有没有人看到任何可能阻止自动重新加载工作的东西?
我通过阅读多个教程、文档以及阅读 react-create-app
生成的文件将这些放在一起。
config/webpack.config.dev.js
'use strict';
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const autoprefixer = require('autoprefixer');
const webpack = require('webpack');
const extractSass = new ExtractTextPlugin('style.css');
module.exports = {
entry : './src/index.jsx',
eslint: {configFile: './src/.eslintrc.json'},
module: {
loaders: [
{
exclude: /node_modules/,
include: ['src'],
loader: 'babel',
test : /(\.js|\.jsx)$/
},
{
exclude: /node_modules/,
include: ['src']
loader : extractSass.extract([ 'css', 'postcss', 'sass' ]),
test : /\.scss$/
}
],
preLoaders: [
{
exclude: /node_modules/,
loader : 'eslint',
query : {presets: [ 'react', 'latest' ]},
test : /(\.js|\.jsx)$/
}
]
},
output: {
filename : 'bundle.js',
path : 'dist',
publicPath: '/'
},
plugins: [
extractSass,
new HtmlWebpackPlugin({
inject : true,
template: paths.appHtml
}),
new webpack.HotModuleReplacementPlugin({multistep: true})
],
postcss: () => [
autoprefixer({
browsers: [
'>1%',
'last 4 versions',
'Firefox ESR',
'not ie < 9'
]
})
]
};
scripts/dev.js
运行 与 $ yarn run dev
'use strict';
const WebpackDevServer = require('webpack-dev-server');
const config = require('../config/webpack.config.dev.js');
const webpack = require('webpack');
const compiler = webpack(config);
const server = new WebpackDevServer(compiler, {
clientLogLevel : 'warn',
compress : true,
contentBase : 'public',
filename : config.output.filename,
historyApiFallback: true,
hot : true,
inline : true,
lazy : false,
noInfo : true,
publicPath : '/',
quiet : true,
stats : 'errors-only',
watchOptions : {
aggregateTimeout: 300,
poll : 1000
}
});
server.listen(8080, 'localhost', () => {
console.log('Listening on port 8080');
});
根据webpack dev server documentation你应该将这个入口点添加到webpack配置中以支持自动刷新。
config.entry.unshift("webpack-dev-server/client?http://localhost:8080/");
jontem 在他的回答中指出我的配置缺少 webpack-dev-server
客户端。
这是我应用他的解决方案并设置 HMR 所采取的步骤。
config/webpack.config.dev.js
module.config = {
// ...
entry: [
// converted entry to an array
// to allow me to unshift the client later
path.resolve(__dirname, '../src/index.jsx')
],
// ...
module: {
loaders: {
// ...
{
// Use style loader instead of ExtractTextPlugin
// To allow for style injection / hot reloading css
exclude: /node_modules/,
loaders: [ 'style', 'css', 'postcss', 'sass' ],
test : /\.scss$/
},
// ...
}
}
}
scripts/dev.js
'use strict';
const WebpackDevServer = require('webpack-dev-server');
const config = require('../config/webpack.config.dev.js');
const webpack = require('webpack');
// unshift `webpack-dev-server` client
// and hot dev-server
config.entry.unshift('webpack-dev-server/client?/', 'webpack/hot/dev-server');
const compiler = webpack(config);
// ...
我遇到了同样的问题,以下配置启用了静态和 in-memory 包 auto-reloading。关键是启用devServer.watchContentBase.
config/webpack.config.dev.js
...
module.exports = {
...
devServer: {
contentBase: ...,
publicPath: ...,
watchContentBase: true
},
...
}
package.json
{
...
"scripts": {
"develop": "webpack-dev-server --open --mode development --config config/webpack.config.dev.js",
...
}
...
}
请在您的 webpack 配置中添加以下内容并尝试。
devServer: {
hot: true,
inline: true,
host: "localhost",
port: 8082,
watchOptions: {
poll: true
}
}
注意:我用的是webpack版本^3.11.0
我遇到了类似的问题,通过添加
修复了它
watchOptions: {
poll: true
}
当我第一次安装 webpack starter 时,一切都完美无缺,在对 webpack.config.js 进行了一周的更改后,它停止工作了。我修改了各种建议,最有效的是 watchOptions: {poll:true }
仅供参考,我是 webpack 4 "webpack": "4.29.6", "webpack-cli": "^3.3.0", "webpack-dev-server": "3.3.1"
devServer: {
port: 3000,
contentBase: './',
watchOptions: {
poll: true
}
}
还有
- 如果您使用
extract-loader
,自动重新加载将不起作用
- 热模块更换功能
devServer: {
// ,,,
contentBase: '/your/path'
watchContentBase: true
hot: true,
},
防止刷新网页中的静态文件,除非您在浏览器中按 F5 按钮
这个第三方 webpack 开发服务器文档有我需要的答案:
https://wohugb.gitbooks.io/webpack/content/dev_tools/webpack-dev-server.html
相关部分转载如下:
webpack-dev-server 配置中没有 inline: true 标志,因为 webpack-dev-server 模块无法访问 webpack 配置。相反,用户必须将 webpack-dev-server 客户端入口点添加到 webpack 配置中。
为此,只需将 webpack-dev-server/client?http://: 添加到(所有)入口点。 IE。使用上述配置:
var config = require("./webpack.config.js");
config.entry.app.unshift("webpack-dev-server/client?http://localhost:8080");
var compiler = webpack(config);
var server = new webpackDevServer(compiler, {...});
server.listen(8080);
我也遇到了同样的问题,添加此代码行后我的问题就解决了。现在可以自动重新加载了
devServer : {
contentBase : './',
watchOptions : {
poll: true
}
}
对于使用 Webpack v5 遇到此问题的任何人,您需要在配置中将 target
设置为 web
,如下所示:
module.exports = {
entry: "...",
target: "web",
.....
}
所以我已经设置了 webpack 和 webpack-dev-server
,但是 webpack-dev-server
不会自动重新加载。如果我修改文件并保存它,在我手动刷新之前,浏览器中没有任何变化。
这是我的 webpack 配置和 运行s webpack-dev-server
的脚本文件。有没有人看到任何可能阻止自动重新加载工作的东西?
我通过阅读多个教程、文档以及阅读 react-create-app
生成的文件将这些放在一起。
config/webpack.config.dev.js
'use strict';
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const autoprefixer = require('autoprefixer');
const webpack = require('webpack');
const extractSass = new ExtractTextPlugin('style.css');
module.exports = {
entry : './src/index.jsx',
eslint: {configFile: './src/.eslintrc.json'},
module: {
loaders: [
{
exclude: /node_modules/,
include: ['src'],
loader: 'babel',
test : /(\.js|\.jsx)$/
},
{
exclude: /node_modules/,
include: ['src']
loader : extractSass.extract([ 'css', 'postcss', 'sass' ]),
test : /\.scss$/
}
],
preLoaders: [
{
exclude: /node_modules/,
loader : 'eslint',
query : {presets: [ 'react', 'latest' ]},
test : /(\.js|\.jsx)$/
}
]
},
output: {
filename : 'bundle.js',
path : 'dist',
publicPath: '/'
},
plugins: [
extractSass,
new HtmlWebpackPlugin({
inject : true,
template: paths.appHtml
}),
new webpack.HotModuleReplacementPlugin({multistep: true})
],
postcss: () => [
autoprefixer({
browsers: [
'>1%',
'last 4 versions',
'Firefox ESR',
'not ie < 9'
]
})
]
};
scripts/dev.js
运行 与 $ yarn run dev
'use strict';
const WebpackDevServer = require('webpack-dev-server');
const config = require('../config/webpack.config.dev.js');
const webpack = require('webpack');
const compiler = webpack(config);
const server = new WebpackDevServer(compiler, {
clientLogLevel : 'warn',
compress : true,
contentBase : 'public',
filename : config.output.filename,
historyApiFallback: true,
hot : true,
inline : true,
lazy : false,
noInfo : true,
publicPath : '/',
quiet : true,
stats : 'errors-only',
watchOptions : {
aggregateTimeout: 300,
poll : 1000
}
});
server.listen(8080, 'localhost', () => {
console.log('Listening on port 8080');
});
根据webpack dev server documentation你应该将这个入口点添加到webpack配置中以支持自动刷新。
config.entry.unshift("webpack-dev-server/client?http://localhost:8080/");
jontem 在他的回答中指出我的配置缺少 webpack-dev-server
客户端。
这是我应用他的解决方案并设置 HMR 所采取的步骤。
config/webpack.config.dev.js
module.config = {
// ...
entry: [
// converted entry to an array
// to allow me to unshift the client later
path.resolve(__dirname, '../src/index.jsx')
],
// ...
module: {
loaders: {
// ...
{
// Use style loader instead of ExtractTextPlugin
// To allow for style injection / hot reloading css
exclude: /node_modules/,
loaders: [ 'style', 'css', 'postcss', 'sass' ],
test : /\.scss$/
},
// ...
}
}
}
scripts/dev.js
'use strict';
const WebpackDevServer = require('webpack-dev-server');
const config = require('../config/webpack.config.dev.js');
const webpack = require('webpack');
// unshift `webpack-dev-server` client
// and hot dev-server
config.entry.unshift('webpack-dev-server/client?/', 'webpack/hot/dev-server');
const compiler = webpack(config);
// ...
我遇到了同样的问题,以下配置启用了静态和 in-memory 包 auto-reloading。关键是启用devServer.watchContentBase.
config/webpack.config.dev.js
...
module.exports = {
...
devServer: {
contentBase: ...,
publicPath: ...,
watchContentBase: true
},
...
}
package.json
{
...
"scripts": {
"develop": "webpack-dev-server --open --mode development --config config/webpack.config.dev.js",
...
}
...
}
请在您的 webpack 配置中添加以下内容并尝试。
devServer: {
hot: true,
inline: true,
host: "localhost",
port: 8082,
watchOptions: {
poll: true
}
}
注意:我用的是webpack版本^3.11.0
我遇到了类似的问题,通过添加
修复了它 watchOptions: {
poll: true
}
当我第一次安装 webpack starter 时,一切都完美无缺,在对 webpack.config.js 进行了一周的更改后,它停止工作了。我修改了各种建议,最有效的是 watchOptions: {poll:true }
仅供参考,我是 webpack 4 "webpack": "4.29.6", "webpack-cli": "^3.3.0", "webpack-dev-server": "3.3.1"
devServer: {
port: 3000,
contentBase: './',
watchOptions: {
poll: true
}
}
还有
- 如果您使用
extract-loader
,自动重新加载将不起作用 - 热模块更换功能
devServer: {
// ,,,
contentBase: '/your/path'
watchContentBase: true
hot: true,
},
防止刷新网页中的静态文件,除非您在浏览器中按 F5 按钮
这个第三方 webpack 开发服务器文档有我需要的答案: https://wohugb.gitbooks.io/webpack/content/dev_tools/webpack-dev-server.html
相关部分转载如下:
webpack-dev-server 配置中没有 inline: true 标志,因为 webpack-dev-server 模块无法访问 webpack 配置。相反,用户必须将 webpack-dev-server 客户端入口点添加到 webpack 配置中。
为此,只需将 webpack-dev-server/client?http://: 添加到(所有)入口点。 IE。使用上述配置:
var config = require("./webpack.config.js");
config.entry.app.unshift("webpack-dev-server/client?http://localhost:8080");
var compiler = webpack(config);
var server = new webpackDevServer(compiler, {...});
server.listen(8080);
我也遇到了同样的问题,添加此代码行后我的问题就解决了。现在可以自动重新加载了
devServer : {
contentBase : './',
watchOptions : {
poll: true
}
}
对于使用 Webpack v5 遇到此问题的任何人,您需要在配置中将 target
设置为 web
,如下所示:
module.exports = {
entry: "...",
target: "web",
.....
}