@aippy/vite-plugins 0.2.7-dev.0 โ 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 +157 -120
- package/package.json +1 -1
|
@@ -1,50 +1,50 @@
|
|
|
1
|
-
var
|
|
2
|
-
var
|
|
3
|
-
var
|
|
4
|
-
import { promises as
|
|
5
|
-
import
|
|
6
|
-
import { parse as
|
|
7
|
-
import
|
|
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
|
+
import { promises as g } from "fs";
|
|
5
|
+
import h from "path";
|
|
6
|
+
import { parse as v } from "@babel/parser";
|
|
7
|
+
import y from "@babel/traverse";
|
|
8
8
|
import * as p from "@babel/types";
|
|
9
|
-
import
|
|
10
|
-
import { generateConstantName as
|
|
11
|
-
import { F as
|
|
12
|
-
function
|
|
13
|
-
const
|
|
14
|
-
for (const [
|
|
15
|
-
|
|
16
|
-
return Array.from(i.values()).forEach((
|
|
17
|
-
|
|
18
|
-
}), JSON.stringify(
|
|
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
|
+
const 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
|
+
}), 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
24
|
if (!i.trim())
|
|
25
25
|
return s;
|
|
26
|
-
const
|
|
27
|
-
for (const [
|
|
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
|
-
} catch (
|
|
31
|
-
console.warn("Warning: Failed to parse existing assets.json:",
|
|
30
|
+
} catch (t) {
|
|
31
|
+
console.warn("Warning: Failed to parse existing assets.json:", t);
|
|
32
32
|
}
|
|
33
33
|
return s;
|
|
34
34
|
}
|
|
35
|
-
function R(i, s,
|
|
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
|
|
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",
|
|
@@ -57,71 +57,71 @@ class T {
|
|
|
57
57
|
this.fileCache.clear();
|
|
58
58
|
}
|
|
59
59
|
generateConstantName(s) {
|
|
60
|
-
const
|
|
61
|
-
...Array.from(this.assets.values()).map((
|
|
60
|
+
const t = /* @__PURE__ */ new Set([
|
|
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
|
|
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(
|
|
72
|
-
|
|
73
|
-
originalUri:
|
|
70
|
+
return n.forEach((o) => {
|
|
71
|
+
const r = this.generateConstantName(o), c = a.findIndex((u) => u.includes(o)) + 1;
|
|
72
|
+
e.push({
|
|
73
|
+
originalUri: o,
|
|
74
74
|
constantName: r,
|
|
75
75
|
filePath: s,
|
|
76
76
|
line: c,
|
|
77
77
|
column: 1
|
|
78
78
|
});
|
|
79
|
-
}),
|
|
80
|
-
} catch (
|
|
81
|
-
return console.warn(`Warning: Failed to scan ${s}:`,
|
|
79
|
+
}), e;
|
|
80
|
+
} catch (t) {
|
|
81
|
+
return console.warn(`Warning: Failed to scan ${s}:`, t), [];
|
|
82
82
|
}
|
|
83
83
|
}
|
|
84
84
|
async scanAllTsFiles() {
|
|
85
85
|
const s = this.options.srcDir;
|
|
86
86
|
try {
|
|
87
|
-
const
|
|
88
|
-
for (const
|
|
89
|
-
if (
|
|
87
|
+
const t = await A(s, [".ts", ".tsx"]);
|
|
88
|
+
for (const e of t) {
|
|
89
|
+
if (e.endsWith("assets.json") || e.endsWith("assets.ts"))
|
|
90
90
|
continue;
|
|
91
|
-
(await this.scanTsFile(
|
|
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
|
-
} catch (
|
|
98
|
-
console.error("Error scanning TypeScript files:",
|
|
97
|
+
} catch (t) {
|
|
98
|
+
console.error("Error scanning TypeScript files:", t);
|
|
99
99
|
}
|
|
100
100
|
}
|
|
101
101
|
async readExistingAssetsFile() {
|
|
102
102
|
try {
|
|
103
|
-
return await
|
|
103
|
+
return await g.readFile(this.options.outputFile, "utf-8");
|
|
104
104
|
} catch {
|
|
105
105
|
return "";
|
|
106
106
|
}
|
|
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
|
|
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
116
|
}
|
|
117
117
|
/**
|
|
118
118
|
* ็ๆ assets.ts ๅ
่ฃ
ๆไปถ
|
|
119
119
|
* ่ชๅจ่ฐ็จ window.getReplacedAssets ๅค็ iOS ๅพ็ๆฟๆข
|
|
120
120
|
*/
|
|
121
121
|
async generateAssetsWrapper() {
|
|
122
|
-
const s = this.options.outputFile.replace(/\.json$/, ".ts"),
|
|
122
|
+
const s = this.options.outputFile.replace(/\.json$/, ".ts"), e = `// ๐ Auto-generated wrapper for assets.json
|
|
123
123
|
// This file handles iOS image replacement via asset-interceptor.js
|
|
124
|
-
import assetsData from "./${
|
|
124
|
+
import assetsData from "./${h.basename(this.options.outputFile)}";
|
|
125
125
|
|
|
126
126
|
// Batch process all asset URLs for potential replacements
|
|
127
127
|
// sessionStorage may contain __aippy_asset_overrides__ with multiple image mappings
|
|
@@ -129,103 +129,103 @@ import assetsData from "./${g.basename(this.options.outputFile)}";
|
|
|
129
129
|
|
|
130
130
|
export default assetsData;
|
|
131
131
|
`;
|
|
132
|
-
await
|
|
132
|
+
await g.writeFile(s, e, "utf-8"), console.log(` โ Generated assets wrapper: ${h.relative(process.cwd(), s)}`);
|
|
133
133
|
}
|
|
134
134
|
getAssets() {
|
|
135
135
|
return Array.from(this.assets.values());
|
|
136
136
|
}
|
|
137
|
-
async replaceUrisInFile(s,
|
|
137
|
+
async replaceUrisInFile(s, t) {
|
|
138
138
|
try {
|
|
139
|
-
const
|
|
139
|
+
const e = await this.fileCache.read(s), n = v(e, {
|
|
140
140
|
sourceType: "module",
|
|
141
141
|
plugins: ["typescript", "jsx", "decorators-legacy"]
|
|
142
142
|
});
|
|
143
|
-
let
|
|
144
|
-
if (
|
|
143
|
+
let a = !1;
|
|
144
|
+
if (x(n, {
|
|
145
145
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
146
146
|
StringLiteral: (l) => {
|
|
147
|
-
const
|
|
148
|
-
if (
|
|
149
|
-
const
|
|
147
|
+
const m = l.node.value, C = S(m);
|
|
148
|
+
if (t.has(C)) {
|
|
149
|
+
const N = t.get(C), j = p.memberExpression(
|
|
150
150
|
p.identifier("assetsData"),
|
|
151
|
-
p.identifier(
|
|
151
|
+
p.identifier(N)
|
|
152
152
|
);
|
|
153
|
-
l.parent && p.isJSXAttribute(l.parent) ? l.replaceWith(p.jsxExpressionContainer(
|
|
153
|
+
l.parent && p.isJSXAttribute(l.parent) ? l.replaceWith(p.jsxExpressionContainer(j)) : l.replaceWith(j), a = !0;
|
|
154
154
|
}
|
|
155
155
|
}
|
|
156
|
-
}), !
|
|
156
|
+
}), !a)
|
|
157
157
|
return;
|
|
158
|
-
const
|
|
159
|
-
let
|
|
160
|
-
if (
|
|
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, {
|
|
161
161
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
162
162
|
ImportDeclaration: (l) => {
|
|
163
|
-
const
|
|
164
|
-
(
|
|
163
|
+
const m = l.node.source.value;
|
|
164
|
+
(m === r || m === c) && (f = !0, l.stop());
|
|
165
165
|
}
|
|
166
|
-
}), !
|
|
166
|
+
}), !f) {
|
|
167
167
|
const l = p.importDeclaration(
|
|
168
168
|
[p.importDefaultSpecifier(p.identifier("assetsData"))],
|
|
169
169
|
p.stringLiteral(r)
|
|
170
170
|
);
|
|
171
|
-
|
|
171
|
+
n.program.body.unshift(l);
|
|
172
172
|
}
|
|
173
|
-
const
|
|
173
|
+
const u = $(n, {
|
|
174
174
|
retainLines: !1,
|
|
175
175
|
comments: !0,
|
|
176
176
|
compact: !1
|
|
177
|
-
},
|
|
178
|
-
await
|
|
179
|
-
} catch (
|
|
180
|
-
console.warn(`Warning: Failed to replace URIs in ${s}:`,
|
|
177
|
+
}, e);
|
|
178
|
+
await g.writeFile(s, u.code, "utf-8"), this.fileCache.invalidate(s), console.log(` โ Replaced URIs in ${h.relative(process.cwd(), s)}`);
|
|
179
|
+
} catch (e) {
|
|
180
|
+
console.warn(`Warning: Failed to replace URIs in ${s}:`, e);
|
|
181
181
|
}
|
|
182
182
|
}
|
|
183
183
|
async replaceAllUris() {
|
|
184
|
-
const s = /* @__PURE__ */ new Map(),
|
|
185
|
-
for (const
|
|
186
|
-
s.set(
|
|
187
|
-
for (const
|
|
188
|
-
await this.replaceUrisInFile(
|
|
184
|
+
const s = /* @__PURE__ */ new Map(), t = /* @__PURE__ */ new Set();
|
|
185
|
+
for (const e of this.assets.values())
|
|
186
|
+
s.set(e.originalUri, e.constantName), t.add(e.filePath);
|
|
187
|
+
for (const e of t)
|
|
188
|
+
await this.replaceUrisInFile(e, s);
|
|
189
189
|
}
|
|
190
190
|
async cleanupUnusedConstants() {
|
|
191
191
|
try {
|
|
192
192
|
const s = await this.readExistingAssetsFile();
|
|
193
193
|
if (!s.trim())
|
|
194
194
|
return;
|
|
195
|
-
const
|
|
195
|
+
const t = /* @__PURE__ */ new Map();
|
|
196
196
|
try {
|
|
197
|
-
const
|
|
198
|
-
for (const [r, c] of Object.entries(
|
|
199
|
-
typeof c == "string" &&
|
|
200
|
-
} catch (
|
|
201
|
-
console.warn("Warning: Failed to parse assets.json:",
|
|
197
|
+
const o = JSON.parse(s);
|
|
198
|
+
for (const [r, c] of Object.entries(o))
|
|
199
|
+
typeof c == "string" && t.set(r, c);
|
|
200
|
+
} catch (o) {
|
|
201
|
+
console.warn("Warning: Failed to parse assets.json:", o);
|
|
202
202
|
return;
|
|
203
203
|
}
|
|
204
|
-
if (
|
|
204
|
+
if (t.size === 0)
|
|
205
205
|
return;
|
|
206
|
-
const
|
|
207
|
-
for (const
|
|
208
|
-
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")))
|
|
209
209
|
try {
|
|
210
|
-
const r = await this.fileCache.read(
|
|
211
|
-
for (const c of
|
|
212
|
-
r.includes(c) &&
|
|
210
|
+
const r = await this.fileCache.read(o);
|
|
211
|
+
for (const c of t.keys())
|
|
212
|
+
r.includes(c) && e.add(c);
|
|
213
213
|
} catch (r) {
|
|
214
|
-
console.warn(`Warning: Failed to read ${
|
|
214
|
+
console.warn(`Warning: Failed to read ${o}:`, r);
|
|
215
215
|
}
|
|
216
|
-
const
|
|
217
|
-
for (const
|
|
218
|
-
|
|
219
|
-
if (
|
|
220
|
-
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) => {
|
|
221
221
|
console.log(` - ${r}`);
|
|
222
222
|
});
|
|
223
|
-
const
|
|
224
|
-
for (const r of
|
|
225
|
-
const c =
|
|
226
|
-
c &&
|
|
223
|
+
const o = [];
|
|
224
|
+
for (const r of a) {
|
|
225
|
+
const c = t.get(r);
|
|
226
|
+
c && o.push(c);
|
|
227
227
|
}
|
|
228
|
-
for (const r of
|
|
228
|
+
for (const r of o)
|
|
229
229
|
this.assets.delete(r);
|
|
230
230
|
} else
|
|
231
231
|
console.log("โ
All constants are in use");
|
|
@@ -233,8 +233,44 @@ export default assetsData;
|
|
|
233
233
|
console.warn("Warning: Failed to cleanup unused constants:", s);
|
|
234
234
|
}
|
|
235
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
|
+
}
|
|
236
272
|
}
|
|
237
|
-
function
|
|
273
|
+
function q(i = {}) {
|
|
238
274
|
return {
|
|
239
275
|
name: "vite-plugin-asset-constants",
|
|
240
276
|
apply: "build",
|
|
@@ -243,14 +279,15 @@ function B(i = {}) {
|
|
|
243
279
|
async buildStart() {
|
|
244
280
|
if (i.devMode)
|
|
245
281
|
try {
|
|
246
|
-
const s = new
|
|
282
|
+
const s = new O(i);
|
|
247
283
|
await s.generateAssetsFile();
|
|
248
|
-
const
|
|
249
|
-
|
|
250
|
-
console.log(` ${
|
|
284
|
+
const t = s.getAssets();
|
|
285
|
+
t.length > 0 && (console.log(`๐ฆ Found ${t.length} asset references:`), t.forEach((e) => {
|
|
286
|
+
console.log(` ${e.constantName} = '${e.originalUri}' (${h.relative(process.cwd(), e.filePath)}:${e.line})`);
|
|
251
287
|
}), console.log(`
|
|
252
288
|
๐ Replacing URIs with constants...`), await s.replaceAllUris(), console.log("โ
URI replacement completed!")), console.log(`
|
|
253
|
-
๐งน 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!
|
|
254
291
|
`), s.clearCache();
|
|
255
292
|
} catch (s) {
|
|
256
293
|
console.error("โ Error in asset constants plugin:", s);
|
|
@@ -259,6 +296,6 @@ function B(i = {}) {
|
|
|
259
296
|
};
|
|
260
297
|
}
|
|
261
298
|
export {
|
|
262
|
-
|
|
263
|
-
|
|
299
|
+
q as assetConstantsPlugin,
|
|
300
|
+
q as default
|
|
264
301
|
};
|