@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.
@@ -1,50 +1,50 @@
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";
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 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) + `
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 I(i) {
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 e = JSON.parse(i);
27
- for (const [t, a] of Object.entries(e))
28
- typeof a == "string" && s.set(a, t);
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 (e) {
31
- console.warn("Warning: Failed to parse existing assets.json:", e);
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, e, t) {
36
- const a = Array.from(e.values()).filter(
37
- (n) => s.has(n.originalUri)
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");
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 v = typeof w == "function" ? w : w.default, O = typeof y == "function" ? y : y.default;
42
- class T {
41
+ const x = typeof y == "function" ? y : y.default, $ = typeof F == "function" ? F : F.default;
42
+ class O {
43
43
  constructor(s = {}) {
44
- u(this, "options");
45
- u(this, "assets", /* @__PURE__ */ new Map());
46
- u(this, "existingAssets", /* @__PURE__ */ new Map());
47
- u(this, "fileCache", new b());
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 e = /* @__PURE__ */ new Set([
61
- ...Array.from(this.assets.values()).map((t) => t.constantName),
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 E(s, e);
64
+ return D(s, t);
65
65
  }
66
66
  async scanTsFile(s) {
67
67
  try {
68
- const e = await this.fileCache.read(s), t = [], a = D(e, "js", this.options.extensions), o = e.split(`
68
+ const t = await this.fileCache.read(s), e = [], n = E(t, "js", this.options.extensions), a = t.split(`
69
69
  `);
70
- return a.forEach((n) => {
71
- const r = this.generateConstantName(n), c = o.findIndex((d) => d.includes(n)) + 1;
72
- t.push({
73
- originalUri: n,
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
- }), t;
80
- } catch (e) {
81
- return console.warn(`Warning: Failed to scan ${s}:`, e), [];
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 e = await x(s, [".ts", ".tsx"]);
88
- for (const t of e) {
89
- if (t.endsWith("assets.json") || t.endsWith("assets.ts"))
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(t)).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
- } catch (e) {
98
- console.error("Error scanning TypeScript files:", e);
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 f.readFile(this.options.outputFile, "utf-8");
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 = I(s);
110
+ this.existingAssets = T(s);
111
111
  }
112
112
  async generateAssetsFile() {
113
113
  await this.parseExistingAssets(), await this.scanAllTsFiles();
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);
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"), t = `// ๐ŸŽ Auto-generated wrapper for assets.json
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 "./${g.basename(this.options.outputFile)}";
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 f.writeFile(s, t, "utf-8"), console.log(` โœ“ Generated assets wrapper: ${g.relative(process.cwd(), s)}`);
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, e) {
137
+ async replaceUrisInFile(s, t) {
138
138
  try {
139
- const t = await this.fileCache.read(s), a = U(t, {
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 o = !1;
144
- if (v(a, {
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 h = l.node.value, A = S(h);
148
- if (e.has(A)) {
149
- const j = e.get(A), F = p.memberExpression(
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(j)
151
+ p.identifier(N)
152
152
  );
153
- l.parent && p.isJSXAttribute(l.parent) ? l.replaceWith(p.jsxExpressionContainer(F)) : l.replaceWith(F), o = !0;
153
+ l.parent && p.isJSXAttribute(l.parent) ? l.replaceWith(p.jsxExpressionContainer(j)) : l.replaceWith(j), a = !0;
154
154
  }
155
155
  }
156
- }), !o)
156
+ }), !a)
157
157
  return;
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, {
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 h = l.node.source.value;
164
- (h === r || h === c) && (m = !0, l.stop());
163
+ const m = l.node.source.value;
164
+ (m === r || m === c) && (f = !0, l.stop());
165
165
  }
166
- }), !m) {
166
+ }), !f) {
167
167
  const l = p.importDeclaration(
168
168
  [p.importDefaultSpecifier(p.identifier("assetsData"))],
169
169
  p.stringLiteral(r)
170
170
  );
171
- a.program.body.unshift(l);
171
+ n.program.body.unshift(l);
172
172
  }
173
- const d = O(a, {
173
+ const u = $(n, {
174
174
  retainLines: !1,
175
175
  comments: !0,
176
176
  compact: !1
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);
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(), 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);
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 e = /* @__PURE__ */ new Map();
195
+ const t = /* @__PURE__ */ new Map();
196
196
  try {
197
- const n = JSON.parse(s);
198
- for (const [r, c] of Object.entries(n))
199
- typeof c == "string" && e.set(r, c);
200
- } catch (n) {
201
- console.warn("Warning: Failed to parse assets.json:", n);
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 (e.size === 0)
204
+ if (t.size === 0)
205
205
  return;
206
- const t = /* @__PURE__ */ new Set(), a = await x(this.options.srcDir, [".ts", ".tsx"]);
207
- for (const n of a)
208
- 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")))
209
209
  try {
210
- const r = await this.fileCache.read(n);
211
- for (const c of e.keys())
212
- r.includes(c) && t.add(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 ${n}:`, r);
214
+ console.warn(`Warning: Failed to read ${o}:`, r);
215
215
  }
216
- const o = [];
217
- for (const n of e.keys())
218
- t.has(n) || o.push(n);
219
- if (o.length > 0) {
220
- 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) => {
221
221
  console.log(` - ${r}`);
222
222
  });
223
- const n = [];
224
- for (const r of o) {
225
- const c = e.get(r);
226
- c && n.push(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 n)
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 B(i = {}) {
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 T(i);
282
+ const s = new O(i);
247
283
  await s.generateAssetsFile();
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})`);
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(`โœ… 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!
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
- B as assetConstantsPlugin,
263
- B as default
299
+ q as assetConstantsPlugin,
300
+ q as default
264
301
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aippy/vite-plugins",
3
- "version": "0.2.7-dev.0",
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",