Skip to content

Commit

Permalink
minify css
Browse files Browse the repository at this point in the history
  • Loading branch information
Fil committed Mar 4, 2024
1 parent 5805947 commit 12926d1
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,11 @@ export async function build(
effects.output.write(`${faint("build")} ${specifier} ${faint("→")} `);
if (specifier.startsWith("observablehq:theme-")) {
const match = /^observablehq:theme-(?<theme>[\w-]+(,[\w-]+)*)?\.css$/.exec(specifier);
const contents = await bundleStyles({theme: match!.groups!.theme?.split(",") ?? []});
const contents = await bundleStyles({theme: match!.groups!.theme?.split(",") ?? [], minify: true});
await effects.writeFile(path, contents);
} else {
const clientPath = getClientPath(path.slice("/_observablehq/".length));
const contents = await bundleStyles({path: clientPath});
const contents = await bundleStyles({path: clientPath, minify: true});
await effects.writeFile(`/_observablehq/${specifier.slice("observablehq:".length)}`, contents);
}
} else if (specifier.startsWith("npm:")) {
Expand All @@ -136,7 +136,7 @@ export async function build(
} else if (!/^\w+:/.test(specifier)) {
const sourcePath = join(root, specifier);
effects.output.write(`${faint("build")} ${sourcePath} ${faint("→")} `);
const contents = await bundleStyles({path: sourcePath});
const contents = await bundleStyles({path: sourcePath, minify: true});
const hash = createHash("sha256").update(contents).digest("hex").slice(0, 8);
const ext = extname(specifier);
const alias = `/${join("_import", dirname(specifier), `${basename(specifier, ext)}.${hash}${ext}`)}`;
Expand Down
11 changes: 10 additions & 1 deletion src/rollup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,20 @@ function rewriteInputsNamespace(code: string) {
return code.replace(/\b__ns__\b/g, "inputs-3a86ea");
}

export async function bundleStyles({path, theme}: {path?: string; theme?: string[]}): Promise<string> {
export async function bundleStyles({
minify = false,
path,
theme
}: {
minify?: boolean;
path?: string;
theme?: string[];
}): Promise<string> {
const result = await build({
bundle: true,
...(path ? {entryPoints: [path]} : {stdin: {contents: renderTheme(theme!), loader: "css"}}),
write: false,
minify,
alias: STYLE_MODULES
});
const text = result.outputFiles[0].text;
Expand Down

0 comments on commit 12926d1

Please sign in to comment.