Skip to content

Commit 3f0f3c2

Browse files
authored
minify css (#787)
1 parent 5805947 commit 3f0f3c2

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

src/build.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,11 @@ export async function build(
121121
effects.output.write(`${faint("build")} ${specifier} ${faint("→")} `);
122122
if (specifier.startsWith("observablehq:theme-")) {
123123
const match = /^observablehq:theme-(?<theme>[\w-]+(,[\w-]+)*)?\.css$/.exec(specifier);
124-
const contents = await bundleStyles({theme: match!.groups!.theme?.split(",") ?? []});
124+
const contents = await bundleStyles({theme: match!.groups!.theme?.split(",") ?? [], minify: true});
125125
await effects.writeFile(path, contents);
126126
} else {
127127
const clientPath = getClientPath(path.slice("/_observablehq/".length));
128-
const contents = await bundleStyles({path: clientPath});
128+
const contents = await bundleStyles({path: clientPath, minify: true});
129129
await effects.writeFile(`/_observablehq/${specifier.slice("observablehq:".length)}`, contents);
130130
}
131131
} else if (specifier.startsWith("npm:")) {
@@ -136,7 +136,7 @@ export async function build(
136136
} else if (!/^\w+:/.test(specifier)) {
137137
const sourcePath = join(root, specifier);
138138
effects.output.write(`${faint("build")} ${sourcePath} ${faint("→")} `);
139-
const contents = await bundleStyles({path: sourcePath});
139+
const contents = await bundleStyles({path: sourcePath, minify: true});
140140
const hash = createHash("sha256").update(contents).digest("hex").slice(0, 8);
141141
const ext = extname(specifier);
142142
const alias = `/${join("_import", dirname(specifier), `${basename(specifier, ext)}.${hash}${ext}`)}`;

src/rollup.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,20 @@ function rewriteInputsNamespace(code: string) {
3333
return code.replace(/\b__ns__\b/g, "inputs-3a86ea");
3434
}
3535

36-
export async function bundleStyles({path, theme}: {path?: string; theme?: string[]}): Promise<string> {
36+
export async function bundleStyles({
37+
minify = false,
38+
path,
39+
theme
40+
}: {
41+
minify?: boolean;
42+
path?: string;
43+
theme?: string[];
44+
}): Promise<string> {
3745
const result = await build({
3846
bundle: true,
3947
...(path ? {entryPoints: [path]} : {stdin: {contents: renderTheme(theme!), loader: "css"}}),
4048
write: false,
49+
minify,
4150
alias: STYLE_MODULES
4251
});
4352
const text = result.outputFiles[0].text;

0 commit comments

Comments
 (0)