@aippy/vite-plugins 0.2.6 โ 0.2.7-dev.1
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/dist/asset-constants/index.js +142 -87
- package/package.json +1 -1
|
@@ -1,50 +1,50 @@
|
|
|
1
|
-
var
|
|
2
|
-
var
|
|
3
|
-
var
|
|
1
|
+
var U = Object.defineProperty;
|
|
2
|
+
var I = (i, s, t) => s in i ? U(i, s, { enumerable: !0, configurable: !0, writable: !0, value: t }) : i[s] = t;
|
|
3
|
+
var d = (i, s, t) => I(i, typeof s != "symbol" ? s + "" : s, t);
|
|
4
4
|
import { promises as g } from "fs";
|
|
5
|
-
import
|
|
6
|
-
import { parse as
|
|
7
|
-
import
|
|
8
|
-
import * as
|
|
9
|
-
import
|
|
10
|
-
import { generateConstantName as
|
|
11
|
-
import { F as
|
|
12
|
-
function
|
|
5
|
+
import h from "path";
|
|
6
|
+
import { parse as v } from "@babel/parser";
|
|
7
|
+
import y from "@babel/traverse";
|
|
8
|
+
import * as p from "@babel/types";
|
|
9
|
+
import F from "@babel/generator";
|
|
10
|
+
import { generateConstantName as D, extractAssetsFromContent as E, processAssetURL as S } from "../utils/index.js";
|
|
11
|
+
import { F as W, g as A, a as w } from "../file-scanner-DHkDSQm8.js";
|
|
12
|
+
function b(i, s) {
|
|
13
13
|
const t = {};
|
|
14
|
-
for (const [
|
|
15
|
-
t[
|
|
16
|
-
return Array.from(
|
|
17
|
-
t[
|
|
14
|
+
for (const [n, a] of s.entries())
|
|
15
|
+
t[a] = n;
|
|
16
|
+
return Array.from(i.values()).forEach((n) => {
|
|
17
|
+
t[n.constantName] = n.originalUri;
|
|
18
18
|
}), JSON.stringify(t, null, 2) + `
|
|
19
19
|
`;
|
|
20
20
|
}
|
|
21
|
-
function
|
|
21
|
+
function T(i) {
|
|
22
22
|
const s = /* @__PURE__ */ new Map();
|
|
23
23
|
try {
|
|
24
|
-
if (!
|
|
24
|
+
if (!i.trim())
|
|
25
25
|
return s;
|
|
26
|
-
const t = JSON.parse(
|
|
27
|
-
for (const [e,
|
|
28
|
-
typeof
|
|
26
|
+
const t = JSON.parse(i);
|
|
27
|
+
for (const [e, n] of Object.entries(t))
|
|
28
|
+
typeof n == "string" && s.set(n, e);
|
|
29
29
|
s.size > 0 && console.log(`๐ Found ${s.size} existing constants in assets.json`);
|
|
30
30
|
} catch (t) {
|
|
31
31
|
console.warn("Warning: Failed to parse existing assets.json:", t);
|
|
32
32
|
}
|
|
33
33
|
return s;
|
|
34
34
|
}
|
|
35
|
-
function
|
|
36
|
-
const
|
|
37
|
-
(
|
|
38
|
-
).length,
|
|
39
|
-
|
|
35
|
+
function R(i, s, t, e) {
|
|
36
|
+
const n = Array.from(t.values()).filter(
|
|
37
|
+
(o) => s.has(o.originalUri)
|
|
38
|
+
).length, a = i - n;
|
|
39
|
+
a > 0 ? console.log(`โ
Generated ${i} asset constants (${a} new, ${n} reused) in ${e}`) : i > 0 ? console.log(`โ
Generated ${i} asset constants (all reused from existing) in ${e}`) : console.log("โ
No asset constants found");
|
|
40
40
|
}
|
|
41
|
-
const x = typeof
|
|
42
|
-
class
|
|
41
|
+
const x = typeof y == "function" ? y : y.default, $ = typeof F == "function" ? F : F.default;
|
|
42
|
+
class O {
|
|
43
43
|
constructor(s = {}) {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
44
|
+
d(this, "options");
|
|
45
|
+
d(this, "assets", /* @__PURE__ */ new Map());
|
|
46
|
+
d(this, "existingAssets", /* @__PURE__ */ new Map());
|
|
47
|
+
d(this, "fileCache", new W());
|
|
48
48
|
this.options = {
|
|
49
49
|
extensions: [".png", ".jpg", ".jpeg", ".gif", ".svg", ".mp4", ".mp3", ".wav", ".ogg", ".webm"],
|
|
50
50
|
srcDir: "src",
|
|
@@ -61,16 +61,16 @@ class R {
|
|
|
61
61
|
...Array.from(this.assets.values()).map((e) => e.constantName),
|
|
62
62
|
...Array.from(this.existingAssets.values())
|
|
63
63
|
]);
|
|
64
|
-
return
|
|
64
|
+
return D(s, t);
|
|
65
65
|
}
|
|
66
66
|
async scanTsFile(s) {
|
|
67
67
|
try {
|
|
68
|
-
const t = await this.fileCache.read(s), e = [],
|
|
68
|
+
const t = await this.fileCache.read(s), e = [], n = E(t, "js", this.options.extensions), a = t.split(`
|
|
69
69
|
`);
|
|
70
|
-
return
|
|
71
|
-
const r = this.generateConstantName(
|
|
70
|
+
return n.forEach((o) => {
|
|
71
|
+
const r = this.generateConstantName(o), c = a.findIndex((u) => u.includes(o)) + 1;
|
|
72
72
|
e.push({
|
|
73
|
-
originalUri:
|
|
73
|
+
originalUri: o,
|
|
74
74
|
constantName: r,
|
|
75
75
|
filePath: s,
|
|
76
76
|
line: c,
|
|
@@ -88,10 +88,10 @@ class R {
|
|
|
88
88
|
for (const e of t) {
|
|
89
89
|
if (e.endsWith("assets.json") || e.endsWith("assets.ts"))
|
|
90
90
|
continue;
|
|
91
|
-
(await this.scanTsFile(e)).forEach((
|
|
92
|
-
this.existingAssets.has(
|
|
93
|
-
const
|
|
94
|
-
this.assets.has(
|
|
91
|
+
(await this.scanTsFile(e)).forEach((a) => {
|
|
92
|
+
this.existingAssets.has(a.originalUri) && (a.constantName = this.existingAssets.get(a.originalUri));
|
|
93
|
+
const o = a.originalUri;
|
|
94
|
+
this.assets.has(o) || this.assets.set(o, a);
|
|
95
95
|
});
|
|
96
96
|
}
|
|
97
97
|
} catch (t) {
|
|
@@ -107,57 +107,75 @@ class R {
|
|
|
107
107
|
}
|
|
108
108
|
async parseExistingAssets() {
|
|
109
109
|
const s = await this.readExistingAssetsFile();
|
|
110
|
-
this.existingAssets =
|
|
110
|
+
this.existingAssets = T(s);
|
|
111
111
|
}
|
|
112
112
|
async generateAssetsFile() {
|
|
113
113
|
await this.parseExistingAssets(), await this.scanAllTsFiles();
|
|
114
|
-
const s =
|
|
115
|
-
await g.mkdir(t, { recursive: !0 }), await g.writeFile(this.options.outputFile, s, "utf-8"),
|
|
114
|
+
const s = b(this.assets, this.existingAssets), t = h.dirname(this.options.outputFile);
|
|
115
|
+
await g.mkdir(t, { recursive: !0 }), await g.writeFile(this.options.outputFile, s, "utf-8"), await this.generateAssetsWrapper(), R(this.assets.size, this.existingAssets, this.assets, this.options.outputFile);
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* ็ๆ assets.ts ๅ
่ฃ
ๆไปถ
|
|
119
|
+
* ่ชๅจ่ฐ็จ window.getReplacedAssets ๅค็ iOS ๅพ็ๆฟๆข
|
|
120
|
+
*/
|
|
121
|
+
async generateAssetsWrapper() {
|
|
122
|
+
const s = this.options.outputFile.replace(/\.json$/, ".ts"), e = `// ๐ Auto-generated wrapper for assets.json
|
|
123
|
+
// This file handles iOS image replacement via asset-interceptor.js
|
|
124
|
+
import assetsData from "./${h.basename(this.options.outputFile)}";
|
|
125
|
+
|
|
126
|
+
// Batch process all asset URLs for potential replacements
|
|
127
|
+
// sessionStorage may contain __aippy_asset_overrides__ with multiple image mappings
|
|
128
|
+
(window as any).getReplacedAssets?.(assetsData);
|
|
129
|
+
|
|
130
|
+
export default assetsData;
|
|
131
|
+
`;
|
|
132
|
+
await g.writeFile(s, e, "utf-8"), console.log(` โ Generated assets wrapper: ${h.relative(process.cwd(), s)}`);
|
|
116
133
|
}
|
|
117
134
|
getAssets() {
|
|
118
135
|
return Array.from(this.assets.values());
|
|
119
136
|
}
|
|
120
137
|
async replaceUrisInFile(s, t) {
|
|
121
138
|
try {
|
|
122
|
-
const e = await this.fileCache.read(s),
|
|
139
|
+
const e = await this.fileCache.read(s), n = v(e, {
|
|
123
140
|
sourceType: "module",
|
|
124
141
|
plugins: ["typescript", "jsx", "decorators-legacy"]
|
|
125
142
|
});
|
|
126
|
-
let
|
|
127
|
-
if (x(
|
|
143
|
+
let a = !1;
|
|
144
|
+
if (x(n, {
|
|
128
145
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
129
146
|
StringLiteral: (l) => {
|
|
130
|
-
const
|
|
131
|
-
if (t.has(
|
|
132
|
-
const
|
|
133
|
-
|
|
134
|
-
|
|
147
|
+
const m = l.node.value, C = S(m);
|
|
148
|
+
if (t.has(C)) {
|
|
149
|
+
const N = t.get(C), j = p.memberExpression(
|
|
150
|
+
p.identifier("assetsData"),
|
|
151
|
+
p.identifier(N)
|
|
135
152
|
);
|
|
136
|
-
l.parent &&
|
|
153
|
+
l.parent && p.isJSXAttribute(l.parent) ? l.replaceWith(p.jsxExpressionContainer(j)) : l.replaceWith(j), a = !0;
|
|
137
154
|
}
|
|
138
155
|
}
|
|
139
|
-
}), !
|
|
156
|
+
}), !a)
|
|
140
157
|
return;
|
|
141
|
-
const
|
|
142
|
-
let
|
|
143
|
-
if (x(
|
|
158
|
+
const o = this.options.outputFile.replace(/\.json$/, ".ts"), r = w(s, o), c = w(s, this.options.outputFile) + ".json";
|
|
159
|
+
let f = !1;
|
|
160
|
+
if (x(n, {
|
|
144
161
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
145
162
|
ImportDeclaration: (l) => {
|
|
146
|
-
l.node.source.value
|
|
163
|
+
const m = l.node.source.value;
|
|
164
|
+
(m === r || m === c) && (f = !0, l.stop());
|
|
147
165
|
}
|
|
148
|
-
}), !
|
|
149
|
-
const l =
|
|
150
|
-
[
|
|
151
|
-
|
|
166
|
+
}), !f) {
|
|
167
|
+
const l = p.importDeclaration(
|
|
168
|
+
[p.importDefaultSpecifier(p.identifier("assetsData"))],
|
|
169
|
+
p.stringLiteral(r)
|
|
152
170
|
);
|
|
153
|
-
|
|
171
|
+
n.program.body.unshift(l);
|
|
154
172
|
}
|
|
155
|
-
const
|
|
173
|
+
const u = $(n, {
|
|
156
174
|
retainLines: !1,
|
|
157
175
|
comments: !0,
|
|
158
176
|
compact: !1
|
|
159
177
|
}, e);
|
|
160
|
-
await g.writeFile(s,
|
|
178
|
+
await g.writeFile(s, u.code, "utf-8"), this.fileCache.invalidate(s), console.log(` โ Replaced URIs in ${h.relative(process.cwd(), s)}`);
|
|
161
179
|
} catch (e) {
|
|
162
180
|
console.warn(`Warning: Failed to replace URIs in ${s}:`, e);
|
|
163
181
|
}
|
|
@@ -176,38 +194,38 @@ class R {
|
|
|
176
194
|
return;
|
|
177
195
|
const t = /* @__PURE__ */ new Map();
|
|
178
196
|
try {
|
|
179
|
-
const
|
|
180
|
-
for (const [r, c] of Object.entries(
|
|
197
|
+
const o = JSON.parse(s);
|
|
198
|
+
for (const [r, c] of Object.entries(o))
|
|
181
199
|
typeof c == "string" && t.set(r, c);
|
|
182
|
-
} catch (
|
|
183
|
-
console.warn("Warning: Failed to parse assets.json:",
|
|
200
|
+
} catch (o) {
|
|
201
|
+
console.warn("Warning: Failed to parse assets.json:", o);
|
|
184
202
|
return;
|
|
185
203
|
}
|
|
186
204
|
if (t.size === 0)
|
|
187
205
|
return;
|
|
188
|
-
const e = /* @__PURE__ */ new Set(),
|
|
189
|
-
for (const
|
|
190
|
-
if (!(
|
|
206
|
+
const e = /* @__PURE__ */ new Set(), n = await A(this.options.srcDir, [".ts", ".tsx"]);
|
|
207
|
+
for (const o of n)
|
|
208
|
+
if (!(o.endsWith("assets.json") || o.endsWith("assets.ts")))
|
|
191
209
|
try {
|
|
192
|
-
const r = await this.fileCache.read(
|
|
210
|
+
const r = await this.fileCache.read(o);
|
|
193
211
|
for (const c of t.keys())
|
|
194
212
|
r.includes(c) && e.add(c);
|
|
195
213
|
} catch (r) {
|
|
196
|
-
console.warn(`Warning: Failed to read ${
|
|
214
|
+
console.warn(`Warning: Failed to read ${o}:`, r);
|
|
197
215
|
}
|
|
198
|
-
const
|
|
199
|
-
for (const
|
|
200
|
-
e.has(
|
|
201
|
-
if (
|
|
202
|
-
console.log(`๐งน Removing ${
|
|
216
|
+
const a = [];
|
|
217
|
+
for (const o of t.keys())
|
|
218
|
+
e.has(o) || a.push(o);
|
|
219
|
+
if (a.length > 0) {
|
|
220
|
+
console.log(`๐งน Removing ${a.length} unused constant(s):`), a.forEach((r) => {
|
|
203
221
|
console.log(` - ${r}`);
|
|
204
222
|
});
|
|
205
|
-
const
|
|
206
|
-
for (const r of
|
|
223
|
+
const o = [];
|
|
224
|
+
for (const r of a) {
|
|
207
225
|
const c = t.get(r);
|
|
208
|
-
c &&
|
|
226
|
+
c && o.push(c);
|
|
209
227
|
}
|
|
210
|
-
for (const r of
|
|
228
|
+
for (const r of o)
|
|
211
229
|
this.assets.delete(r);
|
|
212
230
|
} else
|
|
213
231
|
console.log("โ
All constants are in use");
|
|
@@ -215,24 +233,61 @@ class R {
|
|
|
215
233
|
console.warn("Warning: Failed to cleanup unused constants:", s);
|
|
216
234
|
}
|
|
217
235
|
}
|
|
236
|
+
/**
|
|
237
|
+
* ๐ ๅ็บง็ฐๆ็ .json ๅฏผๅ
ฅไธบ .ts ๅ
่ฃ
ๆไปถๅฏผๅ
ฅ
|
|
238
|
+
* ่งฃๅณๆง้กน็ฎไธญๅทฒ็ปไฝฟ็จ assets.json ็ๆ
ๅต
|
|
239
|
+
*/
|
|
240
|
+
async upgradeJsonImportsToTs() {
|
|
241
|
+
const s = this.options.outputFile.replace(/\.json$/, ".ts"), t = await A(this.options.srcDir, [".ts", ".tsx"]);
|
|
242
|
+
let e = 0;
|
|
243
|
+
for (const n of t)
|
|
244
|
+
if (!(n.endsWith("assets.ts") || n.endsWith("assets.json")))
|
|
245
|
+
try {
|
|
246
|
+
const a = await this.fileCache.read(n), o = w(n, this.options.outputFile) + ".json", r = w(n, s);
|
|
247
|
+
if (!a.includes(o))
|
|
248
|
+
continue;
|
|
249
|
+
const c = v(a, {
|
|
250
|
+
sourceType: "module",
|
|
251
|
+
plugins: ["typescript", "jsx", "decorators-legacy"]
|
|
252
|
+
});
|
|
253
|
+
let f = !1;
|
|
254
|
+
if (x(c, {
|
|
255
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
256
|
+
ImportDeclaration: (u) => {
|
|
257
|
+
u.node.source.value === o && (u.node.source.value = r, f = !0);
|
|
258
|
+
}
|
|
259
|
+
}), f) {
|
|
260
|
+
const u = $(c, {
|
|
261
|
+
retainLines: !1,
|
|
262
|
+
comments: !0,
|
|
263
|
+
compact: !1
|
|
264
|
+
}, a);
|
|
265
|
+
await g.writeFile(n, u.code, "utf-8"), this.fileCache.invalidate(n), console.log(` โ Upgraded import in ${h.relative(process.cwd(), n)}: ${o} โ ${r}`), e++;
|
|
266
|
+
}
|
|
267
|
+
} catch (a) {
|
|
268
|
+
console.warn(`Warning: Failed to upgrade imports in ${n}:`, a);
|
|
269
|
+
}
|
|
270
|
+
e > 0 && console.log(`โ
Upgraded ${e} file(s) to use assets.ts wrapper`);
|
|
271
|
+
}
|
|
218
272
|
}
|
|
219
|
-
function q(
|
|
273
|
+
function q(i = {}) {
|
|
220
274
|
return {
|
|
221
275
|
name: "vite-plugin-asset-constants",
|
|
222
276
|
apply: "build",
|
|
223
277
|
// Ensure this plugin runs before other plugins to modify source files before build
|
|
224
278
|
enforce: "pre",
|
|
225
279
|
async buildStart() {
|
|
226
|
-
if (
|
|
280
|
+
if (i.devMode)
|
|
227
281
|
try {
|
|
228
|
-
const s = new
|
|
282
|
+
const s = new O(i);
|
|
229
283
|
await s.generateAssetsFile();
|
|
230
284
|
const t = s.getAssets();
|
|
231
285
|
t.length > 0 && (console.log(`๐ฆ Found ${t.length} asset references:`), t.forEach((e) => {
|
|
232
|
-
console.log(` ${e.constantName} = '${e.originalUri}' (${
|
|
286
|
+
console.log(` ${e.constantName} = '${e.originalUri}' (${h.relative(process.cwd(), e.filePath)}:${e.line})`);
|
|
233
287
|
}), console.log(`
|
|
234
288
|
๐ Replacing URIs with constants...`), await s.replaceAllUris(), console.log("โ
URI replacement completed!")), console.log(`
|
|
235
|
-
๐งน Checking for unused constants...`), await s.cleanupUnusedConstants(), await s.generateAssetsFile(), console.log(
|
|
289
|
+
๐งน Checking for unused constants...`), await s.cleanupUnusedConstants(), await s.generateAssetsFile(), console.log(`
|
|
290
|
+
๐ Upgrading assets.json imports to assets.ts...`), await s.upgradeJsonImportsToTs(), console.log(`โ
Asset constants finalized!
|
|
236
291
|
`), s.clearCache();
|
|
237
292
|
} catch (s) {
|
|
238
293
|
console.error("โ Error in asset constants plugin:", s);
|