29 lines
997 B
TypeScript
29 lines
997 B
TypeScript
import { compileStringAsync } from "sass";
|
|
import { pathToFileURL } from "node:url";
|
|
import type { Loader, LoaderFile } from "../../contracts/index.ts";
|
|
import { compileCss, type CompileOptions } from "./compiler.ts";
|
|
|
|
export interface SassLoaderOptions extends CompileOptions {
|
|
/** Directory scanned by the Sass integration, relative to the project root. */
|
|
dir?: string;
|
|
/** Extra directories searched by Sass `@use` and `@import`. */
|
|
loadPaths?: string[];
|
|
}
|
|
|
|
/**
|
|
* Compiles Sass to CSS and then runs it through STCSS. This lets a project
|
|
* move Sass files over incrementally while keeping one CSS output path.
|
|
*/
|
|
export function sassLoader(options: SassLoaderOptions = {}): Loader {
|
|
return {
|
|
name: "sass",
|
|
extensions: [".scss", ".sass"],
|
|
async load(file: LoaderFile) {
|
|
const result = await compileStringAsync(file.source, {
|
|
url: pathToFileURL(file.path),
|
|
loadPaths: options.loadPaths,
|
|
});
|
|
return { data: {}, content: compileCss(result.css, options) };
|
|
},
|
|
};
|
|
}
|