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

Function getSassImplementation

src/utils.js:114–166  ·  view source on GitHub ↗

* 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. * @param {SassImplementation | string | undefined} implementation sass implementation * @returns {Promise } resolved sass implemen

(implementation)

Source from the content-addressed store, hash-verified

112 * @returns {Promise<SassImplementation>} resolved sass implementation
113 */
114async function getSassImplementation(implementation) {
115 /** @type {SassImplementation} */
116 let resolvedImplementation;
117
118 if (!implementation) {
119 try {
120 resolvedImplementation = /** @type {SassImplementation} */ (
121 await import("sass-embedded")
122 );
123 } catch (err) {
124 // Only fall back to `sass` when `sass-embedded` is not installed.
125 // Any other failure (e.g. a broken install or a side-effect throw at
126 // module-load time) should surface so the user can diagnose it
127 // instead of being silently masked by the `sass` fallback.
128 const { code } = /** @type {NodeJS.ErrnoException} */ (err);
129 if (code !== "ERR_MODULE_NOT_FOUND" && code !== "MODULE_NOT_FOUND") {
130 throw err;
131 }
132
133 resolvedImplementation = /** @type {SassImplementation} */ (
134 await import("sass")
135 );
136 }
137 } else if (typeof implementation === "string") {
138 resolvedImplementation = /** @type {SassImplementation} */ (
139 await import(normalizeImportSpecifier(implementation))
140 );
141 } else {
142 resolvedImplementation = implementation;
143 }
144
145 const { info } = resolvedImplementation;
146
147 if (!info) {
148 throw new Error("Unknown Sass implementation.");
149 }
150
151 const infoParts = info.split("\t");
152
153 if (infoParts.length < 2) {
154 throw new Error(`Unknown Sass implementation "${info}".`);
155 }
156
157 const [implementationName] = infoParts;
158
159 if (implementationName === "dart-sass") {
160 return resolvedImplementation;
161 } else if (implementationName === "sass-embedded") {
162 return resolvedImplementation;
163 }
164
165 throw new Error(`Unknown Sass implementation "${implementationName}".`);
166}
167
168/**
169 * @param {LoaderContext} loaderContext loader context

Callers 2

loaderFunction · 0.90

Calls 1

normalizeImportSpecifierFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…