@aippy/vite-plugins 0.4.0-dev.3 → 0.4.0-dev.5
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 +6 -2
- package/dist/aippy-preload/index.js +1 -1
- package/dist/{aippy-preload-DtngFmJ5.js → aippy-preload-DZsYdwMm.js} +65 -76
- package/dist/asset-constants/generator-utils.d.ts +8 -0
- package/dist/asset-constants/index.js +2 -325
- package/dist/asset-constants/wrapper-keys.d.ts +1 -0
- package/dist/asset-constants-bI4rQJpw.js +423 -0
- package/dist/index/index.js +2 -2
- package/dist/wrapper-keys-BPP5iTGM.js +126 -0
- package/package.json +3 -5
- package/dist/asset-constants/manifest.d.ts +0 -53
- package/dist/generator-utils-CFYv7n_h.js +0 -435
- package/src/asset-constants/assets.schema.json +0 -239
package/README.md
CHANGED
|
@@ -104,6 +104,10 @@ UNLICENSED - Aippy Team
|
|
|
104
104
|
|
|
105
105
|
## Asset files
|
|
106
106
|
|
|
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;
|
|
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. The plugin does not validate the plan; aippy-chat does. A missing or unreadable plan does not stop the build. Invalid dictionary JSON, a single-file v1 plan stored in the dictionary, or a failed dictionary/wrapper write stops the build without overwriting the file. A source file the plugin cannot parse or rewrite only logs a warning.
|
|
108
108
|
|
|
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.
|
|
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.
|
|
110
|
+
|
|
111
|
+
Source writes the dictionary url. The build rewrites that literal to one dictionary key. A url owned by several keys uses the first key. The url may be any reference aippy-chat accepts (`ValidReference`): http(s), root-relative, or relative.
|
|
112
|
+
|
|
113
|
+
Version 0.4.0-dev.3 is published. The `dev` dist-tag points at it. `latest` remains `0.3.0`.
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { t as e } from "../aippy-preload-
|
|
1
|
+
import { t as e } from "../aippy-preload-DZsYdwMm.js";
|
|
2
2
|
export { e as aippyPreloadPlugin, e as default };
|
|
@@ -1,119 +1,108 @@
|
|
|
1
1
|
import { extractAssetsFromContent as e, isValidAsset as t } from "./utils/index.js";
|
|
2
|
-
import {
|
|
3
|
-
import { promises as
|
|
4
|
-
import
|
|
2
|
+
import { n, s as r } from "./wrapper-keys-BPP5iTGM.js";
|
|
3
|
+
import { promises as i } from "fs";
|
|
4
|
+
import a from "path";
|
|
5
5
|
//#region src/aippy-preload/scanner-utils.ts
|
|
6
|
-
async function
|
|
7
|
-
let n = [], r = [
|
|
8
|
-
for (let
|
|
9
|
-
let r = await
|
|
10
|
-
n.push(...
|
|
6
|
+
async function o(t) {
|
|
7
|
+
let n = [], r = [a.join(process.cwd(), "index.html")];
|
|
8
|
+
for (let a of r) try {
|
|
9
|
+
let r = await i.readFile(a, "utf-8"), o = e(r, "html", t);
|
|
10
|
+
n.push(...o);
|
|
11
11
|
} catch {
|
|
12
12
|
console.warn("⚠️ No index.html found in project root");
|
|
13
13
|
}
|
|
14
14
|
return n;
|
|
15
15
|
}
|
|
16
|
-
async function
|
|
17
|
-
let
|
|
16
|
+
async function s(t, o) {
|
|
17
|
+
let s = [], c = a.resolve(t, "config/assets.json"), l = /* @__PURE__ */ new Set();
|
|
18
18
|
try {
|
|
19
|
-
let e = JSON.parse(await
|
|
20
|
-
|
|
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)));
|
|
19
|
+
let e = JSON.parse(await i.readFile(c, "utf8"));
|
|
20
|
+
e && typeof e == "object" && !Array.isArray(e) && (l = new Set(Object.keys(e)));
|
|
24
21
|
} catch {}
|
|
25
|
-
let
|
|
22
|
+
let u = await r(t, [
|
|
26
23
|
".ts",
|
|
27
24
|
".tsx",
|
|
28
25
|
".js",
|
|
29
26
|
".jsx"
|
|
30
27
|
]);
|
|
31
|
-
for (let
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
o.some((e) => s.has(e)) && (o = e(r(a, i, t, d), "js", n)), u.push(...o);
|
|
28
|
+
for (let r of u) try {
|
|
29
|
+
let a = await i.readFile(r, "utf-8"), u = e(a, "js", o);
|
|
30
|
+
u.some((e) => l.has(e)) && (u = e(n(a, r, t, c), "js", o)), s.push(...u);
|
|
35
31
|
} catch (e) {
|
|
36
|
-
console.warn(`⚠️ Failed to read JS file: ${
|
|
32
|
+
console.warn(`⚠️ Failed to read JS file: ${r}`, e);
|
|
37
33
|
}
|
|
38
|
-
return
|
|
34
|
+
return s;
|
|
39
35
|
}
|
|
40
|
-
async function
|
|
41
|
-
let
|
|
36
|
+
async function c(t, n) {
|
|
37
|
+
let a = [], o = await r(t, [
|
|
42
38
|
".css",
|
|
43
39
|
".scss",
|
|
44
40
|
".sass",
|
|
45
41
|
".less"
|
|
46
42
|
]);
|
|
47
|
-
for (let t of
|
|
48
|
-
let
|
|
49
|
-
|
|
43
|
+
for (let t of o) try {
|
|
44
|
+
let r = await i.readFile(t, "utf-8"), o = e(r, "css", n);
|
|
45
|
+
a.push(...o);
|
|
50
46
|
} catch (e) {
|
|
51
47
|
console.warn(`⚠️ Failed to read CSS file: ${t}`, e);
|
|
52
48
|
}
|
|
53
|
-
return
|
|
49
|
+
return a;
|
|
54
50
|
}
|
|
55
|
-
function
|
|
51
|
+
function l(e, n) {
|
|
56
52
|
if (!t(e) || /["'<>\s`]/.test(e)) return !1;
|
|
57
53
|
let r = e.split(/[?#]/, 1)[0].toLowerCase();
|
|
58
54
|
return n.some((e) => r.endsWith(e.toLowerCase()));
|
|
59
55
|
}
|
|
60
|
-
async function
|
|
61
|
-
let
|
|
62
|
-
for (let
|
|
63
|
-
let
|
|
64
|
-
if (
|
|
65
|
-
if (
|
|
66
|
-
let e = JSON.parse(
|
|
67
|
-
if (a(e)) {
|
|
68
|
-
let e = s(d);
|
|
69
|
-
if (o(e)) {
|
|
70
|
-
u.push(...Object.values(n(e)));
|
|
71
|
-
continue;
|
|
72
|
-
}
|
|
73
|
-
}
|
|
56
|
+
async function u(t, n) {
|
|
57
|
+
let o = [], s = await r(t, [".json"]);
|
|
58
|
+
for (let r of s) try {
|
|
59
|
+
let s = await i.readFile(r, "utf-8");
|
|
60
|
+
if (a.basename(r) === "assets-plan.json") continue;
|
|
61
|
+
if (a.resolve(r) === a.resolve(t, "config/assets.json")) {
|
|
62
|
+
let e = JSON.parse(s);
|
|
74
63
|
if (e && typeof e == "object" && !Array.isArray(e)) {
|
|
75
|
-
for (let t of Object.values(e)) typeof t == "string" &&
|
|
64
|
+
for (let t of Object.values(e)) typeof t == "string" && l(t, n) && o.push(t);
|
|
76
65
|
continue;
|
|
77
66
|
}
|
|
78
67
|
}
|
|
79
|
-
let
|
|
80
|
-
|
|
68
|
+
let c = e(s, "js", n);
|
|
69
|
+
o.push(...c);
|
|
81
70
|
} catch (e) {
|
|
82
|
-
console.warn(`⚠️ Failed to read JSON file: ${
|
|
71
|
+
console.warn(`⚠️ Failed to read JSON file: ${r}`, e);
|
|
83
72
|
}
|
|
84
|
-
return
|
|
73
|
+
return o;
|
|
85
74
|
}
|
|
86
|
-
async function
|
|
75
|
+
async function d(e, t, n) {
|
|
87
76
|
console.log("🔍 Scanning source files for static assets...");
|
|
88
77
|
let r = [];
|
|
89
78
|
if (n) {
|
|
90
79
|
console.log("🔍 Deep scanning source file contents...");
|
|
91
|
-
let [n, i, a,
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
80
|
+
let [n, i, a, l] = await Promise.all([
|
|
81
|
+
o(t),
|
|
82
|
+
s(e, t),
|
|
83
|
+
c(e, t),
|
|
84
|
+
u(e, t)
|
|
96
85
|
]);
|
|
97
|
-
r.push(...n, ...i, ...a, ...
|
|
86
|
+
r.push(...n, ...i, ...a, ...l);
|
|
98
87
|
}
|
|
99
88
|
return [...new Set(r)];
|
|
100
89
|
}
|
|
101
90
|
//#endregion
|
|
102
91
|
//#region src/aippy-preload/html-utils.ts
|
|
103
|
-
var
|
|
104
|
-
function
|
|
92
|
+
var f = "<head>", p = 6;
|
|
93
|
+
function m(e) {
|
|
105
94
|
return e.replace(/&/g, "&").replace(/"/g, """).replace(/'/g, "'").replace(/</g, "<").replace(/>/g, ">");
|
|
106
95
|
}
|
|
107
|
-
function
|
|
108
|
-
return e.map((e) => ` <meta content="${
|
|
96
|
+
function h(e) {
|
|
97
|
+
return e.map((e) => ` <meta content="${m(e)}" name="aippy:preload">`).join("\n");
|
|
109
98
|
}
|
|
110
|
-
function
|
|
111
|
-
let n = e.indexOf(
|
|
99
|
+
function g(e, t) {
|
|
100
|
+
let n = e.indexOf(f);
|
|
112
101
|
if (n === -1) throw Error("Could not find <head> tag in HTML");
|
|
113
|
-
let r = n +
|
|
102
|
+
let r = n + p;
|
|
114
103
|
return e.slice(0, r) + "\n" + t + "\n" + e.slice(r);
|
|
115
104
|
}
|
|
116
|
-
function
|
|
105
|
+
function _(e) {
|
|
117
106
|
let t = e.length;
|
|
118
107
|
if (console.log(`📁 Found ${t} static ${t === 1 ? "asset" : "assets"} in source files`), t === 0) {
|
|
119
108
|
console.log("⚠️ No static assets found in source files.");
|
|
@@ -121,22 +110,22 @@ function x(e) {
|
|
|
121
110
|
}
|
|
122
111
|
console.log(`📋 ${t === 1 ? "Asset" : "Assets"} found:`), e.forEach((e) => console.log(` - ${e}`));
|
|
123
112
|
}
|
|
124
|
-
function
|
|
113
|
+
function v(e) {
|
|
125
114
|
console.log(`✅ Injected ${e} preload meta ${e === 1 ? "tag" : "tags"} into index.html`);
|
|
126
115
|
}
|
|
127
116
|
//#endregion
|
|
128
117
|
//#region src/aippy-preload/index.ts
|
|
129
|
-
var
|
|
130
|
-
function
|
|
118
|
+
var y = 3, b = 1e3;
|
|
119
|
+
function x(e) {
|
|
131
120
|
return new Promise((t) => setTimeout(t, e));
|
|
132
121
|
}
|
|
133
|
-
async function
|
|
122
|
+
async function S(e, t = y, n = b) {
|
|
134
123
|
for (let r = 1; r <= t; r++) try {
|
|
135
|
-
return await
|
|
124
|
+
return await i.readFile(e, "utf-8");
|
|
136
125
|
} catch (e) {
|
|
137
126
|
if (e.code === "ENOENT") {
|
|
138
127
|
if (r < t) {
|
|
139
|
-
console.log(`⏳ index.html not found, waiting ${n}ms before retry (attempt ${r}/${t})...`), await
|
|
128
|
+
console.log(`⏳ index.html not found, waiting ${n}ms before retry (attempt ${r}/${t})...`), await x(n);
|
|
140
129
|
continue;
|
|
141
130
|
}
|
|
142
131
|
return console.log(`⚠️ index.html not found after ${t} attempts, skipping preload injection.`), null;
|
|
@@ -145,7 +134,7 @@ async function E(e, t = C, n = w) {
|
|
|
145
134
|
}
|
|
146
135
|
return null;
|
|
147
136
|
}
|
|
148
|
-
function
|
|
137
|
+
function C(e = {}) {
|
|
149
138
|
let t = {
|
|
150
139
|
extensions: [
|
|
151
140
|
".png",
|
|
@@ -170,12 +159,12 @@ function D(e = {}) {
|
|
|
170
159
|
enforce: "post",
|
|
171
160
|
async writeBundle() {
|
|
172
161
|
try {
|
|
173
|
-
let e = await
|
|
174
|
-
if (
|
|
175
|
-
let n =
|
|
162
|
+
let e = await d(t.srcDir, t.extensions, t.deepScan);
|
|
163
|
+
if (_(e), e.length === 0) return;
|
|
164
|
+
let n = a.join(t.outDir, "index.html"), r = await S(n);
|
|
176
165
|
if (r === null) return;
|
|
177
|
-
let
|
|
178
|
-
await
|
|
166
|
+
let o = g(r, h(e));
|
|
167
|
+
await i.writeFile(n, o, "utf-8"), v(e.length);
|
|
179
168
|
} catch (e) {
|
|
180
169
|
console.error("❌ Error in aippy preload plugin:", e);
|
|
181
170
|
}
|
|
@@ -183,4 +172,4 @@ function D(e = {}) {
|
|
|
183
172
|
};
|
|
184
173
|
}
|
|
185
174
|
//#endregion
|
|
186
|
-
export {
|
|
175
|
+
export { C as t };
|
|
@@ -1,6 +1,14 @@
|
|
|
1
1
|
import { AssetInfo } from './types';
|
|
2
2
|
/** A flat dictionary is a v1 plan only when schemaVersion is a number and assets is an object. */
|
|
3
3
|
export declare function isSingleFilePlan(value: unknown): boolean;
|
|
4
|
+
/**
|
|
5
|
+
* Mirrors aippy-chat `assetmanifest.ValidReference`: every value the server
|
|
6
|
+
* puts in the inventory as a usable url must be accepted here, including
|
|
7
|
+
* root-relative and relative paths.
|
|
8
|
+
*/
|
|
9
|
+
export declare function isDictionaryResourceURL(value: string): boolean;
|
|
10
|
+
/** First dictionary key for each usable URL. Several keys may share one URL. */
|
|
11
|
+
export declare function dictionaryResourceOwners(flat: Record<string, unknown>): Map<string, string>;
|
|
4
12
|
/**
|
|
5
13
|
* Write the flat dictionary. Existing keys and non-string values stay; scanned
|
|
6
14
|
* resources only add a key when that URL is not already present.
|
|
@@ -1,325 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
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 };
|
|
1
|
+
import { t as e } from "../asset-constants-bI4rQJpw.js";
|
|
2
|
+
export { e as assetConstantsPlugin, e as default };
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import * as t from '@babel/types';
|
|
2
2
|
export declare function isDefaultImport(item: t.ImportDeclaration['specifiers'][number]): boolean;
|
|
3
|
+
/** A string literal used as `assets['key']` on the wrapper's default import. */
|
|
3
4
|
export declare function wrapperKeyGuard(ast: t.File, importSources: Set<string>): (p: any) => boolean;
|
|
4
5
|
export declare function wrapperImportSources(filePath: string, srcDir: string, outputFile: string): Set<string>;
|
|
5
6
|
export declare function withoutWrapperKeys(content: string, filePath: string, srcDir: string, outputFile: string): string;
|