@uni-helper/vite-plugin-uni-pages 0.2.8 → 0.2.9

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
@@ -4,16 +4,17 @@ Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  const path = require('node:path');
6
6
  const node_child_process = require('node:child_process');
7
+ const process = require('node:process');
7
8
  const vite = require('vite');
8
9
  const MagicString = require('magic-string');
9
10
  const chokidar = require('chokidar');
10
- const process$1 = require('node:process');
11
11
  const unconfig = require('unconfig');
12
12
  const uniEnv = require('@uni-helper/uni-env');
13
13
  const Debug = require('debug');
14
+ const fs = require('node:fs');
15
+ const promises = require('node:fs/promises');
14
16
  const lodashUnified = require('lodash-unified');
15
17
  const fg = require('fast-glob');
16
- const fs = require('node:fs');
17
18
  const JSON5 = require('json5');
18
19
  const yaml = require('yaml');
19
20
  const compilerSfc = require('@vue/compiler-sfc');
@@ -21,18 +22,64 @@ const compilerSfc = require('@vue/compiler-sfc');
21
22
  function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
22
23
 
23
24
  const path__default = /*#__PURE__*/_interopDefaultCompat(path);
25
+ const process__default = /*#__PURE__*/_interopDefaultCompat(process);
24
26
  const MagicString__default = /*#__PURE__*/_interopDefaultCompat(MagicString);
25
27
  const chokidar__default = /*#__PURE__*/_interopDefaultCompat(chokidar);
26
- const process__default = /*#__PURE__*/_interopDefaultCompat(process$1);
27
28
  const Debug__default = /*#__PURE__*/_interopDefaultCompat(Debug);
28
- const fg__default = /*#__PURE__*/_interopDefaultCompat(fg);
29
29
  const fs__default = /*#__PURE__*/_interopDefaultCompat(fs);
30
+ const fg__default = /*#__PURE__*/_interopDefaultCompat(fg);
30
31
  const JSON5__default = /*#__PURE__*/_interopDefaultCompat(JSON5);
31
32
 
32
33
  function slash(str) {
33
34
  return str.replace(/\\/g, "/");
34
35
  }
35
36
 
37
+ function getDeclaration(ctx) {
38
+ const subPagesPath = ctx.subPageMetaData.map((sub) => {
39
+ return sub.pages.map((v) => `"${vite.normalizePath(path.join(sub.root, v.path))}"`);
40
+ }).flat();
41
+ const tabsPagesPath = ctx.pagesGlobConfig?.tabBar?.list?.map((v) => {
42
+ return `"${v.pagePath}"`;
43
+ }) ?? [];
44
+ const allPagesPath = [...ctx.pageMetaData.filter((page) => !tabsPagesPath.includes(page.path)).map((v) => `"${v.path}"`), ...subPagesPath];
45
+ const code = `/* eslint-disable */
46
+ /* prettier-ignore */
47
+ // @ts-nocheck
48
+ // Generated by vite-plugin-uni-pages
49
+
50
+ interface NavigateToOptions {
51
+ url: ${allPagesPath.join(" |\n ")};
52
+ }
53
+ interface RedirectToOptions extends NavigateToOptions {}
54
+
55
+ interface SwitchTabOptions {
56
+ ${tabsPagesPath.length ? `url: ${tabsPagesPath.join(" | ")}` : ""}
57
+ }
58
+
59
+ type ReLaunchOptions = NavigateToOptions | SwitchTabOptions;
60
+
61
+ declare interface Uni {
62
+ navigateTo(options: UniNamespace.NavigateToOptions & NavigateToOptions): void;
63
+ redirectTo(options: UniNamespace.RedirectToOptions & RedirectToOptions): void;
64
+ switchTab(options: UniNamespace.SwitchTabOptions & SwitchTabOptions): void;
65
+ reLaunch(options: UniNamespace.ReLaunchOptions & ReLaunchOptions): void;
66
+ }
67
+ `;
68
+ return code;
69
+ }
70
+ async function writeFile(filePath, content) {
71
+ await promises.mkdir(path.dirname(filePath), { recursive: true });
72
+ return await promises.writeFile(filePath, content, "utf-8");
73
+ }
74
+ async function writeDeclaration(ctx, filepath) {
75
+ const originalContent = fs.existsSync(filepath) ? await promises.readFile(filepath, "utf-8") : "";
76
+ const code = getDeclaration(ctx);
77
+ if (!code)
78
+ return;
79
+ if (code !== originalContent)
80
+ await writeFile(filepath, code);
81
+ }
82
+
36
83
  const MODULE_ID_VIRTUAL = "virtual:uni-pages";
37
84
  const RESOLVED_MODULE_ID_VIRTUAL = `\0${MODULE_ID_VIRTUAL}`;
38
85
  const OUTPUT_NAME = "pages.json";
@@ -121,7 +168,8 @@ const debug = {
121
168
  pages: Debug__default("vite-plugin-uni-pages:pages"),
122
169
  subPages: Debug__default("vite-plugin-uni-pages:subPages"),
123
170
  error: Debug__default("vite-plugin-uni-pages:error"),
124
- cache: Debug__default("vite-plugin-uni-pages:cache")
171
+ cache: Debug__default("vite-plugin-uni-pages:cache"),
172
+ declaration: Debug__default("vite-plugin-uni-pages:declaration")
125
173
  };
126
174
  function extsToGlob(extensions) {
127
175
  return extensions.length > 1 ? `{${extensions.join(",")}}` : extensions[0] || "";
@@ -176,8 +224,9 @@ function useCachedPages() {
176
224
  };
177
225
  }
178
226
 
179
- function resolveOptions(userOptions, viteRoot) {
227
+ function resolveOptions(userOptions, viteRoot = process__default.cwd()) {
180
228
  const {
229
+ dts = true,
181
230
  configSource = "pages.config",
182
231
  homePage = ["pages/index", "pages/index/index"],
183
232
  mergePages = true,
@@ -210,7 +259,9 @@ function resolveOptions(userOptions, viteRoot) {
210
259
  const resolvedSubDirs = subPackages.map((dir2) => slash(dir2));
211
260
  const resolvedHomePage = typeof homePage === "string" ? [homePage] : homePage;
212
261
  const resolvedConfigSource = typeof configSource === "string" ? [{ files: configSource }] : configSource;
262
+ const resolvedDts = !dts ? false : typeof dts === "string" ? dts : path.resolve(viteRoot, "uni-pages.d.ts");
213
263
  const resolvedOptions = {
264
+ dts: resolvedDts,
214
265
  configSource: Array.isArray(resolvedConfigSource) ? resolvedConfigSource : [resolvedConfigSource],
215
266
  homePage: resolvedHomePage,
216
267
  mergePages,
@@ -336,7 +387,7 @@ class PageContext {
336
387
  watcher.add(configs);
337
388
  }
338
389
  const targetDirs = [...this.options.dirs, ...this.options.subPackages].map((v) => slash(path__default.resolve(this.root, v)));
339
- const isInTargetDirs = (path2) => targetDirs.some((v) => path2.startsWith(v));
390
+ const isInTargetDirs = (filePath) => targetDirs.some((v) => slash(path__default.resolve(this.root, filePath)).startsWith(v));
340
391
  watcher.on("add", async (path2) => {
341
392
  path2 = slash(path2);
342
393
  if (!isTargetFile(path2))
@@ -476,6 +527,7 @@ class PageContext {
476
527
  subPackages: this.subPageMetaData
477
528
  };
478
529
  const pagesJson = JSON.stringify(data, null, this.options.minify ? void 0 : 2);
530
+ this.generateDeclaration();
479
531
  if (lsatPagesJson === pagesJson) {
480
532
  debug.pages("PagesJson Not have change");
481
533
  return false;
@@ -496,6 +548,12 @@ class PageContext {
496
548
  resolveSubRoutes() {
497
549
  return JSON.stringify(this.subPageMetaData, null, 2);
498
550
  }
551
+ generateDeclaration() {
552
+ if (!this.options.dts)
553
+ return;
554
+ debug.declaration("generating");
555
+ return writeDeclaration(this, this.options.dts);
556
+ }
499
557
  }
500
558
  function getPagePaths(dir, options) {
501
559
  const pagesDirPath = slash(path__default.resolve(options.root, dir));
@@ -515,22 +573,22 @@ function defineUniPages(config) {
515
573
 
516
574
  async function restart() {
517
575
  return new Promise((resolve) => {
518
- const build = node_child_process.spawn(process.argv.shift(), process.argv, {
519
- cwd: process.cwd(),
576
+ const build = node_child_process.spawn(process__default.argv.shift(), process__default.argv, {
577
+ cwd: process__default.cwd(),
520
578
  detached: true,
521
- env: process.env
579
+ env: process__default.env
522
580
  });
523
- build.stdout?.pipe(process.stdout);
524
- build.stderr?.pipe(process.stderr);
581
+ build.stdout?.pipe(process__default.stdout);
582
+ build.stderr?.pipe(process__default.stderr);
525
583
  build.on("close", (code) => {
526
- resolve(process.exit(code));
584
+ resolve(process__default.exit(code));
527
585
  });
528
586
  });
529
587
  }
530
588
  function VitePluginUniPages(userOptions = {}) {
531
589
  let ctx;
532
590
  const resolvedPagesJSONPath = path__default.join(
533
- process.cwd(),
591
+ process__default.cwd(),
534
592
  userOptions.outDir ?? "src",
535
593
  OUTPUT_NAME
536
594
  );
package/dist/index.d.cts CHANGED
@@ -665,6 +665,7 @@ declare class PageContext {
665
665
  virtualModule(): string;
666
666
  resolveRoutes(): string;
667
667
  resolveSubRoutes(): string;
668
+ generateDeclaration(): Promise<void> | undefined;
668
669
  }
669
670
 
670
671
  declare function invalidatePagesModule(server: ViteDevServer): void;
@@ -676,6 +677,7 @@ declare const debug: {
676
677
  subPages: Debug.Debugger;
677
678
  error: Debug.Debugger;
678
679
  cache: Debug.Debugger;
680
+ declaration: Debug.Debugger;
679
681
  };
680
682
  declare function extsToGlob(extensions: string[]): string;
681
683
  declare function isTargetFile(path: string): boolean;
@@ -699,6 +701,14 @@ interface CustomBlock {
699
701
  type debugType = keyof typeof debug;
700
702
  type ConfigSource = string | LoadConfigSource<PagesConfig> | LoadConfigSource<PagesConfig>[];
701
703
  interface Options {
704
+ /**
705
+ * Generate TypeScript declaration for pages path
706
+ *
707
+ * Accept boolean or a path related to project root
708
+ *
709
+ * @default true
710
+ */
711
+ dts?: boolean | string;
702
712
  /**
703
713
  * Load from configs files
704
714
  *
@@ -759,12 +769,13 @@ interface Options {
759
769
  onAfterWriteFile: (ctx: PageContext) => void;
760
770
  }
761
771
  type UserOptions = Partial<Options>;
762
- interface ResolvedOptions extends Omit<Options, 'dir' | 'homePage' | 'configSource'> {
772
+ interface ResolvedOptions extends Omit<Options, 'dir' | 'homePage' | 'configSource' | 'dts'> {
763
773
  /**
764
774
  * Resolves to the `root` value from Vite config.
765
775
  * @default config.root
766
776
  */
767
777
  root: string;
778
+ dts: string | false;
768
779
  /**
769
780
  * Resolved page dirs
770
781
  */
package/dist/index.d.mts CHANGED
@@ -665,6 +665,7 @@ declare class PageContext {
665
665
  virtualModule(): string;
666
666
  resolveRoutes(): string;
667
667
  resolveSubRoutes(): string;
668
+ generateDeclaration(): Promise<void> | undefined;
668
669
  }
669
670
 
670
671
  declare function invalidatePagesModule(server: ViteDevServer): void;
@@ -676,6 +677,7 @@ declare const debug: {
676
677
  subPages: Debug.Debugger;
677
678
  error: Debug.Debugger;
678
679
  cache: Debug.Debugger;
680
+ declaration: Debug.Debugger;
679
681
  };
680
682
  declare function extsToGlob(extensions: string[]): string;
681
683
  declare function isTargetFile(path: string): boolean;
@@ -699,6 +701,14 @@ interface CustomBlock {
699
701
  type debugType = keyof typeof debug;
700
702
  type ConfigSource = string | LoadConfigSource<PagesConfig> | LoadConfigSource<PagesConfig>[];
701
703
  interface Options {
704
+ /**
705
+ * Generate TypeScript declaration for pages path
706
+ *
707
+ * Accept boolean or a path related to project root
708
+ *
709
+ * @default true
710
+ */
711
+ dts?: boolean | string;
702
712
  /**
703
713
  * Load from configs files
704
714
  *
@@ -759,12 +769,13 @@ interface Options {
759
769
  onAfterWriteFile: (ctx: PageContext) => void;
760
770
  }
761
771
  type UserOptions = Partial<Options>;
762
- interface ResolvedOptions extends Omit<Options, 'dir' | 'homePage' | 'configSource'> {
772
+ interface ResolvedOptions extends Omit<Options, 'dir' | 'homePage' | 'configSource' | 'dts'> {
763
773
  /**
764
774
  * Resolves to the `root` value from Vite config.
765
775
  * @default config.root
766
776
  */
767
777
  root: string;
778
+ dts: string | false;
768
779
  /**
769
780
  * Resolved page dirs
770
781
  */
package/dist/index.d.ts CHANGED
@@ -665,6 +665,7 @@ declare class PageContext {
665
665
  virtualModule(): string;
666
666
  resolveRoutes(): string;
667
667
  resolveSubRoutes(): string;
668
+ generateDeclaration(): Promise<void> | undefined;
668
669
  }
669
670
 
670
671
  declare function invalidatePagesModule(server: ViteDevServer): void;
@@ -676,6 +677,7 @@ declare const debug: {
676
677
  subPages: Debug.Debugger;
677
678
  error: Debug.Debugger;
678
679
  cache: Debug.Debugger;
680
+ declaration: Debug.Debugger;
679
681
  };
680
682
  declare function extsToGlob(extensions: string[]): string;
681
683
  declare function isTargetFile(path: string): boolean;
@@ -699,6 +701,14 @@ interface CustomBlock {
699
701
  type debugType = keyof typeof debug;
700
702
  type ConfigSource = string | LoadConfigSource<PagesConfig> | LoadConfigSource<PagesConfig>[];
701
703
  interface Options {
704
+ /**
705
+ * Generate TypeScript declaration for pages path
706
+ *
707
+ * Accept boolean or a path related to project root
708
+ *
709
+ * @default true
710
+ */
711
+ dts?: boolean | string;
702
712
  /**
703
713
  * Load from configs files
704
714
  *
@@ -759,12 +769,13 @@ interface Options {
759
769
  onAfterWriteFile: (ctx: PageContext) => void;
760
770
  }
761
771
  type UserOptions = Partial<Options>;
762
- interface ResolvedOptions extends Omit<Options, 'dir' | 'homePage' | 'configSource'> {
772
+ interface ResolvedOptions extends Omit<Options, 'dir' | 'homePage' | 'configSource' | 'dts'> {
763
773
  /**
764
774
  * Resolves to the `root` value from Vite config.
765
775
  * @default config.root
766
776
  */
767
777
  root: string;
778
+ dts: string | false;
768
779
  /**
769
780
  * Resolved page dirs
770
781
  */
package/dist/index.mjs CHANGED
@@ -1,15 +1,16 @@
1
- import path from 'node:path';
1
+ import path, { join, dirname, resolve } from 'node:path';
2
2
  import { spawn } from 'node:child_process';
3
+ import process from 'node:process';
3
4
  import { normalizePath, createLogger } from 'vite';
4
5
  import MagicString from 'magic-string';
5
6
  import chokidar from 'chokidar';
6
- import process$1 from 'node:process';
7
7
  import { loadConfig } from 'unconfig';
8
8
  import { isH5 } from '@uni-helper/uni-env';
9
9
  import Debug from 'debug';
10
+ import fs, { existsSync } from 'node:fs';
11
+ import { readFile, mkdir, writeFile as writeFile$1 } from 'node:fs/promises';
10
12
  import { groupBy } from 'lodash-unified';
11
13
  import fg from 'fast-glob';
12
- import fs from 'node:fs';
13
14
  import JSON5 from 'json5';
14
15
  import { parse as parse$1 } from 'yaml';
15
16
  import { parse } from '@vue/compiler-sfc';
@@ -18,6 +19,52 @@ function slash(str) {
18
19
  return str.replace(/\\/g, "/");
19
20
  }
20
21
 
22
+ function getDeclaration(ctx) {
23
+ const subPagesPath = ctx.subPageMetaData.map((sub) => {
24
+ return sub.pages.map((v) => `"${normalizePath(join(sub.root, v.path))}"`);
25
+ }).flat();
26
+ const tabsPagesPath = ctx.pagesGlobConfig?.tabBar?.list?.map((v) => {
27
+ return `"${v.pagePath}"`;
28
+ }) ?? [];
29
+ const allPagesPath = [...ctx.pageMetaData.filter((page) => !tabsPagesPath.includes(page.path)).map((v) => `"${v.path}"`), ...subPagesPath];
30
+ const code = `/* eslint-disable */
31
+ /* prettier-ignore */
32
+ // @ts-nocheck
33
+ // Generated by vite-plugin-uni-pages
34
+
35
+ interface NavigateToOptions {
36
+ url: ${allPagesPath.join(" |\n ")};
37
+ }
38
+ interface RedirectToOptions extends NavigateToOptions {}
39
+
40
+ interface SwitchTabOptions {
41
+ ${tabsPagesPath.length ? `url: ${tabsPagesPath.join(" | ")}` : ""}
42
+ }
43
+
44
+ type ReLaunchOptions = NavigateToOptions | SwitchTabOptions;
45
+
46
+ declare interface Uni {
47
+ navigateTo(options: UniNamespace.NavigateToOptions & NavigateToOptions): void;
48
+ redirectTo(options: UniNamespace.RedirectToOptions & RedirectToOptions): void;
49
+ switchTab(options: UniNamespace.SwitchTabOptions & SwitchTabOptions): void;
50
+ reLaunch(options: UniNamespace.ReLaunchOptions & ReLaunchOptions): void;
51
+ }
52
+ `;
53
+ return code;
54
+ }
55
+ async function writeFile(filePath, content) {
56
+ await mkdir(dirname(filePath), { recursive: true });
57
+ return await writeFile$1(filePath, content, "utf-8");
58
+ }
59
+ async function writeDeclaration(ctx, filepath) {
60
+ const originalContent = existsSync(filepath) ? await readFile(filepath, "utf-8") : "";
61
+ const code = getDeclaration(ctx);
62
+ if (!code)
63
+ return;
64
+ if (code !== originalContent)
65
+ await writeFile(filepath, code);
66
+ }
67
+
21
68
  const MODULE_ID_VIRTUAL = "virtual:uni-pages";
22
69
  const RESOLVED_MODULE_ID_VIRTUAL = `\0${MODULE_ID_VIRTUAL}`;
23
70
  const OUTPUT_NAME = "pages.json";
@@ -106,7 +153,8 @@ const debug = {
106
153
  pages: Debug("vite-plugin-uni-pages:pages"),
107
154
  subPages: Debug("vite-plugin-uni-pages:subPages"),
108
155
  error: Debug("vite-plugin-uni-pages:error"),
109
- cache: Debug("vite-plugin-uni-pages:cache")
156
+ cache: Debug("vite-plugin-uni-pages:cache"),
157
+ declaration: Debug("vite-plugin-uni-pages:declaration")
110
158
  };
111
159
  function extsToGlob(extensions) {
112
160
  return extensions.length > 1 ? `{${extensions.join(",")}}` : extensions[0] || "";
@@ -161,8 +209,9 @@ function useCachedPages() {
161
209
  };
162
210
  }
163
211
 
164
- function resolveOptions(userOptions, viteRoot) {
212
+ function resolveOptions(userOptions, viteRoot = process.cwd()) {
165
213
  const {
214
+ dts = true,
166
215
  configSource = "pages.config",
167
216
  homePage = ["pages/index", "pages/index/index"],
168
217
  mergePages = true,
@@ -190,12 +239,14 @@ function resolveOptions(userOptions, viteRoot) {
190
239
  onAfterWriteFile = () => {
191
240
  }
192
241
  } = userOptions;
193
- const root = viteRoot || slash(process$1.cwd());
242
+ const root = viteRoot || slash(process.cwd());
194
243
  const resolvedDirs = resolvePageDirs(dir, root, exclude);
195
244
  const resolvedSubDirs = subPackages.map((dir2) => slash(dir2));
196
245
  const resolvedHomePage = typeof homePage === "string" ? [homePage] : homePage;
197
246
  const resolvedConfigSource = typeof configSource === "string" ? [{ files: configSource }] : configSource;
247
+ const resolvedDts = !dts ? false : typeof dts === "string" ? dts : resolve(viteRoot, "uni-pages.d.ts");
198
248
  const resolvedOptions = {
249
+ dts: resolvedDts,
199
250
  configSource: Array.isArray(resolvedConfigSource) ? resolvedConfigSource : [resolvedConfigSource],
200
251
  homePage: resolvedHomePage,
201
252
  mergePages,
@@ -259,7 +310,7 @@ var __publicField = (obj, key, value) => {
259
310
  let lsatPagesJson = "";
260
311
  const { setCache, hasChanged } = useCachedPages();
261
312
  class PageContext {
262
- constructor(userOptions, viteRoot = process$1.cwd()) {
313
+ constructor(userOptions, viteRoot = process.cwd()) {
263
314
  __publicField(this, "_server");
264
315
  __publicField(this, "pagesGlobConfig");
265
316
  __publicField(this, "pagesPath", []);
@@ -321,7 +372,7 @@ class PageContext {
321
372
  watcher.add(configs);
322
373
  }
323
374
  const targetDirs = [...this.options.dirs, ...this.options.subPackages].map((v) => slash(path.resolve(this.root, v)));
324
- const isInTargetDirs = (path2) => targetDirs.some((v) => path2.startsWith(v));
375
+ const isInTargetDirs = (filePath) => targetDirs.some((v) => slash(path.resolve(this.root, filePath)).startsWith(v));
325
376
  watcher.on("add", async (path2) => {
326
377
  path2 = slash(path2);
327
378
  if (!isTargetFile(path2))
@@ -461,6 +512,7 @@ class PageContext {
461
512
  subPackages: this.subPageMetaData
462
513
  };
463
514
  const pagesJson = JSON.stringify(data, null, this.options.minify ? void 0 : 2);
515
+ this.generateDeclaration();
464
516
  if (lsatPagesJson === pagesJson) {
465
517
  debug.pages("PagesJson Not have change");
466
518
  return false;
@@ -481,6 +533,12 @@ class PageContext {
481
533
  resolveSubRoutes() {
482
534
  return JSON.stringify(this.subPageMetaData, null, 2);
483
535
  }
536
+ generateDeclaration() {
537
+ if (!this.options.dts)
538
+ return;
539
+ debug.declaration("generating");
540
+ return writeDeclaration(this, this.options.dts);
541
+ }
484
542
  }
485
543
  function getPagePaths(dir, options) {
486
544
  const pagesDirPath = slash(path.resolve(options.root, dir));
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@uni-helper/vite-plugin-uni-pages",
3
3
  "type": "module",
4
- "version": "0.2.8",
4
+ "version": "0.2.9",
5
5
  "description": "File system based routing for uni-app applications using Vite",
6
6
  "author": "KeJun",
7
7
  "license": "MIT",
@@ -15,13 +15,13 @@
15
15
  "sideEffects": false,
16
16
  "exports": {
17
17
  ".": {
18
- "require": {
19
- "types": "./dist/index.d.cts",
20
- "default": "./dist/index.cjs"
21
- },
22
18
  "import": {
23
19
  "types": "./dist/index.d.mts",
24
20
  "default": "./dist/index.mjs"
21
+ },
22
+ "require": {
23
+ "types": "./dist/index.d.cts",
24
+ "default": "./dist/index.cjs"
25
25
  }
26
26
  },
27
27
  "./client": {