Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

minify css #787

Merged
merged 1 commit into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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