@aippy/vite-plugins 0.4.0-dev.1 → 0.4.0-dev.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 CHANGED
@@ -102,8 +102,8 @@ const projectRoot = findProjectRoot(); // '/path/to/project'
102
102
 
103
103
  UNLICENSED - Aippy Team
104
104
 
105
- ## Asset manifest v1
105
+ ## Asset files
106
106
 
107
- Version 0.4.0-dev.1 reads both legacy flat URL inventories and `schemaVersion: 1` manifests. Assets Agent writes resource plans; development builds preserve their metadata and supplement resources discovered by the existing scanner. Invalid v1 inventories and manifest/wrapper write failures stop the build.
107
+ `src/config/assets.json` is a flat URL dictionary. The plugin writes only that file. The project-root `assets-plan.json` is read-only input for reserved names and `implementation.path` exclusions; a missing or unreadable plan does not stop the build. Invalid dictionary JSON, or a single-file v1 plan stored in the dictionary, stops the build without overwriting the file.
108
108
 
109
- Import the generated flat URL map from `@/config/assets`. Only entries with usable URLs appear in that map; the existing `window.getReplacedAssets` hook still mutates it in place. Source-code assets and generated fallbacks with an exact `implementation.path` retain their original source references. Preload reads resource URLs rather than generation prompts or error text. The JSON schema is exported as `@aippy/vite-plugins/assets.schema.json`; parser checks also enforce byte, reference and semantic constraints.
109
+ Import the generated map from `@/config/assets`. It exports the dictionary's string values. `window.getReplacedAssets` still mutates that object in place. Preload reads those string values and URLs in other source files, not prompts inside the plan. The JSON schema remains `@aippy/vite-plugins/assets.schema.json`. Version 0.4.0-dev.3 is prepared locally and is not published yet.
@@ -1,11 +1,6 @@
1
1
  /**
2
2
  * HTML processing utilities for aippy preload plugin
3
3
  */
4
- /**
5
- * Generate meta tags for asset preloading
6
- * @param assets - Array of asset URLs
7
- * @returns HTML meta tags string
8
- */
9
4
  export declare function generateMetaTags(assets: string[]): string;
10
5
  /**
11
6
  * Inject meta tags into HTML content
@@ -1,2 +1,2 @@
1
- import { t as e } from "../aippy-preload-QZY6fyKx.js";
1
+ import { t as e } from "../aippy-preload-DtngFmJ5.js";
2
2
  export { e as aippyPreloadPlugin, e as default };
@@ -0,0 +1,186 @@
1
+ import { extractAssetsFromContent as e, isValidAsset as t } from "./utils/index.js";
2
+ import { a as n, l as r, m as i, n as a, o, s } from "./generator-utils-CFYv7n_h.js";
3
+ import { promises as c } from "fs";
4
+ import l from "path";
5
+ //#region src/aippy-preload/scanner-utils.ts
6
+ async function u(t) {
7
+ let n = [], r = [l.join(process.cwd(), "index.html")];
8
+ for (let i of r) try {
9
+ let r = await c.readFile(i, "utf-8"), a = e(r, "html", t);
10
+ n.push(...a);
11
+ } catch {
12
+ console.warn("⚠️ No index.html found in project root");
13
+ }
14
+ return n;
15
+ }
16
+ async function d(t, n) {
17
+ let u = [], d = l.resolve(t, "config/assets.json"), f, p = /* @__PURE__ */ new Set();
18
+ try {
19
+ let e = JSON.parse(await c.readFile(d, "utf8"));
20
+ if (a(e)) {
21
+ let t = s(JSON.stringify(e));
22
+ o(t) && (f = t);
23
+ } else e && typeof e == "object" && !Array.isArray(e) && (p = new Set(Object.keys(e)));
24
+ } catch {}
25
+ let m = await i(t, [
26
+ ".ts",
27
+ ".tsx",
28
+ ".js",
29
+ ".jsx"
30
+ ]);
31
+ for (let i of m) try {
32
+ if (f && l.resolve(i) === d.replace(/\.json$/, ".ts")) continue;
33
+ let a = await c.readFile(i, "utf-8"), o = e(a, "js", n), s = /* @__PURE__ */ new Set([...p, ...f ? Object.keys(f.assets) : []]);
34
+ o.some((e) => s.has(e)) && (o = e(r(a, i, t, d), "js", n)), u.push(...o);
35
+ } catch (e) {
36
+ console.warn(`⚠️ Failed to read JS file: ${i}`, e);
37
+ }
38
+ return u;
39
+ }
40
+ async function f(t, n) {
41
+ let r = [], a = await i(t, [
42
+ ".css",
43
+ ".scss",
44
+ ".sass",
45
+ ".less"
46
+ ]);
47
+ for (let t of a) try {
48
+ let i = await c.readFile(t, "utf-8"), a = e(i, "css", n);
49
+ r.push(...a);
50
+ } catch (e) {
51
+ console.warn(`⚠️ Failed to read CSS file: ${t}`, e);
52
+ }
53
+ return r;
54
+ }
55
+ function p(e, n) {
56
+ if (!t(e) || /["'<>\s`]/.test(e)) return !1;
57
+ let r = e.split(/[?#]/, 1)[0].toLowerCase();
58
+ return n.some((e) => r.endsWith(e.toLowerCase()));
59
+ }
60
+ async function m(t, r) {
61
+ let u = [], d = await i(t, [".json"]);
62
+ for (let i of d) try {
63
+ let d = await c.readFile(i, "utf-8");
64
+ if (l.basename(i) === "assets-plan.json") continue;
65
+ if (l.resolve(i) === l.resolve(t, "config/assets.json")) {
66
+ let e = JSON.parse(d);
67
+ if (a(e)) {
68
+ let e = s(d);
69
+ if (o(e)) {
70
+ u.push(...Object.values(n(e)));
71
+ continue;
72
+ }
73
+ }
74
+ if (e && typeof e == "object" && !Array.isArray(e)) {
75
+ for (let t of Object.values(e)) typeof t == "string" && p(t, r) && u.push(t);
76
+ continue;
77
+ }
78
+ }
79
+ let f = e(d, "js", r);
80
+ u.push(...f);
81
+ } catch (e) {
82
+ console.warn(`⚠️ Failed to read JSON file: ${i}`, e);
83
+ }
84
+ return u;
85
+ }
86
+ async function h(e, t, n) {
87
+ console.log("🔍 Scanning source files for static assets...");
88
+ let r = [];
89
+ if (n) {
90
+ console.log("🔍 Deep scanning source file contents...");
91
+ let [n, i, a, o] = await Promise.all([
92
+ u(t),
93
+ d(e, t),
94
+ f(e, t),
95
+ m(e, t)
96
+ ]);
97
+ r.push(...n, ...i, ...a, ...o);
98
+ }
99
+ return [...new Set(r)];
100
+ }
101
+ //#endregion
102
+ //#region src/aippy-preload/html-utils.ts
103
+ var g = "<head>", _ = 6;
104
+ function v(e) {
105
+ return e.replace(/&/g, "&amp;").replace(/"/g, "&quot;").replace(/'/g, "&#39;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
106
+ }
107
+ function y(e) {
108
+ return e.map((e) => ` <meta content="${v(e)}" name="aippy:preload">`).join("\n");
109
+ }
110
+ function b(e, t) {
111
+ let n = e.indexOf(g);
112
+ if (n === -1) throw Error("Could not find <head> tag in HTML");
113
+ let r = n + _;
114
+ return e.slice(0, r) + "\n" + t + "\n" + e.slice(r);
115
+ }
116
+ function x(e) {
117
+ let t = e.length;
118
+ if (console.log(`📁 Found ${t} static ${t === 1 ? "asset" : "assets"} in source files`), t === 0) {
119
+ console.log("⚠️ No static assets found in source files.");
120
+ return;
121
+ }
122
+ console.log(`📋 ${t === 1 ? "Asset" : "Assets"} found:`), e.forEach((e) => console.log(` - ${e}`));
123
+ }
124
+ function S(e) {
125
+ console.log(`✅ Injected ${e} preload meta ${e === 1 ? "tag" : "tags"} into index.html`);
126
+ }
127
+ //#endregion
128
+ //#region src/aippy-preload/index.ts
129
+ var C = 3, w = 1e3;
130
+ function T(e) {
131
+ return new Promise((t) => setTimeout(t, e));
132
+ }
133
+ async function E(e, t = C, n = w) {
134
+ for (let r = 1; r <= t; r++) try {
135
+ return await c.readFile(e, "utf-8");
136
+ } catch (e) {
137
+ if (e.code === "ENOENT") {
138
+ if (r < t) {
139
+ console.log(`⏳ index.html not found, waiting ${n}ms before retry (attempt ${r}/${t})...`), await T(n);
140
+ continue;
141
+ }
142
+ return console.log(`⚠️ index.html not found after ${t} attempts, skipping preload injection.`), null;
143
+ }
144
+ return console.error("❌ Error reading index.html:", e), null;
145
+ }
146
+ return null;
147
+ }
148
+ function D(e = {}) {
149
+ let t = {
150
+ extensions: [
151
+ ".png",
152
+ ".jpg",
153
+ ".jpeg",
154
+ ".gif",
155
+ ".svg",
156
+ ".mp4",
157
+ ".mp3",
158
+ ".wav",
159
+ ".ogg",
160
+ ".webm"
161
+ ],
162
+ srcDir: "src",
163
+ outDir: "dist",
164
+ deepScan: !0,
165
+ ...e
166
+ };
167
+ return {
168
+ name: "vite-plugin-aippy-preload",
169
+ apply: "build",
170
+ enforce: "post",
171
+ async writeBundle() {
172
+ try {
173
+ let e = await h(t.srcDir, t.extensions, t.deepScan);
174
+ if (x(e), e.length === 0) return;
175
+ let n = l.join(t.outDir, "index.html"), r = await E(n);
176
+ if (r === null) return;
177
+ let i = b(r, y(e));
178
+ await c.writeFile(n, i, "utf-8"), S(e.length);
179
+ } catch (e) {
180
+ console.error("❌ Error in aippy preload plugin:", e);
181
+ }
182
+ }
183
+ };
184
+ }
185
+ //#endregion
186
+ export { D as t };
@@ -1,12 +1,11 @@
1
1
  import { AssetInfo } from './types';
2
- import { AssetDocument } from './manifest';
2
+ /** A flat dictionary is a v1 plan only when schemaVersion is a number and assets is an object. */
3
+ export declare function isSingleFilePlan(value: unknown): boolean;
3
4
  /**
4
- * Generate assets.json file content from asset info
5
- * @param assets - Map of assets
6
- * @param existingAssets - Map of existing assets from previous runs
7
- * @returns JSON string
5
+ * Write the flat dictionary. Existing keys and non-string values stay; scanned
6
+ * resources only add a key when that URL is not already present.
8
7
  */
9
- export declare function generateAssetsFileContent(assets: Map<string, AssetInfo>, existingAssets: Map<string, string>, document?: AssetDocument): string;
8
+ export declare function generateAssetsFileContent(assets: Map<string, AssetInfo>, existingAssets: Map<string, string>, flat?: Record<string, unknown>): string;
10
9
  /**
11
10
  * Parse existing assets.json file
12
11
  * @param content - File content
@@ -1,2 +1,325 @@
1
- import { t as e } from "../asset-constants-C59CLHCE.js";
2
- export { e as assetConstantsPlugin, e as default };
1
+ import { extractAssetsFromContent as e, generateConstantName as t, processAssetURL as n } from "../utils/index.js";
2
+ import { c as r, d as i, f as a, g as o, h as s, i as c, l, m as u, n as d, o as f, p, r as m, s as h, t as g, u as _ } from "../generator-utils-CFYv7n_h.js";
3
+ import { promises as v } from "fs";
4
+ import y from "path";
5
+ import { parse as b } from "@babel/parser";
6
+ import x from "@babel/traverse";
7
+ import * as S from "@babel/types";
8
+ import C from "@babel/generator";
9
+ //#region src/asset-constants/index.ts
10
+ var w = typeof x == "function" ? x : x.default, T = typeof C == "function" ? C : C.default, E = class extends Error {}, D = class {
11
+ isStructuredInventory() {
12
+ return this.document !== void 0 && f(this.document);
13
+ }
14
+ constructor(e = {}) {
15
+ o(this, "options", void 0), o(this, "assets", /* @__PURE__ */ new Map()), o(this, "existingAssets", /* @__PURE__ */ new Map()), o(this, "flat", {}), o(this, "fileCache", new a()), o(this, "document", void 0), o(this, "implementationPaths", /* @__PURE__ */ new Set()), this.options = {
16
+ extensions: [
17
+ ".png",
18
+ ".jpg",
19
+ ".jpeg",
20
+ ".gif",
21
+ ".svg",
22
+ ".mp4",
23
+ ".mp3",
24
+ ".wav",
25
+ ".ogg",
26
+ ".webm"
27
+ ],
28
+ srcDir: "src",
29
+ outputFile: "src/config/assets.json",
30
+ devMode: !1,
31
+ ...e
32
+ };
33
+ }
34
+ clearCache() {
35
+ this.fileCache.clear();
36
+ }
37
+ generateConstantName(e) {
38
+ let n = /* @__PURE__ */ new Set([
39
+ ...Array.from(this.assets.values()).map((e) => e.constantName),
40
+ ...Array.from(this.existingAssets.values()),
41
+ ...this.document && f(this.document) ? Object.keys(this.document.assets) : []
42
+ ]);
43
+ return t(e, n);
44
+ }
45
+ async scanTsFile(t) {
46
+ try {
47
+ let n = await this.fileCache.read(t), r = [], i = e(n, "js", this.options.extensions), a = /* @__PURE__ */ new Set([...Object.keys(this.flat), ...this.document && f(this.document) ? Object.keys(this.document.assets) : []]);
48
+ if (i.some((e) => a.has(e))) {
49
+ let r = l(n, t, this.options.srcDir, this.options.outputFile);
50
+ i = e(r, "js", this.options.extensions);
51
+ }
52
+ let o = n.split("\n");
53
+ return i.forEach((e) => {
54
+ if (this.isImplementationReference(t, e)) return;
55
+ let n = this.generateConstantName(e), i = o.findIndex((t) => t.includes(e)) + 1;
56
+ r.push({
57
+ originalUri: e,
58
+ constantName: n,
59
+ filePath: t,
60
+ line: i,
61
+ column: 1
62
+ });
63
+ }), r;
64
+ } catch (e) {
65
+ return console.warn(`Warning: Failed to scan ${t}:`, e), [];
66
+ }
67
+ }
68
+ async scanAllTsFiles() {
69
+ let e = this.options.srcDir;
70
+ try {
71
+ let t = await u(e, [".ts", ".tsx"]);
72
+ for (let e of t) e.endsWith("assets.json") || e.endsWith("assets.ts") || (await this.scanTsFile(e)).forEach((e) => {
73
+ this.existingAssets.has(e.originalUri) && (e.constantName = this.existingAssets.get(e.originalUri));
74
+ let t = e.originalUri;
75
+ this.assets.has(t) || this.assets.set(t, e);
76
+ });
77
+ } catch (e) {
78
+ console.error("Error scanning TypeScript files:", e);
79
+ }
80
+ }
81
+ async readExistingAssetsFile() {
82
+ try {
83
+ return await v.readFile(this.options.outputFile, "utf-8");
84
+ } catch (e) {
85
+ if (e.code === "ENOENT") return "";
86
+ throw new E("Unable to read assets.json: " + String(e));
87
+ }
88
+ }
89
+ sidecarPath() {
90
+ return y.resolve(this.options.srcDir, "..", "assets-plan.json");
91
+ }
92
+ async parseExistingAssets() {
93
+ let e = await this.readExistingAssetsFile();
94
+ if (e.trim()) {
95
+ let t;
96
+ try {
97
+ t = JSON.parse(e);
98
+ } catch {
99
+ throw new E("assets: invalid JSON syntax");
100
+ }
101
+ if (d(t)) throw new E("assets: single-file asset plan is not supported");
102
+ if (!t || typeof t != "object" || Array.isArray(t)) throw new E("assets: URL dictionary object required");
103
+ this.flat = { ...t };
104
+ }
105
+ try {
106
+ this.existingAssets = c(e);
107
+ } catch (e) {
108
+ throw new E(e instanceof Error ? e.message : "assets: invalid JSON syntax");
109
+ }
110
+ await this.readSidecar();
111
+ }
112
+ async readSidecar() {
113
+ let e = "";
114
+ try {
115
+ e = await v.readFile(this.sidecarPath(), "utf-8");
116
+ } catch (e) {
117
+ e.code !== "ENOENT" && console.warn("assets: unable to read assets-plan.json; continuing with the dictionary only");
118
+ return;
119
+ }
120
+ if (!e.trim()) return;
121
+ let t;
122
+ try {
123
+ t = JSON.parse(e);
124
+ } catch {
125
+ console.warn("assets: assets-plan.json is not valid JSON; continuing with the dictionary only");
126
+ return;
127
+ }
128
+ if (!d(t)) {
129
+ console.warn("assets: assets-plan.json is not a plan; continuing with the dictionary only");
130
+ return;
131
+ }
132
+ try {
133
+ this.document = h(e);
134
+ } catch {
135
+ console.warn("assets: assets-plan.json could not be read; continuing with the dictionary only");
136
+ return;
137
+ }
138
+ if (!f(this.document)) return;
139
+ let n = y.resolve(this.options.srcDir, "..");
140
+ this.implementationPaths = new Set(Object.values(this.document.assets).filter((e) => e.implementation?.path).map((e) => y.resolve(n, e.implementation.path)));
141
+ }
142
+ isImplementationReference(e, t) {
143
+ if (!this.implementationPaths.size || /^[a-z][a-z0-9+.-]*:/i.test(t) || t.startsWith("//")) return !1;
144
+ let n = t.split(/[?#]/)[0], r = n.startsWith("@/") ? y.resolve(this.options.srcDir, n.slice(2)) : y.resolve(y.dirname(e), n);
145
+ return this.implementationPaths.has(r);
146
+ }
147
+ async generateAssetsFile() {
148
+ await this.parseExistingAssets(), await this.scanAllTsFiles();
149
+ let e = g(this.assets, this.existingAssets, this.flat), t = y.dirname(this.options.outputFile);
150
+ await v.mkdir(t, { recursive: !0 }), await v.writeFile(this.options.outputFile, e, "utf-8"), await this.generateAssetsWrapper(), m(this.assets.size, this.existingAssets, this.assets, this.options.outputFile);
151
+ }
152
+ async generateAssetsWrapper() {
153
+ let e = this.options.outputFile.replace(/\.json$/, ".ts"), t = `// 🍎 Auto-generated wrapper for assets.json
154
+ // This file handles iOS image replacement via asset-interceptor.js
155
+ import assetsData from "./${y.basename(this.options.outputFile)}";
156
+
157
+ // Batch process all asset URLs for potential replacements
158
+ // sessionStorage may contain __aippy_asset_overrides__ with multiple image mappings
159
+ (window as any).getReplacedAssets?.(assetsData);
160
+
161
+ export default assetsData;
162
+ `;
163
+ await v.writeFile(e, t, "utf-8"), console.log(` ✓ Generated assets wrapper: ${y.relative(process.cwd(), e)}`);
164
+ }
165
+ getAssets() {
166
+ return Array.from(this.assets.values());
167
+ }
168
+ async replaceUrisInFile(e, t) {
169
+ try {
170
+ let a = await this.fileCache.read(e), o = b(a, {
171
+ sourceType: "module",
172
+ plugins: [
173
+ "typescript",
174
+ "jsx",
175
+ "decorators-legacy"
176
+ ]
177
+ }), s = !1, c = this.options.outputFile.replace(/\.json$/, ".ts"), l = p(c, this.options.srcDir), u = _(e, this.options.srcDir, this.options.outputFile), d = i(o, u), f = "assetsData", m = !1, h, g = /* @__PURE__ */ new Set();
178
+ w(o, {
179
+ Identifier: (e) => {
180
+ g.add(e.node.name);
181
+ },
182
+ ImportDeclaration: (e) => {
183
+ if (!(!u.has(e.node.source.value) || e.node.importKind === "type")) for (let t of e.node.specifiers) {
184
+ if (t.importKind === "type") continue;
185
+ let n = e.scope.getBinding(t.local.name);
186
+ r(t) && (f = t.local.name, m = !0, h = n);
187
+ }
188
+ }
189
+ });
190
+ let x = !1;
191
+ if (m && w(o, { StringLiteral: (r) => {
192
+ !S.isImportDeclaration(r.parent) && !d(r) && !this.isImplementationReference(e, r.node.value) && t.has(n(r.node.value)) && r.scope.getBinding(f) !== h && (x = !0);
193
+ } }), !m && g.has(f) || x) {
194
+ let e = "aippyAssetsData";
195
+ for (let t = 2; g.has(e); t++) e = `aippyAssetsData${t}`;
196
+ x && h.scope.rename(f, e), f = e;
197
+ }
198
+ if (w(o, { StringLiteral: (r) => {
199
+ let i = r.node.value;
200
+ if (this.isImplementationReference(e, i) || d(r) || S.isImportDeclaration(r.parent) || S.isExportNamedDeclaration(r.parent) || S.isExportAllDeclaration(r.parent) || r.findParent((e) => S.isTSType(e.node) || S.isTSInterfaceBody(e.node))) return;
201
+ let a = n(i);
202
+ if (t.has(a)) {
203
+ let e = t.get(a), n = S.memberExpression(S.identifier(f), /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(e) ? S.identifier(e) : S.stringLiteral(e), !/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(e));
204
+ if ((S.isObjectProperty(r.parent) || S.isObjectMethod(r.parent) || S.isClassProperty(r.parent) || S.isClassMethod(r.parent)) && r.key === "key" && (r.parent.computed = !0), r.parent && S.isJSXAttribute(r.parent)) r.replaceWith(S.jsxExpressionContainer(n));
205
+ else {
206
+ try {
207
+ S.validate(r.parent, r.key, n);
208
+ } catch {
209
+ return;
210
+ }
211
+ r.replaceWith(n);
212
+ }
213
+ r.skip(), s = !0;
214
+ }
215
+ } }), !s) return;
216
+ if (!m) {
217
+ let e = S.importDeclaration([S.importDefaultSpecifier(S.identifier(f))], S.stringLiteral(l));
218
+ o.program.body.unshift(e);
219
+ }
220
+ let C = T(o, {
221
+ retainLines: !1,
222
+ comments: !0,
223
+ compact: !1
224
+ }, a);
225
+ await v.writeFile(e, C.code, "utf-8"), this.fileCache.invalidate(e), console.log(` ✓ Replaced URIs in ${y.relative(process.cwd(), e)}`);
226
+ } catch (t) {
227
+ if (this.isStructuredInventory()) throw t;
228
+ console.warn(`Warning: Failed to replace URIs in ${e}:`, t);
229
+ }
230
+ }
231
+ async replaceAllUris() {
232
+ let e = /* @__PURE__ */ new Map(), t = /* @__PURE__ */ new Set();
233
+ for (let n of this.assets.values()) e.set(n.originalUri, n.constantName), t.add(n.filePath);
234
+ for (let n of t) await this.replaceUrisInFile(n, e);
235
+ }
236
+ async cleanupUnusedConstants() {
237
+ if (!(this.document && f(this.document))) try {
238
+ let e = await this.readExistingAssetsFile();
239
+ if (!e.trim()) return;
240
+ let t = /* @__PURE__ */ new Map();
241
+ try {
242
+ let n = JSON.parse(e);
243
+ for (let [e, r] of Object.entries(n)) typeof r == "string" && t.set(e, r);
244
+ } catch (e) {
245
+ console.warn("Warning: Failed to parse assets.json:", e);
246
+ return;
247
+ }
248
+ if (t.size === 0) return;
249
+ let n = /* @__PURE__ */ new Set(), r = await u(this.options.srcDir, [".ts", ".tsx"]);
250
+ for (let e of r) if (!(e.endsWith("assets.json") || e.endsWith("assets.ts"))) try {
251
+ let r = await this.fileCache.read(e);
252
+ for (let e of t.keys()) r.includes(e) && n.add(e);
253
+ } catch (t) {
254
+ console.warn(`Warning: Failed to read ${e}:`, t);
255
+ }
256
+ let i = [];
257
+ for (let e of t.keys()) n.has(e) || i.push(e);
258
+ if (i.length > 0) {
259
+ console.log(`🧹 Removing ${i.length} unused constant(s):`), i.forEach((e) => {
260
+ console.log(` - ${e}`);
261
+ });
262
+ let e = [];
263
+ for (let n of i) {
264
+ let r = t.get(n);
265
+ r && e.push(r);
266
+ }
267
+ for (let t of e) this.assets.delete(t);
268
+ } else console.log("✅ All constants are in use");
269
+ } catch (e) {
270
+ console.warn("Warning: Failed to cleanup unused constants:", e);
271
+ }
272
+ }
273
+ async upgradeJsonImportsToTs() {
274
+ let e = this.options.outputFile.replace(/\.json$/, ".ts"), t = p(e, this.options.srcDir), n = await u(this.options.srcDir, [".ts", ".tsx"]), r = 0;
275
+ for (let i of n) if (!(i.endsWith("assets.ts") || i.endsWith("assets.json"))) try {
276
+ let n = await this.fileCache.read(i), a = s(i, this.options.outputFile) + ".json", o = s(i, e), c = p(this.options.outputFile, this.options.srcDir) + ".json";
277
+ if (!n.includes(a) && !n.includes(o) && !n.includes(c)) continue;
278
+ let l = b(n, {
279
+ sourceType: "module",
280
+ plugins: [
281
+ "typescript",
282
+ "jsx",
283
+ "decorators-legacy"
284
+ ]
285
+ }), u = !1;
286
+ if (w(l, { ImportDeclaration: (e) => {
287
+ let n = e.node.source.value;
288
+ (n === a || n === o || n === c) && (e.node.source.value = t, u = !0);
289
+ } }), u) {
290
+ let e = T(l, {
291
+ retainLines: !1,
292
+ comments: !0,
293
+ compact: !1
294
+ }, n);
295
+ await v.writeFile(i, e.code, "utf-8"), this.fileCache.invalidate(i), console.log(` ✓ Upgraded import in ${y.relative(process.cwd(), i)} → ${t}`), r++;
296
+ }
297
+ } catch (e) {
298
+ if (this.isStructuredInventory()) throw e;
299
+ console.warn(`Warning: Failed to upgrade imports in ${i}:`, e);
300
+ }
301
+ r > 0 && console.log(`✅ Upgraded ${r} file(s) to use ${t}`);
302
+ }
303
+ };
304
+ function O(e = {}) {
305
+ return {
306
+ name: "vite-plugin-asset-constants",
307
+ apply: "build",
308
+ enforce: "pre",
309
+ async buildStart() {
310
+ if (!e.devMode) return;
311
+ let t = new D(e);
312
+ try {
313
+ await t.generateAssetsFile();
314
+ let e = t.getAssets();
315
+ e.length > 0 && (console.log(`📦 Found ${e.length} asset references:`), e.forEach((e) => {
316
+ console.log(` ${e.constantName} = '${e.originalUri}' (${y.relative(process.cwd(), e.filePath)}:${e.line})`);
317
+ }), console.log("\n🔄 Replacing URIs with constants..."), await t.replaceAllUris(), console.log("✅ URI replacement completed!")), console.log("\n🧹 Checking for unused constants..."), await t.cleanupUnusedConstants(), await t.generateAssetsFile(), console.log("\n🔄 Upgrading assets.json imports to assets.ts..."), await t.upgradeJsonImportsToTs(), console.log("✅ Asset constants finalized!\n"), t.clearCache();
318
+ } catch (e) {
319
+ console.error("❌ Error in asset constants plugin:", e), (e instanceof E || t.isStructuredInventory()) && this.error(e instanceof Error ? e.message : String(e));
320
+ }
321
+ }
322
+ };
323
+ }
324
+ //#endregion
325
+ export { O as assetConstantsPlugin, O as default };
@@ -43,6 +43,8 @@ export interface AssetManifest {
43
43
  artStyle: string;
44
44
  assets: Record<string, AssetEntry>;
45
45
  planningError?: AssetError;
46
+ legacyInventory?: string;
47
+ legacyPlaceholderKeys?: string[];
46
48
  }
47
49
  export type AssetDocument = AssetManifest | Record<string, string>;
48
50
  export declare function isManifest(doc: AssetDocument): doc is AssetManifest;
@@ -180,7 +180,7 @@ function C(e, t, n = []) {
180
180
  return Object.keys(r).some((e) => !t.includes(e)) && S("unknown field"), n.some((e) => !(e in r) || r[e] === null) && S("required field missing"), r;
181
181
  }
182
182
  function w(e, t, n = !1) {
183
- (typeof e != "string" || Buffer.byteLength(e) > t || /[\x00-\x08\x0b\x0c\x0e-\x1f\x7f\uFEFF]/.test(e) || n && !e.trim()) && S("invalid bounded text");
183
+ (typeof e != "string" || Buffer.byteLength(e) > t || /[\x00-\x08\x0b\x0c\x0e-\x1f\x7f-\x9f\uFEFF]/.test(e) || n && !e.trim()) && S("invalid bounded text");
184
184
  }
185
185
  function T(e, t) {
186
186
  (typeof e != "string" || !t.includes(e)) && S("unsupported field value");
@@ -247,16 +247,23 @@ function A(e) {
247
247
  k(e);
248
248
  let n = C(t, Object.keys(t));
249
249
  if (!("schemaVersion" in n)) return Object.values(n).some((e) => typeof e != "string") && S("legacy inventory must contain URLs"), n;
250
- C(n, [
250
+ if (C(n, [
251
251
  "schemaVersion",
252
252
  "artStyle",
253
253
  "assets",
254
- "planningError"
254
+ "planningError",
255
+ "legacyInventory",
256
+ "legacyPlaceholderKeys"
255
257
  ], [
256
258
  "schemaVersion",
257
259
  "artStyle",
258
260
  "assets"
259
- ]), n.schemaVersion !== 1 && S("unsupported schema version"), w(n.artStyle, 2048), n.planningError !== void 0 && (D(n.planningError), n.planningError.stage !== "planning" && S("planningError stage must be planning"));
261
+ ]), n.schemaVersion !== 1 && S("unsupported schema version"), w(n.artStyle, 2048), n.legacyInventory !== void 0 && w(n.legacyInventory, b), n.legacyPlaceholderKeys !== void 0) {
262
+ Array.isArray(n.legacyPlaceholderKeys) || S("legacy placeholder keys must be an array");
263
+ for (let e of n.legacyPlaceholderKeys) w(e, 256, !0);
264
+ new Set(n.legacyPlaceholderKeys).size !== n.legacyPlaceholderKeys.length && S("duplicate legacy placeholder key");
265
+ }
266
+ n.planningError !== void 0 && (D(n.planningError), n.planningError.stage !== "planning" && S("planningError stage must be planning"));
260
267
  let r = C(n.assets, Object.keys(n.assets ?? {}));
261
268
  for (let [e, t] of Object.entries(r)) {
262
269
  w(e, 256, !0);
@@ -295,7 +302,7 @@ function A(e) {
295
302
  let e = C(n.generation, ["action", "prompt"], ["action", "prompt"]);
296
303
  T(e.action, ["generate", "reuse"]), w(e.prompt, 4096, !0);
297
304
  } else n.generation !== void 0 && S("non-generated assets cannot contain generation");
298
- if ((n.type === "synth" || n.type === "animation") && n.source !== "code" && S("type requires code source"), n.url !== void 0 && (w(n.url, 4096), (typeof n.url != "string" || !j(n.url)) && S("invalid resource reference")), (n.source === "user" || n.source === "existing") && (!n.url || n.status !== "ready") && S("existing resource requires ready URL"), n.source === "generated" && n.status === "ready" && !n.url && S("ready media requires URL"), n.status === "degraded" && (n.source !== "generated" || !n.url || !n.error) && S("degraded media requires temporary URL and reason"), n.params !== void 0) {
305
+ if ((n.type === "synth" || n.type === "animation") && n.source !== "code" && S("type requires code source"), n.url !== void 0 && (w(n.url, 4096), (typeof n.url != "string" || !M(n.url)) && S("invalid resource reference")), (n.source === "user" || n.source === "existing") && (!n.url || n.status !== "ready") && S("existing resource requires ready URL"), n.source === "generated" && n.status === "ready" && !n.url && S("ready media requires URL"), n.status === "degraded" && (n.source !== "generated" || !n.url || !n.error) && S("degraded media requires temporary URL and reason"), n.params !== void 0) {
299
306
  let e = C(n.params, [
300
307
  "width",
301
308
  "height",
@@ -339,11 +346,38 @@ function A(e) {
339
346
  }
340
347
  n.source === "code" && (!n.implementation || n.url || n.output || n.error || !["planned", "ready"].includes(String(n.status))) && S("code requires implementation without media result");
341
348
  }
349
+ for (let e of n.legacyPlaceholderKeys ?? []) {
350
+ let t = r[e];
351
+ (!t || t.url) && S("invalid legacy placeholder key");
352
+ }
342
353
  return n;
343
354
  }
344
355
  function j(e) {
345
- if (!e || e.trim() !== e || /[\r\n\t\\]/.test(e) || e.startsWith("//") || /%(?![0-9a-f]{2})/i.test(e.split("?")[0]) || /%(?![0-9a-f]{2})/i.test(e.split("#")[1] ?? "")) return !1;
346
- if (!/^[a-z][a-z0-9+.-]*:/i.test(e)) return !0;
356
+ return Buffer.byteLength(e, "utf8") <= 4096 && [
357
+ "",
358
+ "-",
359
+ "--",
360
+ "tbd",
361
+ "todo",
362
+ "none",
363
+ "n/a",
364
+ "null",
365
+ "pending",
366
+ "not-generated",
367
+ "待生成",
368
+ "待制作"
369
+ ].includes(e.trim().toLowerCase());
370
+ }
371
+ function M(e) {
372
+ if (!e || e.trim() !== e || /[\s\\]/u.test(e) || e.startsWith("//") || /%(?![0-9a-f]{2})/i.test(e.split("?")[0]) || /%(?![0-9a-f]{2})/i.test(e.split("#")[1] ?? "")) return !1;
373
+ if (!/^[a-z][a-z0-9+.-]*:/i.test(e)) {
374
+ if (e.split(/[?#]/, 1)[0].split("/", 1)[0].includes(":")) return !1;
375
+ let t = e.split(/[?#]/, 1)[0].replace(/%([0-9a-f]{2})/gi, (e, t) => String.fromCharCode(Number.parseInt(t, 16)));
376
+ if (!t || t.endsWith("/") || j(t)) return !1;
377
+ if (t.includes("/")) return !0;
378
+ let n = t.lastIndexOf(".");
379
+ return n > 0 && n < t.length - 1;
380
+ }
347
381
  if (!/^https?:\/\//i.test(e)) return !1;
348
382
  try {
349
383
  let t = e.slice(e.indexOf("://") + 3).split(/[/?#]/, 1)[0];
@@ -354,12 +388,48 @@ function j(e) {
354
388
  return !1;
355
389
  }
356
390
  }
357
- function M(e) {
391
+ function N(e) {
358
392
  return O(e) ? Object.fromEntries(Object.entries(e.assets).filter(([, e]) => e.replaceable && typeof e.url == "string" && e.url.length > 0).map(([e, t]) => [e, t.url])) : { ...e };
359
393
  }
360
- function N(e) {
361
- let t = e.split(/[?#]/)[0].toLowerCase();
362
- return /\.(mp3|wav|ogg|m4a|aac|flac)$/.test(t) ? "sound" : /\.(mp4|webm|mov)$/.test(t) ? "video" : /\.(png|jpg|jpeg|gif|svg|webp|avif)$/.test(t) ? "image" : /\.(glb|gltf|obj)$/.test(t) ? "model" : "file";
394
+ //#endregion
395
+ //#region src/asset-constants/generator-utils.ts
396
+ function P(e) {
397
+ if (!e || typeof e != "object" || Array.isArray(e)) return !1;
398
+ let t = e;
399
+ return typeof t.schemaVersion == "number" && !!t.assets && typeof t.assets == "object" && !Array.isArray(t.assets);
400
+ }
401
+ function F(e, t, n = {}) {
402
+ let r = { ...n }, i = new Set(Object.keys(r));
403
+ for (let [e, n] of t.entries()) i.has(n) || Object.values(r).includes(e) || (r[n] = e, i.add(n));
404
+ for (let t of e.values()) {
405
+ let e = Object.entries(r).find(([, e]) => e === t.originalUri);
406
+ if (e) {
407
+ t.constantName = e[0];
408
+ continue;
409
+ }
410
+ let n = t.constantName, a = n, o = 2;
411
+ for (; i.has(a);) a = `${n}_${o++}`;
412
+ i.add(a), t.constantName = a, r[a] = t.originalUri;
413
+ }
414
+ return JSON.stringify(r, null, 2) + "\n";
415
+ }
416
+ function I(e) {
417
+ let t = /* @__PURE__ */ new Map();
418
+ try {
419
+ if (!e.trim()) return t;
420
+ let n = JSON.parse(e);
421
+ if (P(n)) throw Error("assets: single-file asset plan is not supported");
422
+ if (!n || typeof n != "object" || Array.isArray(n)) throw Error("assets: URL dictionary object required");
423
+ for (let [e, r] of Object.entries(n)) typeof r == "string" && t.set(r, e);
424
+ t.size > 0 && console.log(`📋 Found ${t.size} existing constants in assets.json`);
425
+ } catch (e) {
426
+ throw e instanceof Error && e.message.startsWith("assets:") ? e : Error("assets: invalid JSON syntax");
427
+ }
428
+ return t;
429
+ }
430
+ function L(e, t, n, r) {
431
+ let i = Array.from(n.values()).filter((e) => t.has(e.originalUri)).length, a = e - i;
432
+ console.log(a > 0 ? `✅ Generated ${e} asset constants (${a} new, ${i} reused) in ${r}` : e > 0 ? `✅ Generated ${e} asset constants (all reused from existing) in ${r}` : "✅ No asset constants found");
363
433
  }
364
434
  //#endregion
365
- export { m as a, _ as c, l as d, u as f, A as i, f as l, N as n, y as o, c as p, O as r, v as s, M as t, d as u };
435
+ export { N as a, m as c, _ as d, f, c as g, u as h, I as i, y as l, l as m, P as n, O as o, d as p, L as r, A as s, F as t, v as u };
@@ -1,5 +1,5 @@
1
1
  import { extractAssetsFromContent as e, findProjectRoot as t, generateConstantName as n, isValidAsset as r, processAssetURL as i } from "../utils/index.js";
2
- import { t as a } from "../asset-constants-C59CLHCE.js";
3
- import { t as o } from "../aippy-preload-QZY6fyKx.js";
2
+ import { assetConstantsPlugin as a } from "../asset-constants/index.js";
3
+ import { t as o } from "../aippy-preload-DtngFmJ5.js";
4
4
  import { t as s } from "../aippy-tagger-By5socAS.js";
5
5
  export { o as aippyPreloadPlugin, s as aippyTaggerPlugin, a as assetConstantsPlugin, a as default, e as extractAssetsFromContent, t as findProjectRoot, n as generateConstantName, r as isValidAsset, i as processAssetURL };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aippy/vite-plugins",
3
- "version": "0.4.0-dev.1",
3
+ "version": "0.4.0-dev.3",
4
4
  "description": "Vite plugins for Aippy projects - Asset management and component tagging",
5
5
  "private": false,
6
6
  "type": "module",
@@ -7,6 +7,13 @@
7
7
  "properties": {
8
8
  "schemaVersion": { "const": 1 },
9
9
  "artStyle": { "type": "string" },
10
+ "legacyInventory": { "type": "string" },
11
+ "legacyPlaceholderKeys": {
12
+ "description": "Server-owned historical flat keys without a resource URL, retained as empty wrapper values regardless of current asset type or source.",
13
+ "type": "array",
14
+ "uniqueItems": true,
15
+ "items": { "type": "string", "minLength": 1 }
16
+ },
10
17
  "planningError": {
11
18
  "allOf": [
12
19
  { "$ref": "#/$defs/error" },
@@ -1,170 +0,0 @@
1
- import { extractAssetsFromContent as e } from "./utils/index.js";
2
- import { d as t, i as n, o as r, r as i, t as a } from "./manifest-CI1ALLzl.js";
3
- import { promises as o } from "fs";
4
- import s from "path";
5
- //#region src/aippy-preload/scanner-utils.ts
6
- async function c(t) {
7
- let n = [], r = [s.join(process.cwd(), "index.html")];
8
- for (let i of r) try {
9
- let r = await o.readFile(i, "utf-8"), a = e(r, "html", t);
10
- n.push(...a);
11
- } catch {
12
- console.warn("⚠️ No index.html found in project root");
13
- }
14
- return n;
15
- }
16
- async function l(a, c) {
17
- let l = [], u = s.resolve(a, "config/assets.json"), d;
18
- try {
19
- let e = n(await o.readFile(u, "utf8"));
20
- i(e) && (d = e);
21
- } catch {}
22
- let f = await t(a, [
23
- ".ts",
24
- ".tsx",
25
- ".js",
26
- ".jsx"
27
- ]);
28
- for (let t of f) try {
29
- if (d && s.resolve(t) === u.replace(/\.json$/, ".ts")) continue;
30
- let n = await o.readFile(t, "utf-8"), i = e(n, "js", c);
31
- d && i.some((e) => Object.hasOwn(d.assets, e)) && (i = e(r(n, t, a, u), "js", c)), l.push(...i);
32
- } catch (e) {
33
- console.warn(`⚠️ Failed to read JS file: ${t}`, e);
34
- }
35
- return l;
36
- }
37
- async function u(n, r) {
38
- let i = [], a = await t(n, [
39
- ".css",
40
- ".scss",
41
- ".sass",
42
- ".less"
43
- ]);
44
- for (let t of a) try {
45
- let n = await o.readFile(t, "utf-8"), a = e(n, "css", r);
46
- i.push(...a);
47
- } catch (e) {
48
- console.warn(`⚠️ Failed to read CSS file: ${t}`, e);
49
- }
50
- return i;
51
- }
52
- async function d(r, c) {
53
- let l = [], u = await t(r, [".json"]);
54
- for (let t of u) try {
55
- let r = await o.readFile(t, "utf-8");
56
- if (s.basename(t) === "assets.json") {
57
- let e = JSON.parse(r);
58
- if (e && Object.prototype.hasOwnProperty.call(e, "schemaVersion")) {
59
- let e = n(r);
60
- if (i(e)) {
61
- l.push(...Object.values(a(e)));
62
- continue;
63
- }
64
- }
65
- }
66
- let u = e(r, "js", c);
67
- l.push(...u);
68
- } catch (e) {
69
- console.warn(`⚠️ Failed to read JSON file: ${t}`, e);
70
- }
71
- return l;
72
- }
73
- async function f(e, t, n) {
74
- console.log("🔍 Scanning source files for static assets...");
75
- let r = [];
76
- if (n) {
77
- console.log("🔍 Deep scanning source file contents...");
78
- let [n, i, a, o] = await Promise.all([
79
- c(t),
80
- l(e, t),
81
- u(e, t),
82
- d(e, t)
83
- ]);
84
- r.push(...n, ...i, ...a, ...o);
85
- }
86
- return [...new Set(r)];
87
- }
88
- //#endregion
89
- //#region src/aippy-preload/html-utils.ts
90
- var p = "<head>", m = 6;
91
- function h(e) {
92
- return e.map((e) => ` <meta content="${e}" name="aippy:preload">`).join("\n");
93
- }
94
- function g(e, t) {
95
- let n = e.indexOf(p);
96
- if (n === -1) throw Error("Could not find <head> tag in HTML");
97
- let r = n + m;
98
- return e.slice(0, r) + "\n" + t + "\n" + e.slice(r);
99
- }
100
- function _(e) {
101
- let t = e.length;
102
- if (console.log(`📁 Found ${t} static ${t === 1 ? "asset" : "assets"} in source files`), t === 0) {
103
- console.log("⚠️ No static assets found in source files.");
104
- return;
105
- }
106
- console.log(`📋 ${t === 1 ? "Asset" : "Assets"} found:`), e.forEach((e) => console.log(` - ${e}`));
107
- }
108
- function v(e) {
109
- console.log(`✅ Injected ${e} preload meta ${e === 1 ? "tag" : "tags"} into index.html`);
110
- }
111
- //#endregion
112
- //#region src/aippy-preload/index.ts
113
- var y = 3, b = 1e3;
114
- function x(e) {
115
- return new Promise((t) => setTimeout(t, e));
116
- }
117
- async function S(e, t = y, n = b) {
118
- for (let r = 1; r <= t; r++) try {
119
- return await o.readFile(e, "utf-8");
120
- } catch (e) {
121
- if (e.code === "ENOENT") {
122
- if (r < t) {
123
- console.log(`⏳ index.html not found, waiting ${n}ms before retry (attempt ${r}/${t})...`), await x(n);
124
- continue;
125
- }
126
- return console.log(`⚠️ index.html not found after ${t} attempts, skipping preload injection.`), null;
127
- }
128
- return console.error("❌ Error reading index.html:", e), null;
129
- }
130
- return null;
131
- }
132
- function C(e = {}) {
133
- let t = {
134
- extensions: [
135
- ".png",
136
- ".jpg",
137
- ".jpeg",
138
- ".gif",
139
- ".svg",
140
- ".mp4",
141
- ".mp3",
142
- ".wav",
143
- ".ogg",
144
- ".webm"
145
- ],
146
- srcDir: "src",
147
- outDir: "dist",
148
- deepScan: !0,
149
- ...e
150
- };
151
- return {
152
- name: "vite-plugin-aippy-preload",
153
- apply: "build",
154
- enforce: "post",
155
- async writeBundle() {
156
- try {
157
- let e = await f(t.srcDir, t.extensions, t.deepScan);
158
- if (_(e), e.length === 0) return;
159
- let n = s.join(t.outDir, "index.html"), r = await S(n);
160
- if (r === null) return;
161
- let i = g(r, h(e));
162
- await o.writeFile(n, i, "utf-8"), v(e.length);
163
- } catch (e) {
164
- console.error("❌ Error in aippy preload plugin:", e);
165
- }
166
- }
167
- };
168
- }
169
- //#endregion
170
- export { C as t };
@@ -1,359 +0,0 @@
1
- import { extractAssetsFromContent as e, generateConstantName as t, processAssetURL as n } from "./utils/index.js";
2
- import { a as r, c as i, d as a, f as o, i as s, l as c, n as l, o as u, p as d, r as f, s as p, t as m, u as h } from "./manifest-CI1ALLzl.js";
3
- import { promises as g } from "fs";
4
- import _ from "path";
5
- import { parse as v } from "@babel/parser";
6
- import y from "@babel/traverse";
7
- import * as b from "@babel/types";
8
- import x from "@babel/generator";
9
- //#region src/asset-constants/generator-utils.ts
10
- function S(e, t, n) {
11
- if (n && f(n)) {
12
- let t = structuredClone(n), r = new Set(Object.keys(t.assets));
13
- for (let n of e.values()) {
14
- let e = Object.keys(t.assets).filter((e) => t.assets[e].url === n.originalUri);
15
- if (e.length) {
16
- n.constantName = e[0];
17
- continue;
18
- }
19
- let i = n.constantName, a = i, o = 2;
20
- for (; r.has(a);) a = `${i}_${o++}`;
21
- r.add(a), n.constantName = a, t.assets[a] = {
22
- type: l(n.originalUri),
23
- source: "existing",
24
- replaceable: !0,
25
- description: `Existing asset ${a}`,
26
- status: "ready",
27
- url: n.originalUri
28
- };
29
- }
30
- return JSON.stringify(t, null, 2) + "\n";
31
- }
32
- let r = {};
33
- for (let [e, n] of t.entries()) r[n] = e;
34
- return Array.from(e.values()).forEach((e) => {
35
- r[e.constantName] = e.originalUri;
36
- }), JSON.stringify(r, null, 2) + "\n";
37
- }
38
- function C(e) {
39
- let t = /* @__PURE__ */ new Map();
40
- try {
41
- if (!e.trim()) return t;
42
- let n = JSON.parse(e);
43
- for (let [e, r] of Object.entries(n)) typeof r == "string" && t.set(r, e);
44
- t.size > 0 && console.log(`📋 Found ${t.size} existing constants in assets.json`);
45
- } catch (e) {
46
- console.warn("Warning: Failed to parse existing assets.json:", e);
47
- }
48
- return t;
49
- }
50
- function w(e, t, n, r) {
51
- let i = Array.from(n.values()).filter((e) => t.has(e.originalUri)).length, a = e - i;
52
- console.log(a > 0 ? `✅ Generated ${e} asset constants (${a} new, ${i} reused) in ${r}` : e > 0 ? `✅ Generated ${e} asset constants (all reused from existing) in ${r}` : "✅ No asset constants found");
53
- }
54
- //#endregion
55
- //#region src/asset-constants/index.ts
56
- var T = typeof y == "function" ? y : y.default, E = typeof x == "function" ? x : x.default, D = class extends Error {}, O = class {
57
- isStructuredInventory() {
58
- return this.document !== void 0 && f(this.document);
59
- }
60
- constructor(e = {}) {
61
- d(this, "options", void 0), d(this, "assets", /* @__PURE__ */ new Map()), d(this, "existingAssets", /* @__PURE__ */ new Map()), d(this, "fileCache", new c()), d(this, "document", void 0), d(this, "implementationPaths", /* @__PURE__ */ new Set()), this.options = {
62
- extensions: [
63
- ".png",
64
- ".jpg",
65
- ".jpeg",
66
- ".gif",
67
- ".svg",
68
- ".mp4",
69
- ".mp3",
70
- ".wav",
71
- ".ogg",
72
- ".webm"
73
- ],
74
- srcDir: "src",
75
- outputFile: "src/config/assets.json",
76
- devMode: !1,
77
- ...e
78
- };
79
- }
80
- clearCache() {
81
- this.fileCache.clear();
82
- }
83
- generateConstantName(e) {
84
- let n = /* @__PURE__ */ new Set([
85
- ...Array.from(this.assets.values()).map((e) => e.constantName),
86
- ...Array.from(this.existingAssets.values()),
87
- ...this.document && f(this.document) ? Object.keys(this.document.assets) : []
88
- ]);
89
- return t(e, n);
90
- }
91
- async scanTsFile(t) {
92
- try {
93
- let n = await this.fileCache.read(t), r = [], i = e(n, "js", this.options.extensions), a = this.document;
94
- if (a && f(a) && i.some((e) => Object.hasOwn(a.assets, e))) {
95
- let r = u(n, t, this.options.srcDir, this.options.outputFile);
96
- i = e(r, "js", this.options.extensions);
97
- }
98
- let o = n.split("\n");
99
- return i.forEach((e) => {
100
- if (this.isImplementationReference(t, e)) return;
101
- let n = this.generateConstantName(e), i = o.findIndex((t) => t.includes(e)) + 1;
102
- r.push({
103
- originalUri: e,
104
- constantName: n,
105
- filePath: t,
106
- line: i,
107
- column: 1
108
- });
109
- }), r;
110
- } catch (e) {
111
- return console.warn(`Warning: Failed to scan ${t}:`, e), [];
112
- }
113
- }
114
- async scanAllTsFiles() {
115
- let e = this.options.srcDir;
116
- try {
117
- let t = await a(e, [".ts", ".tsx"]);
118
- for (let e of t) e.endsWith("assets.json") || e.endsWith("assets.ts") || (await this.scanTsFile(e)).forEach((e) => {
119
- this.existingAssets.has(e.originalUri) && (e.constantName = this.existingAssets.get(e.originalUri));
120
- let t = e.originalUri;
121
- this.assets.has(t) || this.assets.set(t, e);
122
- });
123
- } catch (e) {
124
- console.error("Error scanning TypeScript files:", e);
125
- }
126
- }
127
- async readExistingAssetsFile() {
128
- try {
129
- return await g.readFile(this.options.outputFile, "utf-8");
130
- } catch (e) {
131
- if (e.code === "ENOENT") return "";
132
- throw new D("Unable to read assets.json: " + String(e));
133
- }
134
- }
135
- async parseExistingAssets() {
136
- let e = await this.readExistingAssetsFile();
137
- if (e.trim()) {
138
- let t;
139
- try {
140
- t = JSON.parse(e);
141
- } catch {
142
- if (/"schemaVersion(?:["\s:]|$)/i.test(e.replace(/\\u([0-9a-f]{4})/gi, (e, t) => String.fromCharCode(parseInt(t, 16))))) throw new D("assets: invalid JSON syntax");
143
- }
144
- if (t && Object.prototype.hasOwnProperty.call(t, "schemaVersion")) {
145
- try {
146
- this.document = s(e);
147
- } catch (e) {
148
- throw new D(e instanceof Error ? e.message : "Invalid asset manifest");
149
- }
150
- this.existingAssets = /* @__PURE__ */ new Map();
151
- for (let [e, t] of Object.entries(m(this.document))) this.existingAssets.has(t) || this.existingAssets.set(t, e);
152
- if (f(this.document)) {
153
- let e = _.dirname(_.resolve(this.options.srcDir));
154
- this.implementationPaths = new Set(Object.values(this.document.assets).filter((e) => (e.source === "code" || e.source === "generated") && e.implementation?.path).map((t) => _.resolve(e, t.implementation.path)));
155
- }
156
- return;
157
- }
158
- }
159
- this.existingAssets = C(e);
160
- }
161
- isImplementationReference(e, t) {
162
- if (!this.implementationPaths.size || /^[a-z][a-z0-9+.-]*:/i.test(t) || t.startsWith("//")) return !1;
163
- let n = t.split(/[?#]/)[0], r = n.startsWith("@/") ? _.resolve(this.options.srcDir, n.slice(2)) : _.resolve(_.dirname(e), n);
164
- return this.implementationPaths.has(r);
165
- }
166
- async generateAssetsFile() {
167
- await this.parseExistingAssets(), await this.scanAllTsFiles();
168
- let e = S(this.assets, this.existingAssets, this.document), t = _.dirname(this.options.outputFile);
169
- if (await g.mkdir(t, { recursive: !0 }), this.document && f(this.document)) try {
170
- this.document = s(e);
171
- } catch (e) {
172
- throw new D(e instanceof Error ? e.message : "Invalid supplementary asset");
173
- }
174
- await g.writeFile(this.options.outputFile, e, "utf-8"), await this.generateAssetsWrapper(), w(this.assets.size, this.existingAssets, this.assets, this.options.outputFile);
175
- }
176
- async generateAssetsWrapper() {
177
- let e = this.options.outputFile.replace(/\.json$/, ".ts"), t = _.basename(this.options.outputFile), n = `// 🍎 Auto-generated wrapper for assets.json
178
- // This file handles iOS image replacement via asset-interceptor.js
179
- import assetsData from "./${t}";
180
-
181
- // Batch process all asset URLs for potential replacements
182
- // sessionStorage may contain __aippy_asset_overrides__ with multiple image mappings
183
- (window as any).getReplacedAssets?.(assetsData);
184
-
185
- export default assetsData;
186
- `;
187
- if (this.document && f(this.document)) {
188
- let e = Object.keys(m(this.document));
189
- n = `// Auto-generated asset URL map. Planning metadata stays in assets.json.
190
- import manifest from "./${t}";
191
- const keys = ${JSON.stringify(e)} as const;
192
- const assetsData = ${e.length ? "Object.fromEntries(keys.map(name => [name, manifest.assets[name].url]))" : "{}"} as { [K in typeof keys[number]]: string };
193
- (window as any).getReplacedAssets?.(assetsData);
194
- export default assetsData;
195
- `;
196
- }
197
- await g.writeFile(e, n, "utf-8"), console.log(` ✓ Generated assets wrapper: ${_.relative(process.cwd(), e)}`);
198
- }
199
- getAssets() {
200
- return Array.from(this.assets.values());
201
- }
202
- async replaceUrisInFile(e, t) {
203
- try {
204
- let a = await this.fileCache.read(e), o = v(a, {
205
- sourceType: "module",
206
- plugins: [
207
- "typescript",
208
- "jsx",
209
- "decorators-legacy"
210
- ]
211
- }), s = !1, c = this.options.outputFile.replace(/\.json$/, ".ts"), l = h(c, this.options.srcDir), u = p(e, this.options.srcDir, this.options.outputFile), d = i(o, u), f = "assetsData", m = !1, y, x = /* @__PURE__ */ new Set();
212
- T(o, {
213
- Identifier: (e) => {
214
- x.add(e.node.name);
215
- },
216
- ImportDeclaration: (e) => {
217
- if (!(!u.has(e.node.source.value) || e.node.importKind === "type")) for (let t of e.node.specifiers) {
218
- if (t.importKind === "type") continue;
219
- let n = e.scope.getBinding(t.local.name);
220
- r(t) && (f = t.local.name, m = !0, y = n);
221
- }
222
- }
223
- });
224
- let S = !1;
225
- if (m && T(o, { StringLiteral: (r) => {
226
- !b.isImportDeclaration(r.parent) && !d(r) && !this.isImplementationReference(e, r.node.value) && t.has(n(r.node.value)) && r.scope.getBinding(f) !== y && (S = !0);
227
- } }), !m && x.has(f) || S) {
228
- let e = "aippyAssetsData";
229
- for (let t = 2; x.has(e); t++) e = `aippyAssetsData${t}`;
230
- S && y.scope.rename(f, e), f = e;
231
- }
232
- if (T(o, { StringLiteral: (r) => {
233
- let i = r.node.value;
234
- if (this.isImplementationReference(e, i) || d(r) || b.isImportDeclaration(r.parent) || b.isExportNamedDeclaration(r.parent) || b.isExportAllDeclaration(r.parent) || r.findParent((e) => b.isTSType(e.node) || b.isTSInterfaceBody(e.node))) return;
235
- let a = n(i);
236
- if (t.has(a)) {
237
- let e = t.get(a), n = b.memberExpression(b.identifier(f), /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(e) ? b.identifier(e) : b.stringLiteral(e), !/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(e));
238
- if ((b.isObjectProperty(r.parent) || b.isObjectMethod(r.parent) || b.isClassProperty(r.parent) || b.isClassMethod(r.parent)) && r.key === "key" && (r.parent.computed = !0), r.parent && b.isJSXAttribute(r.parent)) r.replaceWith(b.jsxExpressionContainer(n));
239
- else {
240
- try {
241
- b.validate(r.parent, r.key, n);
242
- } catch {
243
- return;
244
- }
245
- r.replaceWith(n);
246
- }
247
- r.skip(), s = !0;
248
- }
249
- } }), !s) return;
250
- if (!m) {
251
- let e = b.importDeclaration([b.importDefaultSpecifier(b.identifier(f))], b.stringLiteral(l));
252
- o.program.body.unshift(e);
253
- }
254
- let C = E(o, {
255
- retainLines: !1,
256
- comments: !0,
257
- compact: !1
258
- }, a);
259
- await g.writeFile(e, C.code, "utf-8"), this.fileCache.invalidate(e), console.log(` ✓ Replaced URIs in ${_.relative(process.cwd(), e)}`);
260
- } catch (t) {
261
- if (this.isStructuredInventory()) throw t;
262
- console.warn(`Warning: Failed to replace URIs in ${e}:`, t);
263
- }
264
- }
265
- async replaceAllUris() {
266
- let e = /* @__PURE__ */ new Map(), t = /* @__PURE__ */ new Set();
267
- for (let n of this.assets.values()) e.set(n.originalUri, n.constantName), t.add(n.filePath);
268
- for (let n of t) await this.replaceUrisInFile(n, e);
269
- }
270
- async cleanupUnusedConstants() {
271
- if (!(this.document && f(this.document))) try {
272
- let e = await this.readExistingAssetsFile();
273
- if (!e.trim()) return;
274
- let t = /* @__PURE__ */ new Map();
275
- try {
276
- let n = JSON.parse(e);
277
- for (let [e, r] of Object.entries(n)) typeof r == "string" && t.set(e, r);
278
- } catch (e) {
279
- console.warn("Warning: Failed to parse assets.json:", e);
280
- return;
281
- }
282
- if (t.size === 0) return;
283
- let n = /* @__PURE__ */ new Set(), r = await a(this.options.srcDir, [".ts", ".tsx"]);
284
- for (let e of r) if (!(e.endsWith("assets.json") || e.endsWith("assets.ts"))) try {
285
- let r = await this.fileCache.read(e);
286
- for (let e of t.keys()) r.includes(e) && n.add(e);
287
- } catch (t) {
288
- console.warn(`Warning: Failed to read ${e}:`, t);
289
- }
290
- let i = [];
291
- for (let e of t.keys()) n.has(e) || i.push(e);
292
- if (i.length > 0) {
293
- console.log(`🧹 Removing ${i.length} unused constant(s):`), i.forEach((e) => {
294
- console.log(` - ${e}`);
295
- });
296
- let e = [];
297
- for (let n of i) {
298
- let r = t.get(n);
299
- r && e.push(r);
300
- }
301
- for (let t of e) this.assets.delete(t);
302
- } else console.log("✅ All constants are in use");
303
- } catch (e) {
304
- console.warn("Warning: Failed to cleanup unused constants:", e);
305
- }
306
- }
307
- async upgradeJsonImportsToTs() {
308
- let e = this.options.outputFile.replace(/\.json$/, ".ts"), t = h(e, this.options.srcDir), n = await a(this.options.srcDir, [".ts", ".tsx"]), r = 0;
309
- for (let i of n) if (!(i.endsWith("assets.ts") || i.endsWith("assets.json"))) try {
310
- let n = await this.fileCache.read(i), a = o(i, this.options.outputFile) + ".json", s = o(i, e), c = h(this.options.outputFile, this.options.srcDir) + ".json";
311
- if (!n.includes(a) && !n.includes(s) && !n.includes(c)) continue;
312
- let l = v(n, {
313
- sourceType: "module",
314
- plugins: [
315
- "typescript",
316
- "jsx",
317
- "decorators-legacy"
318
- ]
319
- }), u = !1;
320
- if (T(l, { ImportDeclaration: (e) => {
321
- let n = e.node.source.value;
322
- (n === a || n === s || n === c) && (e.node.source.value = t, u = !0);
323
- } }), u) {
324
- let e = E(l, {
325
- retainLines: !1,
326
- comments: !0,
327
- compact: !1
328
- }, n);
329
- await g.writeFile(i, e.code, "utf-8"), this.fileCache.invalidate(i), console.log(` ✓ Upgraded import in ${_.relative(process.cwd(), i)} → ${t}`), r++;
330
- }
331
- } catch (e) {
332
- if (this.isStructuredInventory()) throw e;
333
- console.warn(`Warning: Failed to upgrade imports in ${i}:`, e);
334
- }
335
- r > 0 && console.log(`✅ Upgraded ${r} file(s) to use ${t}`);
336
- }
337
- };
338
- function k(e = {}) {
339
- return {
340
- name: "vite-plugin-asset-constants",
341
- apply: "build",
342
- enforce: "pre",
343
- async buildStart() {
344
- if (!e.devMode) return;
345
- let t = new O(e);
346
- try {
347
- await t.generateAssetsFile();
348
- let e = t.getAssets();
349
- e.length > 0 && (console.log(`📦 Found ${e.length} asset references:`), e.forEach((e) => {
350
- console.log(` ${e.constantName} = '${e.originalUri}' (${_.relative(process.cwd(), e.filePath)}:${e.line})`);
351
- }), console.log("\n🔄 Replacing URIs with constants..."), await t.replaceAllUris(), console.log("✅ URI replacement completed!")), console.log("\n🧹 Checking for unused constants..."), await t.cleanupUnusedConstants(), await t.generateAssetsFile(), console.log("\n🔄 Upgrading assets.json imports to assets.ts..."), await t.upgradeJsonImportsToTs(), console.log("✅ Asset constants finalized!\n"), t.clearCache();
352
- } catch (e) {
353
- console.error("❌ Error in asset constants plugin:", e), (e instanceof D || t.isStructuredInventory()) && this.error(e instanceof Error ? e.message : String(e));
354
- }
355
- }
356
- };
357
- }
358
- //#endregion
359
- export { k as t };