使用热重载运行webpack-dev-server,每次更改scss文件时,重新编译需要3秒以上,而js / html只需不到1秒即可重新编译。我无法弄清楚原因。
这是我的配置:
webpack.common.js
const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const CleanWebpackPlugin = require("clean-webpack-plugin");
const CopyWebpackPlugin = require("copy-webpack-plugin");
module.exports = {
entry: {
index: "./webpack.entry.js",
style: "./app/scss/main/app.scss"
},
module: {
rules: [
{
test: /\.js$/,
use: {
loader: "babel-loader",
options: {
presets: ["@babel/preset-env"],
cacheDirectory: true
}
}
}
]
},
plugins: [
new CleanWebpackPlugin(["dist"]),
new HtmlWebpackPlugin({
template: "./app/html/index.html"
})
],
output: {
path: path.resolve(__dirname, "dist")
}
};
webpack.dev.js
const webpack = require("webpack");
const merge = require("webpack-merge");
const common = require("./webpack.common.js");
module.exports = merge(common, {
devtool: "cheap-module-eval-source-map",
devServer: {
contentBase: "./dist",
port: 9000,
hot: true
},
module: {
rules: [
{
test: /\.scss$/,
use: [
{
loader: "style-loader"
},
{
loader: "css-loader",
options: {
sourceMap: true,
url: false
}
},
{
loader: "sass-loader",
options: {
sourceMap: true
}
}
]
}
]
},
plugins: [new webpack.HotModuleReplacementPlugin()],
output: {
filename: "[name].js"
},
mode: "development"
});
app.scss是一堆scss导入,可能导致另一堆scss导入。
@import "../base";
@import "../layout";
@import "../modules";
@import "../state";
@import "../3rdparty";
/* components */
@import "../../component/footer/footer";
@import "../../component/switcher/switcher";
@import "../../component/header/header";
@import "../../component/dropdown/dropdown";
@import "../../component/lgntabset/lgntabset";
@import "../../component/lgnprogressstep/lgnprogressstep";
@import "../../component/lgnwizard/lgnwizard";
@import "../../component/lgnslider/lgnslider";
@import "../../component/lgndropdown/lgndropdown";
@import "../../component/lgnmodal/lgnmodal";
@import "../../component/calendar/weeklycalendar/calendarwidget";
如何改善scss重新编译时间?
答案 0 :(得分:0)
不幸的是,这是一个众所周知的问题:https://github.com/webpack-contrib/sass-loader/issues/296,你可以做很多事情。您的配置是正确的。
还有另一种https://github.com/yibn2008/fast-sass-loader,声称速度提高了5~10倍。我对此没有任何经验,但也许值得一试。
答案 1 :(得分:0)
我最终做了与create-react-app
的{{3}}类似的事情:
将node-sass-chokidar --watch
作为单独的进程运行,每次修改scss文件时都会生成css文件。
在JS和Webpack配置中,引用生成的CSS文件。
这将重新编译时间减少到约1秒,这比fast-sass-loader
(~2秒)更好。
<强>更新强>
如果您不需要解析URL()
和@import
,只需使用raw-loader
,这会将我的重新编译减少到大约0.5秒!