@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.
@@ -1,50 +1,50 @@
1
- var C = Object.defineProperty;
2
- var v = (a, s, t) => s in a ? C(a, s, { enumerable: !0, configurable: !0, writable: !0, value: t }) : a[s] = t;
3
- var f = (a, s, t) => v(a, typeof s != "symbol" ? s + "" : s, t);
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 d from "path";
6
- import { parse as N } from "@babel/parser";
7
- import h from "@babel/traverse";
8
- import * as u from "@babel/types";
9
- import m from "@babel/generator";
10
- import { generateConstantName as U, extractAssetsFromContent as $, processAssetURL as j } from "../utils/index.js";
11
- import { F as E, g as A, a as b } from "../file-scanner-DHkDSQm8.js";
12
- function D(a, s) {
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 [i, o] of s.entries())
15
- t[o] = i;
16
- return Array.from(a.values()).forEach((i) => {
17
- t[i.constantName] = i.originalUri;
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 I(a) {
21
+ function T(i) {
22
22
  const s = /* @__PURE__ */ new Map();
23
23
  try {
24
- if (!a.trim())
24
+ if (!i.trim())
25
25
  return s;
26
- const t = JSON.parse(a);
27
- for (const [e, i] of Object.entries(t))
28
- typeof i == "string" && s.set(i, e);
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 S(a, s, t, e) {
36
- const i = Array.from(t.values()).filter(
37
- (n) => s.has(n.originalUri)
38
- ).length, o = a - i;
39
- o > 0 ? console.log(`โœ… Generated ${a} asset constants (${o} new, ${i} reused) in ${e}`) : a > 0 ? console.log(`โœ… Generated ${a} asset constants (all reused from existing) in ${e}`) : console.log("โœ… No asset constants found");
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 h == "function" ? h : h.default, W = typeof m == "function" ? m : m.default;
42
- class R {
41
+ const x = typeof y == "function" ? y : y.default, $ = typeof F == "function" ? F : F.default;
42
+ class O {
43
43
  constructor(s = {}) {
44
- f(this, "options");
45
- f(this, "assets", /* @__PURE__ */ new Map());
46
- f(this, "existingAssets", /* @__PURE__ */ new Map());
47
- f(this, "fileCache", new E());
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 U(s, t);
64
+ return D(s, t);
65
65
  }
66
66
  async scanTsFile(s) {
67
67
  try {
68
- const t = await this.fileCache.read(s), e = [], i = $(t, "js", this.options.extensions), o = t.split(`
68
+ const t = await this.fileCache.read(s), e = [], n = E(t, "js", this.options.extensions), a = t.split(`
69
69
  `);
70
- return i.forEach((n) => {
71
- const r = this.generateConstantName(n), c = o.findIndex((p) => p.includes(n)) + 1;
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: n,
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((o) => {
92
- this.existingAssets.has(o.originalUri) && (o.constantName = this.existingAssets.get(o.originalUri));
93
- const n = o.originalUri;
94
- this.assets.has(n) || this.assets.set(n, o);
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 = I(s);
110
+ this.existingAssets = T(s);
111
111
  }
112
112
  async generateAssetsFile() {
113
113
  await this.parseExistingAssets(), await this.scanAllTsFiles();
114
- const s = D(this.assets, this.existingAssets), t = d.dirname(this.options.outputFile);
115
- await g.mkdir(t, { recursive: !0 }), await g.writeFile(this.options.outputFile, s, "utf-8"), S(this.assets.size, this.existingAssets, this.assets, this.options.outputFile);
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), i = N(e, {
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 o = !1;
127
- if (x(i, {
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 p = l.node.value, w = j(p);
131
- if (t.has(w)) {
132
- const F = t.get(w), y = u.memberExpression(
133
- u.identifier("assetsData"),
134
- u.identifier(F)
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 && u.isJSXAttribute(l.parent) ? l.replaceWith(u.jsxExpressionContainer(y)) : l.replaceWith(y), o = !0;
153
+ l.parent && p.isJSXAttribute(l.parent) ? l.replaceWith(p.jsxExpressionContainer(j)) : l.replaceWith(j), a = !0;
137
154
  }
138
155
  }
139
- }), !o)
156
+ }), !a)
140
157
  return;
141
- const n = b(s, this.options.outputFile) + ".json";
142
- let r = !1;
143
- if (x(i, {
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 === n && (r = !0, l.stop());
163
+ const m = l.node.source.value;
164
+ (m === r || m === c) && (f = !0, l.stop());
147
165
  }
148
- }), !r) {
149
- const l = u.importDeclaration(
150
- [u.importDefaultSpecifier(u.identifier("assetsData"))],
151
- u.stringLiteral(n)
166
+ }), !f) {
167
+ const l = p.importDeclaration(
168
+ [p.importDefaultSpecifier(p.identifier("assetsData"))],
169
+ p.stringLiteral(r)
152
170
  );
153
- i.program.body.unshift(l);
171
+ n.program.body.unshift(l);
154
172
  }
155
- const c = W(i, {
173
+ const u = $(n, {
156
174
  retainLines: !1,
157
175
  comments: !0,
158
176
  compact: !1
159
177
  }, e);
160
- await g.writeFile(s, c.code, "utf-8"), this.fileCache.invalidate(s), console.log(` โœ“ Replaced URIs in ${d.relative(process.cwd(), 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 n = JSON.parse(s);
180
- for (const [r, c] of Object.entries(n))
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 (n) {
183
- console.warn("Warning: Failed to parse assets.json:", n);
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(), i = await A(this.options.srcDir, [".ts", ".tsx"]);
189
- for (const n of i)
190
- if (!(n.endsWith("assets.json") || n.endsWith("assets.ts")))
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(n);
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 ${n}:`, r);
214
+ console.warn(`Warning: Failed to read ${o}:`, r);
197
215
  }
198
- const o = [];
199
- for (const n of t.keys())
200
- e.has(n) || o.push(n);
201
- if (o.length > 0) {
202
- console.log(`๐Ÿงน Removing ${o.length} unused constant(s):`), o.forEach((r) => {
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 n = [];
206
- for (const r of o) {
223
+ const o = [];
224
+ for (const r of a) {
207
225
  const c = t.get(r);
208
- c && n.push(c);
226
+ c && o.push(c);
209
227
  }
210
- for (const r of n)
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(a = {}) {
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 (a.devMode)
280
+ if (i.devMode)
227
281
  try {
228
- const s = new R(a);
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}' (${d.relative(process.cwd(), e.filePath)}:${e.line})`);
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(`โœ… Asset constants finalized!
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);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aippy/vite-plugins",
3
- "version": "0.2.6",
3
+ "version": "0.2.7-dev.1",
4
4
  "description": "Vite plugins for Aippy projects - Asset management and component tagging",
5
5
  "private": false,
6
6
  "type": "module",