sass-loader 17.0.0 → 17.1.0

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 CHANGED
@@ -41,7 +41,9 @@ pnpm add -D sass-loader sass webpack
41
41
 
42
42
  > [!NOTE]
43
43
  >
44
- > To enable CSS processing in your project, you need to 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`.
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
- 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.
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 `use` at-rules
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 all queries to the webpack resolving engine, enabling you to import your Sass modules from `node_modules`.
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 "bootstrap";
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 `@import` as a relative path. If it cannot be resolved, then the loader will try to resolve `@import` inside [`node_modules`](https://webpack.js.org/configuration/resolve/#resolvemodules).
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
- @import "~bootstrap";
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 `@import "style.scss"` is the same as `@import "./style.scss";`
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 you pass the generated CSS on to the `css-loader`, all URLs must be relative to the entry-file (e.g. `main.scss`).
133
- - If you're just generating CSS without passing it to the `css-loader`, URLs must be relative to your web root.
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 four recommended ways to extract a stylesheet from a bundle:
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
- #### 1. [mini-css-extract-plugin](https://github.com/webpack/mini-css-extract-plugin)
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
- #### 2. [Asset Modules](https://webpack.js.org/guides/asset-modules/)
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
- #### 3. [extract-loader](https://github.com/peerigon/extract-loader) (simpler, but specialized on the css-loader's output)
924
+ #### 4. [extract-loader](https://github.com/peerigon/extract-loader) (simpler, but specialized on the css-loader's output)
791
925
 
792
- #### 4. [file-loader](https://github.com/webpack-contrib/file-loader) (deprecated--should only be used in webpack v4)
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 both the `sass-loader` _and_ the `css-loader`.
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,17 +62,22 @@ 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);
68
72
 
69
- // Custom `importer` can return only `contents` so includedFile will be relative
70
- if (_nodePath.default.isAbsolute(normalizedIncludedFile)) {
73
+ // Custom `importer` can return only `contents` so includedFile will be relative;
74
+ // a `data:` resource's own file exists nowhere.
75
+ if (_nodePath.default.isAbsolute(normalizedIncludedFile) && !(this.resourcePath.startsWith("data:") && normalizedIncludedFile === (0, _utils.getResourcePath)(this))) {
71
76
  this.addDependency(normalizedIncludedFile);
72
77
  }
73
78
  }
74
79
  }
75
- callback(null, result.css.toString(), map || undefined);
80
+ callback(null, css, cssMap || undefined);
76
81
  }
77
82
  var _default = exports.default = loader;module.exports = exports.default;
78
83
  module.exports.default = exports.default;
package/dist/cjs/utils.js CHANGED
@@ -6,10 +6,12 @@ Object.defineProperty(exports, "__esModule", {
6
6
  exports.errorFactory = errorFactory;
7
7
  exports.getCompileFn = getCompileFn;
8
8
  exports.getModernWebpackImporter = getModernWebpackImporter;
9
+ exports.getResourcePath = getResourcePath;
9
10
  exports.getSassImplementation = getSassImplementation;
10
11
  exports.getSassOptions = getSassOptions;
11
12
  exports.getWebpackResolver = getWebpackResolver;
12
13
  exports.normalizeSourceMap = normalizeSourceMap;
14
+ exports.removeBOM = removeBOM;
13
15
  var _nodePath = _interopRequireDefault(require("node:path"));
14
16
  var _nodeUrl = _interopRequireDefault(require("node:url"));
15
17
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
@@ -175,6 +177,23 @@ function isProductionLikeMode(loaderContext) {
175
177
  return loaderContext.mode === "production" || !loaderContext.mode;
176
178
  }
177
179
 
180
+ /**
181
+ * The path a resource's contents resolve from. A `data:` URI has none of its own,
182
+ * so it reads as a file in the loader's context, with the syntax its media type names.
183
+ * @param {LoaderContext} loaderContext loader context
184
+ * @returns {string} the resource's path, or a file in its context for a `data:` URI
185
+ */
186
+ function getResourcePath(loaderContext) {
187
+ const {
188
+ resourcePath
189
+ } = loaderContext;
190
+ if (!resourcePath.startsWith("data:")) {
191
+ return resourcePath;
192
+ }
193
+ const indented = /^data:text\/x-sass[;,]/i.test(resourcePath);
194
+ return _nodePath.default.join(loaderContext.context, indented ? "stdin.sass" : "stdin.scss");
195
+ }
196
+
178
197
  /**
179
198
  * Derives the sass options from the loader context and normalizes its values with sane defaults.
180
199
  * @param {LoaderContext} loaderContext loader context
@@ -249,9 +268,7 @@ async function getSassOptions(loaderContext, loaderOptions, content, useSourceMa
249
268
  }
250
269
  };
251
270
  }
252
- const {
253
- resourcePath
254
- } = loaderContext;
271
+ const resourcePath = getResourcePath(loaderContext);
255
272
  sassOptions.url = _nodeUrl.default.pathToFileURL(resourcePath);
256
273
 
257
274
  // opt.outputStyle
@@ -470,7 +487,7 @@ function getModernWebpackImporter(loaderContext) {
470
487
  const {
471
488
  fromImport
472
489
  } = context;
473
- const prev = context.containingUrl ? _nodeUrl.default.fileURLToPath(context.containingUrl.toString()) : loaderContext.resourcePath;
490
+ const prev = context.containingUrl ? _nodeUrl.default.fileURLToPath(context.containingUrl.toString()) : getResourcePath(loaderContext);
474
491
  let result;
475
492
  try {
476
493
  result = await resolve(prev, originalUrl, fromImport);
@@ -641,6 +658,102 @@ function normalizeSourceMap(map, rootContext) {
641
658
  });
642
659
  return newMap;
643
660
  }
661
+ const BASE64_CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
662
+ const VLQ_BASE_SHIFT = 5;
663
+ const VLQ_BASE = 1 << VLQ_BASE_SHIFT;
664
+ const VLQ_BASE_MASK = VLQ_BASE - 1;
665
+ const VLQ_CONTINUATION_BIT = VLQ_BASE;
666
+
667
+ /**
668
+ * Decodes the first Base64 VLQ value of the given `mappings`.
669
+ * @param {string} mappings mappings of a source map
670
+ * @returns {{ value: number, length: number } | null} the decoded value and how many characters it takes, `null` when `mappings` doesn't start with a value
671
+ */
672
+ function decodeVLQ(mappings) {
673
+ let value = 0;
674
+ let shift = 0;
675
+ let length = 0;
676
+ let digit;
677
+ do {
678
+ digit = BASE64_CHARACTERS.indexOf(mappings[length]);
679
+ if (digit === -1) {
680
+ return null;
681
+ }
682
+ value += (digit & VLQ_BASE_MASK) << shift;
683
+ shift += VLQ_BASE_SHIFT;
684
+ length += 1;
685
+ } while ((digit & VLQ_CONTINUATION_BIT) !== 0);
686
+ const isNegative = (value & 1) === 1;
687
+ value >>= 1;
688
+ return {
689
+ value: isNegative ? -value : value,
690
+ length
691
+ };
692
+ }
693
+
694
+ /**
695
+ * @param {number} value value
696
+ * @returns {string} the Base64 VLQ encoded value
697
+ */
698
+ function encodeVLQ(value) {
699
+ let encoded = "";
700
+ let vlq = value < 0 ? -value << 1 | 1 : value << 1;
701
+ do {
702
+ let digit = vlq & VLQ_BASE_MASK;
703
+ vlq >>>= VLQ_BASE_SHIFT;
704
+ if (vlq > 0) {
705
+ digit |= VLQ_CONTINUATION_BIT;
706
+ }
707
+ encoded += BASE64_CHARACTERS[digit];
708
+ } while (vlq > 0);
709
+ return encoded;
710
+ }
711
+
712
+ /**
713
+ * Removes the byte order mark (BOM) from the compiled CSS.
714
+ *
715
+ * `dart-sass` prepends a BOM when the `charset` option is enabled (by default),
716
+ * the `style` option is `compressed` (the default for the `production` mode) and
717
+ * the compiled CSS contains non ASCII characters.
718
+ * A BOM is only meaningful at the very beginning of a file, but the loader result
719
+ * is just a string for webpack, and tools like `css-loader` move `@import` at-rules
720
+ * above it, so the BOM ends up in the middle of the generated CSS and breaks the
721
+ * first rule after it.
722
+ *
723
+ * Webpack removes a BOM a loader produced itself since
724
+ * https://github.com/webpack/webpack/pull/21857 and keeps the source map in sync
725
+ * with it since https://github.com/webpack/webpack/pull/21861, which makes this a
726
+ * no-op there. It is still needed for the webpack versions the `peerDependencies`
727
+ * range allows and for `rspack`, which passes a string result through untouched.
728
+ * Remove it once both handle this in the minimum version we support.
729
+ * @see https://github.com/webpack/sass-loader/issues/1335
730
+ * @param {string} css compiled CSS
731
+ * @param {RawSourceMap=} map source map
732
+ * @returns {{ css: string, map: RawSourceMap | undefined }} the CSS without the BOM and the source map adjusted to it
733
+ */
734
+ function removeBOM(css, map) {
735
+ if (css.charCodeAt(0) !== 0xfe_ff) {
736
+ return {
737
+ css,
738
+ map
739
+ };
740
+ }
741
+
742
+ // Sass counts the BOM as the first column of the first line, so all mappings
743
+ // of this line need to be shifted by one column.
744
+ // Only the first segment has to be updated - the generated column of the
745
+ // segments after it is relative to the previous one.
746
+ if (map && typeof map.mappings === "string") {
747
+ const decoded = decodeVLQ(map.mappings);
748
+ if (decoded && decoded.value > 0) {
749
+ map.mappings = `${encodeVLQ(decoded.value - 1)}${map.mappings.slice(decoded.length)}`;
750
+ }
751
+ }
752
+ return {
753
+ css: css.slice(1),
754
+ map
755
+ };
756
+ }
644
757
 
645
758
  /**
646
759
  * @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, getResourcePath, 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,16 +56,21 @@ 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);
62
66
 
63
- // Custom `importer` can return only `contents` so includedFile will be relative
64
- if (path.isAbsolute(normalizedIncludedFile)) {
67
+ // Custom `importer` can return only `contents` so includedFile will be relative;
68
+ // a `data:` resource's own file exists nowhere.
69
+ if (path.isAbsolute(normalizedIncludedFile) && !(this.resourcePath.startsWith("data:") && normalizedIncludedFile === getResourcePath(this))) {
65
70
  this.addDependency(normalizedIncludedFile);
66
71
  }
67
72
  }
68
73
  }
69
- callback(null, result.css.toString(), map || undefined);
74
+ callback(null, css, cssMap || undefined);
70
75
  }
71
76
  export default loader;
package/dist/esm/utils.js CHANGED
@@ -163,6 +163,23 @@ function isProductionLikeMode(loaderContext) {
163
163
  return loaderContext.mode === "production" || !loaderContext.mode;
164
164
  }
165
165
 
166
+ /**
167
+ * The path a resource's contents resolve from. A `data:` URI has none of its own,
168
+ * so it reads as a file in the loader's context, with the syntax its media type names.
169
+ * @param {LoaderContext} loaderContext loader context
170
+ * @returns {string} the resource's path, or a file in its context for a `data:` URI
171
+ */
172
+ function getResourcePath(loaderContext) {
173
+ const {
174
+ resourcePath
175
+ } = loaderContext;
176
+ if (!resourcePath.startsWith("data:")) {
177
+ return resourcePath;
178
+ }
179
+ const indented = /^data:text\/x-sass[;,]/i.test(resourcePath);
180
+ return path.join(loaderContext.context, indented ? "stdin.sass" : "stdin.scss");
181
+ }
182
+
166
183
  /**
167
184
  * Derives the sass options from the loader context and normalizes its values with sane defaults.
168
185
  * @param {LoaderContext} loaderContext loader context
@@ -237,9 +254,7 @@ async function getSassOptions(loaderContext, loaderOptions, content, useSourceMa
237
254
  }
238
255
  };
239
256
  }
240
- const {
241
- resourcePath
242
- } = loaderContext;
257
+ const resourcePath = getResourcePath(loaderContext);
243
258
  sassOptions.url = url.pathToFileURL(resourcePath);
244
259
 
245
260
  // opt.outputStyle
@@ -458,7 +473,7 @@ function getModernWebpackImporter(loaderContext) {
458
473
  const {
459
474
  fromImport
460
475
  } = context;
461
- const prev = context.containingUrl ? url.fileURLToPath(context.containingUrl.toString()) : loaderContext.resourcePath;
476
+ const prev = context.containingUrl ? url.fileURLToPath(context.containingUrl.toString()) : getResourcePath(loaderContext);
462
477
  let result;
463
478
  try {
464
479
  result = await resolve(prev, originalUrl, fromImport);
@@ -629,6 +644,102 @@ function normalizeSourceMap(map, rootContext) {
629
644
  });
630
645
  return newMap;
631
646
  }
647
+ const BASE64_CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
648
+ const VLQ_BASE_SHIFT = 5;
649
+ const VLQ_BASE = 1 << VLQ_BASE_SHIFT;
650
+ const VLQ_BASE_MASK = VLQ_BASE - 1;
651
+ const VLQ_CONTINUATION_BIT = VLQ_BASE;
652
+
653
+ /**
654
+ * Decodes the first Base64 VLQ value of the given `mappings`.
655
+ * @param {string} mappings mappings of a source map
656
+ * @returns {{ value: number, length: number } | null} the decoded value and how many characters it takes, `null` when `mappings` doesn't start with a value
657
+ */
658
+ function decodeVLQ(mappings) {
659
+ let value = 0;
660
+ let shift = 0;
661
+ let length = 0;
662
+ let digit;
663
+ do {
664
+ digit = BASE64_CHARACTERS.indexOf(mappings[length]);
665
+ if (digit === -1) {
666
+ return null;
667
+ }
668
+ value += (digit & VLQ_BASE_MASK) << shift;
669
+ shift += VLQ_BASE_SHIFT;
670
+ length += 1;
671
+ } while ((digit & VLQ_CONTINUATION_BIT) !== 0);
672
+ const isNegative = (value & 1) === 1;
673
+ value >>= 1;
674
+ return {
675
+ value: isNegative ? -value : value,
676
+ length
677
+ };
678
+ }
679
+
680
+ /**
681
+ * @param {number} value value
682
+ * @returns {string} the Base64 VLQ encoded value
683
+ */
684
+ function encodeVLQ(value) {
685
+ let encoded = "";
686
+ let vlq = value < 0 ? -value << 1 | 1 : value << 1;
687
+ do {
688
+ let digit = vlq & VLQ_BASE_MASK;
689
+ vlq >>>= VLQ_BASE_SHIFT;
690
+ if (vlq > 0) {
691
+ digit |= VLQ_CONTINUATION_BIT;
692
+ }
693
+ encoded += BASE64_CHARACTERS[digit];
694
+ } while (vlq > 0);
695
+ return encoded;
696
+ }
697
+
698
+ /**
699
+ * Removes the byte order mark (BOM) from the compiled CSS.
700
+ *
701
+ * `dart-sass` prepends a BOM when the `charset` option is enabled (by default),
702
+ * the `style` option is `compressed` (the default for the `production` mode) and
703
+ * the compiled CSS contains non ASCII characters.
704
+ * A BOM is only meaningful at the very beginning of a file, but the loader result
705
+ * is just a string for webpack, and tools like `css-loader` move `@import` at-rules
706
+ * above it, so the BOM ends up in the middle of the generated CSS and breaks the
707
+ * first rule after it.
708
+ *
709
+ * Webpack removes a BOM a loader produced itself since
710
+ * https://github.com/webpack/webpack/pull/21857 and keeps the source map in sync
711
+ * with it since https://github.com/webpack/webpack/pull/21861, which makes this a
712
+ * no-op there. It is still needed for the webpack versions the `peerDependencies`
713
+ * range allows and for `rspack`, which passes a string result through untouched.
714
+ * Remove it once both handle this in the minimum version we support.
715
+ * @see https://github.com/webpack/sass-loader/issues/1335
716
+ * @param {string} css compiled CSS
717
+ * @param {RawSourceMap=} map source map
718
+ * @returns {{ css: string, map: RawSourceMap | undefined }} the CSS without the BOM and the source map adjusted to it
719
+ */
720
+ function removeBOM(css, map) {
721
+ if (css.charCodeAt(0) !== 0xfe_ff) {
722
+ return {
723
+ css,
724
+ map
725
+ };
726
+ }
727
+
728
+ // Sass counts the BOM as the first column of the first line, so all mappings
729
+ // of this line need to be shifted by one column.
730
+ // Only the first segment has to be updated - the generated column of the
731
+ // segments after it is relative to the previous one.
732
+ if (map && typeof map.mappings === "string") {
733
+ const decoded = decodeVLQ(map.mappings);
734
+ if (decoded && decoded.value > 0) {
735
+ map.mappings = `${encodeVLQ(decoded.value - 1)}${map.mappings.slice(decoded.length)}`;
736
+ }
737
+ }
738
+ return {
739
+ css: css.slice(1),
740
+ map
741
+ };
742
+ }
632
743
 
633
744
  /**
634
745
  * @param {Error | SassError} error the original sass error
@@ -644,4 +755,4 @@ function errorFactory(error) {
644
755
  obj.stack = undefined;
645
756
  return obj;
646
757
  }
647
- export { errorFactory, getCompileFn, getModernWebpackImporter, getSassImplementation, getSassOptions, getWebpackResolver, normalizeSourceMap };
758
+ export { errorFactory, getCompileFn, getModernWebpackImporter, getResourcePath, getSassImplementation, getSassOptions, getWebpackResolver, normalizeSourceMap, removeBOM };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sass-loader",
3
- "version": "17.0.0",
3
+ "version": "17.1.0",
4
4
  "description": "Sass loader for webpack",
5
5
  "keywords": [
6
6
  "sass",
package/types/utils.d.ts CHANGED
@@ -245,6 +245,13 @@ export function getCompileFn<T extends SassImplementation>(
245
245
  export function getModernWebpackImporter(
246
246
  loaderContext: LoaderContext,
247
247
  ): Importer;
248
+ /**
249
+ * The path a resource's contents resolve from. A `data:` URI has none of its own,
250
+ * so it reads as a file in the loader's context, with the syntax its media type names.
251
+ * @param {LoaderContext} loaderContext loader context
252
+ * @returns {string} the resource's path, or a file in its context for a `data:` URI
253
+ */
254
+ export function getResourcePath(loaderContext: LoaderContext): string;
248
255
  /**
249
256
  * This function is not Webpack-specific and can be used by tools wishing to mimic `sass-loader`'s behaviour, so its signature should not be changed.
250
257
  * @param {SassImplementation | string | undefined} implementation sass implementation
@@ -292,3 +299,32 @@ export function normalizeSourceMap(
292
299
  map: RawSourceMap,
293
300
  rootContext: string,
294
301
  ): RawSourceMap;
302
+ /**
303
+ * Removes the byte order mark (BOM) from the compiled CSS.
304
+ *
305
+ * `dart-sass` prepends a BOM when the `charset` option is enabled (by default),
306
+ * the `style` option is `compressed` (the default for the `production` mode) and
307
+ * the compiled CSS contains non ASCII characters.
308
+ * A BOM is only meaningful at the very beginning of a file, but the loader result
309
+ * is just a string for webpack, and tools like `css-loader` move `@import` at-rules
310
+ * above it, so the BOM ends up in the middle of the generated CSS and breaks the
311
+ * first rule after it.
312
+ *
313
+ * Webpack removes a BOM a loader produced itself since
314
+ * https://github.com/webpack/webpack/pull/21857 and keeps the source map in sync
315
+ * with it since https://github.com/webpack/webpack/pull/21861, which makes this a
316
+ * no-op there. It is still needed for the webpack versions the `peerDependencies`
317
+ * range allows and for `rspack`, which passes a string result through untouched.
318
+ * Remove it once both handle this in the minimum version we support.
319
+ * @see https://github.com/webpack/sass-loader/issues/1335
320
+ * @param {string} css compiled CSS
321
+ * @param {RawSourceMap=} map source map
322
+ * @returns {{ css: string, map: RawSourceMap | undefined }} the CSS without the BOM and the source map adjusted to it
323
+ */
324
+ export function removeBOM(
325
+ css: string,
326
+ map?: RawSourceMap | undefined,
327
+ ): {
328
+ css: string;
329
+ map: RawSourceMap | undefined;
330
+ };