From d401564a8e1bba199e138649b3f9ea969163b1c7 Mon Sep 17 00:00:00 2001 From: Kyle June Date: Sat, 3 Oct 2026 00:27:22 -0400 Subject: [PATCH 1/2] docs: await builds before stopping esbuild --- README.md | 231 +++++++++++++++++++++++------------------ build-examples.test.ts | 216 ++++++++++++++++++++++++++++++++++++++ less.ts | 20 ++-- postcss.ts | 16 +-- sass.ts | 20 ++-- stylus.ts | 20 ++-- 6 files changed, 391 insertions(+), 132 deletions(-) create mode 100644 build-examples.test.ts diff --git a/README.md b/README.md index 64df323..eec4fde 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,9 @@ entrypoint, the plugin will output the css to a file. When the styles are imported from a JavaScript/TypeScript file, the plugin will output a js file with the css as a constant along with any css module class names from the file. +For one-shot builds, await the build before stopping esbuild. Use `finally` to +await cleanup even when compilation fails. + ### Using with esbuild deno loader plugin This plugin can be used with the esbuild Deno loader to process CSS files during @@ -45,17 +48,20 @@ import { postCSSPlugin } from "@udibo/esbuild-plugin-postcss"; const configPath = path.resolve(Deno.cwd(), "deno.json"); -await esbuild.build({ - plugins: [ - postCSSPlugin({ modules: true }), - denoPlugin({ configPath }), - ], - entryPoints: ["./src/main.ts"], - outdir: "./dist", - bundle: true, - format: "esm", -}); -esbuild.stop(); +try { + await esbuild.build({ + plugins: [ + postCSSPlugin({ modules: true }), + denoPlugin({ configPath }), + ], + entryPoints: ["./src/main.ts"], + outdir: "./dist", + bundle: true, + format: "esm", + }); +} finally { + await esbuild.stop(); +} ``` When using the esbuild Deno loader with this plugin, make sure to put the deno @@ -109,15 +115,18 @@ import { postCSSPlugin } from "@udibo/esbuild-plugin-postcss"; import postcssConfig from "./postcss.config.ts"; -esbuild.build({ - plugins: [ - postCSSPlugin(postcssConfig), - ], - entryPoints: ["./src/index.css"], - outdir: "./dist", - bundle: true, -}); -esbuild.stop(); +try { + await esbuild.build({ + plugins: [ + postCSSPlugin(postcssConfig), + ], + entryPoints: ["./src/index.css"], + outdir: "./dist", + bundle: true, + }); +} finally { + await esbuild.stop(); +} ``` ### PostCSS plugins @@ -137,17 +146,20 @@ import esbuild from "esbuild"; import autoprefixer from "autoprefixer"; import { postCSSPlugin } from "@udibo/esbuild-plugin-postcss"; -esbuild.build({ - plugins: [ - postCSSPlugin({ - plugins: [autoprefixer()], - }), - ], - entryPoints: ["./src/index.css"], - outdir: "./dist", - bundle: true, -}); -esbuild.stop(); +try { + await esbuild.build({ + plugins: [ + postCSSPlugin({ + plugins: [autoprefixer()], + }), + ], + entryPoints: ["./src/index.css"], + outdir: "./dist", + bundle: true, + }); +} finally { + await esbuild.stop(); +} ``` If this was the `./src/index.css` file: @@ -196,25 +208,28 @@ import esbuild from "esbuild"; import tailwindcss from "tailwindcss"; import { postCSSPlugin } from "@udibo/esbuild-plugin-postcss"; -esbuild.build({ - plugins: [ - postCSSPlugin({ - plugins: [ - tailwindcss({ - content: ["./src/**/*.{html,js,ts,jsx,tsx}"], - theme: { - extend: {}, - }, - plugins: [], - }), - ], - }), - ], - entryPoints: ["./src/index.css"], - outdir: "./dist", - bundle: true, -}); -esbuild.stop(); +try { + await esbuild.build({ + plugins: [ + postCSSPlugin({ + plugins: [ + tailwindcss({ + content: ["./src/**/*.{html,js,ts,jsx,tsx}"], + theme: { + extend: {}, + }, + plugins: [], + }), + ], + }), + ], + entryPoints: ["./src/index.css"], + outdir: "./dist", + bundle: true, + }); +} finally { + await esbuild.stop(); +} ``` You can also create a `tailwind.config.ts` file and use that with the plugin. @@ -226,19 +241,22 @@ import { postCSSPlugin } from "@udibo/esbuild-plugin-postcss"; import tailwindConfig from "./tailwind.config.ts"; -esbuild.build({ - plugins: [ - postCSSPlugin({ - plugins: [ - tailwindcss(tailwindConfig), - ], - }), - ], - entryPoints: ["./src/index.css"], - outdir: "./dist", - bundle: true, -}); -esbuild.stop(); +try { + await esbuild.build({ + plugins: [ + postCSSPlugin({ + plugins: [ + tailwindcss(tailwindConfig), + ], + }), + ], + entryPoints: ["./src/index.css"], + outdir: "./dist", + bundle: true, + }); +} finally { + await esbuild.stop(); +} ``` Then in your main.css file, you would import tailwinds base, components, and @@ -260,19 +278,22 @@ import esbuild from "esbuild"; import { postCSSPlugin } from "@udibo/esbuild-plugin-postcss"; import tailwindcss from "@tailwindcss/postcss"; -esbuild.build({ - plugins: [ - postCSSPlugin({ - plugins: [ - tailwindcss(), - ], - }), - ], - entryPoints: ["./src/index.css"], - outdir: "./dist", - bundle: true, -}); -esbuild.stop(); +try { + await esbuild.build({ + plugins: [ + postCSSPlugin({ + plugins: [ + tailwindcss(), + ], + }), + ], + entryPoints: ["./src/index.css"], + outdir: "./dist", + bundle: true, + }); +} finally { + await esbuild.stop(); +} ``` In tailwindcss v4, the configuration is in css. When you import tailwindcss, it @@ -302,19 +323,22 @@ options. import esbuild from "esbuild"; import { postCSSPlugin } from "@udibo/esbuild-plugin-postcss"; -esbuild.build({ - plugins: [ - postCSSPlugin({ - modules: { - // options for css modules - }, - }), - ], - entryPoints: ["./src/main.module.css"], - outdir: "./dist", - bundle: true, -}); -esbuild.stop(); +try { + await esbuild.build({ + plugins: [ + postCSSPlugin({ + modules: { + // options for css modules + }, + }), + ], + entryPoints: ["./src/main.module.css"], + outdir: "./dist", + bundle: true, + }); +} finally { + await esbuild.stop(); +} ``` If this was the `./src/main.module.css` file: @@ -468,19 +492,22 @@ import * as sass from "sass"; import { postCSSPlugin } from "@udibo/esbuild-plugin-postcss"; import { sassPreprocessor } from "@udibo/esbuild-plugin-postcss/sass"; -esbuild.build({ - plugins: [ - postCSSPlugin({ - preprocessors: [ - sassPreprocessor(sass), - ], - }), - ], - entryPoints: ["./src/main.scss"], - outdir: "./dist", - bundle: true, -}); -esbuild.stop(); +try { + await esbuild.build({ + plugins: [ + postCSSPlugin({ + preprocessors: [ + sassPreprocessor(sass), + ], + }), + ], + entryPoints: ["./src/main.scss"], + outdir: "./dist", + bundle: true, + }); +} finally { + await esbuild.stop(); +} ``` If this was the `./src/main.scss` file: diff --git a/build-examples.test.ts b/build-examples.test.ts new file mode 100644 index 0000000..18cb08e --- /dev/null +++ b/build-examples.test.ts @@ -0,0 +1,216 @@ +import { + assert, + assertEquals, + assertMatch, + assertStringIncludes, +} from "@std/assert"; +import { deadline } from "@std/async/deadline"; +import * as path from "@std/path"; +import { describe, it } from "@std/testing/bdd"; + +async function readExample(heading: string): Promise { + const readme = (await Deno.readTextFile( + new URL("./README.md", import.meta.url), + )).replace(/\r\n/g, "\n"); + const start = readme.indexOf(`${heading}\n`); + assert(start >= 0, `Missing README section: ${heading}`); + const remaining = readme.slice(start + heading.length + 1); + const end = remaining.search(/\n#{1,6} /); + const section = end < 0 ? remaining : remaining.slice(0, end); + const blocks = [...section.matchAll(/```ts\n([\s\S]*?)\n```/g)]; + const example = blocks.find((block) => block[1]!.includes("esbuild.build(")); + assert(example, `Missing build example: ${heading}`); + return example[1]!; +} + +async function stopChild( + child: Deno.ChildProcess, + pending: Promise, +) { + try { + try { + child.kill("SIGTERM"); + } catch (error) { + if (!(error instanceof Deno.errors.NotFound)) throw error; + } + } finally { + await pending.catch(() => {}); + } +} +async function runExample( + heading: string, + modules: boolean, + rejectCompilation = false, +) { + const directory = await Deno.makeTempDir({ prefix: "postcss-readme-" }); + try { + await Deno.mkdir(path.join(directory, "src")); + await Deno.writeTextFile( + path.join(directory, "src", modules ? "main.module.css" : "index.css"), + modules ? ".title { color: green; }" : ".box { color: red; }", + ); + await Deno.writeTextFile( + path.join(directory, "postcss.config.ts"), + rejectCompilation + ? `export default { + modules: false, + plugins: [{ + postcssPlugin: "documented-build-failure", + Once() { throw new Error("Expected documented compilation failure"); }, + }], +};` + : "export default { modules: false };", + ); + const eventsFile = path.join(directory, "events.txt"); + await Deno.writeTextFile(eventsFile, ""); + const observerFile = path.join(directory, "esbuild-observer.ts"); + await Deno.writeTextFile( + observerFile, + `import actual from "actual-esbuild"; +export * from "actual-esbuild"; +const eventsFile = ${JSON.stringify(eventsFile)}; +function record(event: string) { + Deno.writeTextFileSync(eventsFile, event + "\\n", { append: true }); +} +export function build(options: import("actual-esbuild").BuildOptions) { + return actual.build(options).then((result) => { + record("build fulfilled"); + return result; + }, (error) => { + record("build rejected"); + throw error; + }); +} +export async function stop() { + record("stop started"); + await actual.stop(); + record("stop settled"); +} +export default { ...actual, build, stop }; +`, + ); + const config = JSON.parse( + await Deno.readTextFile(new URL("./deno.json", import.meta.url)), + ); + const lock = JSON.parse( + await Deno.readTextFile(new URL("./deno.lock", import.meta.url)), + ); + lock.workspace.links = { + ...lock.workspace.links, + [`jsr:${config.name}@${config.version}`]: { + dependencies: lock.workspace.dependencies, + }, + }; + const lockFile = path.join(directory, "deno.lock"); + await Deno.writeTextFile(lockFile, JSON.stringify(lock)); + const configFile = path.join(directory, "deno.json"); + await Deno.writeTextFile( + configFile, + JSON.stringify({ + workspace: [], + nodeModulesDir: "none", + lock: lockFile, + imports: { + ...config.imports, + esbuild: path.toFileUrl(observerFile).href, + "actual-esbuild": config.imports.esbuild, + "@udibo/esbuild-plugin-postcss": new URL( + "./postcss.ts", + import.meta.url, + ).href, + }, + }), + ); + const exampleFile = path.join(directory, "example.ts"); + await Deno.writeTextFile(exampleFile, await readExample(heading)); + const child = new Deno.Command(Deno.execPath(), { + args: [ + "run", + "--quiet", + "--cached-only", + "--frozen", + `--config=${configFile}`, + "--allow-read", + `--allow-write=${directory}`, + "--allow-env", + "--allow-run", + "--allow-sys", + exampleFile, + ], + cwd: directory, + stdout: "piped", + stderr: "piped", + }).spawn(); + let finished = false; + const pending = child.output().finally(() => { + finished = true; + }); + let output: Deno.CommandOutput; + try { + output = await deadline(pending, 15_000); + } finally { + if (!finished) await stopChild(child, pending); + } + const stderr = new TextDecoder().decode(output.stderr); + const events = (await Deno.readTextFile(eventsFile)).trim().split("\n") + .filter(Boolean); + const css = output.success + ? await Deno.readTextFile( + path.join(directory, "dist", modules ? "main.module.css" : "index.css"), + ) + : ""; + const classMap = modules && output.success + ? JSON.parse( + await Deno.readTextFile( + path.join(directory, "src", "main.module.css.json"), + ), + ) as Record + : undefined; + return { output, stderr, events, css, classMap }; + } finally { + await Deno.remove(directory, { recursive: true }); + } +} + +const CONFIGURATION_HEADING = + "#### Separating postcss configuration from esbuild configuration"; + +describe("documented one-shot builds", () => { + it("finishes the actual configuration example before stopping esbuild", async () => { + const result = await runExample(CONFIGURATION_HEADING, false); + assertEquals(result.output.success, true, result.stderr); + assertMatch(result.css, /color:\s*red/); + assertEquals(result.events, [ + "build fulfilled", + "stop started", + "stop settled", + ]); + }); + + it("finishes the actual CSS Module example with its matching class map", async () => { + const result = await runExample("### CSS Modules", true); + assertEquals(result.output.success, true, result.stderr); + assertMatch(result.css, /color:\s*green/); + assert(result.classMap?.title); + assertStringIncludes(result.css, `.${result.classMap.title}`); + assertEquals(result.events, [ + "build fulfilled", + "stop started", + "stop settled", + ]); + }); + + it("propagates real compilation failure after joining owned service cleanup", async () => { + const result = await runExample(CONFIGURATION_HEADING, false, true); + assertEquals(result.output.success, false); + assertStringIncludes( + result.stderr, + "Expected documented compilation failure", + ); + assertEquals(result.events, [ + "build rejected", + "stop started", + "stop settled", + ]); + }); +}); diff --git a/less.ts b/less.ts index 9337c9a..2044df5 100644 --- a/less.ts +++ b/less.ts @@ -35,14 +35,18 @@ export interface LessModule { * import { postCSSPlugin } from "@udibo/esbuild-plugin-postcss"; * import { lessPreprocessor } from "@udibo/esbuild-plugin-postcss/less"; * - * esbuild.build({ - * plugins: [postCSSPlugin({ - * preprocessors: [lessPreprocessor(less)], - * })], - * entryPoints: ["./src/index.less"], - * outdir: "./dist", - * bundle: true, - * }); + * try { + * await esbuild.build({ + * plugins: [postCSSPlugin({ + * preprocessors: [lessPreprocessor(less)], + * })], + * entryPoints: ["./src/index.less"], + * outdir: "./dist", + * bundle: true, + * }); + * } finally { + * await esbuild.stop(); + * } * ``` * * @param less - The `less` module. diff --git a/postcss.ts b/postcss.ts index 594720c..44022d3 100644 --- a/postcss.ts +++ b/postcss.ts @@ -304,12 +304,16 @@ export interface PostCSSPluginOptions { * import esbuild from "esbuild"; * import { postCSSPlugin } from "@udibo/esbuild-plugin-postcss"; * - * esbuild.build({ - * plugins: [postCSSPlugin()], - * entryPoints: ["./src/index.css"], - * outdir: "./dist", - * bundle: true, - * }); + * try { + * await esbuild.build({ + * plugins: [postCSSPlugin()], + * entryPoints: ["./src/index.css"], + * outdir: "./dist", + * bundle: true, + * }); + * } finally { + * await esbuild.stop(); + * } * ``` * * @param options - The options for the postcss plugin. diff --git a/sass.ts b/sass.ts index 7159430..b856376 100644 --- a/sass.ts +++ b/sass.ts @@ -35,14 +35,18 @@ export interface SassModule { * import { postCSSPlugin } from "@udibo/esbuild-plugin-postcss"; * import { sassPreprocessor } from "@udibo/esbuild-plugin-postcss/sass"; * - * esbuild.build({ - * plugins: [postCSSPlugin({ - * preprocessors: [sassPreprocessor(sass)], - * })], - * entryPoints: ["./src/index.scss"], - * outdir: "./dist", - * bundle: true, - * }); + * try { + * await esbuild.build({ + * plugins: [postCSSPlugin({ + * preprocessors: [sassPreprocessor(sass)], + * })], + * entryPoints: ["./src/index.scss"], + * outdir: "./dist", + * bundle: true, + * }); + * } finally { + * await esbuild.stop(); + * } * ``` * * @param sass - The `sass` module. diff --git a/stylus.ts b/stylus.ts index a6059f9..c5ff4e7 100644 --- a/stylus.ts +++ b/stylus.ts @@ -36,14 +36,18 @@ export interface StylusModule { * import { postCSSPlugin } from "@udibo/esbuild-plugin-postcss"; * import { stylusPreprocessor } from "@udibo/esbuild-plugin-postcss/stylus"; * - * esbuild.build({ - * plugins: [postCSSPlugin({ - * preprocessors: [stylusPreprocessor(stylus)], - * })], - * entryPoints: ["./src/index.styl"], - * outdir: "./dist", - * bundle: true, - * }); + * try { + * await esbuild.build({ + * plugins: [postCSSPlugin({ + * preprocessors: [stylusPreprocessor(stylus)], + * })], + * entryPoints: ["./src/index.styl"], + * outdir: "./dist", + * bundle: true, + * }); + * } finally { + * await esbuild.stop(); + * } * ``` * * @param stylus - The `stylus` module. From aee079c842ed7eebfb0e38b854f1deb67e131a43 Mon Sep 17 00:00:00 2001 From: Kyle June Date: Sat, 3 Oct 2026 00:39:20 -0400 Subject: [PATCH 2/2] test: canonicalize example fixture paths --- build-examples.test.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/build-examples.test.ts b/build-examples.test.ts index 18cb08e..6d239eb 100644 --- a/build-examples.test.ts +++ b/build-examples.test.ts @@ -42,8 +42,11 @@ async function runExample( modules: boolean, rejectCompilation = false, ) { - const directory = await Deno.makeTempDir({ prefix: "postcss-readme-" }); + const temporaryDirectory = await Deno.makeTempDir({ + prefix: "postcss-readme-", + }); try { + const directory = await Deno.realPath(temporaryDirectory); await Deno.mkdir(path.join(directory, "src")); await Deno.writeTextFile( path.join(directory, "src", modules ? "main.module.css" : "index.css"), @@ -168,7 +171,7 @@ export default { ...actual, build, stop }; : undefined; return { output, stderr, events, css, classMap }; } finally { - await Deno.remove(directory, { recursive: true }); + await Deno.remove(temporaryDirectory, { recursive: true }); } }