@uni-helper/vite-plugin-uni-pages 0.2.4 → 0.2.6

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
@@ -7,6 +7,7 @@ const node_child_process = require('node:child_process');
7
7
  const vite = require('vite');
8
8
  const MagicString = require('magic-string');
9
9
  const chokidar = require('chokidar');
10
+ const process$1 = require('node:process');
10
11
  const unconfig = require('unconfig');
11
12
  const uniEnv = require('@uni-helper/uni-env');
12
13
  const Debug = require('debug');
@@ -22,6 +23,7 @@ function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'defau
22
23
  const path__default = /*#__PURE__*/_interopDefaultCompat(path);
23
24
  const MagicString__default = /*#__PURE__*/_interopDefaultCompat(MagicString);
24
25
  const chokidar__default = /*#__PURE__*/_interopDefaultCompat(chokidar);
26
+ const process__default = /*#__PURE__*/_interopDefaultCompat(process$1);
25
27
  const Debug__default = /*#__PURE__*/_interopDefaultCompat(Debug);
26
28
  const fg__default = /*#__PURE__*/_interopDefaultCompat(fg);
27
29
  const fs__default = /*#__PURE__*/_interopDefaultCompat(fs);
@@ -202,7 +204,7 @@ function resolveOptions(userOptions, viteRoot) {
202
204
  onAfterWriteFile = () => {
203
205
  }
204
206
  } = userOptions;
205
- const root = viteRoot || slash(process.cwd());
207
+ const root = viteRoot || slash(process__default.cwd());
206
208
  const resolvedDirs = resolvePageDirs(dir, root, exclude);
207
209
  const resolvedSubDirs = subPackages.map((dir2) => slash(dir2));
208
210
  const ResolvedHomePage = typeof homePage === "string" ? [homePage] : homePage;
@@ -263,7 +265,7 @@ function writeFileSync(path, content) {
263
265
  let lsatPagesJson = "";
264
266
  const { setCache, hasChanged } = useCachedPages();
265
267
  class PageContext {
266
- constructor(userOptions, viteRoot = process.cwd()) {
268
+ constructor(userOptions, viteRoot = process__default.cwd()) {
267
269
  this.pagesPath = [];
268
270
  this.subPagesPath = {};
269
271
  this.pageMetaData = [];
@@ -286,10 +288,10 @@ class PageContext {
286
288
  this.logger = logger;
287
289
  }
288
290
  async loadUserPagesConfig() {
289
- const { config } = await unconfig.loadConfig({ sources: [{ files: "pages.config" }] });
291
+ const { config } = await unconfig.loadConfig({ cwd: this.root, sources: [{ files: "pages.config" }] });
290
292
  if (!config) {
291
293
  this.logger?.warn("Can't found pages.config, please create pages.config.(ts|mts|cts|js|cjs|mjs|json)");
292
- process.exit(-1);
294
+ process__default.exit(-1);
293
295
  }
294
296
  this.pagesGlobConfig = config;
295
297
  debug.options(config);
@@ -321,10 +323,14 @@ class PageContext {
321
323
  const configs = await getPagesConfigSourcePaths();
322
324
  watcher.add(configs);
323
325
  }
326
+ const targetDirs = [...this.options.dirs, ...this.options.subPackages].map((v) => slash(path__default.resolve(this.root, v)));
327
+ const isInTargetDirs = (path2) => targetDirs.some((v) => path2.startsWith(v));
324
328
  watcher.on("add", async (path2) => {
325
329
  path2 = slash(path2);
326
330
  if (!isTargetFile(path2))
327
331
  return;
332
+ if (!isInTargetDirs(path2))
333
+ return;
328
334
  debug.pages(`File added: ${path2}`);
329
335
  if (await this.updatePagesJSON())
330
336
  this.onUpdate();
@@ -333,7 +339,11 @@ class PageContext {
333
339
  path2 = slash(path2);
334
340
  if (!isTargetFile(path2) && !isConfigFile(path2))
335
341
  return;
342
+ if (!isInTargetDirs(path2))
343
+ return;
336
344
  debug.pages(`File changed: ${path2}`);
345
+ debug.pages(targetDirs);
346
+ debug.pages(isInTargetDirs(path2));
337
347
  if (await this.updatePagesJSON(path2))
338
348
  this.onUpdate();
339
349
  });
@@ -341,6 +351,8 @@ class PageContext {
341
351
  path2 = slash(path2);
342
352
  if (!isTargetFile(path2))
343
353
  return;
354
+ if (!isInTargetDirs(path2))
355
+ return;
344
356
  debug.pages(`File removed: ${path2}`);
345
357
  if (await this.updatePagesJSON())
346
358
  this.onUpdate();
@@ -534,11 +546,9 @@ function VitePluginUniPages(userOptions = {}) {
534
546
  },
535
547
  // Applet do not support custom route block, so we need to remove the route block here
536
548
  async transform(code, id) {
537
- if (!/\.vue$/.test(id))
549
+ if (!/\.n?vue$/.test(id) && !code.includes("</route>"))
538
550
  return null;
539
- const s = new MagicString__default(code.toString(), {
540
- filename: id
541
- });
551
+ const s = new MagicString__default(code);
542
552
  const routeBlockMatches = s.original.matchAll(
543
553
  /<route[^>]*>([\s\S]*?)<\/route>/g
544
554
  );
@@ -547,12 +557,16 @@ function VitePluginUniPages(userOptions = {}) {
547
557
  const length = match[0].length;
548
558
  s.remove(index, index + length);
549
559
  }
550
- return {
551
- code: s.toString(),
552
- map: s.generateMap({
553
- source: code
554
- })
555
- };
560
+ if (s.hasChanged()) {
561
+ return {
562
+ code: s.toString(),
563
+ map: s.generateMap({
564
+ source: id,
565
+ includeContent: true,
566
+ file: `${id}.map`
567
+ })
568
+ };
569
+ }
556
570
  },
557
571
  configureServer(server) {
558
572
  ctx.setupViteServer(server);
package/dist/index.mjs CHANGED
@@ -3,6 +3,7 @@ import { spawn } from 'node:child_process';
3
3
  import { normalizePath, createLogger } from 'vite';
4
4
  import MagicString from 'magic-string';
5
5
  import chokidar from 'chokidar';
6
+ import process$1 from 'node:process';
6
7
  import { loadConfig } from 'unconfig';
7
8
  import { isH5 } from '@uni-helper/uni-env';
8
9
  import Debug from 'debug';
@@ -188,7 +189,7 @@ function resolveOptions(userOptions, viteRoot) {
188
189
  onAfterWriteFile = () => {
189
190
  }
190
191
  } = userOptions;
191
- const root = viteRoot || slash(process.cwd());
192
+ const root = viteRoot || slash(process$1.cwd());
192
193
  const resolvedDirs = resolvePageDirs(dir, root, exclude);
193
194
  const resolvedSubDirs = subPackages.map((dir2) => slash(dir2));
194
195
  const ResolvedHomePage = typeof homePage === "string" ? [homePage] : homePage;
@@ -249,7 +250,7 @@ function writeFileSync(path, content) {
249
250
  let lsatPagesJson = "";
250
251
  const { setCache, hasChanged } = useCachedPages();
251
252
  class PageContext {
252
- constructor(userOptions, viteRoot = process.cwd()) {
253
+ constructor(userOptions, viteRoot = process$1.cwd()) {
253
254
  this.pagesPath = [];
254
255
  this.subPagesPath = {};
255
256
  this.pageMetaData = [];
@@ -272,10 +273,10 @@ class PageContext {
272
273
  this.logger = logger;
273
274
  }
274
275
  async loadUserPagesConfig() {
275
- const { config } = await loadConfig({ sources: [{ files: "pages.config" }] });
276
+ const { config } = await loadConfig({ cwd: this.root, sources: [{ files: "pages.config" }] });
276
277
  if (!config) {
277
278
  this.logger?.warn("Can't found pages.config, please create pages.config.(ts|mts|cts|js|cjs|mjs|json)");
278
- process.exit(-1);
279
+ process$1.exit(-1);
279
280
  }
280
281
  this.pagesGlobConfig = config;
281
282
  debug.options(config);
@@ -307,10 +308,14 @@ class PageContext {
307
308
  const configs = await getPagesConfigSourcePaths();
308
309
  watcher.add(configs);
309
310
  }
311
+ const targetDirs = [...this.options.dirs, ...this.options.subPackages].map((v) => slash(path.resolve(this.root, v)));
312
+ const isInTargetDirs = (path2) => targetDirs.some((v) => path2.startsWith(v));
310
313
  watcher.on("add", async (path2) => {
311
314
  path2 = slash(path2);
312
315
  if (!isTargetFile(path2))
313
316
  return;
317
+ if (!isInTargetDirs(path2))
318
+ return;
314
319
  debug.pages(`File added: ${path2}`);
315
320
  if (await this.updatePagesJSON())
316
321
  this.onUpdate();
@@ -319,7 +324,11 @@ class PageContext {
319
324
  path2 = slash(path2);
320
325
  if (!isTargetFile(path2) && !isConfigFile(path2))
321
326
  return;
327
+ if (!isInTargetDirs(path2))
328
+ return;
322
329
  debug.pages(`File changed: ${path2}`);
330
+ debug.pages(targetDirs);
331
+ debug.pages(isInTargetDirs(path2));
323
332
  if (await this.updatePagesJSON(path2))
324
333
  this.onUpdate();
325
334
  });
@@ -327,6 +336,8 @@ class PageContext {
327
336
  path2 = slash(path2);
328
337
  if (!isTargetFile(path2))
329
338
  return;
339
+ if (!isInTargetDirs(path2))
340
+ return;
330
341
  debug.pages(`File removed: ${path2}`);
331
342
  if (await this.updatePagesJSON())
332
343
  this.onUpdate();
@@ -520,11 +531,9 @@ function VitePluginUniPages(userOptions = {}) {
520
531
  },
521
532
  // Applet do not support custom route block, so we need to remove the route block here
522
533
  async transform(code, id) {
523
- if (!/\.vue$/.test(id))
534
+ if (!/\.n?vue$/.test(id) && !code.includes("</route>"))
524
535
  return null;
525
- const s = new MagicString(code.toString(), {
526
- filename: id
527
- });
536
+ const s = new MagicString(code);
528
537
  const routeBlockMatches = s.original.matchAll(
529
538
  /<route[^>]*>([\s\S]*?)<\/route>/g
530
539
  );
@@ -533,12 +542,16 @@ function VitePluginUniPages(userOptions = {}) {
533
542
  const length = match[0].length;
534
543
  s.remove(index, index + length);
535
544
  }
536
- return {
537
- code: s.toString(),
538
- map: s.generateMap({
539
- source: code
540
- })
541
- };
545
+ if (s.hasChanged()) {
546
+ return {
547
+ code: s.toString(),
548
+ map: s.generateMap({
549
+ source: id,
550
+ includeContent: true,
551
+ file: `${id}.map`
552
+ })
553
+ };
554
+ }
542
555
  },
543
556
  configureServer(server) {
544
557
  ctx.setupViteServer(server);
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.4",
4
+ "version": "0.2.6",
5
5
  "description": "File system based routing for uni-app applications using Vite",
6
6
  "author": "KeJun",
7
7
  "license": "MIT",
@@ -32,20 +32,20 @@
32
32
  ],
33
33
  "dependencies": {
34
34
  "@uni-helper/uni-env": "^0.0.3",
35
- "@vue/compiler-sfc": "^3.3.1",
35
+ "@vue/compiler-sfc": "^3.3.4",
36
36
  "chokidar": "^3.5.3",
37
37
  "debug": "^4.3.4",
38
- "fast-glob": "^3.2.12",
38
+ "fast-glob": "^3.3.1",
39
39
  "json5": "^2.2.3",
40
40
  "lodash-unified": "^1.0.3",
41
- "magic-string": "^0.30.0",
42
- "unconfig": "^0.3.7",
43
- "yaml": "^2.2.2"
41
+ "magic-string": "^0.30.2",
42
+ "unconfig": "^0.3.10",
43
+ "yaml": "^2.3.1"
44
44
  },
45
45
  "devDependencies": {
46
- "@antfu/utils": "^0.7.2",
47
- "@types/debug": "^4.1.7",
48
- "@types/node": "^18.16.8"
46
+ "@antfu/utils": "^0.7.5",
47
+ "@types/debug": "^4.1.8",
48
+ "@types/node": "^18.17.1"
49
49
  },
50
50
  "scripts": {
51
51
  "build": "unbuild",