@aippy/vite-plugins 0.2.6 โ 0.2.7-dev.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/asset-constants/index.js +118 -100
- 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
|
|
8
|
-
import * as
|
|
9
|
-
import
|
|
10
|
-
import { generateConstantName as
|
|
11
|
-
import { F as
|
|
12
|
-
function
|
|
13
|
-
const
|
|
14
|
-
for (const [
|
|
15
|
-
|
|
16
|
-
return Array.from(
|
|
17
|
-
|
|
18
|
-
}), JSON.stringify(
|
|
1
|
+
var N = Object.defineProperty;
|
|
2
|
+
var $ = (i, s, e) => s in i ? N(i, s, { enumerable: !0, configurable: !0, writable: !0, value: e }) : i[s] = e;
|
|
3
|
+
var u = (i, s, e) => $(i, typeof s != "symbol" ? s + "" : s, e);
|
|
4
|
+
import { promises as f } from "fs";
|
|
5
|
+
import g from "path";
|
|
6
|
+
import { parse as U } from "@babel/parser";
|
|
7
|
+
import w from "@babel/traverse";
|
|
8
|
+
import * as p from "@babel/types";
|
|
9
|
+
import y from "@babel/generator";
|
|
10
|
+
import { generateConstantName as E, extractAssetsFromContent as D, processAssetURL as S } from "../utils/index.js";
|
|
11
|
+
import { F as b, g as x, a as C } from "../file-scanner-DHkDSQm8.js";
|
|
12
|
+
function W(i, s) {
|
|
13
|
+
const e = {};
|
|
14
|
+
for (const [a, o] of s.entries())
|
|
15
|
+
e[o] = a;
|
|
16
|
+
return Array.from(i.values()).forEach((a) => {
|
|
17
|
+
e[a.constantName] = a.originalUri;
|
|
18
|
+
}), JSON.stringify(e, null, 2) + `
|
|
19
19
|
`;
|
|
20
20
|
}
|
|
21
|
-
function I(
|
|
21
|
+
function I(i) {
|
|
22
22
|
const s = /* @__PURE__ */ new Map();
|
|
23
23
|
try {
|
|
24
|
-
if (!
|
|
24
|
+
if (!i.trim())
|
|
25
25
|
return s;
|
|
26
|
-
const
|
|
27
|
-
for (const [
|
|
28
|
-
typeof
|
|
26
|
+
const e = JSON.parse(i);
|
|
27
|
+
for (const [t, a] of Object.entries(e))
|
|
28
|
+
typeof a == "string" && s.set(a, t);
|
|
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 (e) {
|
|
31
|
+
console.warn("Warning: Failed to parse existing assets.json:", e);
|
|
32
32
|
}
|
|
33
33
|
return s;
|
|
34
34
|
}
|
|
35
|
-
function
|
|
36
|
-
const
|
|
35
|
+
function R(i, s, e, t) {
|
|
36
|
+
const a = Array.from(e.values()).filter(
|
|
37
37
|
(n) => s.has(n.originalUri)
|
|
38
|
-
).length, o =
|
|
39
|
-
o > 0 ? console.log(`โ
Generated ${
|
|
38
|
+
).length, o = i - a;
|
|
39
|
+
o > 0 ? console.log(`โ
Generated ${i} asset constants (${o} new, ${a} reused) in ${t}`) : i > 0 ? console.log(`โ
Generated ${i} asset constants (all reused from existing) in ${t}`) : console.log("โ
No asset constants found");
|
|
40
40
|
}
|
|
41
|
-
const
|
|
42
|
-
class
|
|
41
|
+
const v = typeof w == "function" ? w : w.default, O = typeof y == "function" ? y : y.default;
|
|
42
|
+
class T {
|
|
43
43
|
constructor(s = {}) {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
44
|
+
u(this, "options");
|
|
45
|
+
u(this, "assets", /* @__PURE__ */ new Map());
|
|
46
|
+
u(this, "existingAssets", /* @__PURE__ */ new Map());
|
|
47
|
+
u(this, "fileCache", new b());
|
|
48
48
|
this.options = {
|
|
49
49
|
extensions: [".png", ".jpg", ".jpeg", ".gif", ".svg", ".mp4", ".mp3", ".wav", ".ogg", ".webm"],
|
|
50
50
|
srcDir: "src",
|
|
@@ -57,50 +57,50 @@ class R {
|
|
|
57
57
|
this.fileCache.clear();
|
|
58
58
|
}
|
|
59
59
|
generateConstantName(s) {
|
|
60
|
-
const
|
|
61
|
-
...Array.from(this.assets.values()).map((
|
|
60
|
+
const e = /* @__PURE__ */ new Set([
|
|
61
|
+
...Array.from(this.assets.values()).map((t) => t.constantName),
|
|
62
62
|
...Array.from(this.existingAssets.values())
|
|
63
63
|
]);
|
|
64
|
-
return
|
|
64
|
+
return E(s, e);
|
|
65
65
|
}
|
|
66
66
|
async scanTsFile(s) {
|
|
67
67
|
try {
|
|
68
|
-
const
|
|
68
|
+
const e = await this.fileCache.read(s), t = [], a = D(e, "js", this.options.extensions), o = e.split(`
|
|
69
69
|
`);
|
|
70
|
-
return
|
|
71
|
-
const r = this.generateConstantName(n), c = o.findIndex((
|
|
72
|
-
|
|
70
|
+
return a.forEach((n) => {
|
|
71
|
+
const r = this.generateConstantName(n), c = o.findIndex((d) => d.includes(n)) + 1;
|
|
72
|
+
t.push({
|
|
73
73
|
originalUri: n,
|
|
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
|
+
}), t;
|
|
80
|
+
} catch (e) {
|
|
81
|
+
return console.warn(`Warning: Failed to scan ${s}:`, e), [];
|
|
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 e = await x(s, [".ts", ".tsx"]);
|
|
88
|
+
for (const t of e) {
|
|
89
|
+
if (t.endsWith("assets.json") || t.endsWith("assets.ts"))
|
|
90
90
|
continue;
|
|
91
|
-
(await this.scanTsFile(
|
|
91
|
+
(await this.scanTsFile(t)).forEach((o) => {
|
|
92
92
|
this.existingAssets.has(o.originalUri) && (o.constantName = this.existingAssets.get(o.originalUri));
|
|
93
93
|
const n = o.originalUri;
|
|
94
94
|
this.assets.has(n) || this.assets.set(n, o);
|
|
95
95
|
});
|
|
96
96
|
}
|
|
97
|
-
} catch (
|
|
98
|
-
console.error("Error scanning TypeScript files:",
|
|
97
|
+
} catch (e) {
|
|
98
|
+
console.error("Error scanning TypeScript files:", e);
|
|
99
99
|
}
|
|
100
100
|
}
|
|
101
101
|
async readExistingAssetsFile() {
|
|
102
102
|
try {
|
|
103
|
-
return await
|
|
103
|
+
return await f.readFile(this.options.outputFile, "utf-8");
|
|
104
104
|
} catch {
|
|
105
105
|
return "";
|
|
106
106
|
}
|
|
@@ -111,100 +111,118 @@ class R {
|
|
|
111
111
|
}
|
|
112
112
|
async generateAssetsFile() {
|
|
113
113
|
await this.parseExistingAssets(), await this.scanAllTsFiles();
|
|
114
|
-
const s =
|
|
115
|
-
await
|
|
114
|
+
const s = W(this.assets, this.existingAssets), e = g.dirname(this.options.outputFile);
|
|
115
|
+
await f.mkdir(e, { recursive: !0 }), await f.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"), t = `// ๐ Auto-generated wrapper for assets.json
|
|
123
|
+
// This file handles iOS image replacement via asset-interceptor.js
|
|
124
|
+
import assetsData from "./${g.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 f.writeFile(s, t, "utf-8"), console.log(` โ Generated assets wrapper: ${g.relative(process.cwd(), s)}`);
|
|
116
133
|
}
|
|
117
134
|
getAssets() {
|
|
118
135
|
return Array.from(this.assets.values());
|
|
119
136
|
}
|
|
120
|
-
async replaceUrisInFile(s,
|
|
137
|
+
async replaceUrisInFile(s, e) {
|
|
121
138
|
try {
|
|
122
|
-
const
|
|
139
|
+
const t = await this.fileCache.read(s), a = U(t, {
|
|
123
140
|
sourceType: "module",
|
|
124
141
|
plugins: ["typescript", "jsx", "decorators-legacy"]
|
|
125
142
|
});
|
|
126
143
|
let o = !1;
|
|
127
|
-
if (
|
|
144
|
+
if (v(a, {
|
|
128
145
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
129
146
|
StringLiteral: (l) => {
|
|
130
|
-
const
|
|
131
|
-
if (
|
|
132
|
-
const
|
|
133
|
-
|
|
134
|
-
|
|
147
|
+
const h = l.node.value, A = S(h);
|
|
148
|
+
if (e.has(A)) {
|
|
149
|
+
const j = e.get(A), F = p.memberExpression(
|
|
150
|
+
p.identifier("assetsData"),
|
|
151
|
+
p.identifier(j)
|
|
135
152
|
);
|
|
136
|
-
l.parent &&
|
|
153
|
+
l.parent && p.isJSXAttribute(l.parent) ? l.replaceWith(p.jsxExpressionContainer(F)) : l.replaceWith(F), o = !0;
|
|
137
154
|
}
|
|
138
155
|
}
|
|
139
156
|
}), !o)
|
|
140
157
|
return;
|
|
141
|
-
const n =
|
|
142
|
-
let
|
|
143
|
-
if (
|
|
158
|
+
const n = this.options.outputFile.replace(/\.json$/, ".ts"), r = C(s, n), c = C(s, this.options.outputFile) + ".json";
|
|
159
|
+
let m = !1;
|
|
160
|
+
if (v(a, {
|
|
144
161
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
145
162
|
ImportDeclaration: (l) => {
|
|
146
|
-
l.node.source.value
|
|
163
|
+
const h = l.node.source.value;
|
|
164
|
+
(h === r || h === c) && (m = !0, l.stop());
|
|
147
165
|
}
|
|
148
|
-
}), !
|
|
149
|
-
const l =
|
|
150
|
-
[
|
|
151
|
-
|
|
166
|
+
}), !m) {
|
|
167
|
+
const l = p.importDeclaration(
|
|
168
|
+
[p.importDefaultSpecifier(p.identifier("assetsData"))],
|
|
169
|
+
p.stringLiteral(r)
|
|
152
170
|
);
|
|
153
|
-
|
|
171
|
+
a.program.body.unshift(l);
|
|
154
172
|
}
|
|
155
|
-
const
|
|
173
|
+
const d = O(a, {
|
|
156
174
|
retainLines: !1,
|
|
157
175
|
comments: !0,
|
|
158
176
|
compact: !1
|
|
159
|
-
},
|
|
160
|
-
await
|
|
161
|
-
} catch (
|
|
162
|
-
console.warn(`Warning: Failed to replace URIs in ${s}:`,
|
|
177
|
+
}, t);
|
|
178
|
+
await f.writeFile(s, d.code, "utf-8"), this.fileCache.invalidate(s), console.log(` โ Replaced URIs in ${g.relative(process.cwd(), s)}`);
|
|
179
|
+
} catch (t) {
|
|
180
|
+
console.warn(`Warning: Failed to replace URIs in ${s}:`, t);
|
|
163
181
|
}
|
|
164
182
|
}
|
|
165
183
|
async replaceAllUris() {
|
|
166
|
-
const s = /* @__PURE__ */ new Map(),
|
|
167
|
-
for (const
|
|
168
|
-
s.set(
|
|
169
|
-
for (const
|
|
170
|
-
await this.replaceUrisInFile(
|
|
184
|
+
const s = /* @__PURE__ */ new Map(), e = /* @__PURE__ */ new Set();
|
|
185
|
+
for (const t of this.assets.values())
|
|
186
|
+
s.set(t.originalUri, t.constantName), e.add(t.filePath);
|
|
187
|
+
for (const t of e)
|
|
188
|
+
await this.replaceUrisInFile(t, s);
|
|
171
189
|
}
|
|
172
190
|
async cleanupUnusedConstants() {
|
|
173
191
|
try {
|
|
174
192
|
const s = await this.readExistingAssetsFile();
|
|
175
193
|
if (!s.trim())
|
|
176
194
|
return;
|
|
177
|
-
const
|
|
195
|
+
const e = /* @__PURE__ */ new Map();
|
|
178
196
|
try {
|
|
179
197
|
const n = JSON.parse(s);
|
|
180
198
|
for (const [r, c] of Object.entries(n))
|
|
181
|
-
typeof c == "string" &&
|
|
199
|
+
typeof c == "string" && e.set(r, c);
|
|
182
200
|
} catch (n) {
|
|
183
201
|
console.warn("Warning: Failed to parse assets.json:", n);
|
|
184
202
|
return;
|
|
185
203
|
}
|
|
186
|
-
if (
|
|
204
|
+
if (e.size === 0)
|
|
187
205
|
return;
|
|
188
|
-
const
|
|
189
|
-
for (const n of
|
|
206
|
+
const t = /* @__PURE__ */ new Set(), a = await x(this.options.srcDir, [".ts", ".tsx"]);
|
|
207
|
+
for (const n of a)
|
|
190
208
|
if (!(n.endsWith("assets.json") || n.endsWith("assets.ts")))
|
|
191
209
|
try {
|
|
192
210
|
const r = await this.fileCache.read(n);
|
|
193
|
-
for (const c of
|
|
194
|
-
r.includes(c) &&
|
|
211
|
+
for (const c of e.keys())
|
|
212
|
+
r.includes(c) && t.add(c);
|
|
195
213
|
} catch (r) {
|
|
196
214
|
console.warn(`Warning: Failed to read ${n}:`, r);
|
|
197
215
|
}
|
|
198
216
|
const o = [];
|
|
199
|
-
for (const n of
|
|
200
|
-
|
|
217
|
+
for (const n of e.keys())
|
|
218
|
+
t.has(n) || o.push(n);
|
|
201
219
|
if (o.length > 0) {
|
|
202
220
|
console.log(`๐งน Removing ${o.length} unused constant(s):`), o.forEach((r) => {
|
|
203
221
|
console.log(` - ${r}`);
|
|
204
222
|
});
|
|
205
223
|
const n = [];
|
|
206
224
|
for (const r of o) {
|
|
207
|
-
const c =
|
|
225
|
+
const c = e.get(r);
|
|
208
226
|
c && n.push(c);
|
|
209
227
|
}
|
|
210
228
|
for (const r of n)
|
|
@@ -216,20 +234,20 @@ class R {
|
|
|
216
234
|
}
|
|
217
235
|
}
|
|
218
236
|
}
|
|
219
|
-
function
|
|
237
|
+
function B(i = {}) {
|
|
220
238
|
return {
|
|
221
239
|
name: "vite-plugin-asset-constants",
|
|
222
240
|
apply: "build",
|
|
223
241
|
// Ensure this plugin runs before other plugins to modify source files before build
|
|
224
242
|
enforce: "pre",
|
|
225
243
|
async buildStart() {
|
|
226
|
-
if (
|
|
244
|
+
if (i.devMode)
|
|
227
245
|
try {
|
|
228
|
-
const s = new
|
|
246
|
+
const s = new T(i);
|
|
229
247
|
await s.generateAssetsFile();
|
|
230
|
-
const
|
|
231
|
-
|
|
232
|
-
console.log(` ${
|
|
248
|
+
const e = s.getAssets();
|
|
249
|
+
e.length > 0 && (console.log(`๐ฆ Found ${e.length} asset references:`), e.forEach((t) => {
|
|
250
|
+
console.log(` ${t.constantName} = '${t.originalUri}' (${g.relative(process.cwd(), t.filePath)}:${t.line})`);
|
|
233
251
|
}), console.log(`
|
|
234
252
|
๐ Replacing URIs with constants...`), await s.replaceAllUris(), console.log("โ
URI replacement completed!")), console.log(`
|
|
235
253
|
๐งน Checking for unused constants...`), await s.cleanupUnusedConstants(), await s.generateAssetsFile(), console.log(`โ
Asset constants finalized!
|
|
@@ -241,6 +259,6 @@ function q(a = {}) {
|
|
|
241
259
|
};
|
|
242
260
|
}
|
|
243
261
|
export {
|
|
244
|
-
|
|
245
|
-
|
|
262
|
+
B as assetConstantsPlugin,
|
|
263
|
+
B as default
|
|
246
264
|
};
|