sass-loader 17.0.0 → 17.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +187 -56
- package/dist/cjs/index.js +5 -1
- package/dist/cjs/utils.js +97 -0
- package/dist/esm/index.js +6 -2
- package/dist/esm/utils.js +97 -1
- package/package.json +1 -1
- package/types/utils.d.ts +29 -0
package/README.md
CHANGED
|
@@ -41,7 +41,9 @@ pnpm add -D sass-loader sass webpack
|
|
|
41
41
|
|
|
42
42
|
> [!NOTE]
|
|
43
43
|
>
|
|
44
|
-
>
|
|
44
|
+
> Webpack has [built-in CSS support](https://webpack.js.org/configuration/experiments/#experimentscss), so no extra loaders are required to process the CSS generated by `sass-loader` - just enable `experiments.css` and set the module `type` to `css/auto`.
|
|
45
|
+
>
|
|
46
|
+
> If you prefer the loader-based setup, install [style-loader](https://webpack.js.org/loaders/style-loader/) and [css-loader](https://webpack.js.org/loaders/css-loader/) via `npm i style-loader css-loader` and chain them with `sass-loader` instead.
|
|
45
47
|
|
|
46
48
|
`sass-loader` requires you to install either [Dart Sass](https://github.com/sass/dart-sass) or [Sass Embedded](https://github.com/sass/embedded-host-node) on your own (more documentation can be found below).
|
|
47
49
|
|
|
@@ -51,7 +53,9 @@ This allows you to control the versions of all your dependencies and to choose w
|
|
|
51
53
|
>
|
|
52
54
|
> We highly recommend using [Sass Embedded](https://github.com/sass/embedded-host-node) or [Dart Sass](https://github.com/sass/dart-sass).
|
|
53
55
|
|
|
54
|
-
|
|
56
|
+
Use the `sass-loader` with the built-in CSS support of webpack (the `css/auto` module type) to let webpack handle the generated CSS - it extracts styles into a separate file and injects them into the document.
|
|
57
|
+
|
|
58
|
+
Alternatively, you can chain the `sass-loader` with the [css-loader](https://github.com/webpack/css-loader) and the [style-loader](https://github.com/webpack/style-loader) to immediately apply all styles to the DOM, or with the [mini-css-extract-plugin](https://github.com/webpack/mini-css-extract-plugin) to extract it into a separate file.
|
|
55
59
|
|
|
56
60
|
Then add the loader to your webpack configuration. For example:
|
|
57
61
|
|
|
@@ -79,58 +83,141 @@ module.exports = {
|
|
|
79
83
|
rules: [
|
|
80
84
|
{
|
|
81
85
|
test: /\.s[ac]ss$/i,
|
|
86
|
+
// Lets webpack handle the generated CSS using its built-in CSS support,
|
|
87
|
+
// `css/auto` also enables CSS modules for `*.module.scss` files
|
|
88
|
+
type: "css/auto",
|
|
82
89
|
use: [
|
|
83
|
-
// Creates `style` nodes from JS strings
|
|
84
|
-
"style-loader",
|
|
85
|
-
// Translates CSS into CommonJS
|
|
86
|
-
"css-loader",
|
|
87
90
|
// Compiles Sass to CSS
|
|
88
91
|
"sass-loader",
|
|
89
92
|
],
|
|
90
93
|
},
|
|
91
94
|
],
|
|
92
95
|
},
|
|
96
|
+
experiments: {
|
|
97
|
+
// Enables the built-in CSS support of webpack
|
|
98
|
+
css: true,
|
|
99
|
+
},
|
|
93
100
|
};
|
|
94
101
|
```
|
|
95
102
|
|
|
96
103
|
Finally run `webpack` via your preferred method (e.g., via CLI or an npm script).
|
|
97
104
|
|
|
105
|
+
> [!NOTE]
|
|
106
|
+
>
|
|
107
|
+
> All examples below use the built-in CSS support of webpack.
|
|
108
|
+
> If you use [css-loader](https://github.com/webpack/css-loader) and [style-loader](https://github.com/webpack/style-loader) (or [mini-css-extract-plugin](https://github.com/webpack/mini-css-extract-plugin)) instead, remove the `type` and `experiments` options and put them before the `sass-loader` in the `use` array:
|
|
109
|
+
>
|
|
110
|
+
> ```js
|
|
111
|
+
> module.exports = {
|
|
112
|
+
> module: {
|
|
113
|
+
> rules: [
|
|
114
|
+
> {
|
|
115
|
+
> test: /\.s[ac]ss$/i,
|
|
116
|
+
> use: ["style-loader", "css-loader", "sass-loader"],
|
|
117
|
+
> },
|
|
118
|
+
> ],
|
|
119
|
+
> },
|
|
120
|
+
> };
|
|
121
|
+
> ```
|
|
122
|
+
|
|
98
123
|
### The `style` option in `production` mode
|
|
99
124
|
|
|
100
125
|
For `production` mode, the `style` option defaults to `compressed` unless otherwise specified in `sassOptions`.
|
|
101
126
|
|
|
102
|
-
### Resolving `import` and `
|
|
127
|
+
### Resolving `import`, `use` and `forward` at-rules
|
|
103
128
|
|
|
104
129
|
Webpack provides an [advanced mechanism to resolve files](https://webpack.js.org/concepts/module-resolution/).
|
|
105
130
|
|
|
106
|
-
The `sass-loader` uses Sass's custom importer feature to pass
|
|
131
|
+
The `sass-loader` uses Sass's custom importer feature to pass every `@use`, `@import` and `@forward` request to the webpack resolving engine, so your webpack [`resolve`](https://webpack.js.org/configuration/resolve/) configuration applies to stylesheets, and you can load Sass modules from `node_modules`:
|
|
132
|
+
|
|
133
|
+
```scss
|
|
134
|
+
@use "bootstrap";
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
#### How a request is resolved
|
|
138
|
+
|
|
139
|
+
For `@use "theme"` inside `src/app.scss`, the loader asks webpack for the following and takes the first hit:
|
|
140
|
+
|
|
141
|
+
1. the partial `src/_theme.sass`, `src/_theme.scss`, `src/_theme.css`
|
|
142
|
+
2. `src/theme.sass`, `src/theme.scss`, `src/theme.css`
|
|
143
|
+
3. `theme` as written, so aliases and package requests resolve
|
|
144
|
+
|
|
145
|
+
Directories resolve through their `_index`/`index` file. For `@import` only, the import-only files `_theme.import.scss` and `theme.import.scss` are tried before everything else.
|
|
146
|
+
|
|
147
|
+
Relative requests win over module ones, so `@use "theme"` behaves like `@use "./theme"` when both could match. Keeping both `_theme.scss` and `theme.scss` in one directory is ambiguous and Sass reports an error for it, so the order within a directory rarely matters.
|
|
148
|
+
|
|
149
|
+
#### What your `resolve` configuration controls
|
|
150
|
+
|
|
151
|
+
- [`alias`](https://webpack.js.org/configuration/resolve/#resolvealias) - applied to every request, and tried before `node_modules`
|
|
152
|
+
- [`modules`](https://webpack.js.org/configuration/resolve/#resolvemodules) - extra directories to look in, e.g. `src`
|
|
153
|
+
- [`byDependency.sass`](https://webpack.js.org/configuration/resolve/#resolvebydependency) - requests are resolved with `dependencyType: "sass"`, so this targets stylesheets only
|
|
154
|
+
- [`plugins`](https://webpack.js.org/configuration/resolve/#resolveplugins), [`symlinks`](https://webpack.js.org/configuration/resolve/#resolvesymlinks), [`roots`](https://webpack.js.org/configuration/resolve/#resolveroots) and the rest of the resolver options
|
|
155
|
+
|
|
156
|
+
**webpack.config.js**
|
|
157
|
+
|
|
158
|
+
```js
|
|
159
|
+
module.exports = {
|
|
160
|
+
resolve: {
|
|
161
|
+
alias: { "@styles": path.resolve(__dirname, "src/styles") },
|
|
162
|
+
modules: [path.resolve(__dirname, "src"), "node_modules"],
|
|
163
|
+
},
|
|
164
|
+
};
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
**style.scss**
|
|
168
|
+
|
|
169
|
+
```scss
|
|
170
|
+
@use "@styles/theme" as *; // resolved by `resolve.alias`
|
|
171
|
+
@use "abstracts" as *; // resolved by `resolve.modules` to `src/abstracts/_index.scss`
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
Some options are fixed to match Sass's own algorithm and can't be changed through `resolve`: the extensions are `.sass`, `.scss` and `.css` (so [`resolve.extensions`](https://webpack.js.org/configuration/resolve/#resolveextensions) doesn't apply here), `mainFiles` prefer `_index`/`index`, `mainFields` prefer `sass` and `style` over `main`, and `conditionNames` prefer the `sass` and `style` [export conditions](https://webpack.js.org/guides/package-exports/). Your own `mainFields` and `conditionNames` are kept after those.
|
|
175
|
+
|
|
176
|
+
#### Packages
|
|
177
|
+
|
|
178
|
+
A package request resolves through the `sass` and `style` conditions of its `exports` field, falling back to the `sass`, `style` and `main` fields. The [`pkg:` URL scheme](https://sass-lang.com/documentation/at-rules/use/#pkg) is supported as well:
|
|
179
|
+
|
|
180
|
+
```scss
|
|
181
|
+
@use "pkg:bootstrap";
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
#### When webpack can't resolve a request
|
|
185
|
+
|
|
186
|
+
The importer hands the request back to Sass, which then applies its own resolution - [`sassOptions.loadPaths`](#sassoptions), the `SASS_PATH` environment variable and any custom importer you configured.
|
|
187
|
+
|
|
188
|
+
#### Plain CSS files
|
|
189
|
+
|
|
190
|
+
Sass compiles `@import "theme.css"` to a plain CSS `@import`, so the loader leaves it in the output untouched - whatever handles the CSS afterwards (the built-in CSS support of webpack, `css-loader`, or the browser) decides what happens with it. `@use "theme.css"` includes the file's content instead, and is resolved like any other request:
|
|
107
191
|
|
|
108
192
|
```scss
|
|
109
|
-
@import "
|
|
193
|
+
@import "theme.css"; // stays `@import "theme.css";` in the output
|
|
194
|
+
@use "theme.css"; // inlines the content of the file
|
|
110
195
|
```
|
|
111
196
|
|
|
197
|
+
#### The `~` prefix
|
|
198
|
+
|
|
112
199
|
Using `~` is deprecated and should be removed from your code, but we still support it for historical reasons.
|
|
113
200
|
|
|
114
|
-
Why can you remove it? The loader will first try to resolve `@
|
|
201
|
+
Why can you remove it? The loader will first try to resolve `@use` as a relative path. If it cannot be resolved, then the loader will try to resolve it inside [`node_modules`](https://webpack.js.org/configuration/resolve/#resolvemodules).
|
|
115
202
|
|
|
116
203
|
Prepending module paths with a `~` tells webpack to search through [`node_modules`](https://webpack.js.org/configuration/resolve/#resolvemodules).
|
|
117
204
|
|
|
118
205
|
```scss
|
|
119
|
-
@
|
|
206
|
+
@use "~bootstrap";
|
|
120
207
|
```
|
|
121
208
|
|
|
122
209
|
It's important to prepend the path with only `~`, because `~/` resolves to the home directory.
|
|
123
210
|
|
|
124
211
|
Webpack needs to distinguish between `bootstrap` and `~bootstrap` because CSS and Sass files have no special syntax for importing relative files.
|
|
125
212
|
|
|
126
|
-
Writing `@
|
|
213
|
+
Writing `@use "style.scss"` is the same as `@use "./style.scss";`
|
|
127
214
|
|
|
128
215
|
### Problems with `url(...)`
|
|
129
216
|
|
|
130
217
|
Since Sass implementations don't provide [url rewriting](https://github.com/sass/libsass/issues/532), all linked assets must be relative to the output.
|
|
131
218
|
|
|
132
|
-
- If
|
|
133
|
-
- If you're just generating CSS without
|
|
219
|
+
- If webpack handles the generated CSS (i.e. the built-in CSS support or the `css-loader`), all URLs must be relative to the entry-file (e.g. `main.scss`).
|
|
220
|
+
- If you're just generating CSS without letting webpack handle it, URLs must be relative to your web root.
|
|
134
221
|
|
|
135
222
|
You might be surprised by this first issue, as it is natural to expect relative references to be resolved against the `.sass`/`.scss` file in which they are specified (like in regular `.css` files).
|
|
136
223
|
|
|
@@ -215,9 +302,8 @@ module.exports = {
|
|
|
215
302
|
rules: [
|
|
216
303
|
{
|
|
217
304
|
test: /\.s[ac]ss$/i,
|
|
305
|
+
type: "css/auto",
|
|
218
306
|
use: [
|
|
219
|
-
"style-loader",
|
|
220
|
-
"css-loader",
|
|
221
307
|
{
|
|
222
308
|
loader: "sass-loader",
|
|
223
309
|
options: {
|
|
@@ -229,6 +315,9 @@ module.exports = {
|
|
|
229
315
|
},
|
|
230
316
|
],
|
|
231
317
|
},
|
|
318
|
+
experiments: {
|
|
319
|
+
css: true,
|
|
320
|
+
},
|
|
232
321
|
};
|
|
233
322
|
```
|
|
234
323
|
|
|
@@ -242,9 +331,8 @@ module.exports = {
|
|
|
242
331
|
rules: [
|
|
243
332
|
{
|
|
244
333
|
test: /\.s[ac]ss$/i,
|
|
334
|
+
type: "css/auto",
|
|
245
335
|
use: [
|
|
246
|
-
"style-loader",
|
|
247
|
-
"css-loader",
|
|
248
336
|
{
|
|
249
337
|
loader: "sass-loader",
|
|
250
338
|
options: {
|
|
@@ -256,6 +344,9 @@ module.exports = {
|
|
|
256
344
|
},
|
|
257
345
|
],
|
|
258
346
|
},
|
|
347
|
+
experiments: {
|
|
348
|
+
css: true,
|
|
349
|
+
},
|
|
259
350
|
};
|
|
260
351
|
```
|
|
261
352
|
|
|
@@ -308,9 +399,8 @@ module.exports = {
|
|
|
308
399
|
rules: [
|
|
309
400
|
{
|
|
310
401
|
test: /\.s[ac]ss$/i,
|
|
402
|
+
type: "css/auto",
|
|
311
403
|
use: [
|
|
312
|
-
"style-loader",
|
|
313
|
-
"css-loader",
|
|
314
404
|
{
|
|
315
405
|
loader: "sass-loader",
|
|
316
406
|
options: {
|
|
@@ -324,6 +414,9 @@ module.exports = {
|
|
|
324
414
|
},
|
|
325
415
|
],
|
|
326
416
|
},
|
|
417
|
+
experiments: {
|
|
418
|
+
css: true,
|
|
419
|
+
},
|
|
327
420
|
};
|
|
328
421
|
```
|
|
329
422
|
|
|
@@ -337,9 +430,8 @@ module.exports = {
|
|
|
337
430
|
rules: [
|
|
338
431
|
{
|
|
339
432
|
test: /\.s[ac]ss$/i,
|
|
433
|
+
type: "css/auto",
|
|
340
434
|
use: [
|
|
341
|
-
"style-loader",
|
|
342
|
-
"css-loader",
|
|
343
435
|
{
|
|
344
436
|
loader: "sass-loader",
|
|
345
437
|
options: {
|
|
@@ -364,6 +456,9 @@ module.exports = {
|
|
|
364
456
|
},
|
|
365
457
|
],
|
|
366
458
|
},
|
|
459
|
+
experiments: {
|
|
460
|
+
css: true,
|
|
461
|
+
},
|
|
367
462
|
};
|
|
368
463
|
```
|
|
369
464
|
|
|
@@ -392,14 +487,8 @@ module.exports = {
|
|
|
392
487
|
rules: [
|
|
393
488
|
{
|
|
394
489
|
test: /\.s[ac]ss$/i,
|
|
490
|
+
type: "css/auto",
|
|
395
491
|
use: [
|
|
396
|
-
"style-loader",
|
|
397
|
-
{
|
|
398
|
-
loader: "css-loader",
|
|
399
|
-
options: {
|
|
400
|
-
sourceMap: true,
|
|
401
|
-
},
|
|
402
|
-
},
|
|
403
492
|
{
|
|
404
493
|
loader: "sass-loader",
|
|
405
494
|
options: {
|
|
@@ -410,6 +499,9 @@ module.exports = {
|
|
|
410
499
|
},
|
|
411
500
|
],
|
|
412
501
|
},
|
|
502
|
+
experiments: {
|
|
503
|
+
css: true,
|
|
504
|
+
},
|
|
413
505
|
};
|
|
414
506
|
```
|
|
415
507
|
|
|
@@ -421,9 +513,8 @@ module.exports = {
|
|
|
421
513
|
rules: [
|
|
422
514
|
{
|
|
423
515
|
test: /\.s[ac]ss$/i,
|
|
516
|
+
type: "css/auto",
|
|
424
517
|
use: [
|
|
425
|
-
"style-loader",
|
|
426
|
-
"css-loader",
|
|
427
518
|
{
|
|
428
519
|
loader: "sass-loader",
|
|
429
520
|
options: {
|
|
@@ -437,6 +528,9 @@ module.exports = {
|
|
|
437
528
|
},
|
|
438
529
|
],
|
|
439
530
|
},
|
|
531
|
+
experiments: {
|
|
532
|
+
css: true,
|
|
533
|
+
},
|
|
440
534
|
};
|
|
441
535
|
```
|
|
442
536
|
|
|
@@ -465,9 +559,8 @@ module.exports = {
|
|
|
465
559
|
rules: [
|
|
466
560
|
{
|
|
467
561
|
test: /\.s[ac]ss$/i,
|
|
562
|
+
type: "css/auto",
|
|
468
563
|
use: [
|
|
469
|
-
"style-loader",
|
|
470
|
-
"css-loader",
|
|
471
564
|
{
|
|
472
565
|
loader: "sass-loader",
|
|
473
566
|
options: {
|
|
@@ -478,6 +571,9 @@ module.exports = {
|
|
|
478
571
|
},
|
|
479
572
|
],
|
|
480
573
|
},
|
|
574
|
+
experiments: {
|
|
575
|
+
css: true,
|
|
576
|
+
},
|
|
481
577
|
};
|
|
482
578
|
```
|
|
483
579
|
|
|
@@ -491,9 +587,8 @@ module.exports = {
|
|
|
491
587
|
rules: [
|
|
492
588
|
{
|
|
493
589
|
test: /\.s[ac]ss$/i,
|
|
590
|
+
type: "css/auto",
|
|
494
591
|
use: [
|
|
495
|
-
"style-loader",
|
|
496
|
-
"css-loader",
|
|
497
592
|
{
|
|
498
593
|
loader: "sass-loader",
|
|
499
594
|
options: {
|
|
@@ -514,6 +609,9 @@ module.exports = {
|
|
|
514
609
|
},
|
|
515
610
|
],
|
|
516
611
|
},
|
|
612
|
+
experiments: {
|
|
613
|
+
css: true,
|
|
614
|
+
},
|
|
517
615
|
};
|
|
518
616
|
```
|
|
519
617
|
|
|
@@ -525,9 +623,8 @@ module.exports = {
|
|
|
525
623
|
rules: [
|
|
526
624
|
{
|
|
527
625
|
test: /\.s[ac]ss$/i,
|
|
626
|
+
type: "css/auto",
|
|
528
627
|
use: [
|
|
529
|
-
"style-loader",
|
|
530
|
-
"css-loader",
|
|
531
628
|
{
|
|
532
629
|
loader: "sass-loader",
|
|
533
630
|
options: {
|
|
@@ -548,6 +645,9 @@ module.exports = {
|
|
|
548
645
|
},
|
|
549
646
|
],
|
|
550
647
|
},
|
|
648
|
+
experiments: {
|
|
649
|
+
css: true,
|
|
650
|
+
},
|
|
551
651
|
};
|
|
552
652
|
```
|
|
553
653
|
|
|
@@ -574,9 +674,8 @@ module.exports = {
|
|
|
574
674
|
rules: [
|
|
575
675
|
{
|
|
576
676
|
test: /\.s[ac]ss$/i,
|
|
677
|
+
type: "css/auto",
|
|
577
678
|
use: [
|
|
578
|
-
"style-loader",
|
|
579
|
-
"css-loader",
|
|
580
679
|
{
|
|
581
680
|
loader: "sass-loader",
|
|
582
681
|
options: {
|
|
@@ -587,6 +686,9 @@ module.exports = {
|
|
|
587
686
|
},
|
|
588
687
|
],
|
|
589
688
|
},
|
|
689
|
+
experiments: {
|
|
690
|
+
css: true,
|
|
691
|
+
},
|
|
590
692
|
};
|
|
591
693
|
```
|
|
592
694
|
|
|
@@ -636,9 +738,8 @@ module.exports = {
|
|
|
636
738
|
rules: [
|
|
637
739
|
{
|
|
638
740
|
test: /\.s[ac]ss$/i,
|
|
741
|
+
type: "css/auto",
|
|
639
742
|
use: [
|
|
640
|
-
"style-loader",
|
|
641
|
-
"css-loader",
|
|
642
743
|
{
|
|
643
744
|
loader: "sass-loader",
|
|
644
745
|
options: {
|
|
@@ -649,6 +750,9 @@ module.exports = {
|
|
|
649
750
|
},
|
|
650
751
|
],
|
|
651
752
|
},
|
|
753
|
+
experiments: {
|
|
754
|
+
css: true,
|
|
755
|
+
},
|
|
652
756
|
};
|
|
653
757
|
```
|
|
654
758
|
|
|
@@ -682,9 +786,8 @@ module.exports = {
|
|
|
682
786
|
rules: [
|
|
683
787
|
{
|
|
684
788
|
test: /\.s[ac]ss$/i,
|
|
789
|
+
type: "css/auto",
|
|
685
790
|
use: [
|
|
686
|
-
"style-loader",
|
|
687
|
-
"css-loader",
|
|
688
791
|
{
|
|
689
792
|
loader: "sass-loader",
|
|
690
793
|
options: {
|
|
@@ -698,6 +801,9 @@ module.exports = {
|
|
|
698
801
|
},
|
|
699
802
|
],
|
|
700
803
|
},
|
|
804
|
+
experiments: {
|
|
805
|
+
css: true,
|
|
806
|
+
},
|
|
701
807
|
};
|
|
702
808
|
```
|
|
703
809
|
|
|
@@ -721,9 +827,37 @@ module.exports = {
|
|
|
721
827
|
|
|
722
828
|
For production builds, it's recommended to extract the CSS from your bundle to enable parallel loading of CSS/JS resources.
|
|
723
829
|
|
|
724
|
-
There are
|
|
830
|
+
There are five recommended ways to extract a stylesheet from a bundle:
|
|
831
|
+
|
|
832
|
+
#### 1. [Built-in CSS support](https://webpack.js.org/configuration/experiments/#experimentscss)
|
|
725
833
|
|
|
726
|
-
|
|
834
|
+
Webpack emits CSS into separate files on its own, so nothing but `experiments.css` is required.
|
|
835
|
+
|
|
836
|
+
**webpack.config.js**
|
|
837
|
+
|
|
838
|
+
```js
|
|
839
|
+
module.exports = {
|
|
840
|
+
module: {
|
|
841
|
+
rules: [
|
|
842
|
+
{
|
|
843
|
+
test: /\.s[ac]ss$/i,
|
|
844
|
+
type: "css/auto",
|
|
845
|
+
use: ["sass-loader"],
|
|
846
|
+
},
|
|
847
|
+
],
|
|
848
|
+
},
|
|
849
|
+
output: {
|
|
850
|
+
// Both options are optional
|
|
851
|
+
cssFilename: "[name].css",
|
|
852
|
+
cssChunkFilename: "[id].css",
|
|
853
|
+
},
|
|
854
|
+
experiments: {
|
|
855
|
+
css: true,
|
|
856
|
+
},
|
|
857
|
+
};
|
|
858
|
+
```
|
|
859
|
+
|
|
860
|
+
#### 2. [mini-css-extract-plugin](https://github.com/webpack/mini-css-extract-plugin)
|
|
727
861
|
|
|
728
862
|
**webpack.config.js**
|
|
729
863
|
|
|
@@ -757,7 +891,7 @@ module.exports = {
|
|
|
757
891
|
};
|
|
758
892
|
```
|
|
759
893
|
|
|
760
|
-
####
|
|
894
|
+
#### 3. [Asset Modules](https://webpack.js.org/guides/asset-modules/)
|
|
761
895
|
|
|
762
896
|
**webpack.config.js**
|
|
763
897
|
|
|
@@ -787,9 +921,9 @@ module.exports = {
|
|
|
787
921
|
};
|
|
788
922
|
```
|
|
789
923
|
|
|
790
|
-
####
|
|
924
|
+
#### 4. [extract-loader](https://github.com/peerigon/extract-loader) (simpler, but specialized on the css-loader's output)
|
|
791
925
|
|
|
792
|
-
####
|
|
926
|
+
#### 5. [file-loader](https://github.com/webpack-contrib/file-loader) (deprecated--should only be used in webpack v4)
|
|
793
927
|
|
|
794
928
|
**webpack.config.js**
|
|
795
929
|
|
|
@@ -827,7 +961,7 @@ module.exports = {
|
|
|
827
961
|
|
|
828
962
|
Enables/disables generation of source maps.
|
|
829
963
|
|
|
830
|
-
To enable CSS source maps, you'll need to pass the `sourceMap` option to
|
|
964
|
+
To enable CSS source maps, you'll need to pass the `sourceMap` option to the `sass-loader` (and to the `css-loader` too, when you use it).
|
|
831
965
|
|
|
832
966
|
**webpack.config.js**
|
|
833
967
|
|
|
@@ -838,14 +972,8 @@ module.exports = {
|
|
|
838
972
|
rules: [
|
|
839
973
|
{
|
|
840
974
|
test: /\.s[ac]ss$/i,
|
|
975
|
+
type: "css/auto",
|
|
841
976
|
use: [
|
|
842
|
-
"style-loader",
|
|
843
|
-
{
|
|
844
|
-
loader: "css-loader",
|
|
845
|
-
options: {
|
|
846
|
-
sourceMap: true,
|
|
847
|
-
},
|
|
848
|
-
},
|
|
849
977
|
{
|
|
850
978
|
loader: "sass-loader",
|
|
851
979
|
options: {
|
|
@@ -856,6 +984,9 @@ module.exports = {
|
|
|
856
984
|
},
|
|
857
985
|
],
|
|
858
986
|
},
|
|
987
|
+
experiments: {
|
|
988
|
+
css: true,
|
|
989
|
+
},
|
|
859
990
|
};
|
|
860
991
|
```
|
|
861
992
|
|
package/dist/cjs/index.js
CHANGED
|
@@ -62,6 +62,10 @@ async function loader(content) {
|
|
|
62
62
|
if (map && useSourceMap) {
|
|
63
63
|
map = (0, _utils.normalizeSourceMap)(map, this.rootContext);
|
|
64
64
|
}
|
|
65
|
+
const {
|
|
66
|
+
css,
|
|
67
|
+
map: cssMap
|
|
68
|
+
} = (0, _utils.removeBOM)(result.css.toString(), map);
|
|
65
69
|
if (typeof result.loadedUrls !== "undefined") {
|
|
66
70
|
for (const includedFile of result.loadedUrls.filter(loadedUrl => loadedUrl.protocol === "file:")) {
|
|
67
71
|
const normalizedIncludedFile = _nodeUrl.default.fileURLToPath(includedFile);
|
|
@@ -72,7 +76,7 @@ async function loader(content) {
|
|
|
72
76
|
}
|
|
73
77
|
}
|
|
74
78
|
}
|
|
75
|
-
callback(null,
|
|
79
|
+
callback(null, css, cssMap || undefined);
|
|
76
80
|
}
|
|
77
81
|
var _default = exports.default = loader;module.exports = exports.default;
|
|
78
82
|
module.exports.default = exports.default;
|
package/dist/cjs/utils.js
CHANGED
|
@@ -10,6 +10,7 @@ exports.getSassImplementation = getSassImplementation;
|
|
|
10
10
|
exports.getSassOptions = getSassOptions;
|
|
11
11
|
exports.getWebpackResolver = getWebpackResolver;
|
|
12
12
|
exports.normalizeSourceMap = normalizeSourceMap;
|
|
13
|
+
exports.removeBOM = removeBOM;
|
|
13
14
|
var _nodePath = _interopRequireDefault(require("node:path"));
|
|
14
15
|
var _nodeUrl = _interopRequireDefault(require("node:url"));
|
|
15
16
|
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
@@ -641,6 +642,102 @@ function normalizeSourceMap(map, rootContext) {
|
|
|
641
642
|
});
|
|
642
643
|
return newMap;
|
|
643
644
|
}
|
|
645
|
+
const BASE64_CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
646
|
+
const VLQ_BASE_SHIFT = 5;
|
|
647
|
+
const VLQ_BASE = 1 << VLQ_BASE_SHIFT;
|
|
648
|
+
const VLQ_BASE_MASK = VLQ_BASE - 1;
|
|
649
|
+
const VLQ_CONTINUATION_BIT = VLQ_BASE;
|
|
650
|
+
|
|
651
|
+
/**
|
|
652
|
+
* Decodes the first Base64 VLQ value of the given `mappings`.
|
|
653
|
+
* @param {string} mappings mappings of a source map
|
|
654
|
+
* @returns {{ value: number, length: number } | null} the decoded value and how many characters it takes, `null` when `mappings` doesn't start with a value
|
|
655
|
+
*/
|
|
656
|
+
function decodeVLQ(mappings) {
|
|
657
|
+
let value = 0;
|
|
658
|
+
let shift = 0;
|
|
659
|
+
let length = 0;
|
|
660
|
+
let digit;
|
|
661
|
+
do {
|
|
662
|
+
digit = BASE64_CHARACTERS.indexOf(mappings[length]);
|
|
663
|
+
if (digit === -1) {
|
|
664
|
+
return null;
|
|
665
|
+
}
|
|
666
|
+
value += (digit & VLQ_BASE_MASK) << shift;
|
|
667
|
+
shift += VLQ_BASE_SHIFT;
|
|
668
|
+
length += 1;
|
|
669
|
+
} while ((digit & VLQ_CONTINUATION_BIT) !== 0);
|
|
670
|
+
const isNegative = (value & 1) === 1;
|
|
671
|
+
value >>= 1;
|
|
672
|
+
return {
|
|
673
|
+
value: isNegative ? -value : value,
|
|
674
|
+
length
|
|
675
|
+
};
|
|
676
|
+
}
|
|
677
|
+
|
|
678
|
+
/**
|
|
679
|
+
* @param {number} value value
|
|
680
|
+
* @returns {string} the Base64 VLQ encoded value
|
|
681
|
+
*/
|
|
682
|
+
function encodeVLQ(value) {
|
|
683
|
+
let encoded = "";
|
|
684
|
+
let vlq = value < 0 ? -value << 1 | 1 : value << 1;
|
|
685
|
+
do {
|
|
686
|
+
let digit = vlq & VLQ_BASE_MASK;
|
|
687
|
+
vlq >>>= VLQ_BASE_SHIFT;
|
|
688
|
+
if (vlq > 0) {
|
|
689
|
+
digit |= VLQ_CONTINUATION_BIT;
|
|
690
|
+
}
|
|
691
|
+
encoded += BASE64_CHARACTERS[digit];
|
|
692
|
+
} while (vlq > 0);
|
|
693
|
+
return encoded;
|
|
694
|
+
}
|
|
695
|
+
|
|
696
|
+
/**
|
|
697
|
+
* Removes the byte order mark (BOM) from the compiled CSS.
|
|
698
|
+
*
|
|
699
|
+
* `dart-sass` prepends a BOM when the `charset` option is enabled (by default),
|
|
700
|
+
* the `style` option is `compressed` (the default for the `production` mode) and
|
|
701
|
+
* the compiled CSS contains non ASCII characters.
|
|
702
|
+
* A BOM is only meaningful at the very beginning of a file, but the loader result
|
|
703
|
+
* is just a string for webpack, and tools like `css-loader` move `@import` at-rules
|
|
704
|
+
* above it, so the BOM ends up in the middle of the generated CSS and breaks the
|
|
705
|
+
* first rule after it.
|
|
706
|
+
*
|
|
707
|
+
* Webpack removes a BOM a loader produced itself since
|
|
708
|
+
* https://github.com/webpack/webpack/pull/21857 and keeps the source map in sync
|
|
709
|
+
* with it since https://github.com/webpack/webpack/pull/21861, which makes this a
|
|
710
|
+
* no-op there. It is still needed for the webpack versions the `peerDependencies`
|
|
711
|
+
* range allows and for `rspack`, which passes a string result through untouched.
|
|
712
|
+
* Remove it once both handle this in the minimum version we support.
|
|
713
|
+
* @see https://github.com/webpack/sass-loader/issues/1335
|
|
714
|
+
* @param {string} css compiled CSS
|
|
715
|
+
* @param {RawSourceMap=} map source map
|
|
716
|
+
* @returns {{ css: string, map: RawSourceMap | undefined }} the CSS without the BOM and the source map adjusted to it
|
|
717
|
+
*/
|
|
718
|
+
function removeBOM(css, map) {
|
|
719
|
+
if (css.charCodeAt(0) !== 0xfe_ff) {
|
|
720
|
+
return {
|
|
721
|
+
css,
|
|
722
|
+
map
|
|
723
|
+
};
|
|
724
|
+
}
|
|
725
|
+
|
|
726
|
+
// Sass counts the BOM as the first column of the first line, so all mappings
|
|
727
|
+
// of this line need to be shifted by one column.
|
|
728
|
+
// Only the first segment has to be updated - the generated column of the
|
|
729
|
+
// segments after it is relative to the previous one.
|
|
730
|
+
if (map && typeof map.mappings === "string") {
|
|
731
|
+
const decoded = decodeVLQ(map.mappings);
|
|
732
|
+
if (decoded && decoded.value > 0) {
|
|
733
|
+
map.mappings = `${encodeVLQ(decoded.value - 1)}${map.mappings.slice(decoded.length)}`;
|
|
734
|
+
}
|
|
735
|
+
}
|
|
736
|
+
return {
|
|
737
|
+
css: css.slice(1),
|
|
738
|
+
map
|
|
739
|
+
};
|
|
740
|
+
}
|
|
644
741
|
|
|
645
742
|
/**
|
|
646
743
|
* @param {Error | SassError} error the original sass error
|
package/dist/esm/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import path from "node:path";
|
|
2
2
|
import url from "node:url";
|
|
3
3
|
import schema from "./options.json" with { type: "json" };
|
|
4
|
-
import { errorFactory, getCompileFn, getModernWebpackImporter, getSassImplementation, getSassOptions, normalizeSourceMap } from "./utils.js";
|
|
4
|
+
import { errorFactory, getCompileFn, getModernWebpackImporter, getSassImplementation, getSassOptions, normalizeSourceMap, removeBOM } from "./utils.js";
|
|
5
5
|
|
|
6
6
|
/** @typedef {import("webpack").LoaderContext<LoaderOptions>} LoaderContext */
|
|
7
7
|
/** @typedef {import("schema-utils/declarations/validate").Schema} Schema */
|
|
@@ -56,6 +56,10 @@ async function loader(content) {
|
|
|
56
56
|
if (map && useSourceMap) {
|
|
57
57
|
map = normalizeSourceMap(map, this.rootContext);
|
|
58
58
|
}
|
|
59
|
+
const {
|
|
60
|
+
css,
|
|
61
|
+
map: cssMap
|
|
62
|
+
} = removeBOM(result.css.toString(), map);
|
|
59
63
|
if (typeof result.loadedUrls !== "undefined") {
|
|
60
64
|
for (const includedFile of result.loadedUrls.filter(loadedUrl => loadedUrl.protocol === "file:")) {
|
|
61
65
|
const normalizedIncludedFile = url.fileURLToPath(includedFile);
|
|
@@ -66,6 +70,6 @@ async function loader(content) {
|
|
|
66
70
|
}
|
|
67
71
|
}
|
|
68
72
|
}
|
|
69
|
-
callback(null,
|
|
73
|
+
callback(null, css, cssMap || undefined);
|
|
70
74
|
}
|
|
71
75
|
export default loader;
|
package/dist/esm/utils.js
CHANGED
|
@@ -629,6 +629,102 @@ function normalizeSourceMap(map, rootContext) {
|
|
|
629
629
|
});
|
|
630
630
|
return newMap;
|
|
631
631
|
}
|
|
632
|
+
const BASE64_CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
633
|
+
const VLQ_BASE_SHIFT = 5;
|
|
634
|
+
const VLQ_BASE = 1 << VLQ_BASE_SHIFT;
|
|
635
|
+
const VLQ_BASE_MASK = VLQ_BASE - 1;
|
|
636
|
+
const VLQ_CONTINUATION_BIT = VLQ_BASE;
|
|
637
|
+
|
|
638
|
+
/**
|
|
639
|
+
* Decodes the first Base64 VLQ value of the given `mappings`.
|
|
640
|
+
* @param {string} mappings mappings of a source map
|
|
641
|
+
* @returns {{ value: number, length: number } | null} the decoded value and how many characters it takes, `null` when `mappings` doesn't start with a value
|
|
642
|
+
*/
|
|
643
|
+
function decodeVLQ(mappings) {
|
|
644
|
+
let value = 0;
|
|
645
|
+
let shift = 0;
|
|
646
|
+
let length = 0;
|
|
647
|
+
let digit;
|
|
648
|
+
do {
|
|
649
|
+
digit = BASE64_CHARACTERS.indexOf(mappings[length]);
|
|
650
|
+
if (digit === -1) {
|
|
651
|
+
return null;
|
|
652
|
+
}
|
|
653
|
+
value += (digit & VLQ_BASE_MASK) << shift;
|
|
654
|
+
shift += VLQ_BASE_SHIFT;
|
|
655
|
+
length += 1;
|
|
656
|
+
} while ((digit & VLQ_CONTINUATION_BIT) !== 0);
|
|
657
|
+
const isNegative = (value & 1) === 1;
|
|
658
|
+
value >>= 1;
|
|
659
|
+
return {
|
|
660
|
+
value: isNegative ? -value : value,
|
|
661
|
+
length
|
|
662
|
+
};
|
|
663
|
+
}
|
|
664
|
+
|
|
665
|
+
/**
|
|
666
|
+
* @param {number} value value
|
|
667
|
+
* @returns {string} the Base64 VLQ encoded value
|
|
668
|
+
*/
|
|
669
|
+
function encodeVLQ(value) {
|
|
670
|
+
let encoded = "";
|
|
671
|
+
let vlq = value < 0 ? -value << 1 | 1 : value << 1;
|
|
672
|
+
do {
|
|
673
|
+
let digit = vlq & VLQ_BASE_MASK;
|
|
674
|
+
vlq >>>= VLQ_BASE_SHIFT;
|
|
675
|
+
if (vlq > 0) {
|
|
676
|
+
digit |= VLQ_CONTINUATION_BIT;
|
|
677
|
+
}
|
|
678
|
+
encoded += BASE64_CHARACTERS[digit];
|
|
679
|
+
} while (vlq > 0);
|
|
680
|
+
return encoded;
|
|
681
|
+
}
|
|
682
|
+
|
|
683
|
+
/**
|
|
684
|
+
* Removes the byte order mark (BOM) from the compiled CSS.
|
|
685
|
+
*
|
|
686
|
+
* `dart-sass` prepends a BOM when the `charset` option is enabled (by default),
|
|
687
|
+
* the `style` option is `compressed` (the default for the `production` mode) and
|
|
688
|
+
* the compiled CSS contains non ASCII characters.
|
|
689
|
+
* A BOM is only meaningful at the very beginning of a file, but the loader result
|
|
690
|
+
* is just a string for webpack, and tools like `css-loader` move `@import` at-rules
|
|
691
|
+
* above it, so the BOM ends up in the middle of the generated CSS and breaks the
|
|
692
|
+
* first rule after it.
|
|
693
|
+
*
|
|
694
|
+
* Webpack removes a BOM a loader produced itself since
|
|
695
|
+
* https://github.com/webpack/webpack/pull/21857 and keeps the source map in sync
|
|
696
|
+
* with it since https://github.com/webpack/webpack/pull/21861, which makes this a
|
|
697
|
+
* no-op there. It is still needed for the webpack versions the `peerDependencies`
|
|
698
|
+
* range allows and for `rspack`, which passes a string result through untouched.
|
|
699
|
+
* Remove it once both handle this in the minimum version we support.
|
|
700
|
+
* @see https://github.com/webpack/sass-loader/issues/1335
|
|
701
|
+
* @param {string} css compiled CSS
|
|
702
|
+
* @param {RawSourceMap=} map source map
|
|
703
|
+
* @returns {{ css: string, map: RawSourceMap | undefined }} the CSS without the BOM and the source map adjusted to it
|
|
704
|
+
*/
|
|
705
|
+
function removeBOM(css, map) {
|
|
706
|
+
if (css.charCodeAt(0) !== 0xfe_ff) {
|
|
707
|
+
return {
|
|
708
|
+
css,
|
|
709
|
+
map
|
|
710
|
+
};
|
|
711
|
+
}
|
|
712
|
+
|
|
713
|
+
// Sass counts the BOM as the first column of the first line, so all mappings
|
|
714
|
+
// of this line need to be shifted by one column.
|
|
715
|
+
// Only the first segment has to be updated - the generated column of the
|
|
716
|
+
// segments after it is relative to the previous one.
|
|
717
|
+
if (map && typeof map.mappings === "string") {
|
|
718
|
+
const decoded = decodeVLQ(map.mappings);
|
|
719
|
+
if (decoded && decoded.value > 0) {
|
|
720
|
+
map.mappings = `${encodeVLQ(decoded.value - 1)}${map.mappings.slice(decoded.length)}`;
|
|
721
|
+
}
|
|
722
|
+
}
|
|
723
|
+
return {
|
|
724
|
+
css: css.slice(1),
|
|
725
|
+
map
|
|
726
|
+
};
|
|
727
|
+
}
|
|
632
728
|
|
|
633
729
|
/**
|
|
634
730
|
* @param {Error | SassError} error the original sass error
|
|
@@ -644,4 +740,4 @@ function errorFactory(error) {
|
|
|
644
740
|
obj.stack = undefined;
|
|
645
741
|
return obj;
|
|
646
742
|
}
|
|
647
|
-
export { errorFactory, getCompileFn, getModernWebpackImporter, getSassImplementation, getSassOptions, getWebpackResolver, normalizeSourceMap };
|
|
743
|
+
export { errorFactory, getCompileFn, getModernWebpackImporter, getSassImplementation, getSassOptions, getWebpackResolver, normalizeSourceMap, removeBOM };
|
package/package.json
CHANGED
package/types/utils.d.ts
CHANGED
|
@@ -292,3 +292,32 @@ export function normalizeSourceMap(
|
|
|
292
292
|
map: RawSourceMap,
|
|
293
293
|
rootContext: string,
|
|
294
294
|
): RawSourceMap;
|
|
295
|
+
/**
|
|
296
|
+
* Removes the byte order mark (BOM) from the compiled CSS.
|
|
297
|
+
*
|
|
298
|
+
* `dart-sass` prepends a BOM when the `charset` option is enabled (by default),
|
|
299
|
+
* the `style` option is `compressed` (the default for the `production` mode) and
|
|
300
|
+
* the compiled CSS contains non ASCII characters.
|
|
301
|
+
* A BOM is only meaningful at the very beginning of a file, but the loader result
|
|
302
|
+
* is just a string for webpack, and tools like `css-loader` move `@import` at-rules
|
|
303
|
+
* above it, so the BOM ends up in the middle of the generated CSS and breaks the
|
|
304
|
+
* first rule after it.
|
|
305
|
+
*
|
|
306
|
+
* Webpack removes a BOM a loader produced itself since
|
|
307
|
+
* https://github.com/webpack/webpack/pull/21857 and keeps the source map in sync
|
|
308
|
+
* with it since https://github.com/webpack/webpack/pull/21861, which makes this a
|
|
309
|
+
* no-op there. It is still needed for the webpack versions the `peerDependencies`
|
|
310
|
+
* range allows and for `rspack`, which passes a string result through untouched.
|
|
311
|
+
* Remove it once both handle this in the minimum version we support.
|
|
312
|
+
* @see https://github.com/webpack/sass-loader/issues/1335
|
|
313
|
+
* @param {string} css compiled CSS
|
|
314
|
+
* @param {RawSourceMap=} map source map
|
|
315
|
+
* @returns {{ css: string, map: RawSourceMap | undefined }} the CSS without the BOM and the source map adjusted to it
|
|
316
|
+
*/
|
|
317
|
+
export function removeBOM(
|
|
318
|
+
css: string,
|
|
319
|
+
map?: RawSourceMap | undefined,
|
|
320
|
+
): {
|
|
321
|
+
css: string;
|
|
322
|
+
map: RawSourceMap | undefined;
|
|
323
|
+
};
|