@kirana-style/esbuild 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 +19 -2
- package/package.json +42 -4
- package/src/index.d.ts +14 -0
- package/src/index.js +111 -0
package/README.md
CHANGED
|
@@ -1,3 +1,20 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @kirana-style/esbuild
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The esbuild adapter for Kirana ([11-integrations](../../../../spec/11-integrations.md)).
|
|
4
|
+
|
|
5
|
+
```js
|
|
6
|
+
import kirana from "@kirana-style/esbuild"
|
|
7
|
+
import * as esbuild from "esbuild"
|
|
8
|
+
|
|
9
|
+
await esbuild.build({
|
|
10
|
+
entryPoints: ["src/main.tsx"],
|
|
11
|
+
bundle: true,
|
|
12
|
+
outdir: "www",
|
|
13
|
+
plugins: [kirana()],
|
|
14
|
+
})
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
`onLoad` compiles modules that carry styles; the stylesheet entry loads as a placeholder esbuild
|
|
18
|
+
bundles as CSS, and `onEnd` writes the finished stylesheet into it — on disk, or in `outputFiles`
|
|
19
|
+
with `write: false`. Under `context().watch()` and `serve()` the entry reloads from a file the plugin
|
|
20
|
+
keeps current, so the served stylesheet follows every edit.
|
package/package.json
CHANGED
|
@@ -1,6 +1,44 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kirana-style/esbuild",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Kirana for esbuild: compiles modules and finishes the stylesheet esbuild bundles.",
|
|
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/esbuild"
|
|
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
|
+
"peerDependencies": {
|
|
29
|
+
"esbuild": ">=0.25.0"
|
|
30
|
+
},
|
|
31
|
+
"files": [
|
|
32
|
+
"src",
|
|
33
|
+
"bin",
|
|
34
|
+
"*.d.ts",
|
|
35
|
+
"*.css",
|
|
36
|
+
"README.md",
|
|
37
|
+
"builders.json",
|
|
38
|
+
"schemas",
|
|
39
|
+
"gatsby-node.mjs",
|
|
40
|
+
"rules",
|
|
41
|
+
"recommended.json",
|
|
42
|
+
"strict.json"
|
|
43
|
+
]
|
|
44
|
+
}
|
package/src/index.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { DriverOptions } from "@kirana-style/compiler";
|
|
2
|
+
import type { KiranaConfig } from "@kirana-style/core/config";
|
|
3
|
+
import type { Plugin } from "esbuild";
|
|
4
|
+
export type KiranaEsbuildOptions = {
|
|
5
|
+
/** The project root: where `kirana.config.*` and `@kirana-style/core` are found. Default: the working directory. */
|
|
6
|
+
root?: string;
|
|
7
|
+
/** Options that override `kirana.config.*`. */
|
|
8
|
+
config?: KiranaConfig;
|
|
9
|
+
/** Report `info` diagnostics (such as `dynamic-bridged`) as warnings. */
|
|
10
|
+
reportInfo?: boolean;
|
|
11
|
+
/** Injected Driver options (tests). */
|
|
12
|
+
driver?: Pick<DriverOptions, "native" | "importModule">;
|
|
13
|
+
};
|
|
14
|
+
export default function kirana(options?: KiranaEsbuildOptions): Plugin;
|
package/src/index.js
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The esbuild host adapter (KRN-INT-001, KRN-INT-003): `onLoad` compiles modules, and `onEnd` writes
|
|
3
|
+
* the finished stylesheet into the CSS esbuild bundled. It never interprets styles (D-028).
|
|
4
|
+
*
|
|
5
|
+
* The stylesheet entry loads as the placeholder rule, which esbuild bundles like any CSS import
|
|
6
|
+
* (`main.css` beside `main.js`); once every module has loaded, the plugin replaces it — in the written
|
|
7
|
+
* file, or in `outputFiles` when esbuild keeps them in memory (`write: false`). Watch and serve reload
|
|
8
|
+
* the entry itself from a file the plugin keeps current, so what `serve` sends follows every edit. A
|
|
9
|
+
* `naming` pattern that hashes CSS keeps the placeholder's hash.
|
|
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 path 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
|
+
/** An output file with new text, in the shape esbuild gives them. */
|
|
26
|
+
function outputFile(file, text) {
|
|
27
|
+
return { ...file, contents: new TextEncoder().encode(text), text };
|
|
28
|
+
}
|
|
29
|
+
export default function kirana(options = {}) {
|
|
30
|
+
return {
|
|
31
|
+
name: "kirana",
|
|
32
|
+
setup(build) {
|
|
33
|
+
const root = options.root ?? process.cwd();
|
|
34
|
+
const stylesheetFile = stylesheetPath(root);
|
|
35
|
+
// The written outputs are found through the metafile.
|
|
36
|
+
build.initialOptions.metafile = true;
|
|
37
|
+
let driver;
|
|
38
|
+
const session = () => (driver ??= Driver.create({
|
|
39
|
+
root,
|
|
40
|
+
config: options.config,
|
|
41
|
+
...options.driver,
|
|
42
|
+
}));
|
|
43
|
+
const reporter = createReporter(options);
|
|
44
|
+
const context = {
|
|
45
|
+
warn: (message) => console.warn(message),
|
|
46
|
+
error: (message) => {
|
|
47
|
+
throw new Error(message);
|
|
48
|
+
},
|
|
49
|
+
};
|
|
50
|
+
// Watch and serve: the entry follows a file `onEnd` writes whenever the stylesheet changes, so a
|
|
51
|
+
// rebuild reloads it with the live stylesheet — which is what `serve` sends, from its own
|
|
52
|
+
// copy of the outputs. The first load, and every load of a one-shot build, is the placeholder.
|
|
53
|
+
const live = path.join(tmpdir(), `kirana-esbuild-${createHash("sha256").update(root).digest("hex").slice(0, 12)}.css`);
|
|
54
|
+
let written;
|
|
55
|
+
writeFileSync(live, "");
|
|
56
|
+
build.onLoad({ filter: /stylesheet\.css$/ }, async ({ path: file }) => file === stylesheetFile
|
|
57
|
+
? {
|
|
58
|
+
contents: written === undefined
|
|
59
|
+
? PLACEHOLDER
|
|
60
|
+
: (await session()).stylesheet(Boolean(build.initialOptions.minify)),
|
|
61
|
+
loader: "css",
|
|
62
|
+
watchFiles: [live],
|
|
63
|
+
}
|
|
64
|
+
: undefined);
|
|
65
|
+
build.onLoad({ filter: /\.[cm]?[jt]sx?$/ }, async ({ path: file }) => {
|
|
66
|
+
const compiler = await session();
|
|
67
|
+
if (!compiler.compiles(file))
|
|
68
|
+
return undefined;
|
|
69
|
+
const code = await readFile(file, "utf8");
|
|
70
|
+
if (!shouldCompile(file, code, compiler.libraries))
|
|
71
|
+
return undefined;
|
|
72
|
+
const result = compiler.transform(file, code);
|
|
73
|
+
reporter.module(context, file, code, result.diagnostics);
|
|
74
|
+
if (result.code === undefined)
|
|
75
|
+
return undefined;
|
|
76
|
+
return {
|
|
77
|
+
contents: result.code,
|
|
78
|
+
loader: LOADERS[file.slice(file.lastIndexOf(".") + 1)] ?? "js",
|
|
79
|
+
};
|
|
80
|
+
});
|
|
81
|
+
build.onEnd(async (result) => {
|
|
82
|
+
const compiler = await session();
|
|
83
|
+
reporter.project(context, root, compiler.diagnostics());
|
|
84
|
+
const css = compiler.stylesheet(Boolean(build.initialOptions.minify));
|
|
85
|
+
if (css !== written) {
|
|
86
|
+
written = css;
|
|
87
|
+
await writeFile(live, css);
|
|
88
|
+
}
|
|
89
|
+
if (result.outputFiles) {
|
|
90
|
+
result.outputFiles.forEach((file, index) => {
|
|
91
|
+
const source = file.path.endsWith(".css")
|
|
92
|
+
? fillStylesheet(file.text, css)
|
|
93
|
+
: undefined;
|
|
94
|
+
if (source !== undefined)
|
|
95
|
+
result.outputFiles[index] = outputFile(file, source);
|
|
96
|
+
});
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
const base = build.initialOptions.absWorkingDir ?? process.cwd();
|
|
100
|
+
for (const output of Object.keys(result.metafile.outputs)) {
|
|
101
|
+
const file = path.resolve(base, output);
|
|
102
|
+
if (!file.endsWith(".css") || !existsSync(file))
|
|
103
|
+
continue;
|
|
104
|
+
const source = fillStylesheet(await readFile(file, "utf8"), css);
|
|
105
|
+
if (source !== undefined)
|
|
106
|
+
await writeFile(file, source);
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
},
|
|
110
|
+
};
|
|
111
|
+
}
|