vite-plugin-uni-inject 0.3.0 → 0.4.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/index.cjs CHANGED
@@ -24,12 +24,10 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
24
24
  let _babel_parser = require("@babel/parser");
25
25
  let _vue_compiler_sfc = require("@vue/compiler-sfc");
26
26
  let fs = require("fs");
27
- fs = __toESM(fs);
27
+ fs = __toESM(fs, 1);
28
28
  let path = require("path");
29
- path = __toESM(path);
30
- let magic_string = require("magic-string");
31
- magic_string = __toESM(magic_string);
32
- //#region src/inject.ts
29
+ path = __toESM(path, 1);
30
+ //#region src/modules/inject/index.ts
33
31
  /**
34
32
  * 注入代码插件
35
33
  */
@@ -84,25 +82,35 @@ function uniInject(opts) {
84
82
  const start = newDescriptor.scriptSetup.loc.start.offset;
85
83
  const end = newDescriptor.scriptSetup.loc.end.offset;
86
84
  const scriptCode = newDescriptor.scriptSetup.content;
87
- const s = new magic_string.default(scriptCode);
88
85
  const ast = (0, _babel_parser.parse)(scriptCode, {
89
86
  sourceType: "module",
90
87
  plugins: ["typescript"]
91
88
  });
92
89
  const imports = [];
93
- for (const node of ast.program.body) if (node.type === "ImportDeclaration") {
94
- imports.push(scriptCode.slice(node.start, node.end));
95
- s.remove(node.start, node.end + 1);
90
+ for (const node of ast.program.body) if (node.type === "ImportDeclaration" && typeof node.start === "number" && typeof node.end === "number") imports.push({
91
+ code: scriptCode.slice(node.start, node.end),
92
+ start: node.start,
93
+ end: node.end
94
+ });
95
+ let nextScriptCode = scriptCode;
96
+ if (imports.length) {
97
+ const ranges = [...imports].sort((a, b) => b.start - a.start);
98
+ for (const { start: rangeStart, end: rangeEnd } of ranges) {
99
+ let removeEnd = rangeEnd;
100
+ if (nextScriptCode.startsWith("\r\n", removeEnd)) removeEnd += 2;
101
+ else if (nextScriptCode[removeEnd] === "\n") removeEnd += 1;
102
+ nextScriptCode = nextScriptCode.slice(0, rangeStart) + nextScriptCode.slice(removeEnd);
103
+ }
104
+ nextScriptCode = `\n${imports.map((item) => item.code).join("\n")}${nextScriptCode}`;
96
105
  }
97
- if (imports.length) s.prepend("\n" + imports.join("\n"));
98
- newCode = newCode.slice(0, start) + s.toString() + newCode.slice(end);
106
+ newCode = newCode.slice(0, start) + nextScriptCode + newCode.slice(end);
99
107
  }
100
108
  return { code: newCode };
101
109
  }
102
110
  };
103
111
  }
104
112
  //#endregion
105
- //#region src/auto-pages.ts
113
+ //#region src/modules/auto-pages/index.ts
106
114
  function toPosixPath(value) {
107
115
  return value.replace(/\\/g, "/");
108
116
  }
@@ -169,6 +177,8 @@ function hasPagesJsonChanged(current, next) {
169
177
  });
170
178
  }
171
179
  function getPagesByFileRoute(routes, pagesJson, subPackageRoots) {
180
+ const originPages = pagesJson.pages ?? [];
181
+ const originSubPackages = pagesJson.subPackages ?? [];
172
182
  const mainRoutes = [];
173
183
  const subRouteMap = /* @__PURE__ */ new Map();
174
184
  const normalizedSubRoots = subPackageRoots.map(normalizeDir);
@@ -183,12 +193,17 @@ function getPagesByFileRoute(routes, pagesJson, subPackageRoots) {
183
193
  subRouteMap.set(matchedRoot, list);
184
194
  } else mainRoutes.push(route);
185
195
  }
186
- const pages = mergePages(mainRoutes, pagesJson.pages ?? []);
187
- const existSubs = pagesJson.subPackages ?? [];
196
+ const homePage = originPages[0]?.path;
197
+ if (mainRoutes.includes(homePage)) {
198
+ const idx = mainRoutes.indexOf(homePage);
199
+ mainRoutes.splice(idx, 1);
200
+ mainRoutes.unshift(homePage);
201
+ }
202
+ const pages = mergePages(mainRoutes, originPages);
188
203
  const subPackages = normalizedSubRoots.map((root) => {
189
204
  return {
190
205
  root,
191
- pages: mergePages(subRouteMap.get(root) ?? [], existSubs.find((s) => {
206
+ pages: mergePages(subRouteMap.get(root) ?? [], originSubPackages.find((s) => {
192
207
  return normalizeDir(s.root) === root;
193
208
  })?.pages ?? [])
194
209
  };
@@ -234,11 +249,13 @@ function uniAutoPages(opts) {
234
249
  if (!pagesJson) return;
235
250
  const routes = collectFileRoutes(srcRoot, getScanDirs(dir, subPackages));
236
251
  const { merged, changed } = getPagesByFileRoute(routes, pagesJson, subPackages);
237
- if (changed) fs.default.writeFileSync(pagesJsonPath, JSON.stringify(merged, null, 2) + "\n");
238
- if (dts) {
239
- const dtsFilePath = resolveDtsFilePath(srcRoot, dts);
240
- const dtsContent = buildRouteDts(routes);
241
- fs.default.writeFileSync(dtsFilePath, dtsContent);
252
+ if (changed) {
253
+ if (dts) {
254
+ const dtsFilePath = resolveDtsFilePath(srcRoot, dts);
255
+ const dtsContent = buildRouteDts(routes);
256
+ fs.default.writeFileSync(dtsFilePath, dtsContent);
257
+ }
258
+ fs.default.writeFileSync(pagesJsonPath, JSON.stringify(merged, null, 2) + "\n");
242
259
  }
243
260
  }
244
261
  };
package/dist/index.d.cts CHANGED
@@ -3,7 +3,7 @@
3
3
  interface InjectPluginOptions {
4
4
  /**
5
5
  * 注入的文件路径
6
- * @default 'src/App.inject.vue'
6
+ * @default './App.inject.vue'
7
7
  */
8
8
  path?: string;
9
9
  }
@@ -14,19 +14,19 @@ interface AutoPagesPluginOptions {
14
14
  * @default 'pages'
15
15
  */
16
16
  dir?: string;
17
+ /**
18
+ * 输出类型
19
+ * @default './uni-pages.d.ts'
20
+ */
21
+ dts?: string;
17
22
  /**
18
23
  * 分包目录
19
24
  * @default []
20
25
  */
21
26
  subPackages?: string[];
22
- /**
23
- * 生成类型声明
24
- * @default 'src/uni-pages.d.ts'
25
- */
26
- dts?: string;
27
27
  }
28
28
  //#endregion
29
- //#region src/inject.d.ts
29
+ //#region src/modules/inject/index.d.ts
30
30
  /**
31
31
  * 注入代码插件
32
32
  */
@@ -41,7 +41,7 @@ declare function uniInject(opts?: InjectPluginOptions): {
41
41
  } | undefined;
42
42
  };
43
43
  //#endregion
44
- //#region src/auto-pages.d.ts
44
+ //#region src/modules/auto-pages/index.d.ts
45
45
  /**
46
46
  * 自动补全 pages.json 插件
47
47
  */
package/dist/index.d.mts CHANGED
@@ -3,7 +3,7 @@
3
3
  interface InjectPluginOptions {
4
4
  /**
5
5
  * 注入的文件路径
6
- * @default 'src/App.inject.vue'
6
+ * @default './App.inject.vue'
7
7
  */
8
8
  path?: string;
9
9
  }
@@ -14,19 +14,19 @@ interface AutoPagesPluginOptions {
14
14
  * @default 'pages'
15
15
  */
16
16
  dir?: string;
17
+ /**
18
+ * 输出类型
19
+ * @default './uni-pages.d.ts'
20
+ */
21
+ dts?: string;
17
22
  /**
18
23
  * 分包目录
19
24
  * @default []
20
25
  */
21
26
  subPackages?: string[];
22
- /**
23
- * 生成类型声明
24
- * @default 'src/uni-pages.d.ts'
25
- */
26
- dts?: string;
27
27
  }
28
28
  //#endregion
29
- //#region src/inject.d.ts
29
+ //#region src/modules/inject/index.d.ts
30
30
  /**
31
31
  * 注入代码插件
32
32
  */
@@ -41,7 +41,7 @@ declare function uniInject(opts?: InjectPluginOptions): {
41
41
  } | undefined;
42
42
  };
43
43
  //#endregion
44
- //#region src/auto-pages.d.ts
44
+ //#region src/modules/auto-pages/index.d.ts
45
45
  /**
46
46
  * 自动补全 pages.json 插件
47
47
  */
package/dist/index.mjs CHANGED
@@ -2,8 +2,7 @@ import { parse } from "@babel/parser";
2
2
  import { parse as parse$1 } from "@vue/compiler-sfc";
3
3
  import fs from "fs";
4
4
  import path from "path";
5
- import MagicString from "magic-string";
6
- //#region src/inject.ts
5
+ //#region src/modules/inject/index.ts
7
6
  /**
8
7
  * 注入代码插件
9
8
  */
@@ -58,25 +57,35 @@ function uniInject(opts) {
58
57
  const start = newDescriptor.scriptSetup.loc.start.offset;
59
58
  const end = newDescriptor.scriptSetup.loc.end.offset;
60
59
  const scriptCode = newDescriptor.scriptSetup.content;
61
- const s = new MagicString(scriptCode);
62
60
  const ast = parse(scriptCode, {
63
61
  sourceType: "module",
64
62
  plugins: ["typescript"]
65
63
  });
66
64
  const imports = [];
67
- for (const node of ast.program.body) if (node.type === "ImportDeclaration") {
68
- imports.push(scriptCode.slice(node.start, node.end));
69
- s.remove(node.start, node.end + 1);
65
+ for (const node of ast.program.body) if (node.type === "ImportDeclaration" && typeof node.start === "number" && typeof node.end === "number") imports.push({
66
+ code: scriptCode.slice(node.start, node.end),
67
+ start: node.start,
68
+ end: node.end
69
+ });
70
+ let nextScriptCode = scriptCode;
71
+ if (imports.length) {
72
+ const ranges = [...imports].sort((a, b) => b.start - a.start);
73
+ for (const { start: rangeStart, end: rangeEnd } of ranges) {
74
+ let removeEnd = rangeEnd;
75
+ if (nextScriptCode.startsWith("\r\n", removeEnd)) removeEnd += 2;
76
+ else if (nextScriptCode[removeEnd] === "\n") removeEnd += 1;
77
+ nextScriptCode = nextScriptCode.slice(0, rangeStart) + nextScriptCode.slice(removeEnd);
78
+ }
79
+ nextScriptCode = `\n${imports.map((item) => item.code).join("\n")}${nextScriptCode}`;
70
80
  }
71
- if (imports.length) s.prepend("\n" + imports.join("\n"));
72
- newCode = newCode.slice(0, start) + s.toString() + newCode.slice(end);
81
+ newCode = newCode.slice(0, start) + nextScriptCode + newCode.slice(end);
73
82
  }
74
83
  return { code: newCode };
75
84
  }
76
85
  };
77
86
  }
78
87
  //#endregion
79
- //#region src/auto-pages.ts
88
+ //#region src/modules/auto-pages/index.ts
80
89
  function toPosixPath(value) {
81
90
  return value.replace(/\\/g, "/");
82
91
  }
@@ -143,6 +152,8 @@ function hasPagesJsonChanged(current, next) {
143
152
  });
144
153
  }
145
154
  function getPagesByFileRoute(routes, pagesJson, subPackageRoots) {
155
+ const originPages = pagesJson.pages ?? [];
156
+ const originSubPackages = pagesJson.subPackages ?? [];
146
157
  const mainRoutes = [];
147
158
  const subRouteMap = /* @__PURE__ */ new Map();
148
159
  const normalizedSubRoots = subPackageRoots.map(normalizeDir);
@@ -157,12 +168,17 @@ function getPagesByFileRoute(routes, pagesJson, subPackageRoots) {
157
168
  subRouteMap.set(matchedRoot, list);
158
169
  } else mainRoutes.push(route);
159
170
  }
160
- const pages = mergePages(mainRoutes, pagesJson.pages ?? []);
161
- const existSubs = pagesJson.subPackages ?? [];
171
+ const homePage = originPages[0]?.path;
172
+ if (mainRoutes.includes(homePage)) {
173
+ const idx = mainRoutes.indexOf(homePage);
174
+ mainRoutes.splice(idx, 1);
175
+ mainRoutes.unshift(homePage);
176
+ }
177
+ const pages = mergePages(mainRoutes, originPages);
162
178
  const subPackages = normalizedSubRoots.map((root) => {
163
179
  return {
164
180
  root,
165
- pages: mergePages(subRouteMap.get(root) ?? [], existSubs.find((s) => {
181
+ pages: mergePages(subRouteMap.get(root) ?? [], originSubPackages.find((s) => {
166
182
  return normalizeDir(s.root) === root;
167
183
  })?.pages ?? [])
168
184
  };
@@ -208,11 +224,13 @@ function uniAutoPages(opts) {
208
224
  if (!pagesJson) return;
209
225
  const routes = collectFileRoutes(srcRoot, getScanDirs(dir, subPackages));
210
226
  const { merged, changed } = getPagesByFileRoute(routes, pagesJson, subPackages);
211
- if (changed) fs.writeFileSync(pagesJsonPath, JSON.stringify(merged, null, 2) + "\n");
212
- if (dts) {
213
- const dtsFilePath = resolveDtsFilePath(srcRoot, dts);
214
- const dtsContent = buildRouteDts(routes);
215
- fs.writeFileSync(dtsFilePath, dtsContent);
227
+ if (changed) {
228
+ if (dts) {
229
+ const dtsFilePath = resolveDtsFilePath(srcRoot, dts);
230
+ const dtsContent = buildRouteDts(routes);
231
+ fs.writeFileSync(dtsFilePath, dtsContent);
232
+ }
233
+ fs.writeFileSync(pagesJsonPath, JSON.stringify(merged, null, 2) + "\n");
216
234
  }
217
235
  }
218
236
  };
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "author": "Kriac",
5
5
  "license": "MIT",
6
6
  "type": "module",
7
- "version": "0.3.0",
7
+ "version": "0.4.0",
8
8
  "homepage": "https://github.com/Kriac/vite-plugin-uni-inject",
9
9
  "repository": {
10
10
  "type": "git",
@@ -20,12 +20,11 @@
20
20
  },
21
21
  "dependencies": {
22
22
  "@vue/compiler-sfc": "3.5.27",
23
- "@babel/parser": "7.29.0",
24
- "magic-string": "0.30.21"
23
+ "@babel/parser": "7.29.0"
25
24
  },
26
25
  "devDependencies": {
27
26
  "@types/node": "25.0.10",
28
- "tsdown": "0.21.0",
27
+ "tsdown": "0.22.0",
29
28
  "vue-tsc": "3.2.1"
30
29
  },
31
30
  "files": [