Compile SCSS with Webpack
Nowdays, the frameworks supply a whole configs to compile our project.
If you just want to compile .scss
file, gulp
may help us to do that. But I faced that it can not recognize ~
grammar, so I turn to webpack.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const OptimizeCSSAssetsPlugin = require("optimize-css-assets-webpack-plugin");
const path = require("path")
module.exports = {
entry: './packages/src/main.scss',
output: {
path: path.resolve(__dirname, 'lib')
},
module: {
rules: [
// Extracts the compiled CSS from the SASS files defined in the entry
{
test: /\.scss$/,
use: [
{
loader: MiniCssExtractPlugin.loader
},
{
// Interprets CSS
loader: "css-loader",
options: {
importLoaders: 2
}
},
{
loader: 'sass-loader' // 将 Sass 编译成 CSS
}
]
}
],
},
plugins: [
// Where the compiled SASS is saved to
new MiniCssExtractPlugin({
filename: 'index.css',
allChunks: true,
})
],
optimization: {
minimizer: [
new OptimizeCSSAssetsPlugin({
cssProcessorOptions: {
safe: true
}
})
]
},
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
{
...
"devDependencies": {
"css-loader": "^2.1.0",
"mini-css-extract-plugin": "^0.5.0",
"node-sass": "^4.11.0",
"optimize-css-assets-webpack-plugin": "^5.0.1",
"sass-loader": "^7.1.0",
"webpack": "^4.29.6",
"webpack-cli": "^3.2.3",
"webpack-dev-server": "^3.2.1"
}
...
}
You may want to delete the main.js
in output path, you can try this package:webpack-fix-style-only-entries
Demo:
const FixStyleOnlyEntriesPlugin = require("webpack-fix-style-only-entries")
...
plugins: [
new FixStyleOnlyEntriesPlugin(),
...
],
...
👇 Please leave your comment if you like this.👇