-
-
Notifications
You must be signed in to change notification settings - Fork 430
/
utils.js
918 lines (783 loc) · 28.1 KB
/
utils.js
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
import url from "url";
import path from "path";
function getDefaultSassImplementation() {
let sassImplPkg = "sass";
try {
require.resolve("sass-embedded");
sassImplPkg = "sass-embedded";
} catch (ignoreError) {
try {
require.resolve("sass");
} catch (_ignoreError) {
try {
require.resolve("node-sass");
sassImplPkg = "node-sass";
} catch (__ignoreError) {
sassImplPkg = "sass";
}
}
}
// eslint-disable-next-line import/no-dynamic-require, global-require
return require(sassImplPkg);
}
/**
* 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.
*/
function getSassImplementation(loaderContext, implementation) {
let resolvedImplementation = implementation;
if (!resolvedImplementation) {
resolvedImplementation = getDefaultSassImplementation();
}
if (typeof resolvedImplementation === "string") {
// eslint-disable-next-line import/no-dynamic-require, global-require
resolvedImplementation = require(resolvedImplementation);
}
const { info } = resolvedImplementation;
if (!info) {
throw new Error("Unknown Sass implementation.");
}
const infoParts = info.split("\t");
if (infoParts.length < 2) {
throw new Error(`Unknown Sass implementation "${info}".`);
}
const [implementationName] = infoParts;
if (implementationName === "dart-sass") {
// eslint-disable-next-line consistent-return
return resolvedImplementation;
} else if (implementationName === "node-sass") {
// eslint-disable-next-line consistent-return
return resolvedImplementation;
} else if (implementationName === "sass-embedded") {
// eslint-disable-next-line consistent-return
return resolvedImplementation;
}
throw new Error(`Unknown Sass implementation "${implementationName}".`);
}
/**
* @param {any} loaderContext
* @returns {boolean}
*/
function isProductionLikeMode(loaderContext) {
return loaderContext.mode === "production" || !loaderContext.mode;
}
function proxyCustomImporters(importers, loaderContext) {
return [].concat(importers).map(
(importer) =>
function proxyImporter(...args) {
const self = { ...this, webpackLoaderContext: loaderContext };
return importer.apply(self, args);
},
);
}
/**
* Derives the sass options from the loader context and normalizes its values with sane defaults.
*
* @param {object} loaderContext
* @param {object} loaderOptions
* @param {string} content
* @param {object} implementation
* @param {boolean} useSourceMap
* @param {"legacy" | "modern" | "modern-compiler"} apiType
* @returns {Object}
*/
async function getSassOptions(
loaderContext,
loaderOptions,
content,
implementation,
useSourceMap,
apiType,
) {
const options = loaderOptions.sassOptions
? typeof loaderOptions.sassOptions === "function"
? loaderOptions.sassOptions(loaderContext) || {}
: loaderOptions.sassOptions
: {};
const sassOptions = {
...options,
data: loaderOptions.additionalData
? typeof loaderOptions.additionalData === "function"
? await loaderOptions.additionalData(content, loaderContext)
: `${loaderOptions.additionalData}\n${content}`
: content,
};
if (!sassOptions.logger) {
const needEmitWarning = loaderOptions.warnRuleAsWarning !== false;
const logger = loaderContext.getLogger("sass-loader");
const formatSpan = (span) =>
`Warning on line ${span.start.line}, column ${span.start.column} of ${
span.url || "-"
}:${span.start.line}:${span.start.column}:\n`;
const formatDebugSpan = (span) =>
`[debug:${span.start.line}:${span.start.column}] `;
sassOptions.logger = {
debug(message, loggerOptions) {
let builtMessage = "";
if (loggerOptions.span) {
builtMessage = formatDebugSpan(loggerOptions.span);
}
builtMessage += message;
logger.debug(builtMessage);
},
warn(message, loggerOptions) {
let builtMessage = "";
if (loggerOptions.deprecation) {
builtMessage += "Deprecation ";
}
if (loggerOptions.span) {
builtMessage += formatSpan(loggerOptions.span);
}
builtMessage += message;
if (loggerOptions.span && loggerOptions.span.context) {
builtMessage += `\n\n${loggerOptions.span.start.line} | ${loggerOptions.span.context}`;
}
if (loggerOptions.stack && loggerOptions.stack !== "null") {
builtMessage += `\n\n${loggerOptions.stack}`;
}
if (needEmitWarning) {
const warning = new Error(builtMessage);
warning.name = "SassWarning";
warning.stack = null;
loaderContext.emitWarning(warning);
} else {
logger.warn(builtMessage);
}
},
};
}
const isModernAPI = apiType === "modern" || apiType === "modern-compiler";
const { resourcePath } = loaderContext;
if (isModernAPI) {
sassOptions.url = url.pathToFileURL(resourcePath);
// opt.outputStyle
if (!sassOptions.style && isProductionLikeMode(loaderContext)) {
sassOptions.style = "compressed";
}
if (useSourceMap) {
sassOptions.sourceMap = true;
sassOptions.sourceMapIncludeSources = true;
}
// If we are compiling sass and indentedSyntax isn't set, automatically set it.
if (typeof sassOptions.syntax === "undefined") {
const ext = path.extname(resourcePath);
if (ext && ext.toLowerCase() === ".scss") {
sassOptions.syntax = "scss";
} else if (ext && ext.toLowerCase() === ".sass") {
sassOptions.syntax = "indented";
} else if (ext && ext.toLowerCase() === ".css") {
sassOptions.syntax = "css";
}
}
sassOptions.loadPaths = []
.concat(
// We use `loadPaths` in context for resolver, so it should be always absolute
(sassOptions.loadPaths ? sassOptions.loadPaths.slice() : []).map(
(includePath) =>
path.isAbsolute(includePath)
? includePath
: path.join(process.cwd(), includePath),
),
)
.concat(
process.env.SASS_PATH
? process.env.SASS_PATH.split(
process.platform === "win32" ? ";" : ":",
)
: [],
);
sassOptions.importers = sassOptions.importers
? Array.isArray(sassOptions.importers)
? sassOptions.importers.slice()
: [sassOptions.importers]
: [];
} else {
sassOptions.file = resourcePath;
// opt.outputStyle
if (!sassOptions.outputStyle && isProductionLikeMode(loaderContext)) {
sassOptions.outputStyle = "compressed";
}
if (useSourceMap) {
// Deliberately overriding the sourceMap option here.
// node-sass won't produce source maps if the data option is used and options.sourceMap is not a string.
// In case it is a string, options.sourceMap should be a path where the source map is written.
// But since we're using the data option, the source map will not actually be written, but
// all paths in sourceMap.sources will be relative to that path.
// Pretty complicated... :(
sassOptions.sourceMap = true;
sassOptions.outFile = path.join(
loaderContext.rootContext,
"style.css.map",
);
sassOptions.sourceMapContents = true;
sassOptions.omitSourceMapUrl = true;
sassOptions.sourceMapEmbed = false;
}
const ext = path.extname(resourcePath);
// If we are compiling sass and indentedSyntax isn't set, automatically set it.
if (
ext &&
ext.toLowerCase() === ".sass" &&
typeof sassOptions.indentedSyntax === "undefined"
) {
sassOptions.indentedSyntax = true;
} else {
sassOptions.indentedSyntax = Boolean(sassOptions.indentedSyntax);
}
// Allow passing custom importers to `sass`/`node-sass`. Accepts `Function` or an array of `Function`s.
sassOptions.importer = sassOptions.importer
? proxyCustomImporters(
Array.isArray(sassOptions.importer)
? sassOptions.importer.slice()
: [sassOptions.importer],
loaderContext,
)
: [];
// Regression on the `sass-embedded` side
if (
loaderOptions.webpackImporter === false &&
sassOptions.importer.length === 0
) {
// eslint-disable-next-line no-undefined
sassOptions.importer = undefined;
}
sassOptions.includePaths = []
.concat(process.cwd())
.concat(
// We use `includePaths` in context for resolver, so it should be always absolute
(sassOptions.includePaths ? sassOptions.includePaths.slice() : []).map(
(includePath) =>
path.isAbsolute(includePath)
? includePath
: path.join(process.cwd(), includePath),
),
)
.concat(
process.env.SASS_PATH
? process.env.SASS_PATH.split(
process.platform === "win32" ? ";" : ":",
)
: [],
);
if (typeof sassOptions.charset === "undefined") {
sassOptions.charset = true;
}
}
return sassOptions;
}
const MODULE_REQUEST_REGEX = /^[^?]*~/;
// Examples:
// - ~package
// - ~package/
// - ~@org
// - ~@org/
// - ~@org/package
// - ~@org/package/
const IS_MODULE_IMPORT =
/^~([^/]+|[^/]+\/|@[^/]+[/][^/]+|@[^/]+\/?|@[^/]+[/][^/]+\/)$/;
const IS_PKG_SCHEME = /^pkg:/i;
/**
* When `sass`/`node-sass` tries to resolve an import, it uses a special algorithm.
* Since the `sass-loader` uses webpack to resolve the modules, we need to simulate that algorithm.
* This function returns an array of import paths to try.
* The last entry in the array is always the original url to enable straight-forward webpack.config aliases.
*
* We don't need emulate `dart-sass` "It's not clear which file to import." errors (when "file.ext" and "_file.ext" files are present simultaneously in the same directory).
* This reduces performance and `dart-sass` always do it on own side.
*
* @param {string} url
* @param {boolean} forWebpackResolver
* @param {boolean} fromImport
* @returns {Array<string>}
*/
function getPossibleRequests(
// eslint-disable-next-line no-shadow
url,
forWebpackResolver = false,
fromImport = false,
) {
let request = url;
// In case there is module request, send this to webpack resolver
if (forWebpackResolver) {
if (MODULE_REQUEST_REGEX.test(url)) {
request = request.replace(MODULE_REQUEST_REGEX, "");
}
if (IS_PKG_SCHEME.test(url)) {
request = `${request.slice(4)}`;
return [...new Set([request, url])];
}
if (IS_MODULE_IMPORT.test(url) || IS_PKG_SCHEME.test(url)) {
request = request[request.length - 1] === "/" ? request : `${request}/`;
return [...new Set([request, url])];
}
}
// Keep in mind: ext can also be something like '.datepicker' when the true extension is omitted and the filename contains a dot.
// @see https://github.com/webpack-contrib/sass-loader/issues/167
const extension = path.extname(request).toLowerCase();
// Because @import is also defined in CSS, Sass needs a way of compiling plain CSS @imports without trying to import the files at compile time.
// To accomplish this, and to ensure SCSS is as much of a superset of CSS as possible, Sass will compile any @imports with the following characteristics to plain CSS imports:
// - imports where the URL ends with .css.
// - imports where the URL begins http:// or https://.
// - imports where the URL is written as a url().
// - imports that have media queries.
//
// The `node-sass` package sends `@import` ending on `.css` to importer, it is bug, so we skip resolve
if (extension === ".css") {
return [];
}
const dirname = path.dirname(request);
const normalizedDirname = dirname === "." ? "" : `${dirname}/`;
const basename = path.basename(request);
const basenameWithoutExtension = path.basename(request, extension);
return [
...new Set(
[]
.concat(
fromImport
? [
`${normalizedDirname}_${basenameWithoutExtension}.import${extension}`,
`${normalizedDirname}${basenameWithoutExtension}.import${extension}`,
]
: [],
)
.concat([
`${normalizedDirname}_${basename}`,
`${normalizedDirname}${basename}`,
])
.concat(forWebpackResolver ? [url] : []),
),
];
}
function promiseResolve(callbackResolve) {
return (context, request) =>
new Promise((resolve, reject) => {
callbackResolve(context, request, (error, result) => {
if (error) {
reject(error);
} else {
resolve(result);
}
});
});
}
async function startResolving(resolutionMap) {
if (resolutionMap.length === 0) {
return Promise.reject();
}
const [{ possibleRequests }] = resolutionMap;
if (possibleRequests.length === 0) {
return Promise.reject();
}
const [{ resolve, context }] = resolutionMap;
try {
return await resolve(context, possibleRequests[0]);
} catch (_ignoreError) {
const [, ...tailResult] = possibleRequests;
if (tailResult.length === 0) {
const [, ...tailResolutionMap] = resolutionMap;
return startResolving(tailResolutionMap);
}
// eslint-disable-next-line no-param-reassign
resolutionMap[0].possibleRequests = tailResult;
return startResolving(resolutionMap);
}
}
const IS_SPECIAL_MODULE_IMPORT = /^~[^/]+$/;
// `[drive_letter]:\` + `\\[server]\[sharename]\`
const IS_NATIVE_WIN32_PATH = /^[a-z]:[/\\]|^\\\\/i;
/**
* @public
* Create the resolve function used in the custom Sass importer.
*
* Can be used by external tools to mimic how `sass-loader` works, for example
* in a Jest transform. Such usages will want to wrap `resolve.create` from
* [`enhanced-resolve`]{@link https://github.com/webpack/enhanced-resolve} to
* pass as the `resolverFactory` argument.
*
* @param {Function} resolverFactory - A factory function for creating a Webpack
* resolver.
* @param {Object} implementation - The imported Sass implementation, both
* `sass` (Dart Sass) and `node-sass` are supported.
* @param {string[]} [includePaths] - The list of include paths passed to Sass.
*
* @throws If a compatible Sass implementation cannot be found.
*/
function getWebpackResolver(
resolverFactory,
implementation,
includePaths = [],
) {
const isModernSass =
implementation && typeof implementation.compileStringAsync !== "undefined";
// We only have one difference with the built-in sass resolution logic and out resolution logic:
// First, we look at the files starting with `_`, then without `_` (i.e. `_name.sass`, `_name.scss`, `_name.css`, `name.sass`, `name.scss`, `name.css`),
// although `sass` look together by extensions (i.e. `_name.sass`/`name.sass`/`_name.scss`/`name.scss`/`_name.css`/`name.css`).
// It shouldn't be a problem because `sass` throw errors:
// - on having `_name.sass` and `name.sass` (extension can be `sass`, `scss` or `css`) in the same directory
// - on having `_name.sass` and `_name.scss` in the same directory
//
// Also `sass` prefer `sass`/`scss` over `css`.
const sassModuleResolve = promiseResolve(
resolverFactory({
alias: [],
aliasFields: [],
conditionNames: [],
descriptionFiles: [],
extensions: [".sass", ".scss", ".css"],
exportsFields: [],
mainFields: [],
mainFiles: ["_index", "index"],
modules: [],
restrictions: [/\.((sa|sc|c)ss)$/i],
preferRelative: true,
}),
);
const sassImportResolve = promiseResolve(
resolverFactory({
alias: [],
aliasFields: [],
conditionNames: [],
descriptionFiles: [],
extensions: [".sass", ".scss", ".css"],
exportsFields: [],
mainFields: [],
mainFiles: ["_index.import", "_index", "index.import", "index"],
modules: [],
restrictions: [/\.((sa|sc|c)ss)$/i],
preferRelative: true,
}),
);
const webpackModuleResolve = promiseResolve(
resolverFactory({
dependencyType: "sass",
conditionNames: ["sass", "style", "..."],
mainFields: ["sass", "style", "main", "..."],
mainFiles: ["_index", "index", "..."],
extensions: [".sass", ".scss", ".css"],
restrictions: [/\.((sa|sc|c)ss)$/i],
preferRelative: true,
}),
);
const webpackImportResolve = promiseResolve(
resolverFactory({
dependencyType: "sass",
conditionNames: ["sass", "style", "..."],
mainFields: ["sass", "style", "main", "..."],
mainFiles: ["_index.import", "_index", "index.import", "index", "..."],
extensions: [".sass", ".scss", ".css"],
restrictions: [/\.((sa|sc|c)ss)$/i],
preferRelative: true,
}),
);
return (context, request, fromImport) => {
// See https://github.com/webpack/webpack/issues/12340
// Because `node-sass` calls our importer before `1. Filesystem imports relative to the base file.`
// custom importer may not return `{ file: '/path/to/name.ext' }` and therefore our `context` will be relative
if (!isModernSass && !path.isAbsolute(context)) {
return Promise.reject();
}
const originalRequest = request;
const isFileScheme = originalRequest.slice(0, 5).toLowerCase() === "file:";
if (isFileScheme) {
try {
// eslint-disable-next-line no-param-reassign
request = url.fileURLToPath(originalRequest);
} catch (ignoreError) {
// eslint-disable-next-line no-param-reassign
request = request.slice(7);
}
}
let resolutionMap = [];
const needEmulateSassResolver =
// `sass` doesn't support module import
!IS_SPECIAL_MODULE_IMPORT.test(request) &&
// don't handle `pkg:` scheme
!IS_PKG_SCHEME.test(request) &&
// We need improve absolute paths handling.
// Absolute paths should be resolved:
// - Server-relative URLs - `<context>/path/to/file.ext` (where `<context>` is root context)
// - Absolute path - `/full/path/to/file.ext` or `C:\\full\path\to\file.ext`
!isFileScheme &&
!originalRequest.startsWith("/") &&
!IS_NATIVE_WIN32_PATH.test(originalRequest);
if (includePaths.length > 0 && needEmulateSassResolver) {
// The order of import precedence is as follows:
//
// 1. Filesystem imports relative to the base file.
// 2. Custom importer imports.
// 3. Filesystem imports relative to the working directory.
// 4. Filesystem imports relative to an `includePaths` path.
// 5. Filesystem imports relative to a `SASS_PATH` path.
//
// `sass` run custom importers before `3`, `4` and `5` points, we need to emulate this behavior to avoid wrong resolution.
const sassPossibleRequests = getPossibleRequests(
request,
false,
fromImport,
);
// `node-sass` calls our importer before `1. Filesystem imports relative to the base file.`, so we need emulate this too
if (!isModernSass) {
resolutionMap = resolutionMap.concat({
resolve: fromImport ? sassImportResolve : sassModuleResolve,
context: path.dirname(context),
possibleRequests: sassPossibleRequests,
});
}
resolutionMap = resolutionMap.concat(
// eslint-disable-next-line no-shadow
includePaths.map((context) => {
return {
resolve: fromImport ? sassImportResolve : sassModuleResolve,
context,
possibleRequests: sassPossibleRequests,
};
}),
);
}
const webpackPossibleRequests = getPossibleRequests(
request,
true,
fromImport,
);
resolutionMap = resolutionMap.concat({
resolve: fromImport ? webpackImportResolve : webpackModuleResolve,
context: path.dirname(context),
possibleRequests: webpackPossibleRequests,
});
return startResolving(resolutionMap);
};
}
const MATCH_CSS = /\.css$/i;
function getModernWebpackImporter(loaderContext, implementation, loadPaths) {
const resolve = getWebpackResolver(
loaderContext.getResolve,
implementation,
loadPaths,
);
return {
async canonicalize(originalUrl, context) {
const { fromImport } = context;
const prev = context.containingUrl
? url.fileURLToPath(context.containingUrl.toString())
: loaderContext.resourcePath;
let result;
try {
result = await resolve(prev, originalUrl, fromImport);
} catch (err) {
// If no stylesheets are found, the importer should return null.
return null;
}
loaderContext.addDependency(path.normalize(result));
return url.pathToFileURL(result);
},
async load(canonicalUrl) {
const ext = path.extname(canonicalUrl.pathname);
let syntax;
if (ext && ext.toLowerCase() === ".scss") {
syntax = "scss";
} else if (ext && ext.toLowerCase() === ".sass") {
syntax = "indented";
} else if (ext && ext.toLowerCase() === ".css") {
syntax = "css";
} else {
// Fallback to default value
syntax = "scss";
}
try {
// eslint-disable-next-line no-shadow
const contents = await new Promise((resolve, reject) => {
// Old version of `enhanced-resolve` supports only path as a string
// TODO simplify in the next major release and pass URL
const canonicalPath = url.fileURLToPath(canonicalUrl);
loaderContext.fs.readFile(canonicalPath, "utf8", (err, content) => {
if (err) {
reject(err);
return;
}
resolve(content);
});
});
return { contents, syntax, sourceMapUrl: canonicalUrl };
} catch (err) {
return null;
}
},
};
}
function getWebpackImporter(loaderContext, implementation, includePaths) {
const resolve = getWebpackResolver(
loaderContext.getResolve,
implementation,
includePaths,
);
return function importer(originalUrl, prev, done) {
const { fromImport } = this;
resolve(prev, originalUrl, fromImport)
.then((result) => {
// Add the result as dependency.
// Although we're also using stats.includedFiles, this might come in handy when an error occurs.
// In this case, we don't get stats.includedFiles from node-sass/sass.
loaderContext.addDependency(path.normalize(result));
// By removing the CSS file extension, we trigger node-sass to include the CSS file instead of just linking it.
done({ file: result.replace(MATCH_CSS, "") });
})
// Catch all resolving errors, return the original file and pass responsibility back to other custom importers
.catch(() => {
done({ file: originalUrl });
});
};
}
let nodeSassJobQueue = null;
const sassModernCompilers = new WeakMap();
/**
* Verifies that the implementation and version of Sass is supported by this loader.
*
* @param {Object} loaderContext
* @param {Object} implementation
* @param {"legacy" | "modern" | "modern-compiler"} apiType
* @returns {Function}
*/
function getCompileFn(loaderContext, implementation, apiType) {
if (typeof implementation.compileStringAsync !== "undefined") {
if (apiType === "modern") {
return (sassOptions) => {
const { data, ...rest } = sassOptions;
return implementation.compileStringAsync(data, rest);
};
}
if (apiType === "modern-compiler") {
return async (sassOptions) => {
// eslint-disable-next-line no-underscore-dangle
const webpackCompiler = loaderContext._compiler;
const { data, ...rest } = sassOptions;
// Some people can run the loader in a multi-threading way;
// there is no webpack compiler object in such case.
if (webpackCompiler) {
if (!sassModernCompilers.has(webpackCompiler)) {
// Create a long-running compiler process that can be reused
// for compiling individual files.
const compiler = await implementation.initAsyncCompiler();
// Check again because awaiting the initialization function
// introduces a race condition.
if (!sassModernCompilers.has(webpackCompiler)) {
sassModernCompilers.set(webpackCompiler, compiler);
webpackCompiler.hooks.shutdown.tap("sass-loader", () => {
compiler.dispose();
});
} else {
compiler.dispose();
}
}
return sassModernCompilers
.get(webpackCompiler)
.compileStringAsync(data, rest);
}
return implementation.compileStringAsync(data, rest);
};
}
return (sassOptions) =>
new Promise((resolve, reject) => {
implementation.render(sassOptions, (error, result) => {
if (error) {
reject(error);
return;
}
resolve(result);
});
});
}
if (apiType === "modern" || apiType === "modern-compiler") {
throw new Error("Modern API is not supported for 'node-sass'");
}
// There is an issue with node-sass when async custom importers are used
// See https://github.com/sass/node-sass/issues/857#issuecomment-93594360
// We need to use a job queue to make sure that one thread is always available to the UV lib
if (nodeSassJobQueue === null) {
const threadPoolSize = Number(process.env.UV_THREADPOOL_SIZE || 4);
// Only used for `node-sass`, so let's load it lazily
// eslint-disable-next-line global-require
const async = require("neo-async");
nodeSassJobQueue = async.queue(
implementation.render.bind(implementation),
threadPoolSize - 1,
);
}
return (sassOptions) =>
new Promise((resolve, reject) => {
nodeSassJobQueue.push.bind(nodeSassJobQueue)(
sassOptions,
(error, result) => {
if (error) {
reject(error);
return;
}
resolve(result);
},
);
});
}
const ABSOLUTE_SCHEME = /^[A-Za-z0-9+\-.]+:/;
/**
* @param {string} source
* @returns {"absolute" | "scheme-relative" | "path-absolute" | "path-absolute"}
*/
function getURLType(source) {
if (source[0] === "/") {
if (source[1] === "/") {
return "scheme-relative";
}
return "path-absolute";
}
if (IS_NATIVE_WIN32_PATH.test(source)) {
return "path-absolute";
}
return ABSOLUTE_SCHEME.test(source) ? "absolute" : "path-relative";
}
function normalizeSourceMap(map, rootContext) {
const newMap = map;
// result.map.file is an optional property that provides the output filename.
// Since we don't know the final filename in the webpack build chain yet, it makes no sense to have it.
// eslint-disable-next-line no-param-reassign
if (typeof newMap.file !== "undefined") {
delete newMap.file;
}
// eslint-disable-next-line no-param-reassign
newMap.sourceRoot = "";
// node-sass returns POSIX paths, that's why we need to transform them back to native paths.
// This fixes an error on windows where the source-map module cannot resolve the source maps.
// @see https://github.com/webpack-contrib/sass-loader/issues/366#issuecomment-279460722
// eslint-disable-next-line no-param-reassign
newMap.sources = newMap.sources.map((source) => {
const sourceType = getURLType(source);
// Do no touch `scheme-relative`, `path-absolute` and `absolute` types (except `file:`)
if (sourceType === "absolute" && /^file:/i.test(source)) {
return url.fileURLToPath(source);
} else if (sourceType === "path-relative") {
return path.resolve(rootContext, path.normalize(source));
}
return source;
});
return newMap;
}
function errorFactory(error) {
let message;
if (error.formatted) {
message = error.formatted.replace(/^(.+)?Error: /, "");
} else {
// Keep original error if `sassError.formatted` is unavailable
message = (error.message || error.toString()).replace(/^(.+)?Error: /, "");
}
const obj = new Error(message, { cause: error });
obj.name = error.name;
obj.stack = null;
return obj;
}
export {
getSassImplementation,
getSassOptions,
getModernWebpackImporter,
getWebpackResolver,
getWebpackImporter,
getCompileFn,
normalizeSourceMap,
errorFactory,
};