From 12926d1fbf870cf32202145f382bb4c9d3cc05f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philippe=20Rivi=C3=A8re?= Date: Mon, 4 Mar 2024 16:12:41 +0100 Subject: [PATCH] minify css --- src/build.ts | 6 +++--- src/rollup.ts | 11 ++++++++++- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/build.ts b/src/build.ts index b8e3c3645..10fb8d063 100644 --- a/src/build.ts +++ b/src/build.ts @@ -121,11 +121,11 @@ export async function build( effects.output.write(`${faint("build")} ${specifier} ${faint("→")} `); if (specifier.startsWith("observablehq:theme-")) { const match = /^observablehq: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:")) { @@ -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}`)}`; diff --git a/src/rollup.ts b/src/rollup.ts index 504452e9b..e0d42a604 100644 --- a/src/rollup.ts +++ b/src/rollup.ts @@ -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 { +export async function bundleStyles({ + minify = false, + path, + theme +}: { + minify?: boolean; + path?: string; + theme?: string[]; +}): Promise { 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;