vite-plugin-uni-inject 0.3.1 → 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
  }
@@ -241,11 +249,13 @@ function uniAutoPages(opts) {
241
249
  if (!pagesJson) return;
242
250
  const routes = collectFileRoutes(srcRoot, getScanDirs(dir, subPackages));
243
251
  const { merged, changed } = getPagesByFileRoute(routes, pagesJson, subPackages);
244
- if (changed) fs.default.writeFileSync(pagesJsonPath, JSON.stringify(merged, null, 2) + "\n");
245
- if (dts) {
246
- const dtsFilePath = resolveDtsFilePath(srcRoot, dts);
247
- const dtsContent = buildRouteDts(routes);
248
- 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");
249
259
  }
250
260
  }
251
261
  };
package/dist/index.d.cts CHANGED
@@ -9,16 +9,16 @@ interface InjectPluginOptions {
9
9
  }
10
10
  /** 自动补全 pages 插件配置 */
11
11
  interface AutoPagesPluginOptions {
12
- /**
13
- * 输出类型
14
- * @default './uni-pages.d.ts'
15
- */
16
- dts?: string;
17
12
  /**
18
13
  * 扫描目录
19
14
  * @default 'pages'
20
15
  */
21
16
  dir?: string;
17
+ /**
18
+ * 输出类型
19
+ * @default './uni-pages.d.ts'
20
+ */
21
+ dts?: string;
22
22
  /**
23
23
  * 分包目录
24
24
  * @default []
@@ -26,7 +26,7 @@ interface AutoPagesPluginOptions {
26
26
  subPackages?: 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
@@ -9,16 +9,16 @@ interface InjectPluginOptions {
9
9
  }
10
10
  /** 自动补全 pages 插件配置 */
11
11
  interface AutoPagesPluginOptions {
12
- /**
13
- * 输出类型
14
- * @default './uni-pages.d.ts'
15
- */
16
- dts?: string;
17
12
  /**
18
13
  * 扫描目录
19
14
  * @default 'pages'
20
15
  */
21
16
  dir?: string;
17
+ /**
18
+ * 输出类型
19
+ * @default './uni-pages.d.ts'
20
+ */
21
+ dts?: string;
22
22
  /**
23
23
  * 分包目录
24
24
  * @default []
@@ -26,7 +26,7 @@ interface AutoPagesPluginOptions {
26
26
  subPackages?: 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
  }
@@ -215,11 +224,13 @@ function uniAutoPages(opts) {
215
224
  if (!pagesJson) return;
216
225
  const routes = collectFileRoutes(srcRoot, getScanDirs(dir, subPackages));
217
226
  const { merged, changed } = getPagesByFileRoute(routes, pagesJson, subPackages);
218
- if (changed) fs.writeFileSync(pagesJsonPath, JSON.stringify(merged, null, 2) + "\n");
219
- if (dts) {
220
- const dtsFilePath = resolveDtsFilePath(srcRoot, dts);
221
- const dtsContent = buildRouteDts(routes);
222
- 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");
223
234
  }
224
235
  }
225
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.1",
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": [