gpt4 book ai didi

javascript - webpack-dev-server 未在 html 或 sass 更改时重新加载

转载 作者:行者123 更新时间:2023-12-04 00:41:55 25 4
gpt4 key购买 nike

我的项目有这样的结构:

\root    
\webpack.config.js
\public
\ index.html
\ ...
\ css
\ directives
\ views
\ dist (webpack output)
\app.js
\ index.html
\ app.js.map
\ style.css
\ style.css.map

当我使用 webpack-dev-server 时,我从/root 启动它并加载应用程序。但是,当我更改 sass 文件或 html 文件时,它不会重新加载。 webpack 配置有什么问题?

启动 webpack 的命令:
$ cd root
$ webpack-dev-server

Webpack.config.js
const path = require('path');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
var HtmlWebpackPlugin = require('html-webpack-plugin');

const webConfig = {
entry: path.join(__dirname, 'public', 'js', 'app'),
output: {
path: path.join(__dirname, 'public', 'dist'),
filename: 'app.js'
},
resolve: {
modules: [__dirname, 'node_modules'],
extensions: ['.js'],
enforceExtension: false,
},
devtool: 'source-map',
devServer:{
contentBase: 'public/',
publicPath: 'public/'
},
module: {
loaders: [
{
test: /\.scss$/,
loader: ExtractTextPlugin.extract({
loader: 'css-loader?modules,localIdentName=[name]__[local]--[hash:base64:5]!sass-loader'
}),
exclude: /node_modules/
},
{
test: /\.html$/,
loader: "raw-loader" // loaders: ['raw-loader'] is also perfectly acceptable.
}
]
},
plugins: [
new ExtractTextPlugin({filename: 'style.css', allChunks: true}),
new HtmlWebpackPlugin({
template: './public/index.html'
})
]
};

module.exports = webConfig;

最佳答案

https://medium.com/@rajaraodv/webpack-the-confusing-parts-58712f8fcad9

“inline” option adds “Live reloading” for the entire page. “hot” option enables “Hot Module Reloading” that tries to reload just the component that’s changed (instead of the entire page). If we pass both options, then, when the source changes, the webpack-dev-server will try to HMR first. If that doesn’t work, then it will reload the entire page.



尝试将其添加到您的 webpack.config 文件中:
devServer:{
contentBase: 'public/',
publicPath: 'public/',
inline: true,
hot: true,
},

如果需要,您还可以从 package.json 文件中调用脚本。像这样的东西:
...
scripts: {
"watch": "webpack-dev-server --progress --colors --hot --inline",
}
...

关于javascript - webpack-dev-server 未在 html 或 sass 更改时重新加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43965272/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com