Skip to content

Commit ee6f955

Browse files
committed
Update Sass importers to modern API
1 parent 6928633 commit ee6f955

3 files changed

Lines changed: 539 additions & 27 deletions

File tree

astro.config.ts

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,7 @@ import type { AstroIntegration, AstroUserConfig } from "astro";
33
import type { Options as AutolinkHeadingsOptions } from "rehype-autolink-headings";
44
import type { Options as ClassNamesOptions } from "rehype-class-names";
55
import type { Options as ExternalLinksOptions } from "rehype-external-links";
6-
import type {
7-
LegacyAsyncImporter,
8-
LegacySharedOptions,
9-
LegacySyncImporter
10-
} from "sass";
6+
import type { FileImporter, Importer, StringOptions } from "sass-embedded";
117
import type { ShikiTransformer } from "shiki";
128
import type { PluginOption } from "vite";
139

@@ -338,29 +334,30 @@ const optimizeImagesIntegration: AstroIntegration = {
338334
// Vite - SCSS
339335
//==================================================
340336

341-
const stylesDir = nodeUrl.fileURLToPath(
342-
path.join(path.dirname(import.meta.url), "src", "styles")
343-
);
337+
const stylesDir = path.join(path.dirname(import.meta.url), "src", "styles");
344338

345339
const partialsImport = '@use "partials:" as *;';
346-
const importScssPartials: LegacySyncImporter = (url) => {
347-
if (!url.startsWith("partials:")) {
348-
return null;
349-
}
340+
const importScssPartials: FileImporter<"sync"> = {
341+
findFileUrl(url) {
342+
if (url !== "partials:") {
343+
return null;
344+
}
350345

351-
return { file: path.join(stylesDir, "partials", "_index.scss") };
346+
return new URL(path.join(stylesDir, "partials", "_index.scss"));
347+
}
352348
};
353349

354-
const importScssJson: LegacyAsyncImporter = (url, _, done) => {
355-
(async () => {
350+
const importScssJson: Importer<"async"> = {
351+
canonicalize(url) {
356352
if (!url.startsWith("json:")) {
357-
done(null);
353+
return null;
358354
}
359355

360356
const [, baseName = ""] = addExtension(url, ".json").split(":");
361-
const json = JSON.parse(
362-
await fs.readFile(path.join(stylesDir, "data", baseName), "utf-8")
363-
);
357+
return new URL(path.join(stylesDir, "data", baseName));
358+
},
359+
async load(url) {
360+
const json = JSON.parse(await fs.readFile(url, "utf-8"));
364361

365362
let scss = "";
366363
for (const [key, value] of Object.entries(json)) {
@@ -373,19 +370,31 @@ const importScssJson: LegacyAsyncImporter = (url, _, done) => {
373370
scss += ");\n";
374371
}
375372

376-
done({ contents: scss });
377-
})();
373+
return {
374+
syntax: "scss",
375+
contents: scss
376+
};
377+
}
378378
};
379379

380-
type ViteSassOptions = LegacySharedOptions<"sync" | "async"> & {
380+
/**
381+
* @see
382+
* {@link https://vite.dev/config/shared-options.html#css-preprocessoroptions}
383+
*/
384+
type ViteSassOptions = StringOptions<"async"> & {
385+
/**
386+
* Sets which Sass API to use.
387+
*/
388+
api?: "legacy" | "modern" | "modern-compiler";
381389
/**
382390
* Injects code at the top of each stylesheet.
383391
*/
384392
additionalData?: string;
385393
};
386394
const scss: ViteSassOptions = {
395+
api: "modern-compiler",
387396
additionalData: partialsImport,
388-
importer: [importScssPartials, importScssJson]
397+
importers: [importScssPartials, importScssJson]
389398
};
390399

391400
//==================================================

0 commit comments

Comments
 (0)