|
| 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 | +}); |
0 commit comments