下面是一个对我有用的最小示例:
包.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.
喵喵时光机
哆啦的时光机
相关分类