首页 > 解决方案 > webpack-dev-server hot reloved 未检测到 scss 中的更改

问题描述

我尝试用 node 和 webpack 做静态页面来热 relod 和自动重新编译 scss。我想在 scss 文件每次更改后刷新项目,所以我需要 webpack-dev-server 来检测 scss 文件更改,重新编译 scss 并刷新页面。我在 HTML() 中使用 cmd。

我有两个问题。我使用 node-scss 重新编译它,但首先:

node-sass --output-style 压缩 -o dist src/scss

在每次重新加载之前,我都会尝试 devServer.before,plugins.WebpackShellPluginNext - 但不是每次刷新都有效,只有当我启动 webpack 时

包.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",
    }),
  ],
};

也许你有一些想法如何解决它或做得更好/更容易?

标签: webpacksasshot-reload

解决方案


推荐阅读