webpack - webpack-dev-server hot reloved 未检测到 scss 中的更改
问题描述
我尝试用 node 和 webpack 做静态页面来热 relod 和自动重新编译 scss。我想在 scss 文件每次更改后刷新项目,所以我需要 webpack-dev-server 来检测 scss 文件更改,重新编译 scss 并刷新页面。我在 HTML() 中使用 cmd。
我有两个问题。我使用 node-scss 重新编译它,但首先:
- 我需要从 webpack 运行:
node-sass --output-style 压缩 -o dist src/scss
在每次重新加载之前,我都会尝试 devServer.before,plugins.WebpackShellPluginNext - 但不是每次刷新都有效,只有当我启动 webpack 时
- webpack 不会检测到我的 scss 文件中的更改。我当然有 sass-loader,但它没有反应。
包.json
{
"name": "pluto",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "webpack serve --mode development --env development --config webpack.config.js",
"css": "node-sass --output-style compressed -o dist src/scss"
},
"author": "",
"license": "ISC",
"devDependencies": {
"autoprefixer": "^10.3.1",
"babel-core": "^6.26.3",
"babel-loader": "^8.2.2",
"babel-plugin-transform-runtime": "^6.23.0",
"babel-preset-es2015": "^6.24.1",
"css-loader": "^6.2.0",
"html-webpack-plugin": "^5.3.2",
"mini-css-extract-plugin": "^2.1.0",
"node-sass": "^6.0.1",
"postcss-cli": "^8.3.1",
"sass": "^1.36.0",
"sass-loader": "^12.1.0",
"style-loader": "^3.2.1",
"webpack": "^5.46.0",
"webpack-cli": "^4.7.2",
"webpack-dev-server": "^3.11.2"
},
"dependencies": {
"babel-polyfill": "^6.26.0",
"babel-runtime": "^6.26.0",
"webpack-shell-plugin-next": "^2.2.2"
}
}
webpack.config:
const HtmlWebpackPlugin = require("html-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const path = require("path");
const WebpackShellPluginNext = require('webpack-shell-plugin-next');
const spawn = require('child_process').spawn;
module.exports = {
devServer: {
hot: true,
index: 'index.html'
},
entry: {
app: "./src/index.js",
},
output: {
path: path.resolve(__dirname, "dist"),
filename: "bundle.js",
},
module: {
rules: [
{
test: /\.css$/,
use: ["style-loader", "css-loader", "postcss-loader"],
},
{
test: /\.scss$/,
use: [MiniCssExtractPlugin.loader, 'style-loader', "css-loader", "sass-loader"]
},
],
},
plugins: [
new WebpackShellPluginNext({
onBuildStart:{
scripts: ['node-sass --output-style compressed -o dist src/scss'],
blocking: true,
parallel: false
},
onBuildEnd:{
scripts: ['echo "Webpack End"'],
blocking: false,
parallel: true
}
}),
new HtmlWebpackPlugin({ filename: "index.html", template: "index.html" }),
new MiniCssExtractPlugin({
filename: "[name].css",
chunkFilename: "[id].css",
}),
],
};
也许你有一些想法如何解决它或做得更好/更容易?
解决方案
推荐阅读
- regex - 为什么这个正则表达式匹配这个字符串?
- ms-word - 如何在 Word 文档中设置药丸样式?
- chart.js - ChartJS timeline graph with events
- vue.js - 如何更新以下代码中的 msg 变量?
- node.js - 如何在 Elastic Beanstalk 上使用 Django 提供 React 应用程序?
- flutter - Flutter中的异步函数不返回null
- pycharm - 在 Windows 10 上使用 Pycharm 配置 Odoo
- sitecore8.2 - 在哪里下载 Sitecore.support.98798 补丁
- javascript - 如何使用 react-compound-timer getTime() 方法更新组件状态
- python - 在 python 中安装和导入带有 rasa-nlu 的 Matplotlib 库时出错