Skip to content

Commit 8ecb2f0

Browse files
fix: resolve a data: resource from the loader's context (#1352)
* fix: resolve a data: resource from the loader's context A data: URI has no path, so relative @use / @import resolved against the URI itself and the syntax could not be read from an extension. Resolve from this.context (the referrer's directory with webpack giving data: modules their issuer's context) and read text/x-sass as indented. * test: give the data: resource its referrer's context on older webpack The test loader sets this.context the way webpack/webpack#22940 does, so the test checks the loader on the released webpack too. * chore: update types
1 parent 6670e5a commit 8ecb2f0

6 files changed

Lines changed: 129 additions & 4 deletions

File tree

‎.changeset/data-uri-resource.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"sass-loader": minor
3+
---
4+
5+
Resolve a `data:` resource from the loader's context, reading `text/x-sass` as the indented syntax.

‎src/index.js‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
errorFactory,
77
getCompileFn,
88
getModernWebpackImporter,
9+
getResourcePath,
910
getSassImplementation,
1011
getSassOptions,
1112
normalizeSourceMap,
@@ -98,8 +99,15 @@ async function loader(content) {
9899
)) {
99100
const normalizedIncludedFile = url.fileURLToPath(includedFile);
100101

101-
// Custom `importer` can return only `contents` so includedFile will be relative
102-
if (path.isAbsolute(normalizedIncludedFile)) {
102+
// Custom `importer` can return only `contents` so includedFile will be relative;
103+
// a `data:` resource's own file exists nowhere.
104+
if (
105+
path.isAbsolute(normalizedIncludedFile) &&
106+
!(
107+
this.resourcePath.startsWith("data:") &&
108+
normalizedIncludedFile === getResourcePath(this)
109+
)
110+
) {
103111
this.addDependency(normalizedIncludedFile);
104112
}
105113
}

‎src/utils.js‎

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,27 @@ function isProductionLikeMode(loaderContext) {
173173
return loaderContext.mode === "production" || !loaderContext.mode;
174174
}
175175

176+
/**
177+
* The path a resource's contents resolve from. A `data:` URI has none of its own,
178+
* so it reads as a file in the loader's context, with the syntax its media type names.
179+
* @param {LoaderContext} loaderContext loader context
180+
* @returns {string} the resource's path, or a file in its context for a `data:` URI
181+
*/
182+
function getResourcePath(loaderContext) {
183+
const { resourcePath } = loaderContext;
184+
185+
if (!resourcePath.startsWith("data:")) {
186+
return resourcePath;
187+
}
188+
189+
const indented = /^data:text\/x-sass[;,]/i.test(resourcePath);
190+
191+
return path.join(
192+
loaderContext.context,
193+
indented ? "stdin.sass" : "stdin.scss",
194+
);
195+
}
196+
176197
/**
177198
* Derives the sass options from the loader context and normalizes its values with sane defaults.
178199
* @param {LoaderContext} loaderContext loader context
@@ -278,7 +299,7 @@ async function getSassOptions(
278299
};
279300
}
280301

281-
const { resourcePath } = loaderContext;
302+
const resourcePath = getResourcePath(loaderContext);
282303

283304
sassOptions.url = url.pathToFileURL(resourcePath);
284305

@@ -566,7 +587,7 @@ function getModernWebpackImporter(loaderContext) {
566587
const { fromImport } = context;
567588
const prev = context.containingUrl
568589
? url.fileURLToPath(context.containingUrl.toString())
569-
: loaderContext.resourcePath;
590+
: getResourcePath(loaderContext);
570591

571592
let result;
572593

@@ -888,6 +909,7 @@ export {
888909
errorFactory,
889910
getCompileFn,
890911
getModernWebpackImporter,
912+
getResourcePath,
891913
getSassImplementation,
892914
getSassOptions,
893915
getWebpackResolver,

‎test/data-uri.test.js‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import assert from "node:assert";
2+
import path from "node:path";
3+
import { describe, it } from "node:test";
4+
import url from "node:url";
5+
6+
import {
7+
close,
8+
compile,
9+
getCodeFromCssBundle,
10+
getCompiler,
11+
getErrors,
12+
getImplementationsAndAPI,
13+
} from "./helpers/index.js";
14+
15+
const __dirname = path.dirname(url.fileURLToPath(import.meta.url));
16+
17+
describe("data: URI resource", () => {
18+
for (const { name, api, implementation } of getImplementationsAndAPI()) {
19+
it(`should resolve from the loader's context and read the syntax from the media type ('${name}', '${api}' API)`, async () => {
20+
// Inline <style lang> bodies from an HTML page reach the loader as data: URIs.
21+
const compiler = getCompiler("", {
22+
entry: [
23+
"data:text/x-scss;base64,QHVzZSAiLi9zY3NzL3dvcmQiOwouZGF0YS11cmktc2NzcyB7IGNvbG9yOiBibHVlOyB9Cg==",
24+
"data:text/x-sass;base64,LmRhdGEtdXJpLXNhc3MKICBjb2xvcjogZ3JlZW4K",
25+
],
26+
css: true,
27+
rules: [
28+
{
29+
mimetype: /^text\/x-s[ac]ss$/,
30+
type: "css/auto",
31+
use: [
32+
{
33+
loader: path.join(
34+
__dirname,
35+
"./helpers/dataUriContextLoader.cjs",
36+
),
37+
options: { context: __dirname },
38+
},
39+
{
40+
loader: path.join(__dirname, "../src/index.js"),
41+
options: { implementation, api },
42+
},
43+
],
44+
},
45+
],
46+
});
47+
const stats = await compile(compiler);
48+
const css = getCodeFromCssBundle(stats, compiler);
49+
50+
assert.deepStrictEqual(getErrors(stats), []);
51+
// `@use "./scss/word"` resolves from the context the data: URI is given.
52+
assert.match(css, /\.word\s*\{\s*color: red;/);
53+
assert.match(css, /\.data-uri-scss\s*\{\s*color: blue;/);
54+
// `text/x-sass` is read as the indented syntax.
55+
assert.match(css, /\.data-uri-sass\s*\{\s*color: green;/);
56+
57+
await close(compiler);
58+
});
59+
}
60+
});
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
"use strict";
2+
3+
/**
4+
* Gives a `data:` resource the context webpack gives one from webpack/webpack#22940
5+
* on: the directory of what referenced it, so tests run on older webpack too.
6+
* @this {import("webpack").LoaderContext<{ context: string }>}
7+
* @param {string} source source
8+
* @returns {string} the source, untouched
9+
*/
10+
function loader(source) {
11+
return source;
12+
}
13+
14+
/**
15+
* @this {import("webpack").LoaderContext<{ context: string }>}
16+
*/
17+
loader.pitch = function pitch() {
18+
if (this.resourcePath.startsWith("data:")) {
19+
this.context = this.getOptions().context;
20+
}
21+
};
22+
23+
module.exports = loader;

‎types/utils.d.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,13 @@ export function getCompileFn<T extends SassImplementation>(
245245
export function getModernWebpackImporter(
246246
loaderContext: LoaderContext,
247247
): 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;
248255
/**
249256
* 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.
250257
* @param {SassImplementation | string | undefined} implementation sass implementation

0 commit comments

Comments
 (0)