【发布时间】:2019-08-07 17:24:17
【问题描述】:
因此,在我的 npm run start 期间,它不会捕获对编译成 css 文件的 scss 文件的任何更改。当我运行 npm run build 时,scss 工作得很好,但它没有捕捉到任何 css 更改。我添加了 css 脚本,我也会跑到一边,但是,这也不起作用。大家对如何解决这个问题有任何想法吗?或者你们中的任何人都有一个可以使用 npm run start 捕获 scss 更改的有效 webpack 设置?
当我进行更改时,我实际上看到终端重新编译,所以我知道它被捕获了。这是一个反应应用程序仅供参考。这是我的 webpack 配置文件(在运行构建期间有效)
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin')
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
module.exports = {
devtool: 'inline-source-map',
plugins: [
new HtmlWebpackPlugin({
template: './src/index.html',
filename: "./index.html"
}),
new MiniCssExtractPlugin({
// Options similar to the same options in webpackOptions.output
// both options are optional
filename: "/static/cs/styles.css",
chunkFilename: "styles.css"
})
],
entry: './src/index.js',
output: {
path: path.join(__dirname, 'build'),
filename: 'static/js/bundle.js'
},
module: {
rules: [
{
test: /\.s?css$/,
use: [
"style-loader",
MiniCssExtractPlugin.loader,
"css-loader",
"sass-loader"
]
},
{
use: { loader: 'babel-loader' },
test: /\.js$/,
exclude: /node_modules/
},
{
test: /\.(png|svg|jpg|gif)$/,
use: [
{
loader: 'file-loader',
options: {
outputPath: '',
},
}
]
},
]
},
devServer: {
contentBase: path.join(__dirname, 'build'),
},
}
这是我的脚本
"scripts": {
"start": "webpack-dev-server --mode development --open --hot ",
"build": "webpack --mode production",
"build-task:scss-compile": "node-sass-chokidar --source-map true src/scss/ -o dist/css",
"build-task:autoprefixer": "postcss dist/css/*.css --use autoprefixer -d dist/css",
"sass:build": "npm-run-all -p build-task:*",
"sass:watch": "chokidar 'src/styles/*.scss' -c 'npm run sass:build'",
"dev": "npm-run-all -p sass:*",
"css": "node-sass src/styles/styles.scss -o dist",
"css:watch": "npm run css && node-sass src/styles/styles.scss -wo dist"
},
【问题讨论】:
-
尝试改用
npm run dev。