@aippy/vite-plugins 0.2.7-dev.1 → 0.2.8-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,7 +1,7 @@
1
1
  import { promises as c } from "fs";
2
2
  import u from "path";
3
3
  import { extractAssetsFromContent as a } from "../utils/index.js";
4
- import { g as l } from "../file-scanner-DHkDSQm8.js";
4
+ import { g as l } from "../file-scanner-DSr-Ek-8.js";
5
5
  async function p(t) {
6
6
  const s = [], n = [u.join(process.cwd(), "index.html")];
7
7
  for (const e of n)
@@ -1,50 +1,50 @@
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);
1
+ var E = Object.defineProperty;
2
+ var I = (i, s, t) => s in i ? E(i, s, { enumerable: !0, configurable: !0, writable: !0, value: t }) : i[s] = t;
3
+ var w = (i, s, t) => I(i, typeof s != "symbol" ? s + "" : s, t);
4
4
  import { promises as g } from "fs";
5
5
  import h from "path";
6
- import { parse as v } from "@babel/parser";
7
- import y from "@babel/traverse";
6
+ import { parse as $ } from "@babel/parser";
7
+ import F from "@babel/traverse";
8
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) {
9
+ import A from "@babel/generator";
10
+ import { generateConstantName as W, extractAssetsFromContent as S, processAssetURL as T } from "../utils/index.js";
11
+ import { F as b, g as x, a as N, b as y } from "../file-scanner-DSr-Ek-8.js";
12
+ function R(i, s) {
13
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;
14
+ for (const [a, o] of s.entries())
15
+ t[o] = a;
16
+ return Array.from(i.values()).forEach((a) => {
17
+ t[a.constantName] = a.originalUri;
18
18
  }), JSON.stringify(t, null, 2) + `
19
19
  `;
20
20
  }
21
- function T(i) {
21
+ function O(i) {
22
22
  const s = /* @__PURE__ */ new Map();
23
23
  try {
24
24
  if (!i.trim())
25
25
  return s;
26
26
  const t = JSON.parse(i);
27
- for (const [e, n] of Object.entries(t))
28
- typeof n == "string" && s.set(n, e);
27
+ for (const [e, a] of Object.entries(t))
28
+ typeof a == "string" && s.set(a, 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 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");
35
+ function J(i, s, t, e) {
36
+ const a = Array.from(t.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 ${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 y == "function" ? y : y.default, $ = typeof F == "function" ? F : F.default;
42
- class O {
41
+ const C = typeof F == "function" ? F : F.default, U = typeof A == "function" ? A : A.default;
42
+ class P {
43
43
  constructor(s = {}) {
44
- d(this, "options");
45
- d(this, "assets", /* @__PURE__ */ new Map());
46
- d(this, "existingAssets", /* @__PURE__ */ new Map());
47
- d(this, "fileCache", new W());
44
+ w(this, "options");
45
+ w(this, "assets", /* @__PURE__ */ new Map());
46
+ w(this, "existingAssets", /* @__PURE__ */ new Map());
47
+ w(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",
@@ -61,16 +61,16 @@ class O {
61
61
  ...Array.from(this.assets.values()).map((e) => e.constantName),
62
62
  ...Array.from(this.existingAssets.values())
63
63
  ]);
64
- return D(s, t);
64
+ return W(s, t);
65
65
  }
66
66
  async scanTsFile(s) {
67
67
  try {
68
- const t = await this.fileCache.read(s), e = [], n = E(t, "js", this.options.extensions), a = t.split(`
68
+ const t = await this.fileCache.read(s), e = [], a = S(t, "js", this.options.extensions), o = t.split(`
69
69
  `);
70
- return n.forEach((o) => {
71
- const r = this.generateConstantName(o), c = a.findIndex((u) => u.includes(o)) + 1;
70
+ return a.forEach((n) => {
71
+ const r = this.generateConstantName(n), c = o.findIndex((u) => u.includes(n)) + 1;
72
72
  e.push({
73
- originalUri: o,
73
+ originalUri: n,
74
74
  constantName: r,
75
75
  filePath: s,
76
76
  line: c,
@@ -84,14 +84,14 @@ class O {
84
84
  async scanAllTsFiles() {
85
85
  const s = this.options.srcDir;
86
86
  try {
87
- const t = await A(s, [".ts", ".tsx"]);
87
+ const t = await x(s, [".ts", ".tsx"]);
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((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);
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);
95
95
  });
96
96
  }
97
97
  } catch (t) {
@@ -107,12 +107,12 @@ class O {
107
107
  }
108
108
  async parseExistingAssets() {
109
109
  const s = await this.readExistingAssetsFile();
110
- this.existingAssets = T(s);
110
+ this.existingAssets = O(s);
111
111
  }
112
112
  async generateAssetsFile() {
113
113
  await this.parseExistingAssets(), await this.scanAllTsFiles();
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);
114
+ const s = R(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(), J(this.assets.size, this.existingAssets, this.assets, this.options.outputFile);
116
116
  }
117
117
  /**
118
118
  * 生成 assets.ts 包装文件
@@ -136,46 +136,46 @@ export default assetsData;
136
136
  }
137
137
  async replaceUrisInFile(s, t) {
138
138
  try {
139
- const e = await this.fileCache.read(s), n = v(e, {
139
+ const e = await this.fileCache.read(s), a = $(e, {
140
140
  sourceType: "module",
141
141
  plugins: ["typescript", "jsx", "decorators-legacy"]
142
142
  });
143
- let a = !1;
144
- if (x(n, {
143
+ let o = !1;
144
+ if (C(a, {
145
145
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
146
146
  StringLiteral: (l) => {
147
- const m = l.node.value, C = S(m);
148
- if (t.has(C)) {
149
- const N = t.get(C), j = p.memberExpression(
147
+ const m = l.node.value, v = T(m);
148
+ if (t.has(v)) {
149
+ const D = t.get(v), j = p.memberExpression(
150
150
  p.identifier("assetsData"),
151
- p.identifier(N)
151
+ p.identifier(D)
152
152
  );
153
- l.parent && p.isJSXAttribute(l.parent) ? l.replaceWith(p.jsxExpressionContainer(j)) : l.replaceWith(j), a = !0;
153
+ l.parent && p.isJSXAttribute(l.parent) ? l.replaceWith(p.jsxExpressionContainer(j)) : l.replaceWith(j), o = !0;
154
154
  }
155
155
  }
156
- }), !a)
156
+ }), !o)
157
157
  return;
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, {
158
+ const n = this.options.outputFile.replace(/\.json$/, ".ts"), r = N(n, this.options.srcDir), c = y(s, n), d = y(s, this.options.outputFile) + ".json";
159
+ let u = !1;
160
+ if (C(a, {
161
161
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
162
162
  ImportDeclaration: (l) => {
163
163
  const m = l.node.source.value;
164
- (m === r || m === c) && (f = !0, l.stop());
164
+ (m === r || m === c || m === d) && (u = !0, l.stop());
165
165
  }
166
- }), !f) {
166
+ }), !u) {
167
167
  const l = p.importDeclaration(
168
168
  [p.importDefaultSpecifier(p.identifier("assetsData"))],
169
169
  p.stringLiteral(r)
170
170
  );
171
- n.program.body.unshift(l);
171
+ a.program.body.unshift(l);
172
172
  }
173
- const u = $(n, {
173
+ const f = U(a, {
174
174
  retainLines: !1,
175
175
  comments: !0,
176
176
  compact: !1
177
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)}`);
178
+ await g.writeFile(s, f.code, "utf-8"), this.fileCache.invalidate(s), console.log(` ✓ Replaced URIs in ${h.relative(process.cwd(), s)}`);
179
179
  } catch (e) {
180
180
  console.warn(`Warning: Failed to replace URIs in ${s}:`, e);
181
181
  }
@@ -194,38 +194,38 @@ export default assetsData;
194
194
  return;
195
195
  const t = /* @__PURE__ */ new Map();
196
196
  try {
197
- const o = JSON.parse(s);
198
- for (const [r, c] of Object.entries(o))
197
+ const n = JSON.parse(s);
198
+ for (const [r, c] of Object.entries(n))
199
199
  typeof c == "string" && t.set(r, c);
200
- } catch (o) {
201
- console.warn("Warning: Failed to parse assets.json:", o);
200
+ } catch (n) {
201
+ console.warn("Warning: Failed to parse assets.json:", n);
202
202
  return;
203
203
  }
204
204
  if (t.size === 0)
205
205
  return;
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")))
206
+ const e = /* @__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")))
209
209
  try {
210
- const r = await this.fileCache.read(o);
210
+ const r = await this.fileCache.read(n);
211
211
  for (const c of t.keys())
212
212
  r.includes(c) && e.add(c);
213
213
  } catch (r) {
214
- console.warn(`Warning: Failed to read ${o}:`, r);
214
+ console.warn(`Warning: Failed to read ${n}:`, r);
215
215
  }
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) => {
216
+ const o = [];
217
+ for (const n of t.keys())
218
+ e.has(n) || o.push(n);
219
+ if (o.length > 0) {
220
+ console.log(`🧹 Removing ${o.length} unused constant(s):`), o.forEach((r) => {
221
221
  console.log(` - ${r}`);
222
222
  });
223
- const o = [];
224
- for (const r of a) {
223
+ const n = [];
224
+ for (const r of o) {
225
225
  const c = t.get(r);
226
- c && o.push(c);
226
+ c && n.push(c);
227
227
  }
228
- for (const r of o)
228
+ for (const r of n)
229
229
  this.assets.delete(r);
230
230
  } else
231
231
  console.log("✅ All constants are in use");
@@ -234,43 +234,43 @@ export default assetsData;
234
234
  }
235
235
  }
236
236
  /**
237
- * 🍎 升级现有的 .json 导入为 .ts 包装文件导入
238
- * 解决旧项目中已经使用 assets.json 的情况
237
+ * 🍎 升级现有的相对路径或 .json 导入为 @/ 别名的 .ts 包装文件导入
239
238
  */
240
239
  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")))
240
+ const s = this.options.outputFile.replace(/\.json$/, ".ts"), t = N(s, this.options.srcDir), e = await x(this.options.srcDir, [".ts", ".tsx"]);
241
+ let a = 0;
242
+ for (const o of e)
243
+ if (!(o.endsWith("assets.ts") || o.endsWith("assets.json")))
245
244
  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))
245
+ const n = await this.fileCache.read(o), r = y(o, this.options.outputFile) + ".json", c = y(o, s);
246
+ if (!n.includes(r) && !n.includes(c))
248
247
  continue;
249
- const c = v(a, {
248
+ const d = $(n, {
250
249
  sourceType: "module",
251
250
  plugins: ["typescript", "jsx", "decorators-legacy"]
252
251
  });
253
- let f = !1;
254
- if (x(c, {
252
+ let u = !1;
253
+ if (C(d, {
255
254
  // 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);
255
+ ImportDeclaration: (f) => {
256
+ const l = f.node.source.value;
257
+ (l === r || l === c) && (f.node.source.value = t, u = !0);
258
258
  }
259
- }), f) {
260
- const u = $(c, {
259
+ }), u) {
260
+ const f = U(d, {
261
261
  retainLines: !1,
262
262
  comments: !0,
263
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++;
264
+ }, n);
265
+ await g.writeFile(o, f.code, "utf-8"), this.fileCache.invalidate(o), console.log(` ✓ Upgraded import in ${h.relative(process.cwd(), o)} → ${t}`), a++;
266
266
  }
267
- } catch (a) {
268
- console.warn(`Warning: Failed to upgrade imports in ${n}:`, a);
267
+ } catch (n) {
268
+ console.warn(`Warning: Failed to upgrade imports in ${o}:`, n);
269
269
  }
270
- e > 0 && console.log(`✅ Upgraded ${e} file(s) to use assets.ts wrapper`);
270
+ a > 0 && console.log(`✅ Upgraded ${a} file(s) to use ${t}`);
271
271
  }
272
272
  }
273
- function q(i = {}) {
273
+ function X(i = {}) {
274
274
  return {
275
275
  name: "vite-plugin-asset-constants",
276
276
  apply: "build",
@@ -279,7 +279,7 @@ function q(i = {}) {
279
279
  async buildStart() {
280
280
  if (i.devMode)
281
281
  try {
282
- const s = new O(i);
282
+ const s = new P(i);
283
283
  await s.generateAssetsFile();
284
284
  const t = s.getAssets();
285
285
  t.length > 0 && (console.log(`📦 Found ${t.length} asset references:`), t.forEach((e) => {
@@ -296,6 +296,6 @@ function q(i = {}) {
296
296
  };
297
297
  }
298
298
  export {
299
- q as assetConstantsPlugin,
300
- q as default
299
+ X as assetConstantsPlugin,
300
+ X as default
301
301
  };
@@ -0,0 +1,54 @@
1
+ var h = Object.defineProperty;
2
+ var u = (e, t, s) => t in e ? h(e, t, { enumerable: !0, configurable: !0, writable: !0, value: s }) : e[t] = s;
3
+ var o = (e, t, s) => u(e, typeof t != "symbol" ? t + "" : t, s);
4
+ import { promises as l } from "fs";
5
+ import r from "path";
6
+ async function p(e, t) {
7
+ const s = [];
8
+ try {
9
+ const a = await l.readdir(e, { withFileTypes: !0 });
10
+ for (const i of a) {
11
+ const c = r.join(e, i.name);
12
+ if (i.isDirectory()) {
13
+ const n = await p(c, t);
14
+ s.push(...n);
15
+ } else if (i.isFile()) {
16
+ const n = r.extname(i.name);
17
+ t.includes(n) && s.push(c);
18
+ }
19
+ }
20
+ } catch (a) {
21
+ console.warn(`Warning: Failed to read directory ${e}:`, a);
22
+ }
23
+ return s;
24
+ }
25
+ function d(e, t) {
26
+ const a = r.relative(r.dirname(e), t).replace(/\.(ts|tsx|js|jsx|json)$/, "");
27
+ return a.startsWith(".") ? a : `./${a}`;
28
+ }
29
+ function w(e, t) {
30
+ return `@/${r.relative(t, e).replace(/\.(ts|tsx|js|jsx|json)$/, "")}`;
31
+ }
32
+ class y {
33
+ constructor() {
34
+ o(this, "cache", /* @__PURE__ */ new Map());
35
+ }
36
+ async read(t) {
37
+ if (this.cache.has(t))
38
+ return this.cache.get(t);
39
+ const s = await l.readFile(t, "utf-8");
40
+ return this.cache.set(t, s), s;
41
+ }
42
+ invalidate(t) {
43
+ this.cache.delete(t);
44
+ }
45
+ clear() {
46
+ this.cache.clear();
47
+ }
48
+ }
49
+ export {
50
+ y as F,
51
+ w as a,
52
+ d as b,
53
+ p as g
54
+ };
@@ -15,6 +15,11 @@ export declare function getFilesByExtension(dir: string, extensions: string[]):
15
15
  * @returns Relative import path (without extension)
16
16
  */
17
17
  export declare function getRelativeImportPath(fromFile: string, toFile: string): string;
18
+ /**
19
+ * Convert a file path under srcDir to a `@/`-prefixed alias import path.
20
+ * E.g. `src/config/assets.ts` with srcDir=`src` → `@/config/assets`
21
+ */
22
+ export declare function getAliasImportPath(filePath: string, srcDir: string): string;
18
23
  /**
19
24
  * Read file with caching support
20
25
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aippy/vite-plugins",
3
- "version": "0.2.7-dev.1",
3
+ "version": "0.2.8-dev.0",
4
4
  "description": "Vite plugins for Aippy projects - Asset management and component tagging",
5
5
  "private": false,
6
6
  "type": "module",
@@ -1,50 +0,0 @@
1
- var l = Object.defineProperty;
2
- var u = (e, t, a) => t in e ? l(e, t, { enumerable: !0, configurable: !0, writable: !0, value: a }) : e[t] = a;
3
- var o = (e, t, a) => u(e, typeof t != "symbol" ? t + "" : t, a);
4
- import { promises as h } from "fs";
5
- import i from "path";
6
- async function f(e, t) {
7
- const a = [];
8
- try {
9
- const s = await h.readdir(e, { withFileTypes: !0 });
10
- for (const r of s) {
11
- const c = i.join(e, r.name);
12
- if (r.isDirectory()) {
13
- const n = await f(c, t);
14
- a.push(...n);
15
- } else if (r.isFile()) {
16
- const n = i.extname(r.name);
17
- t.includes(n) && a.push(c);
18
- }
19
- }
20
- } catch (s) {
21
- console.warn(`Warning: Failed to read directory ${e}:`, s);
22
- }
23
- return a;
24
- }
25
- function y(e, t) {
26
- const s = i.relative(i.dirname(e), t).replace(/\.(ts|tsx|js|jsx|json)$/, "");
27
- return s.startsWith(".") ? s : `./${s}`;
28
- }
29
- class F {
30
- constructor() {
31
- o(this, "cache", /* @__PURE__ */ new Map());
32
- }
33
- async read(t) {
34
- if (this.cache.has(t))
35
- return this.cache.get(t);
36
- const a = await h.readFile(t, "utf-8");
37
- return this.cache.set(t, a), a;
38
- }
39
- invalidate(t) {
40
- this.cache.delete(t);
41
- }
42
- clear() {
43
- this.cache.clear();
44
- }
45
- }
46
- export {
47
- F,
48
- y as a,
49
- f as g
50
- };