@uni-helper/vite-plugin-uni-pages 0.1.2 → 0.1.4

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
@@ -65,6 +65,13 @@ function mergePageMetaDataArray(pageMetaData) {
65
65
  }
66
66
  return result;
67
67
  }
68
+ const getPagesConfigSourcePaths = async () => {
69
+ return await fg("pages.config.(ts|mts|cts|js|cjs|mjs|json)", {
70
+ deep: 0,
71
+ onlyFiles: true,
72
+ absolute: true
73
+ });
74
+ };
68
75
 
69
76
  function resolveOptions(userOptions, viteRoot) {
70
77
  const {
@@ -154,10 +161,15 @@ async function parseSFC(code) {
154
161
  }
155
162
  function parseCustomBlock(block, filePath, options) {
156
163
  const lang = block.lang ?? options.routeBlockLang;
164
+ const attr = {
165
+ type: "page",
166
+ ...block.attrs
167
+ };
168
+ let content;
157
169
  debug.routeBlock(`use ${lang} parser`);
158
170
  if (lang === "json5") {
159
171
  try {
160
- return JSON5.parse(block.content);
172
+ content = JSON5.parse(block.content);
161
173
  } catch (err) {
162
174
  throw new Error(
163
175
  `Invalid JSON5 format of <${block.type}> content in ${filePath}
@@ -166,7 +178,7 @@ ${err.message}`
166
178
  }
167
179
  } else if (lang === "json") {
168
180
  try {
169
- return JSON.parse(block.content);
181
+ content = JSON.parse(block.content);
170
182
  } catch (err) {
171
183
  throw new Error(
172
184
  `Invalid JSON format of <${block.type}> content in ${filePath}
@@ -175,7 +187,7 @@ ${err.message}`
175
187
  }
176
188
  } else if (lang === "yaml" || lang === "yml") {
177
189
  try {
178
- return yaml.parse(block.content);
190
+ content = yaml.parse(block.content);
179
191
  } catch (err) {
180
192
  throw new Error(
181
193
  `Invalid YAML format of <${block.type}> content in ${filePath}
@@ -183,6 +195,12 @@ ${err.message}`
183
195
  );
184
196
  }
185
197
  }
198
+ if (content) {
199
+ return {
200
+ attr,
201
+ content
202
+ };
203
+ }
186
204
  }
187
205
  async function getRouteBlock(path, options) {
188
206
  const content = fs.readFileSync(path, "utf8");
@@ -239,7 +257,11 @@ class PageContext {
239
257
  this._server = server;
240
258
  this.setupWatcher(server.watcher);
241
259
  }
242
- setupWatcher(watcher) {
260
+ async setupWatcher(watcher) {
261
+ if (process.env.UNI_PLATFORM !== "h5") {
262
+ const configs = await getPagesConfigSourcePaths();
263
+ watcher.add(configs);
264
+ }
243
265
  watcher.on("add", async (path2) => {
244
266
  path2 = slash(path2);
245
267
  if (!isTargetFile(path2))
@@ -278,9 +300,9 @@ class PageContext {
278
300
  const { relativePath, absolutePath } = page;
279
301
  const routeBlock = await getRouteBlock(absolutePath, this.options);
280
302
  const relativePathWithFileName = relativePath.replace(path.extname(relativePath), "");
281
- const pageMetaDatum = { path: vite.normalizePath(relativePathWithFileName), type: "page" };
303
+ const pageMetaDatum = { path: vite.normalizePath(relativePathWithFileName), type: routeBlock?.attr.type ?? "page" };
282
304
  if (routeBlock)
283
- Object.assign(pageMetaDatum, routeBlock);
305
+ Object.assign(pageMetaDatum, routeBlock.content);
284
306
  return pageMetaDatum;
285
307
  }
286
308
  async mergePageMetaData() {
@@ -296,6 +318,7 @@ class PageContext {
296
318
  else
297
319
  this.pageMetaData = generatedPageMetaData;
298
320
  }
321
+ this.pageMetaData.sort((page) => page.type === "home" ? -1 : 0);
299
322
  debug.pages(generatedPageMetaData);
300
323
  }
301
324
  async updatePagesJSON() {
@@ -380,6 +403,7 @@ exports.default = VitePluginUniPages;
380
403
  exports.defineUniPages = defineUniPages;
381
404
  exports.extsToGlob = extsToGlob;
382
405
  exports.getPageFiles = getPageFiles;
406
+ exports.getPagesConfigSourcePaths = getPagesConfigSourcePaths;
383
407
  exports.getRouteBlock = getRouteBlock;
384
408
  exports.invalidatePagesModule = invalidatePagesModule;
385
409
  exports.isConfigFile = isConfigFile;
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { Logger, ViteDevServer, Plugin } from 'vite';
2
- import { FSWatcher } from 'fs';
2
+ import { FSWatcher } from 'chokidar';
3
3
  import Debug from 'debug';
4
4
  import { SFCDescriptor, SFCBlock } from '@vue/compiler-sfc';
5
5
 
@@ -624,7 +624,7 @@ declare class PageContext {
624
624
  loadUserPagesConfig(): Promise<void>;
625
625
  scanPages(): Promise<void>;
626
626
  setupViteServer(server: ViteDevServer): void;
627
- setupWatcher(watcher: FSWatcher): void;
627
+ setupWatcher(watcher: FSWatcher): Promise<void>;
628
628
  onUpdate(): void;
629
629
  parsePage(page: PagePath): Promise<PageMetaDatum>;
630
630
  mergePageMetaData(): Promise<void>;
@@ -633,7 +633,10 @@ declare class PageContext {
633
633
  resolveRoutes(): string;
634
634
  }
635
635
 
636
- type CustomBlock = Record<string, any>;
636
+ interface CustomBlock {
637
+ attr: Record<string, any>;
638
+ content: Record<string, any>;
639
+ }
637
640
  interface Options {
638
641
  /**
639
642
  * Whether to merge pages in pages.json
@@ -720,6 +723,7 @@ declare function isConfigFile(path: string): boolean;
720
723
  * TODO: support merge middleware
721
724
  */
722
725
  declare function mergePageMetaDataArray(pageMetaData: PageMetaDatum[]): PageMetaDatum[];
726
+ declare const getPagesConfigSourcePaths: () => Promise<string[]>;
723
727
 
724
728
  /**
725
729
  * Resolves the files that are valid pages for the given context.
@@ -735,9 +739,9 @@ declare function resolveOptions(userOptions: UserOptions, viteRoot?: string): Re
735
739
  declare function resolvePageDirs(dir: string, root: string, exclude: string[]): string[];
736
740
 
737
741
  declare function parseSFC(code: string): Promise<SFCDescriptor>;
738
- declare function parseCustomBlock(block: SFCBlock, filePath: string, options: ResolvedOptions): any;
742
+ declare function parseCustomBlock(block: SFCBlock, filePath: string, options: ResolvedOptions): CustomBlock | undefined;
739
743
  declare function getRouteBlock(path: string, options: ResolvedOptions): Promise<CustomBlock | undefined>;
740
744
 
741
745
  declare const VitePluginUniPages: (userOptions?: UserOptions) => Plugin;
742
746
 
743
- export { Condition, ConditionItem, CustomBlock, EasyCom, FILE_EXTENSIONS, GlobalStyle, IconFont, MODULE_ID_VIRTUAL, MidButton, OUTPUT_NAME, Options, PageContext, PageMetaDatum, PagePath, PagesConfig, PreloadRule, RESOLVED_MODULE_ID_VIRTUAL, ResolvedOptions, SubPackage, TabBar, TabBarItem, TheWindow, UserOptions, UserPagesConfig, VitePluginUniPages, checkPagesJsonFile, debug, VitePluginUniPages as default, defineUniPages, extsToGlob, getPageFiles, getRouteBlock, invalidatePagesModule, isConfigFile, isTargetFile, mergePageMetaDataArray, parseCustomBlock, parseSFC, resolveOptions, resolvePageDirs, writeFileSync };
747
+ export { Condition, ConditionItem, CustomBlock, EasyCom, FILE_EXTENSIONS, GlobalStyle, IconFont, MODULE_ID_VIRTUAL, MidButton, OUTPUT_NAME, Options, PageContext, PageMetaDatum, PagePath, PagesConfig, PreloadRule, RESOLVED_MODULE_ID_VIRTUAL, ResolvedOptions, SubPackage, TabBar, TabBarItem, TheWindow, UserOptions, UserPagesConfig, VitePluginUniPages, checkPagesJsonFile, debug, VitePluginUniPages as default, defineUniPages, extsToGlob, getPageFiles, getPagesConfigSourcePaths, getRouteBlock, invalidatePagesModule, isConfigFile, isTargetFile, mergePageMetaDataArray, parseCustomBlock, parseSFC, resolveOptions, resolvePageDirs, writeFileSync };
package/dist/index.mjs CHANGED
@@ -61,6 +61,13 @@ function mergePageMetaDataArray(pageMetaData) {
61
61
  }
62
62
  return result;
63
63
  }
64
+ const getPagesConfigSourcePaths = async () => {
65
+ return await fg("pages.config.(ts|mts|cts|js|cjs|mjs|json)", {
66
+ deep: 0,
67
+ onlyFiles: true,
68
+ absolute: true
69
+ });
70
+ };
64
71
 
65
72
  function resolveOptions(userOptions, viteRoot) {
66
73
  const {
@@ -150,10 +157,15 @@ async function parseSFC(code) {
150
157
  }
151
158
  function parseCustomBlock(block, filePath, options) {
152
159
  const lang = block.lang ?? options.routeBlockLang;
160
+ const attr = {
161
+ type: "page",
162
+ ...block.attrs
163
+ };
164
+ let content;
153
165
  debug.routeBlock(`use ${lang} parser`);
154
166
  if (lang === "json5") {
155
167
  try {
156
- return JSON5.parse(block.content);
168
+ content = JSON5.parse(block.content);
157
169
  } catch (err) {
158
170
  throw new Error(
159
171
  `Invalid JSON5 format of <${block.type}> content in ${filePath}
@@ -162,7 +174,7 @@ ${err.message}`
162
174
  }
163
175
  } else if (lang === "json") {
164
176
  try {
165
- return JSON.parse(block.content);
177
+ content = JSON.parse(block.content);
166
178
  } catch (err) {
167
179
  throw new Error(
168
180
  `Invalid JSON format of <${block.type}> content in ${filePath}
@@ -171,7 +183,7 @@ ${err.message}`
171
183
  }
172
184
  } else if (lang === "yaml" || lang === "yml") {
173
185
  try {
174
- return parse$1(block.content);
186
+ content = parse$1(block.content);
175
187
  } catch (err) {
176
188
  throw new Error(
177
189
  `Invalid YAML format of <${block.type}> content in ${filePath}
@@ -179,6 +191,12 @@ ${err.message}`
179
191
  );
180
192
  }
181
193
  }
194
+ if (content) {
195
+ return {
196
+ attr,
197
+ content
198
+ };
199
+ }
182
200
  }
183
201
  async function getRouteBlock(path, options) {
184
202
  const content = fs.readFileSync(path, "utf8");
@@ -235,7 +253,11 @@ class PageContext {
235
253
  this._server = server;
236
254
  this.setupWatcher(server.watcher);
237
255
  }
238
- setupWatcher(watcher) {
256
+ async setupWatcher(watcher) {
257
+ if (process.env.UNI_PLATFORM !== "h5") {
258
+ const configs = await getPagesConfigSourcePaths();
259
+ watcher.add(configs);
260
+ }
239
261
  watcher.on("add", async (path2) => {
240
262
  path2 = slash(path2);
241
263
  if (!isTargetFile(path2))
@@ -274,9 +296,9 @@ class PageContext {
274
296
  const { relativePath, absolutePath } = page;
275
297
  const routeBlock = await getRouteBlock(absolutePath, this.options);
276
298
  const relativePathWithFileName = relativePath.replace(path.extname(relativePath), "");
277
- const pageMetaDatum = { path: normalizePath(relativePathWithFileName), type: "page" };
299
+ const pageMetaDatum = { path: normalizePath(relativePathWithFileName), type: routeBlock?.attr.type ?? "page" };
278
300
  if (routeBlock)
279
- Object.assign(pageMetaDatum, routeBlock);
301
+ Object.assign(pageMetaDatum, routeBlock.content);
280
302
  return pageMetaDatum;
281
303
  }
282
304
  async mergePageMetaData() {
@@ -292,6 +314,7 @@ class PageContext {
292
314
  else
293
315
  this.pageMetaData = generatedPageMetaData;
294
316
  }
317
+ this.pageMetaData.sort((page) => page.type === "home" ? -1 : 0);
295
318
  debug.pages(generatedPageMetaData);
296
319
  }
297
320
  async updatePagesJSON() {
@@ -364,4 +387,4 @@ const VitePluginUniPages = (userOptions = {}) => {
364
387
  };
365
388
  };
366
389
 
367
- export { FILE_EXTENSIONS, MODULE_ID_VIRTUAL, OUTPUT_NAME, PageContext, RESOLVED_MODULE_ID_VIRTUAL, VitePluginUniPages, checkPagesJsonFile, debug, VitePluginUniPages as default, defineUniPages, extsToGlob, getPageFiles, getRouteBlock, invalidatePagesModule, isConfigFile, isTargetFile, mergePageMetaDataArray, parseCustomBlock, parseSFC, resolveOptions, resolvePageDirs, writeFileSync };
390
+ export { FILE_EXTENSIONS, MODULE_ID_VIRTUAL, OUTPUT_NAME, PageContext, RESOLVED_MODULE_ID_VIRTUAL, VitePluginUniPages, checkPagesJsonFile, debug, VitePluginUniPages as default, defineUniPages, extsToGlob, getPageFiles, getPagesConfigSourcePaths, getRouteBlock, invalidatePagesModule, isConfigFile, isTargetFile, mergePageMetaDataArray, parseCustomBlock, parseSFC, resolveOptions, resolvePageDirs, writeFileSync };
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.1.2",
4
+ "version": "0.1.4",
5
5
  "packageManager": "pnpm@7.16.1",
6
6
  "description": "File system based routing for uni-app applications using Vite",
7
7
  "author": "KeJun",