@ascii-fx/vite 0.2.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/LICENSE +21 -0
- package/README.md +31 -0
- package/client.d.ts +11 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.js +235 -0
- package/dist/index.js.map +1 -0
- package/package.json +64 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Amir Abushanab
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# @ascii-fx/vite
|
|
2
|
+
|
|
3
|
+
Thin Vite integration over `@ascii-fx/compiler`: compiles font profiles and static frames at build time, exposes them as typed virtual modules backed by hashed assets, watches fonts/images for HMR, and keeps all compiler code out of the client bundle.
|
|
4
|
+
|
|
5
|
+
```ts
|
|
6
|
+
// vite.config.ts
|
|
7
|
+
import { ascii } from '@ascii-fx/vite'
|
|
8
|
+
export default defineConfig({ plugins: [ascii({ config: './ascii-fx.config.ts' })] })
|
|
9
|
+
|
|
10
|
+
// ascii-fx.config.ts
|
|
11
|
+
import { defineAsciiConfig } from '@ascii-fx/vite'
|
|
12
|
+
export default defineAsciiConfig({
|
|
13
|
+
profiles: {
|
|
14
|
+
default: { font: './public/fonts/GeistMono-Regular.woff2', charset: 'ascii' },
|
|
15
|
+
},
|
|
16
|
+
frames: {
|
|
17
|
+
hero: { image: './src/assets/hero.png', profile: 'default', columns: 140, color: 'full' },
|
|
18
|
+
},
|
|
19
|
+
})
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
```ts
|
|
23
|
+
/// <reference types="@ascii-fx/vite/client" />
|
|
24
|
+
import profileRef from 'virtual:ascii-profile/default' // { url, id }
|
|
25
|
+
import heroRef from 'virtual:ascii-frame/hero' // { url, id, profile }
|
|
26
|
+
|
|
27
|
+
const profile = await loadProfile(profileRef)
|
|
28
|
+
const frame = await loadFrame(heroRef, profile) // zero matching at runtime (spec §16)
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
Compiled assets cache in `node_modules/.ascii-fx`, content-addressed — rebuilt only when the font/image/options change. Static frames are explicit config, never AST magic (spec §16). Size reality: a 160-column full-color frame is ~22KB brotli; the ascii profile ~13KB brotli.
|
package/client.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
declare module 'virtual:ascii-profile/*' {
|
|
2
|
+
/** Pass to loadProfile()/useAsciiProfile()/component profile props. */
|
|
3
|
+
const profileRef: { url: string; id: string }
|
|
4
|
+
export default profileRef
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
declare module 'virtual:ascii-frame/*' {
|
|
8
|
+
/** Pass to loadFrame() together with the matching profile. */
|
|
9
|
+
const frameRef: { url: string; id: string; profile: string }
|
|
10
|
+
export default frameRef
|
|
11
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { Plugin } from 'vite';
|
|
2
|
+
import type { AsciiConfig } from '@ascii-fx/compiler';
|
|
3
|
+
export { defineAsciiConfig } from '@ascii-fx/compiler';
|
|
4
|
+
export type { AsciiConfig, AsciiFrameConfig, AsciiProfileConfig } from '@ascii-fx/compiler';
|
|
5
|
+
export interface AsciiPluginOptions {
|
|
6
|
+
/** Inline config or path to an ascii-fx.config.(ts|js|mjs). Default './ascii-fx.config.ts'. */
|
|
7
|
+
config?: string | AsciiConfig;
|
|
8
|
+
/** Compiled asset cache. Default '<root>/node_modules/.ascii-fx'. */
|
|
9
|
+
cacheDir?: string;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* ASCII FX Vite plugin (spec §17): compiles font profiles and static frames at
|
|
13
|
+
* build time and exposes them as typed virtual modules
|
|
14
|
+
* (`virtual:ascii-profile/<name>`, `virtual:ascii-frame/<name>`). All
|
|
15
|
+
* compilation lives in @ascii-fx/compiler; nothing of it reaches the client
|
|
16
|
+
* bundle — modules resolve to hashed .asciip/.asciif assets via `?url`.
|
|
17
|
+
*/
|
|
18
|
+
export declare function ascii(options?: AsciiPluginOptions): Plugin;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import { createHash } from "crypto";
|
|
3
|
+
import { existsSync, mkdirSync, readFileSync, renameSync, writeFileSync } from "fs";
|
|
4
|
+
import { resolve } from "path";
|
|
5
|
+
import process from "process";
|
|
6
|
+
import { loadConfigFromFile, normalizePath } from "vite";
|
|
7
|
+
import { decodeProfile, peekFrame } from "@ascii-fx/core";
|
|
8
|
+
import { COMPILER_PACKAGE_VERSION, buildFrame, buildProfile, decodePng } from "@ascii-fx/compiler";
|
|
9
|
+
import { defineAsciiConfig } from "@ascii-fx/compiler";
|
|
10
|
+
var PROFILE_PREFIX = "virtual:ascii-profile/";
|
|
11
|
+
var FRAME_PREFIX = "virtual:ascii-frame/";
|
|
12
|
+
var RESOLVED = "\0";
|
|
13
|
+
var sha = (data) => createHash("sha256").update(data).digest("hex");
|
|
14
|
+
var abs = (...segments) => normalizePath(resolve(...segments));
|
|
15
|
+
var writeAtomic = (path, bytes) => {
|
|
16
|
+
const tmp = `${path}.tmp-${process.pid}`;
|
|
17
|
+
writeFileSync(tmp, bytes);
|
|
18
|
+
renameSync(tmp, path);
|
|
19
|
+
};
|
|
20
|
+
function ascii(options = {}) {
|
|
21
|
+
let root = process.cwd();
|
|
22
|
+
let cacheDir = "";
|
|
23
|
+
let config;
|
|
24
|
+
let configDeps = [];
|
|
25
|
+
const profiles = /* @__PURE__ */ new Map();
|
|
26
|
+
const frames = /* @__PURE__ */ new Map();
|
|
27
|
+
const pendingHmrIds = /* @__PURE__ */ new Set();
|
|
28
|
+
async function ensureConfig() {
|
|
29
|
+
if (config) return config;
|
|
30
|
+
const opt = options.config;
|
|
31
|
+
if (opt && typeof opt === "object") {
|
|
32
|
+
config = opt;
|
|
33
|
+
return config;
|
|
34
|
+
}
|
|
35
|
+
const file = resolve(root, typeof opt === "string" ? opt : "ascii-fx.config.ts");
|
|
36
|
+
const loaded = await loadConfigFromFile({ command: "build", mode: "production" }, file, root);
|
|
37
|
+
if (!loaded) {
|
|
38
|
+
throw new Error(
|
|
39
|
+
`[ascii-fx] Could not load config at ${file}. Create it with defineAsciiConfig({ profiles: ... }).`
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
configDeps = [normalizePath(loaded.path), ...loaded.dependencies.map((d) => abs(root, d))];
|
|
43
|
+
config = loaded.config;
|
|
44
|
+
return config;
|
|
45
|
+
}
|
|
46
|
+
async function ensureProfile(name) {
|
|
47
|
+
const cached = profiles.get(name);
|
|
48
|
+
if (cached) return cached;
|
|
49
|
+
const cfg = await ensureConfig();
|
|
50
|
+
const pc = cfg.profiles?.[name];
|
|
51
|
+
if (!pc) {
|
|
52
|
+
throw new Error(
|
|
53
|
+
`[ascii-fx] Profile "${name}" is not defined in the config (available: ${Object.keys(cfg.profiles ?? {}).join(", ") || "none"}).`
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
const fontPath = abs(root, pc.font);
|
|
57
|
+
if (!existsSync(fontPath))
|
|
58
|
+
throw new Error(`[ascii-fx] Font not found for profile "${name}": ${fontPath}`);
|
|
59
|
+
const font = new Uint8Array(readFileSync(fontPath));
|
|
60
|
+
const key = sha(
|
|
61
|
+
JSON.stringify([
|
|
62
|
+
sha(font),
|
|
63
|
+
pc.charset ?? "ascii",
|
|
64
|
+
pc.characters ?? null,
|
|
65
|
+
pc.shape6 ?? false,
|
|
66
|
+
COMPILER_PACKAGE_VERSION,
|
|
67
|
+
"asciip/1"
|
|
68
|
+
])
|
|
69
|
+
).slice(0, 16);
|
|
70
|
+
const cachePath = abs(cacheDir, `${name}-${key}.asciip`);
|
|
71
|
+
let profile;
|
|
72
|
+
if (existsSync(cachePath)) {
|
|
73
|
+
try {
|
|
74
|
+
profile = decodeProfile(new Uint8Array(readFileSync(cachePath)));
|
|
75
|
+
} catch {
|
|
76
|
+
profile = void 0;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
if (profile === void 0) {
|
|
80
|
+
const built = buildProfile({
|
|
81
|
+
font,
|
|
82
|
+
id: name,
|
|
83
|
+
charset: pc.charset,
|
|
84
|
+
characters: pc.characters,
|
|
85
|
+
shape6: pc.shape6
|
|
86
|
+
});
|
|
87
|
+
mkdirSync(cacheDir, { recursive: true });
|
|
88
|
+
writeAtomic(cachePath, built.binary);
|
|
89
|
+
profile = built.profile;
|
|
90
|
+
}
|
|
91
|
+
const entry = { cachePath, profile, fontPath };
|
|
92
|
+
profiles.set(name, entry);
|
|
93
|
+
return entry;
|
|
94
|
+
}
|
|
95
|
+
async function ensureFrame(name) {
|
|
96
|
+
const cached = frames.get(name);
|
|
97
|
+
if (cached) return cached;
|
|
98
|
+
const cfg = await ensureConfig();
|
|
99
|
+
const fc = cfg.frames?.[name];
|
|
100
|
+
if (!fc) {
|
|
101
|
+
throw new Error(
|
|
102
|
+
`[ascii-fx] Frame "${name}" is not defined in the config (available: ${Object.keys(cfg.frames ?? {}).join(", ") || "none"}).`
|
|
103
|
+
);
|
|
104
|
+
}
|
|
105
|
+
const profileName = fc.profile ?? "default";
|
|
106
|
+
const { profile, fontPath } = await ensureProfile(profileName);
|
|
107
|
+
const imagePath = abs(root, fc.image);
|
|
108
|
+
if (!existsSync(imagePath))
|
|
109
|
+
throw new Error(`[ascii-fx] Image not found for frame "${name}": ${imagePath}`);
|
|
110
|
+
const imageBytes = new Uint8Array(readFileSync(imagePath));
|
|
111
|
+
const key = sha(
|
|
112
|
+
JSON.stringify([
|
|
113
|
+
sha(imageBytes),
|
|
114
|
+
profile.fingerprint,
|
|
115
|
+
fc.columns,
|
|
116
|
+
fc.rows,
|
|
117
|
+
fc.color,
|
|
118
|
+
fc.alpha,
|
|
119
|
+
COMPILER_PACKAGE_VERSION,
|
|
120
|
+
"asciif/1"
|
|
121
|
+
])
|
|
122
|
+
).slice(0, 16);
|
|
123
|
+
const cachePath = abs(cacheDir, `${name}-${key}.asciif`);
|
|
124
|
+
let cacheHit = false;
|
|
125
|
+
if (existsSync(cachePath)) {
|
|
126
|
+
try {
|
|
127
|
+
peekFrame(new Uint8Array(readFileSync(cachePath)));
|
|
128
|
+
cacheHit = true;
|
|
129
|
+
} catch {
|
|
130
|
+
cacheHit = false;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
if (!cacheHit) {
|
|
134
|
+
const built = buildFrame({
|
|
135
|
+
image: decodePng(imageBytes),
|
|
136
|
+
profile,
|
|
137
|
+
columns: fc.columns,
|
|
138
|
+
rows: fc.rows,
|
|
139
|
+
color: fc.color,
|
|
140
|
+
alpha: fc.alpha
|
|
141
|
+
});
|
|
142
|
+
mkdirSync(cacheDir, { recursive: true });
|
|
143
|
+
writeAtomic(cachePath, built.binary);
|
|
144
|
+
}
|
|
145
|
+
const entry = { cachePath, profileName, imagePath, fontPath };
|
|
146
|
+
frames.set(name, entry);
|
|
147
|
+
return entry;
|
|
148
|
+
}
|
|
149
|
+
function invalidateFile(id) {
|
|
150
|
+
const affectedProfiles = /* @__PURE__ */ new Set();
|
|
151
|
+
const affectedFrames = /* @__PURE__ */ new Set();
|
|
152
|
+
if (configDeps.includes(id)) {
|
|
153
|
+
for (const name of profiles.keys()) affectedProfiles.add(name);
|
|
154
|
+
for (const name of frames.keys()) affectedFrames.add(name);
|
|
155
|
+
config = typeof options.config === "object" ? options.config : void 0;
|
|
156
|
+
configDeps = [];
|
|
157
|
+
profiles.clear();
|
|
158
|
+
frames.clear();
|
|
159
|
+
pendingHmrIds.clear();
|
|
160
|
+
} else {
|
|
161
|
+
for (const [name, entry] of profiles) {
|
|
162
|
+
if (entry.fontPath === id) affectedProfiles.add(name);
|
|
163
|
+
}
|
|
164
|
+
for (const [name, entry] of frames) {
|
|
165
|
+
if (entry.imagePath === id || entry.fontPath === id || affectedProfiles.has(entry.profileName)) {
|
|
166
|
+
affectedFrames.add(name);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
for (const name of affectedProfiles) profiles.delete(name);
|
|
170
|
+
for (const name of affectedFrames) frames.delete(name);
|
|
171
|
+
}
|
|
172
|
+
return [
|
|
173
|
+
...Array.from(affectedProfiles, (name) => RESOLVED + PROFILE_PREFIX + name),
|
|
174
|
+
...Array.from(affectedFrames, (name) => RESOLVED + FRAME_PREFIX + name)
|
|
175
|
+
];
|
|
176
|
+
}
|
|
177
|
+
return {
|
|
178
|
+
name: "ascii-fx",
|
|
179
|
+
configResolved(resolved) {
|
|
180
|
+
root = resolved.root;
|
|
181
|
+
cacheDir = options.cacheDir ? abs(root, options.cacheDir) : abs(root, "node_modules/.ascii-fx");
|
|
182
|
+
config = typeof options.config === "object" ? options.config : void 0;
|
|
183
|
+
configDeps = [];
|
|
184
|
+
profiles.clear();
|
|
185
|
+
frames.clear();
|
|
186
|
+
pendingHmrIds.clear();
|
|
187
|
+
},
|
|
188
|
+
resolveId(id) {
|
|
189
|
+
if (id.startsWith(PROFILE_PREFIX) || id.startsWith(FRAME_PREFIX)) return RESOLVED + id;
|
|
190
|
+
return null;
|
|
191
|
+
},
|
|
192
|
+
watchChange(id) {
|
|
193
|
+
for (const moduleId of invalidateFile(id)) pendingHmrIds.add(moduleId);
|
|
194
|
+
},
|
|
195
|
+
handleHotUpdate(ctx) {
|
|
196
|
+
const ids = [...pendingHmrIds, ...invalidateFile(ctx.file)];
|
|
197
|
+
pendingHmrIds.clear();
|
|
198
|
+
if (ids.length === 0) return;
|
|
199
|
+
const modules = ids.flatMap((id) => {
|
|
200
|
+
const module = ctx.server.moduleGraph.getModuleById(id);
|
|
201
|
+
if (!module) return [];
|
|
202
|
+
ctx.server.moduleGraph.invalidateModule(module);
|
|
203
|
+
return [module];
|
|
204
|
+
});
|
|
205
|
+
return [.../* @__PURE__ */ new Set([...ctx.modules, ...modules])];
|
|
206
|
+
},
|
|
207
|
+
async load(id) {
|
|
208
|
+
if (id.startsWith(RESOLVED + PROFILE_PREFIX)) {
|
|
209
|
+
const name = id.slice(RESOLVED.length + PROFILE_PREFIX.length);
|
|
210
|
+
const entry = await ensureProfile(name);
|
|
211
|
+
this.addWatchFile(entry.fontPath);
|
|
212
|
+
for (const dep of configDeps) this.addWatchFile(dep);
|
|
213
|
+
return `import url from ${JSON.stringify(`${entry.cachePath}?url`)}
|
|
214
|
+
export default { url, id: ${JSON.stringify(name)} }
|
|
215
|
+
`;
|
|
216
|
+
}
|
|
217
|
+
if (id.startsWith(RESOLVED + FRAME_PREFIX)) {
|
|
218
|
+
const name = id.slice(RESOLVED.length + FRAME_PREFIX.length);
|
|
219
|
+
const entry = await ensureFrame(name);
|
|
220
|
+
this.addWatchFile(entry.imagePath);
|
|
221
|
+
this.addWatchFile(entry.fontPath);
|
|
222
|
+
for (const dep of configDeps) this.addWatchFile(dep);
|
|
223
|
+
return `import url from ${JSON.stringify(`${entry.cachePath}?url`)}
|
|
224
|
+
export default { url, id: ${JSON.stringify(name)}, profile: ${JSON.stringify(entry.profileName)} }
|
|
225
|
+
`;
|
|
226
|
+
}
|
|
227
|
+
return null;
|
|
228
|
+
}
|
|
229
|
+
};
|
|
230
|
+
}
|
|
231
|
+
export {
|
|
232
|
+
ascii,
|
|
233
|
+
defineAsciiConfig
|
|
234
|
+
};
|
|
235
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { createHash } from 'node:crypto'\nimport { existsSync, mkdirSync, readFileSync, renameSync, writeFileSync } from 'node:fs'\nimport { resolve } from 'node:path'\nimport process from 'node:process'\nimport type { Plugin } from 'vite'\nimport { loadConfigFromFile, normalizePath } from 'vite'\nimport type { AsciiProfile } from '@ascii-fx/core'\nimport { decodeProfile, peekFrame } from '@ascii-fx/core'\nimport type { AsciiConfig } from '@ascii-fx/compiler'\nimport { COMPILER_PACKAGE_VERSION, buildFrame, buildProfile, decodePng } from '@ascii-fx/compiler'\n\nexport { defineAsciiConfig } from '@ascii-fx/compiler'\nexport type { AsciiConfig, AsciiFrameConfig, AsciiProfileConfig } from '@ascii-fx/compiler'\n\nconst PROFILE_PREFIX = 'virtual:ascii-profile/'\nconst FRAME_PREFIX = 'virtual:ascii-frame/'\nconst RESOLVED = '\\0'\n\nexport interface AsciiPluginOptions {\n /** Inline config or path to an ascii-fx.config.(ts|js|mjs). Default './ascii-fx.config.ts'. */\n config?: string | AsciiConfig\n /** Compiled asset cache. Default '<root>/node_modules/.ascii-fx'. */\n cacheDir?: string\n}\n\ninterface ProfileEntry {\n cachePath: string\n profile: AsciiProfile\n fontPath: string\n}\n\ninterface FrameEntry {\n cachePath: string\n profileName: string\n imagePath: string\n fontPath: string\n}\n\nconst sha = (data: string | Uint8Array): string => createHash('sha256').update(data).digest('hex')\n\n// Vite hands watcher ids to watchChange/handleHotUpdate POSIX-normalized, so\n// every path stored for === comparison — and every specifier emitted into a\n// virtual module — must be normalized the same way, or invalidation (and, for\n// specifiers, resolution) is dead on Windows.\nconst abs = (...segments: string[]): string => normalizePath(resolve(...segments))\n\n// Temp + rename: a crash mid-write must not leave a truncated file at the\n// final path, where every later build would trust it. The read sites still\n// treat an undecodable entry as a cache miss, so a bad file from any other\n// cause cannot wedge the build either.\nconst writeAtomic = (path: string, bytes: Uint8Array): void => {\n const tmp = `${path}.tmp-${process.pid}`\n writeFileSync(tmp, bytes)\n renameSync(tmp, path)\n}\n\n/**\n * ASCII FX Vite plugin (spec §17): compiles font profiles and static frames at\n * build time and exposes them as typed virtual modules\n * (`virtual:ascii-profile/<name>`, `virtual:ascii-frame/<name>`). All\n * compilation lives in @ascii-fx/compiler; nothing of it reaches the client\n * bundle — modules resolve to hashed .asciip/.asciif assets via `?url`.\n */\nexport function ascii(options: AsciiPluginOptions = {}): Plugin {\n let root = process.cwd()\n let cacheDir = ''\n let config: AsciiConfig | undefined\n let configDeps: string[] = []\n const profiles = new Map<string, ProfileEntry>()\n const frames = new Map<string, FrameEntry>()\n const pendingHmrIds = new Set<string>()\n\n async function ensureConfig(): Promise<AsciiConfig> {\n if (config) return config\n const opt = options.config\n if (opt && typeof opt === 'object') {\n config = opt\n return config\n }\n const file = resolve(root, typeof opt === 'string' ? opt : 'ascii-fx.config.ts')\n const loaded = await loadConfigFromFile({ command: 'build', mode: 'production' }, file, root)\n if (!loaded) {\n throw new Error(\n `[ascii-fx] Could not load config at ${file}. Create it with defineAsciiConfig({ profiles: ... }).`,\n )\n }\n configDeps = [normalizePath(loaded.path), ...loaded.dependencies.map((d) => abs(root, d))]\n config = loaded.config as AsciiConfig\n return config\n }\n\n async function ensureProfile(name: string): Promise<ProfileEntry> {\n const cached = profiles.get(name)\n if (cached) return cached\n const cfg = await ensureConfig()\n const pc = cfg.profiles?.[name]\n if (!pc) {\n throw new Error(\n `[ascii-fx] Profile \"${name}\" is not defined in the config (available: ${Object.keys(cfg.profiles ?? {}).join(', ') || 'none'}).`,\n )\n }\n const fontPath = abs(root, pc.font)\n if (!existsSync(fontPath))\n throw new Error(`[ascii-fx] Font not found for profile \"${name}\": ${fontPath}`)\n const font = new Uint8Array(readFileSync(fontPath))\n // COMPILER_PACKAGE_VERSION salts the key because the cache dir survives installs: without\n // it, upgrading the packages would keep serving bytes built by the old compiler.\n const key = sha(\n JSON.stringify([\n sha(font),\n pc.charset ?? 'ascii',\n pc.characters ?? null,\n pc.shape6 ?? false,\n COMPILER_PACKAGE_VERSION,\n 'asciip/1',\n ]),\n ).slice(0, 16)\n const cachePath = abs(cacheDir, `${name}-${key}.asciip`)\n let profile: AsciiProfile | undefined\n if (existsSync(cachePath)) {\n try {\n profile = decodeProfile(new Uint8Array(readFileSync(cachePath)))\n } catch {\n profile = undefined // corrupt entry — treat as a miss, rebuild over it\n }\n }\n if (profile === undefined) {\n const built = buildProfile({\n font,\n id: name,\n charset: pc.charset,\n characters: pc.characters,\n shape6: pc.shape6,\n })\n mkdirSync(cacheDir, { recursive: true })\n writeAtomic(cachePath, built.binary)\n profile = built.profile\n }\n const entry = { cachePath, profile, fontPath }\n profiles.set(name, entry)\n return entry\n }\n\n async function ensureFrame(name: string): Promise<FrameEntry> {\n const cached = frames.get(name)\n if (cached) return cached\n const cfg = await ensureConfig()\n const fc = cfg.frames?.[name]\n if (!fc) {\n throw new Error(\n `[ascii-fx] Frame \"${name}\" is not defined in the config (available: ${Object.keys(cfg.frames ?? {}).join(', ') || 'none'}).`,\n )\n }\n const profileName = fc.profile ?? 'default'\n const { profile, fontPath } = await ensureProfile(profileName)\n const imagePath = abs(root, fc.image)\n if (!existsSync(imagePath))\n throw new Error(`[ascii-fx] Image not found for frame \"${name}\": ${imagePath}`)\n const imageBytes = new Uint8Array(readFileSync(imagePath))\n // Salted like the profile key: matching lives in @ascii-fx/core, but the fixed release\n // group moves COMPILER_PACKAGE_VERSION whenever any of it changes.\n const key = sha(\n JSON.stringify([\n sha(imageBytes),\n profile.fingerprint,\n fc.columns,\n fc.rows,\n fc.color,\n fc.alpha,\n COMPILER_PACKAGE_VERSION,\n 'asciif/1',\n ]),\n ).slice(0, 16)\n const cachePath = abs(cacheDir, `${name}-${key}.asciif`)\n let cacheHit = false\n if (existsSync(cachePath)) {\n try {\n // Frames are served to the browser without a build-time decode, so a\n // corrupt entry would surface as a runtime failure in the app; peek\n // the header here and rebuild over anything unreadable.\n peekFrame(new Uint8Array(readFileSync(cachePath)))\n cacheHit = true\n } catch {\n cacheHit = false\n }\n }\n if (!cacheHit) {\n const built = buildFrame({\n image: decodePng(imageBytes),\n profile,\n columns: fc.columns,\n rows: fc.rows,\n color: fc.color,\n alpha: fc.alpha,\n })\n mkdirSync(cacheDir, { recursive: true })\n writeAtomic(cachePath, built.binary)\n }\n const entry = { cachePath, profileName, imagePath, fontPath }\n frames.set(name, entry)\n return entry\n }\n\n function invalidateFile(id: string): string[] {\n const affectedProfiles = new Set<string>()\n const affectedFrames = new Set<string>()\n if (configDeps.includes(id)) {\n for (const name of profiles.keys()) affectedProfiles.add(name)\n for (const name of frames.keys()) affectedFrames.add(name)\n config = typeof options.config === 'object' ? options.config : undefined\n configDeps = []\n profiles.clear()\n frames.clear()\n pendingHmrIds.clear()\n } else {\n for (const [name, entry] of profiles) {\n if (entry.fontPath === id) affectedProfiles.add(name)\n }\n for (const [name, entry] of frames) {\n if (\n entry.imagePath === id ||\n entry.fontPath === id ||\n affectedProfiles.has(entry.profileName)\n ) {\n affectedFrames.add(name)\n }\n }\n for (const name of affectedProfiles) profiles.delete(name)\n for (const name of affectedFrames) frames.delete(name)\n }\n return [\n ...Array.from(affectedProfiles, (name) => RESOLVED + PROFILE_PREFIX + name),\n ...Array.from(affectedFrames, (name) => RESOLVED + FRAME_PREFIX + name),\n ]\n }\n\n return {\n name: 'ascii-fx',\n configResolved(resolved) {\n root = resolved.root\n cacheDir = options.cacheDir\n ? abs(root, options.cacheDir)\n : abs(root, 'node_modules/.ascii-fx')\n // Config/watch state resets per build so edits are picked up.\n config = typeof options.config === 'object' ? options.config : undefined\n configDeps = []\n profiles.clear()\n frames.clear()\n pendingHmrIds.clear()\n },\n resolveId(id) {\n if (id.startsWith(PROFILE_PREFIX) || id.startsWith(FRAME_PREFIX)) return RESOLVED + id\n return null\n },\n watchChange(id) {\n for (const moduleId of invalidateFile(id)) pendingHmrIds.add(moduleId)\n },\n handleHotUpdate(ctx) {\n const ids = [...pendingHmrIds, ...invalidateFile(ctx.file)]\n pendingHmrIds.clear()\n if (ids.length === 0) return\n const modules = ids.flatMap((id) => {\n const module = ctx.server.moduleGraph.getModuleById(id)\n if (!module) return []\n ctx.server.moduleGraph.invalidateModule(module)\n return [module]\n })\n return [...new Set([...ctx.modules, ...modules])]\n },\n async load(id) {\n if (id.startsWith(RESOLVED + PROFILE_PREFIX)) {\n const name = id.slice(RESOLVED.length + PROFILE_PREFIX.length)\n const entry = await ensureProfile(name)\n this.addWatchFile(entry.fontPath)\n for (const dep of configDeps) this.addWatchFile(dep)\n return (\n `import url from ${JSON.stringify(`${entry.cachePath}?url`)}\\n` +\n `export default { url, id: ${JSON.stringify(name)} }\\n`\n )\n }\n if (id.startsWith(RESOLVED + FRAME_PREFIX)) {\n const name = id.slice(RESOLVED.length + FRAME_PREFIX.length)\n const entry = await ensureFrame(name)\n this.addWatchFile(entry.imagePath)\n this.addWatchFile(entry.fontPath)\n for (const dep of configDeps) this.addWatchFile(dep)\n return (\n `import url from ${JSON.stringify(`${entry.cachePath}?url`)}\\n` +\n `export default { url, id: ${JSON.stringify(name)}, profile: ${JSON.stringify(entry.profileName)} }\\n`\n )\n }\n return null\n },\n }\n}\n"],"mappings":";AAAA,SAAS,kBAAkB;AAC3B,SAAS,YAAY,WAAW,cAAc,YAAY,qBAAqB;AAC/E,SAAS,eAAe;AACxB,OAAO,aAAa;AAEpB,SAAS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C;AAAA;AAAA,MAErD;AACA,UAAI,GAAG,WAAW,WAAW,YAAY,GAAG;AAC1C,cAAM,OAAO,GAAG,MAAM,SAAS,SAAS,aAAa,MAAM;AAC3D,cAAM,QAAQ,MAAM,YAAY,IAAI;AACpC,aAAK,aAAa,MAAM,SAAS;AACjC,aAAK,aAAa,MAAM,QAAQ;AAChC,mBAAW,OAAO,WAAY,MAAK,aAAa,GAAG;AACnD,eACE,mBAAmB,KAAK,UAAU,GAAG,MAAM,SAAS,MAAM,CAAC;AAAA,4BAC9B,KAAK,UAAU,IAAI,CAAC,cAAc,KAAK,UAAU,MAAM,WAAW,CAAC;AAAA;AAAA,MAEpG;AACA,aAAO;AAAA,IACT;AAAA,EACF;AACF;","names":[]}
|
package/package.json
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@ascii-fx/vite",
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "Vite integration for ASCII FX: build-time profile compilation and static frame generation as virtual modules.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"ascii",
|
|
7
|
+
"ascii-art",
|
|
8
|
+
"ascii-fx",
|
|
9
|
+
"build-tool",
|
|
10
|
+
"vite",
|
|
11
|
+
"vite-plugin"
|
|
12
|
+
],
|
|
13
|
+
"homepage": "https://github.com/Amir-Abushanab/ascii-fx#readme",
|
|
14
|
+
"bugs": {
|
|
15
|
+
"url": "https://github.com/Amir-Abushanab/ascii-fx/issues"
|
|
16
|
+
},
|
|
17
|
+
"license": "MIT",
|
|
18
|
+
"author": "Amir Abushanab (https://github.com/Amir-Abushanab)",
|
|
19
|
+
"repository": {
|
|
20
|
+
"type": "git",
|
|
21
|
+
"url": "git+https://github.com/Amir-Abushanab/ascii-fx.git",
|
|
22
|
+
"directory": "packages/vite"
|
|
23
|
+
},
|
|
24
|
+
"files": [
|
|
25
|
+
"dist",
|
|
26
|
+
"client.d.ts"
|
|
27
|
+
],
|
|
28
|
+
"type": "module",
|
|
29
|
+
"sideEffects": false,
|
|
30
|
+
"exports": {
|
|
31
|
+
".": {
|
|
32
|
+
"types": "./dist/index.d.ts",
|
|
33
|
+
"default": "./dist/index.js"
|
|
34
|
+
},
|
|
35
|
+
"./client": {
|
|
36
|
+
"types": "./client.d.ts"
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
"publishConfig": {
|
|
40
|
+
"access": "public"
|
|
41
|
+
},
|
|
42
|
+
"dependencies": {
|
|
43
|
+
"@ascii-fx/compiler": "0.2.0",
|
|
44
|
+
"@ascii-fx/core": "0.2.0"
|
|
45
|
+
},
|
|
46
|
+
"devDependencies": {
|
|
47
|
+
"@types/node": "^26.2.0",
|
|
48
|
+
"@types/pngjs": "^6.0.5",
|
|
49
|
+
"pngjs": "^7.0.0",
|
|
50
|
+
"tsup": "^8.5.1",
|
|
51
|
+
"typescript": "^7.0.2",
|
|
52
|
+
"vite": "^8.2.1"
|
|
53
|
+
},
|
|
54
|
+
"peerDependencies": {
|
|
55
|
+
"vite": "^5.0.0 || ^6.0.0 || ^7.0.0 || ^8.0.0"
|
|
56
|
+
},
|
|
57
|
+
"engines": {
|
|
58
|
+
"node": ">=22"
|
|
59
|
+
},
|
|
60
|
+
"scripts": {
|
|
61
|
+
"build": "tsup && tsc -p tsconfig.build.json",
|
|
62
|
+
"typecheck": "tsc --noEmit"
|
|
63
|
+
}
|
|
64
|
+
}
|