@kirana-style/bun 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,3 +1,19 @@
1
- # Temporary Holding Version
1
+ # @kirana-style/bun
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ The Bun bundler adapter for Kirana ([11-integrations](../../../../spec/11-integrations.md)).
4
+
5
+ ```ts
6
+ // build.ts
7
+ import kirana from "@kirana-style/bun"
8
+
9
+ await Bun.build({
10
+ entrypoints: ["./src/main.tsx"],
11
+ outdir: "./dist",
12
+ plugins: [kirana()],
13
+ })
14
+ ```
15
+
16
+ `onLoad` compiles modules that carry styles; the stylesheet entry loads as a placeholder that Bun
17
+ bundles as CSS, and `onEnd` writes the finished stylesheet into the emitted file (`main.css` for CSS
18
+ imported from `main.tsx`). A `naming` pattern that hashes CSS keeps the placeholder's hash, and
19
+ `write: false` keeps the placeholder.
package/package.json CHANGED
@@ -1,6 +1,41 @@
1
1
  {
2
2
  "name": "@kirana-style/bun",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "description": "Kirana for Bun's bundler: compiles modules and finishes the stylesheet.",
5
+ "license": "MIT",
6
+ "author": "The Kirana authors",
7
+ "homepage": "https://kirana.style",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "git+https://github.com/kirana-style/kirana.git",
11
+ "directory": "packages/kirana/npm/bun"
12
+ },
13
+ "bugs": {
14
+ "url": "https://github.com/kirana-style/kirana/issues"
15
+ },
16
+ "type": "module",
17
+ "exports": {
18
+ ".": {
19
+ "types": "./src/index.d.ts",
20
+ "default": "./src/index.js"
21
+ },
22
+ "./package.json": "./package.json"
23
+ },
24
+ "dependencies": {
25
+ "@kirana-style/compiler": "0.1.0",
26
+ "@kirana-style/core": "0.1.0"
27
+ },
28
+ "files": [
29
+ "src",
30
+ "bin",
31
+ "*.d.ts",
32
+ "*.css",
33
+ "README.md",
34
+ "builders.json",
35
+ "schemas",
36
+ "gatsby-node.mjs",
37
+ "rules",
38
+ "recommended.json",
39
+ "strict.json"
40
+ ]
41
+ }
package/src/index.d.ts ADDED
@@ -0,0 +1,46 @@
1
+ import type { DriverOptions } from "@kirana-style/compiler";
2
+ import type { KiranaConfig } from "@kirana-style/core/config";
3
+ export type KiranaBunOptions = {
4
+ /** The project root: where `kirana.config.*` and `@kirana-style/core` are found. Default: the working directory. */
5
+ root?: string;
6
+ /** Options that override `kirana.config.*`. */
7
+ config?: KiranaConfig;
8
+ /** Report `info` diagnostics (such as `dynamic-bridged`) as warnings. */
9
+ reportInfo?: boolean;
10
+ /** Injected Driver options (tests). */
11
+ driver?: Pick<DriverOptions, "native" | "importModule">;
12
+ };
13
+ type Loaded = {
14
+ contents: string;
15
+ loader: string;
16
+ } | undefined;
17
+ /** The part of Bun's plugin builder the plugin uses. */
18
+ export type PluginBuilder = {
19
+ /** `Bun.build` passes its options, `entrypoints` included; the dev server (`bun ./index.html`) does not. */
20
+ config: {
21
+ minify?: boolean | object;
22
+ entrypoints?: unknown;
23
+ };
24
+ onResolve: (options: {
25
+ filter: RegExp;
26
+ }, resolve: (args: {
27
+ path: string;
28
+ }) => {
29
+ path: string;
30
+ } | undefined) => void;
31
+ onLoad: (options: {
32
+ filter: RegExp;
33
+ }, load: (args: {
34
+ path: string;
35
+ }) => Loaded | Promise<Loaded>) => void;
36
+ onEnd: (end: (result: {
37
+ outputs: {
38
+ path: string;
39
+ }[];
40
+ }) => void | Promise<void>) => void;
41
+ };
42
+ export default function kirana(options?: KiranaBunOptions): {
43
+ name: string;
44
+ setup(build: PluginBuilder): void;
45
+ };
46
+ export {};
package/src/index.js ADDED
@@ -0,0 +1,95 @@
1
+ /**
2
+ * The Bun bundler host adapter (KRN-INT-001, KRN-INT-003): `onLoad` compiles modules, and `onEnd`
3
+ * writes the finished stylesheet into the CSS Bun emitted. It never interprets styles (D-028).
4
+ *
5
+ * The stylesheet entry loads as the placeholder rule, which Bun bundles like any CSS import; once
6
+ * every module has loaded, the plugin replaces it in the written file. Bun names CSS imported from a
7
+ * JavaScript entry after the entry (`main.css`), so the name stays right; a `naming` pattern that
8
+ * hashes assets would carry the placeholder's hash, and a build with `write: false` keeps the
9
+ * placeholder, since there is no file to finish.
10
+ */
11
+ import { createHash } from "node:crypto";
12
+ import { existsSync, writeFileSync } from "node:fs";
13
+ import { readFile, writeFile } from "node:fs/promises";
14
+ import { tmpdir } from "node:os";
15
+ import { join } from "node:path";
16
+ import { Driver } from "@kirana-style/compiler";
17
+ import { PLACEHOLDER, createReporter, fillStylesheet, shouldCompile, stylesheetPath, } from "@kirana-style/compiler/host";
18
+ const LOADERS = {
19
+ tsx: "tsx",
20
+ ts: "ts",
21
+ mts: "ts",
22
+ cts: "ts",
23
+ jsx: "jsx",
24
+ };
25
+ export default function kirana(options = {}) {
26
+ return {
27
+ name: "kirana",
28
+ setup(build) {
29
+ const root = options.root ?? process.cwd();
30
+ const stylesheetFile = stylesheetPath(root);
31
+ let driver;
32
+ const session = () => (driver ??= Driver.create({
33
+ root,
34
+ config: options.config,
35
+ ...options.driver,
36
+ }));
37
+ const reporter = createReporter(options);
38
+ const context = {
39
+ warn: (message) => console.warn(message),
40
+ error: (message) => {
41
+ throw new Error(message);
42
+ },
43
+ };
44
+ // The dev server: the stylesheet import resolves to a file this plugin rewrites whenever the
45
+ // stylesheet changes, which Bun watches and hot-reloads (KRN-INT-009). A build instead gets
46
+ // the placeholder and has it replaced in `onEnd`.
47
+ const live = !("entrypoints" in build.config)
48
+ ? join(tmpdir(), `kirana-bun-${createHash("sha256").update(root).digest("hex").slice(0, 12)}.css`)
49
+ : undefined;
50
+ const refresh = async () => {
51
+ if (live)
52
+ await writeFile(live, (await session()).stylesheet(false));
53
+ };
54
+ if (live) {
55
+ writeFileSync(live, "");
56
+ build.onResolve({ filter: /^@kirana-style\/core\/stylesheet\.css$/ }, () => ({
57
+ path: live,
58
+ }));
59
+ }
60
+ build.onLoad({ filter: /stylesheet\.css$/ }, ({ path }) => path === stylesheetFile
61
+ ? { contents: PLACEHOLDER, loader: "css" }
62
+ : undefined);
63
+ build.onLoad({ filter: /\.[cm]?[jt]sx?$/ }, async ({ path }) => {
64
+ const compiler = await session();
65
+ if (!compiler.compiles(path))
66
+ return undefined;
67
+ const code = await readFile(path, "utf8");
68
+ if (!shouldCompile(path, code, compiler.libraries))
69
+ return undefined;
70
+ const result = compiler.transform(path, code);
71
+ reporter.module(context, path, code, result.diagnostics);
72
+ if (result.cssChanged)
73
+ await refresh();
74
+ if (result.code === undefined)
75
+ return undefined;
76
+ return {
77
+ contents: result.code,
78
+ loader: LOADERS[path.slice(path.lastIndexOf(".") + 1)] ?? "js",
79
+ };
80
+ });
81
+ build.onEnd(async ({ outputs }) => {
82
+ const compiled = await session();
83
+ reporter.project(context, root, compiled.diagnostics());
84
+ const css = compiled.stylesheet(Boolean(build.config.minify));
85
+ for (const { path } of outputs) {
86
+ if (!path.endsWith(".css") || !existsSync(path))
87
+ continue;
88
+ const source = fillStylesheet(await readFile(path, "utf8"), css);
89
+ if (source !== undefined)
90
+ await writeFile(path, source);
91
+ }
92
+ });
93
+ },
94
+ };
95
+ }