@teacss/bun 0.9.4 → 0.10.3
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 +24 -48
- package/dist/index.d.ts +28 -27
- package/dist/index.js +188 -1
- package/package.json +32 -26
package/README.md
CHANGED
|
@@ -1,65 +1,41 @@
|
|
|
1
1
|
# @teacss/bun
|
|
2
2
|
|
|
3
|
-
Bun
|
|
3
|
+
The `Bun.build` plugin for TeaCSS. It calls the Rust engine (`@teacss/engine`) in process and turns `virtual:teacss.css`, or a CSS entry's `@teacss;`, into the generated CSS. It needs Bun 1.4 or later.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
4
6
|
|
|
5
7
|
```sh
|
|
6
|
-
bun add --dev
|
|
8
|
+
bun add --dev @teacss/bun
|
|
7
9
|
```
|
|
8
10
|
|
|
11
|
+
## Use
|
|
12
|
+
|
|
9
13
|
```ts
|
|
10
|
-
|
|
14
|
+
// build.ts, run with `bun build.ts`
|
|
15
|
+
import { teacss } from "@teacss/bun";
|
|
11
16
|
|
|
12
|
-
await Bun.build({
|
|
13
|
-
entrypoints: ["./src/index.ts"],
|
|
14
|
-
outdir: "./dist",
|
|
15
|
-
plugins: [pluginTeacss()],
|
|
16
|
-
});
|
|
17
|
+
await Bun.build({ entrypoints: ["./src/index.ts"], outdir: "./dist", plugins: [teacss()] });
|
|
17
18
|
```
|
|
18
19
|
|
|
19
|
-
|
|
20
|
-
one-shot build because Bun's plugin API does not expose watch invalidation.
|
|
21
|
-
|
|
22
|
-
For programmatic watch builds, use `watchTeacssBuild`:
|
|
23
|
-
|
|
24
|
-
```ts
|
|
25
|
-
import { watchTeacssBuild } from "@teacss/bun";
|
|
26
|
-
|
|
27
|
-
const session = await watchTeacssBuild(
|
|
28
|
-
{ entrypoints: ["./src/index.ts"], outdir: "./dist" },
|
|
29
|
-
{
|
|
30
|
-
configOrPath: "./index.css",
|
|
31
|
-
onError: (error) => console.error(error),
|
|
32
|
-
},
|
|
33
|
-
);
|
|
20
|
+
Configuration lives in a CSS entry. Create `index.css` at the project root:
|
|
34
21
|
|
|
35
|
-
|
|
22
|
+
```css
|
|
23
|
+
@presets "standard,icons,official";
|
|
24
|
+
@source "./src/**/*.{js,jsx,ts,tsx,html}";
|
|
25
|
+
@teacss;
|
|
36
26
|
```
|
|
37
27
|
|
|
38
|
-
|
|
39
|
-
successful-build `version`. Failed rebuilds preserve the last valid result and
|
|
40
|
-
continue watching configuration, module, entry, and positive `@source` roots.
|
|
41
|
-
`dispose()` waits for active work and is safe to call more than once.
|
|
28
|
+
Import the generated CSS once from the application entry, either as `virtual:teacss.css` or as the entry itself:
|
|
42
29
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
30
|
+
```ts
|
|
31
|
+
// src/index.ts
|
|
32
|
+
import "virtual:teacss.css";
|
|
33
|
+
```
|
|
46
34
|
|
|
47
|
-
|
|
35
|
+
With `outdir` set, the Standard preset's fonts are written to `teacss-fonts/` beside the CSS. `watch(options, onBuild)` reruns `Bun.build` when a project file changes.
|
|
48
36
|
|
|
49
|
-
|
|
50
|
-
source-root creation, removal, and recreation, including external absolute
|
|
51
|
-
sources. Changes during configuration loading or a build cause another build
|
|
52
|
-
before the session settles.
|
|
37
|
+
## Documentation
|
|
53
38
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
callbacks; unchanged declarations do not reload configuration or rebuild.
|
|
58
|
-
Nested source directory symlinks observe physical target changes, including empty
|
|
59
|
-
targets and target deletion or recreation. Missing routes use shallow parent
|
|
60
|
-
observation. Failed reads retain the last accepted configuration. An initial
|
|
61
|
-
failed declaration read also keeps its source routes watched, so repairing the
|
|
62
|
-
source can produce the first build without touching the entry. If native watch
|
|
63
|
-
creation fails with `EACCES`, successful desired watches remain active, including
|
|
64
|
-
shallow recovery sentinels. The original error is reported; repeated failure
|
|
65
|
-
alone does not keep retrying.
|
|
39
|
+
- [Build integrations](https://github.com/teacss/teacss-rs/blob/master/skills/teacss/references/integrations.md) and [their options](https://github.com/teacss/teacss-rs/blob/master/skills/teacss/references/integrations.md#options)
|
|
40
|
+
- [The CSS entry](https://github.com/teacss/teacss-rs/blob/master/skills/teacss/references/setup.md#css-entry)
|
|
41
|
+
- [The TeaCSS skill](https://github.com/teacss/teacss-rs/blob/master/skills/teacss/SKILL.md) for syntax, presets and tooling
|
package/dist/index.d.ts
CHANGED
|
@@ -1,31 +1,32 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { BuildConfig, BuildOutput, BunPlugin } from "bun";
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
1
|
+
import { type ShellOptions } from "@teacss/engine";
|
|
2
|
+
import type { BuildConfig, BuildOutput, BunPlugin } from "bun";
|
|
3
|
+
export type { ShellOptions } from "@teacss/engine";
|
|
4
|
+
/**
|
|
5
|
+
* v10.2 §3.4. `import "virtual:teacss.css"` (or `"teacss.css"`) becomes the generated stylesheet; every other
|
|
6
|
+
* module goes through `project.transform`, and in a CSS file whose top level has `@teacss;` the generated
|
|
7
|
+
* stylesheet then takes that rule's place. `minify` defaults to false, as teacss-ts's Bun plugin emits the
|
|
8
|
+
* stylesheet unminified.
|
|
9
|
+
*/
|
|
10
|
+
export declare function teacss(options?: ShellOptions): BunPlugin;
|
|
11
|
+
export interface WatchOptions {
|
|
12
|
+
/** The `Bun.build` configuration; a plugin of this package already in `plugins` is replaced. */
|
|
13
|
+
build: BuildConfig;
|
|
14
|
+
shell?: ShellOptions;
|
|
15
|
+
/** Milliseconds between the last file event and the rebuild. Default 200. */
|
|
16
|
+
debounce?: number;
|
|
17
|
+
/** Called with a failed build's error or a watcher error. Default `console.error`. */
|
|
18
|
+
onError?: (error: unknown) => void;
|
|
14
19
|
}
|
|
15
|
-
export interface
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
readonly result: BuildOutput | undefined;
|
|
20
|
-
/** Number of successful builds. */
|
|
21
|
-
readonly version: number;
|
|
22
|
-
/** Stop this session without affecting other watch sessions. */
|
|
23
|
-
dispose(): Promise<void>;
|
|
20
|
+
export interface WatchSession {
|
|
21
|
+
/** Successful builds so far, the first one included. */
|
|
22
|
+
readonly builds: number;
|
|
23
|
+
close(): void;
|
|
24
24
|
}
|
|
25
25
|
/**
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
26
|
+
* Builds once, then rebuilds whenever a file in a directory of `project.watchFiles()` (or the root) changes,
|
|
27
|
+
* or a file anywhere under a `@source` glob's directory (`project.sourceDirectories()`), outside `outdir`.
|
|
28
|
+
* Bun's plugin API has no dependency registration, so the watch owns the file list; it resolves after the
|
|
29
|
+
* first build has been reported.
|
|
29
30
|
*/
|
|
30
|
-
export declare function
|
|
31
|
-
//#
|
|
31
|
+
export declare function watch(options: WatchOptions, onBuild: (result: BuildOutput) => void | Promise<void>): Promise<WatchSession>;
|
|
32
|
+
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.js
CHANGED
|
@@ -1 +1,188 @@
|
|
|
1
|
-
import{
|
|
1
|
+
import { existsSync, watch as watchDirectory } from "node:fs";
|
|
2
|
+
import { mkdir, readFile, writeFile } from "node:fs/promises";
|
|
3
|
+
import { dirname, join, resolve, sep } from "node:path";
|
|
4
|
+
import { createProject, extractBundledFonts, replaceEntry } from "@teacss/engine";
|
|
5
|
+
const PLUGIN = "teacss";
|
|
6
|
+
const VIRTUAL_ENTRY = /^(?:virtual:)?teacss\.css$/;
|
|
7
|
+
const MODULE = /\.(?:[cm]?[jt]sx?|html)$/;
|
|
8
|
+
const CSS = /\.css$/;
|
|
9
|
+
const NODE_MODULES = `${sep}node_modules${sep}`;
|
|
10
|
+
// teacss-ts `rewriteBundledAssets` writes each bundled font as `teacss-fonts/teacss-<sha256>.woff2` beside the CSS.
|
|
11
|
+
const FONT_URL_PREFIX = "./teacss-fonts/teacss-";
|
|
12
|
+
const FONT_URL = /^\.\/teacss-fonts\/teacss-[a-f0-9]{64}\.woff2$/;
|
|
13
|
+
/**
|
|
14
|
+
* v10.2 §3.4: the tokens of every `@source` file read once and of `inline.content.inline`. `warned` holds the
|
|
15
|
+
* unmatched-token warnings already printed by earlier builds of the same plugin.
|
|
16
|
+
*/
|
|
17
|
+
function generateCss(project, minify, warned) {
|
|
18
|
+
const union = project.engine.extractFilesUnion(project.info.sourceFiles);
|
|
19
|
+
for (const path of project.info.unreadableDirectories)
|
|
20
|
+
console.warn(`[@teacss/bun] cannot read ${path} (E_IO)`);
|
|
21
|
+
for (const { path, error } of union.failed)
|
|
22
|
+
console.warn(`[@teacss/bun] cannot read ${path} (${error})`);
|
|
23
|
+
const tokens = [...new Set([...union.tokens, ...project.inlineTokens()])];
|
|
24
|
+
const result = project.engine.generate({ kind: "tokens", tokens }, { minify, css: true });
|
|
25
|
+
project.reportUnmatched(result, {
|
|
26
|
+
tokens,
|
|
27
|
+
warn: (message) => {
|
|
28
|
+
if (warned.has(message))
|
|
29
|
+
return;
|
|
30
|
+
warned.add(message);
|
|
31
|
+
console.warn(`[@teacss/bun] ${message}`);
|
|
32
|
+
},
|
|
33
|
+
});
|
|
34
|
+
return result.css;
|
|
35
|
+
}
|
|
36
|
+
function externalizeFonts(css, fonts) {
|
|
37
|
+
const extracted = extractBundledFonts(css, (digest) => `${FONT_URL_PREFIX}${digest}.woff2`);
|
|
38
|
+
for (const font of extracted.fonts)
|
|
39
|
+
fonts.set(font.digest, font.bytes);
|
|
40
|
+
return extracted.css;
|
|
41
|
+
}
|
|
42
|
+
async function writeFonts(result, fonts) {
|
|
43
|
+
if (!result.success || fonts.size === 0)
|
|
44
|
+
return;
|
|
45
|
+
for (const output of result.outputs) {
|
|
46
|
+
if (!output.path.endsWith(".css"))
|
|
47
|
+
continue;
|
|
48
|
+
const css = await output.text();
|
|
49
|
+
const directory = join(dirname(output.path), "teacss-fonts");
|
|
50
|
+
for (const [digest, bytes] of fonts) {
|
|
51
|
+
const name = `teacss-${digest}.woff2`;
|
|
52
|
+
if (!css.includes(`teacss-fonts/${name}`))
|
|
53
|
+
continue;
|
|
54
|
+
await mkdir(directory, { recursive: true });
|
|
55
|
+
await writeFile(join(directory, name), bytes);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
function projectRoot(options, config) {
|
|
60
|
+
return resolve(options.root ?? config.root ?? process.cwd());
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* v10.2 §3.4. `import "virtual:teacss.css"` (or `"teacss.css"`) becomes the generated stylesheet; every other
|
|
64
|
+
* module goes through `project.transform`, and in a CSS file whose top level has `@teacss;` the generated
|
|
65
|
+
* stylesheet then takes that rule's place. `minify` defaults to false, as teacss-ts's Bun plugin emits the
|
|
66
|
+
* stylesheet unminified.
|
|
67
|
+
*/
|
|
68
|
+
export function teacss(options = {}) {
|
|
69
|
+
// `watch()` runs every build through this one plugin; each build gets a fresh project, so that no module or
|
|
70
|
+
// compile-class definition of a file deleted or renamed since lingers, and the warnings outlive it here.
|
|
71
|
+
const warned = new Set();
|
|
72
|
+
return {
|
|
73
|
+
name: PLUGIN,
|
|
74
|
+
setup(build) {
|
|
75
|
+
const project = createProject({ ...options, root: projectRoot(options, build.config) });
|
|
76
|
+
const fonts = new Map();
|
|
77
|
+
const externalize = build.config.outdir ? (css) => externalizeFonts(css, fonts) : (css) => css;
|
|
78
|
+
const generated = () => externalize(generateCss(project, options.minify ?? false, warned));
|
|
79
|
+
build.onResolve({ filter: VIRTUAL_ENTRY }, () => ({ path: "teacss.css", namespace: PLUGIN }));
|
|
80
|
+
// Bun bundles every `url()` of a stylesheet; the fonts exist only after `onEnd` writes them.
|
|
81
|
+
build.onResolve({ filter: FONT_URL }, (args) => (fonts.has(args.path.slice(FONT_URL_PREFIX.length, -".woff2".length)) ? { path: args.path, external: true } : undefined));
|
|
82
|
+
build.onLoad({ filter: /.*/, namespace: PLUGIN }, () => ({ contents: generated(), loader: "css" }));
|
|
83
|
+
build.onLoad({ filter: CSS }, async (args) => {
|
|
84
|
+
const { code } = project.transform(args.path, await readFile(args.path, "utf8"));
|
|
85
|
+
return { contents: replaceEntry(code, generated) ?? externalize(code), loader: "css" };
|
|
86
|
+
});
|
|
87
|
+
build.onLoad({ filter: MODULE }, async (args) => {
|
|
88
|
+
const code = await readFile(args.path, "utf8");
|
|
89
|
+
if (args.path.includes(NODE_MODULES) && !code.includes("@teacss-include"))
|
|
90
|
+
return;
|
|
91
|
+
const result = project.transform(args.path, code);
|
|
92
|
+
return result.changed ? { contents: result.code, loader: args.loader } : undefined;
|
|
93
|
+
});
|
|
94
|
+
build.onEnd(async (result) => {
|
|
95
|
+
project.dispose();
|
|
96
|
+
await writeFonts(result, fonts);
|
|
97
|
+
});
|
|
98
|
+
},
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Builds once, then rebuilds whenever a file in a directory of `project.watchFiles()` (or the root) changes,
|
|
103
|
+
* or a file anywhere under a `@source` glob's directory (`project.sourceDirectories()`), outside `outdir`.
|
|
104
|
+
* Bun's plugin API has no dependency registration, so the watch owns the file list; it resolves after the
|
|
105
|
+
* first build has been reported.
|
|
106
|
+
*/
|
|
107
|
+
export async function watch(options, onBuild) {
|
|
108
|
+
const { build, shell = {}, debounce = 200, onError = console.error } = options;
|
|
109
|
+
const root = projectRoot(shell, build);
|
|
110
|
+
const plugins = [...(build.plugins ?? []).filter((plugin) => plugin.name !== PLUGIN), teacss({ ...shell, root })];
|
|
111
|
+
const project = createProject({ ...shell, root });
|
|
112
|
+
const outdir = build.outdir === undefined ? undefined : resolve(build.outdir);
|
|
113
|
+
const watchers = new Map();
|
|
114
|
+
let timer;
|
|
115
|
+
let running = false;
|
|
116
|
+
let queued = false;
|
|
117
|
+
let closed = false;
|
|
118
|
+
let builds = 0;
|
|
119
|
+
function syncWatchers() {
|
|
120
|
+
project.reload();
|
|
121
|
+
const wanted = new Map([root, ...project.watchFiles().map((file) => dirname(file))].map((directory) => [directory, false]));
|
|
122
|
+
for (const { dir } of project.sourceDirectories())
|
|
123
|
+
if (existsSync(dir))
|
|
124
|
+
wanted.set(dir, true);
|
|
125
|
+
for (const [directory, { watcher, recursive }] of watchers) {
|
|
126
|
+
if (wanted.get(directory) === recursive)
|
|
127
|
+
continue;
|
|
128
|
+
watcher.close();
|
|
129
|
+
watchers.delete(directory);
|
|
130
|
+
}
|
|
131
|
+
for (const [directory, recursive] of wanted) {
|
|
132
|
+
if (watchers.has(directory))
|
|
133
|
+
continue;
|
|
134
|
+
const watcher = watchDirectory(directory, { recursive }, (_event, file) => {
|
|
135
|
+
if (file === null || !isOutput(resolve(directory, file)))
|
|
136
|
+
schedule();
|
|
137
|
+
});
|
|
138
|
+
watchers.set(directory, { watcher: watcher.on("error", onError), recursive });
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
function isOutput(file) {
|
|
142
|
+
return outdir !== undefined && (file === outdir || file.startsWith(`${outdir}${sep}`));
|
|
143
|
+
}
|
|
144
|
+
async function run() {
|
|
145
|
+
running = true;
|
|
146
|
+
do {
|
|
147
|
+
queued = false;
|
|
148
|
+
try {
|
|
149
|
+
syncWatchers();
|
|
150
|
+
const result = await Bun.build({ ...build, plugins });
|
|
151
|
+
if (!result.success)
|
|
152
|
+
throw new AggregateError(result.logs, "[@teacss/bun] build failed");
|
|
153
|
+
builds++;
|
|
154
|
+
await onBuild(result);
|
|
155
|
+
}
|
|
156
|
+
catch (error) {
|
|
157
|
+
onError(error);
|
|
158
|
+
}
|
|
159
|
+
} while (queued && !closed);
|
|
160
|
+
running = false;
|
|
161
|
+
}
|
|
162
|
+
function schedule() {
|
|
163
|
+
if (closed)
|
|
164
|
+
return;
|
|
165
|
+
clearTimeout(timer);
|
|
166
|
+
timer = setTimeout(() => {
|
|
167
|
+
if (running)
|
|
168
|
+
queued = true;
|
|
169
|
+
else
|
|
170
|
+
void run();
|
|
171
|
+
}, debounce);
|
|
172
|
+
}
|
|
173
|
+
await run();
|
|
174
|
+
return {
|
|
175
|
+
get builds() {
|
|
176
|
+
return builds;
|
|
177
|
+
},
|
|
178
|
+
close() {
|
|
179
|
+
closed = true;
|
|
180
|
+
clearTimeout(timer);
|
|
181
|
+
for (const { watcher } of watchers.values())
|
|
182
|
+
watcher.close();
|
|
183
|
+
watchers.clear();
|
|
184
|
+
project.dispose();
|
|
185
|
+
},
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
//# sourceMappingURL=index.js.map
|
package/package.json
CHANGED
|
@@ -1,44 +1,50 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@teacss/bun",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "Bun plugin for TeaCSS
|
|
5
|
-
"homepage": "https://css.teasim.com",
|
|
6
|
-
"funding": "https://github.com/sponsors/billgo",
|
|
3
|
+
"version": "0.10.3",
|
|
4
|
+
"description": "Bun.build plugin for TeaCSS: virtual:teacss.css and the entry's @teacss become the CSS the Rust engine generates",
|
|
7
5
|
"license": "MIT",
|
|
8
|
-
"author": "Billgo <hi@billgo.me>",
|
|
9
|
-
"sideEffects": false,
|
|
10
6
|
"type": "module",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/teacss/teacss-rs.git",
|
|
10
|
+
"directory": "packages/bun"
|
|
11
|
+
},
|
|
12
|
+
"homepage": "https://github.com/teacss/teacss-rs/tree/master/packages/bun#readme",
|
|
13
|
+
"main": "./dist/index.js",
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
11
15
|
"exports": {
|
|
12
|
-
".":
|
|
16
|
+
".": {
|
|
17
|
+
"types": "./dist/index.d.ts",
|
|
18
|
+
"default": "./dist/index.js"
|
|
19
|
+
},
|
|
13
20
|
"./package.json": "./package.json"
|
|
14
21
|
},
|
|
15
|
-
"types": "./dist/index.d.ts",
|
|
16
22
|
"files": [
|
|
17
|
-
"dist",
|
|
18
|
-
"
|
|
23
|
+
"dist/**/*.js",
|
|
24
|
+
"dist/**/*.d.ts",
|
|
25
|
+
"README.md",
|
|
26
|
+
"LICENSE"
|
|
19
27
|
],
|
|
20
|
-
"
|
|
21
|
-
|
|
22
|
-
"
|
|
28
|
+
"sideEffects": false,
|
|
29
|
+
"engines": {
|
|
30
|
+
"bun": ">=1.4.0",
|
|
31
|
+
"node": ">=26"
|
|
23
32
|
},
|
|
24
33
|
"dependencies": {
|
|
25
|
-
"@teacss/
|
|
26
|
-
"@teacss/core": "0.9.4",
|
|
27
|
-
"@teacss/directives": "0.9.4",
|
|
28
|
-
"@teacss/engine": "0.9.4",
|
|
29
|
-
"@teacss/runtime": "0.9.4",
|
|
30
|
-
"tinyglobby": "^0.2.17"
|
|
34
|
+
"@teacss/engine": "0.10.3"
|
|
31
35
|
},
|
|
32
36
|
"devDependencies": {
|
|
33
|
-
"@
|
|
34
|
-
"
|
|
35
|
-
"consola": "^3.4.2"
|
|
37
|
+
"@types/bun": "1.4.3",
|
|
38
|
+
"typescript": "^5.9.3"
|
|
36
39
|
},
|
|
37
|
-
"
|
|
38
|
-
"
|
|
39
|
-
"
|
|
40
|
+
"scripts": {
|
|
41
|
+
"build:ts": "node -e \"require('fs').rmSync('dist',{recursive:true,force:true})\" && tsc -p tsconfig.json",
|
|
42
|
+
"build": "npm run build:ts",
|
|
43
|
+
"test": "bun test",
|
|
44
|
+
"check:types": "tsc -p tsconfig.json --noEmit && tsc -p tsconfig.test.json --noEmit"
|
|
40
45
|
},
|
|
41
46
|
"publishConfig": {
|
|
42
|
-
"access": "public"
|
|
47
|
+
"access": "public",
|
|
48
|
+
"registry": "https://registry.npmjs.org/"
|
|
43
49
|
}
|
|
44
50
|
}
|