MCPcopy
hub / github.com/webpack/sass-loader / getModernWebpackImporter

Function getModernWebpackImporter

src/utils.js:556–637  ·  view source on GitHub ↗

* @param {LoaderContext} loaderContext loader context * @returns {Importer} the modern webpack importer

(loaderContext)

Source from the content-addressed store, hash-verified

554 * @returns {Importer} the modern webpack importer
555 */
556function getModernWebpackImporter(loaderContext) {
557 const resolve = getWebpackResolver(loaderContext.getResolve);
558
559 return {
560 /**
561 * @param {string} originalUrl original url
562 * @param {{ fromImport: boolean, containingUrl: URL | null }} context context
563 * @returns {Promise<URL | null>} canonicalized URL
564 */
565 async canonicalize(originalUrl, context) {
566 const { fromImport } = context;
567 const prev = context.containingUrl
568 ? url.fileURLToPath(context.containingUrl.toString())
569 : loaderContext.resourcePath;
570
571 let result;
572
573 try {
574 result = await resolve(prev, originalUrl, fromImport);
575 } catch {
576 // If no stylesheets are found, the importer should return null.
577 return null;
578 }
579
580 loaderContext.addDependency(path.normalize(result));
581
582 return url.pathToFileURL(result);
583 },
584 /**
585 * @param {URL} canonicalUrl canonical url
586 * @returns {Promise<{ contents: string, syntax: "scss" | "indented" | "css", sourceMapUrl: URL } | null>} load result
587 */
588 async load(canonicalUrl) {
589 const ext = path.extname(canonicalUrl.pathname);
590
591 /** @type {"scss" | "indented" | "css"} */
592 let syntax;
593
594 if (ext && ext.toLowerCase() === ".scss") {
595 syntax = "scss";
596 } else if (ext && ext.toLowerCase() === ".sass") {
597 syntax = "indented";
598 } else if (ext && ext.toLowerCase() === ".css") {
599 syntax = "css";
600 } else {
601 // Fallback to default value
602 syntax = "scss";
603 }
604
605 try {
606 const contents = /** @type {string} */ (
607 await new Promise((resolve, reject) => {
608 // Old version of `enhanced-resolve` supports only path as a string
609 // TODO simplify in the next major release and pass URL
610 const canonicalPath = url.fileURLToPath(canonicalUrl);
611
612 loaderContext.fs.readFile(
613 canonicalPath,

Callers 2

getCodeFromSassFunction · 0.90
loaderFunction · 0.90

Calls 1

getWebpackResolverFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…