@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.
@@ -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);
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) {
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;
18
- }), JSON.stringify(t, null, 2) + `
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(a) {
21
+ function I(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 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 (t) {
31
- console.warn("Warning: Failed to parse existing assets.json:", t);
30
+ } catch (e) {
31
+ console.warn("Warning: Failed to parse existing assets.json:", e);
32
32
  }
33
33
  return s;
34
34
  }
35
- function S(a, s, t, e) {
36
- const i = Array.from(t.values()).filter(
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 = 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");
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 x = typeof h == "function" ? h : h.default, W = typeof m == "function" ? m : m.default;
42
- class R {
41
+ const v = typeof w == "function" ? w : w.default, O = typeof y == "function" ? y : y.default;
42
+ class T {
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
+ 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 t = /* @__PURE__ */ new Set([
61
- ...Array.from(this.assets.values()).map((e) => e.constantName),
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 U(s, t);
64
+ return E(s, e);
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 e = await this.fileCache.read(s), t = [], a = D(e, "js", this.options.extensions), o = e.split(`
69
69
  `);
70
- return i.forEach((n) => {
71
- const r = this.generateConstantName(n), c = o.findIndex((p) => p.includes(n)) + 1;
72
- e.push({
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
- }), e;
80
- } catch (t) {
81
- return console.warn(`Warning: Failed to scan ${s}:`, t), [];
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 t = await A(s, [".ts", ".tsx"]);
88
- for (const e of t) {
89
- if (e.endsWith("assets.json") || e.endsWith("assets.ts"))
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(e)).forEach((o) => {
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 (t) {
98
- console.error("Error scanning TypeScript files:", t);
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 g.readFile(this.options.outputFile, "utf-8");
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 = 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 = 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, t) {
137
+ async replaceUrisInFile(s, e) {
121
138
  try {
122
- const e = await this.fileCache.read(s), i = N(e, {
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 (x(i, {
144
+ if (v(a, {
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 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 && 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(F)) : l.replaceWith(F), o = !0;
137
154
  }
138
155
  }
139
156
  }), !o)
140
157
  return;
141
- const n = b(s, this.options.outputFile) + ".json";
142
- let r = !1;
143
- if (x(i, {
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 === n && (r = !0, l.stop());
163
+ const h = l.node.source.value;
164
+ (h === r || h === c) && (m = !0, l.stop());
147
165
  }
148
- }), !r) {
149
- const l = u.importDeclaration(
150
- [u.importDefaultSpecifier(u.identifier("assetsData"))],
151
- u.stringLiteral(n)
166
+ }), !m) {
167
+ const l = p.importDeclaration(
168
+ [p.importDefaultSpecifier(p.identifier("assetsData"))],
169
+ p.stringLiteral(r)
152
170
  );
153
- i.program.body.unshift(l);
171
+ a.program.body.unshift(l);
154
172
  }
155
- const c = W(i, {
173
+ const d = O(a, {
156
174
  retainLines: !1,
157
175
  comments: !0,
158
176
  compact: !1
159
- }, e);
160
- await g.writeFile(s, c.code, "utf-8"), this.fileCache.invalidate(s), console.log(` โœ“ Replaced URIs in ${d.relative(process.cwd(), s)}`);
161
- } catch (e) {
162
- console.warn(`Warning: Failed to replace URIs in ${s}:`, e);
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(), t = /* @__PURE__ */ new Set();
167
- for (const e of this.assets.values())
168
- s.set(e.originalUri, e.constantName), t.add(e.filePath);
169
- for (const e of t)
170
- await this.replaceUrisInFile(e, s);
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 t = /* @__PURE__ */ new Map();
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" && t.set(r, c);
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 (t.size === 0)
204
+ if (e.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)
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 t.keys())
194
- r.includes(c) && e.add(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 t.keys())
200
- e.has(n) || o.push(n);
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 = t.get(r);
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 q(a = {}) {
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 (a.devMode)
244
+ if (i.devMode)
227
245
  try {
228
- const s = new R(a);
246
+ const s = new T(i);
229
247
  await s.generateAssetsFile();
230
- const t = s.getAssets();
231
- 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})`);
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
- q as assetConstantsPlugin,
245
- q as default
262
+ B as assetConstantsPlugin,
263
+ B as default
246
264
  };
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.0",
4
4
  "description": "Vite plugins for Aippy projects - Asset management and component tagging",
5
5
  "private": false,
6
6
  "type": "module",