如何获取我的SCSS源并将其编译成(奖金:缩小的)CSS文件?
阅读文档并在网上搜索可以清楚地看到一件事,就是每个人似乎都在做自己的汤,这种通用任务没有标准/推荐的方法。
下面是一个对我有用的最小示例:
package.json:
{
…
"devDependencies": {
"css-loader": "^3.2.0",
"mini-css-extract-plugin": "^0.8.0",
"node-sass": "^4.12.0",
"optimize-css-assets-webpack-plugin": "^5.0.3",
"sass-loader": "^8.0.0",
"webpack": "^4.40.2",
"webpack-cli": "^3.3.9"
}
}
webpack.config.js:
const path = require("path");
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin');
module.exports = {
…
module: {
rules: [
{ // CSS assets remaining in the pipeline (e.g. frameworks)
test: /\.css$/,
use: [
"style-loader", // or e.g. "vue-style-loader" etc. (optional)
MiniCssExtractPlugin.loader,
"css-loader"
]
},
{ // SCSS
test: /\.scss$/,
use: [
"style-loader" // (optional)
MiniCssExtractPlugin.loader,
"css-loader",
"sass-loader"
]
}
]
},
// Plugins
plugins: [
new MiniCssExtractPlugin({
filename: "./css/my-style.css" // relative to `output.path` by default
}),
new OptimizeCssAssetsPlugin() // construction suffices, no additional calls needed
]
};
该node-sass
模块已经输出了缩小的CSS,因此,如果您知道在项目中不会遇到任何原始CSS,则可以摆脱OptimizeCssAssetsPlugin
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句