@uni-helper/vite-plugin-uni-pages 0.1.4 → 0.1.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 +76 -28
- package/dist/index.d.ts +47 -12
- package/dist/index.mjs +48 -10
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -3,9 +3,10 @@
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
5
|
const path = require('path');
|
|
6
|
+
const child_process = require('child_process');
|
|
7
|
+
const vite = require('vite');
|
|
6
8
|
const MagicString = require('magic-string');
|
|
7
9
|
const chokidar = require('chokidar');
|
|
8
|
-
const vite = require('vite');
|
|
9
10
|
const unconfig = require('unconfig');
|
|
10
11
|
const Debug = require('debug');
|
|
11
12
|
const lodash = require('lodash');
|
|
@@ -15,6 +16,16 @@ const JSON5 = require('json5');
|
|
|
15
16
|
const yaml = require('yaml');
|
|
16
17
|
const compilerSfc = require('@vue/compiler-sfc');
|
|
17
18
|
|
|
19
|
+
function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
|
|
20
|
+
|
|
21
|
+
const path__default = /*#__PURE__*/_interopDefaultCompat(path);
|
|
22
|
+
const MagicString__default = /*#__PURE__*/_interopDefaultCompat(MagicString);
|
|
23
|
+
const chokidar__default = /*#__PURE__*/_interopDefaultCompat(chokidar);
|
|
24
|
+
const Debug__default = /*#__PURE__*/_interopDefaultCompat(Debug);
|
|
25
|
+
const fg__default = /*#__PURE__*/_interopDefaultCompat(fg);
|
|
26
|
+
const fs__default = /*#__PURE__*/_interopDefaultCompat(fs);
|
|
27
|
+
const JSON5__default = /*#__PURE__*/_interopDefaultCompat(JSON5);
|
|
28
|
+
|
|
18
29
|
function slash(str) {
|
|
19
30
|
return str.replace(/\\/g, "/");
|
|
20
31
|
}
|
|
@@ -35,11 +46,11 @@ function invalidatePagesModule(server) {
|
|
|
35
46
|
}
|
|
36
47
|
}
|
|
37
48
|
const debug = {
|
|
38
|
-
hmr:
|
|
39
|
-
routeBlock:
|
|
40
|
-
options:
|
|
41
|
-
pages:
|
|
42
|
-
error:
|
|
49
|
+
hmr: Debug__default("vite-plugin-uni-pages:hmr"),
|
|
50
|
+
routeBlock: Debug__default("vite-plugin-uni-pages:routeBlock"),
|
|
51
|
+
options: Debug__default("vite-plugin-uni-pages:options"),
|
|
52
|
+
pages: Debug__default("vite-plugin-uni-pages:pages"),
|
|
53
|
+
error: Debug__default("vite-plugin-uni-pages:error")
|
|
43
54
|
};
|
|
44
55
|
function extsToGlob(extensions) {
|
|
45
56
|
return extensions.length > 1 ? `{${extensions.join(",")}}` : extensions[0] || "";
|
|
@@ -66,7 +77,7 @@ function mergePageMetaDataArray(pageMetaData) {
|
|
|
66
77
|
return result;
|
|
67
78
|
}
|
|
68
79
|
const getPagesConfigSourcePaths = async () => {
|
|
69
|
-
return await
|
|
80
|
+
return await fg__default("pages.config.(ts|mts|cts|js|cjs|mjs|json)", {
|
|
70
81
|
deep: 0,
|
|
71
82
|
onlyFiles: true,
|
|
72
83
|
absolute: true
|
|
@@ -80,6 +91,7 @@ function resolveOptions(userOptions, viteRoot) {
|
|
|
80
91
|
outDir = "src",
|
|
81
92
|
exclude = ["node_modules", ".git", "**/__*__/**"],
|
|
82
93
|
routeBlockLang = "json5",
|
|
94
|
+
minify = false,
|
|
83
95
|
onBeforeLoadUserConfig = () => {
|
|
84
96
|
},
|
|
85
97
|
onAfterLoadUserConfig = () => {
|
|
@@ -106,6 +118,7 @@ function resolveOptions(userOptions, viteRoot) {
|
|
|
106
118
|
exclude,
|
|
107
119
|
routeBlockLang,
|
|
108
120
|
root,
|
|
121
|
+
minify,
|
|
109
122
|
onBeforeLoadUserConfig,
|
|
110
123
|
onAfterLoadUserConfig,
|
|
111
124
|
onBeforeScanPages,
|
|
@@ -118,7 +131,7 @@ function resolveOptions(userOptions, viteRoot) {
|
|
|
118
131
|
return resolvedOptions;
|
|
119
132
|
}
|
|
120
133
|
function resolvePageDirs(dir, root, exclude) {
|
|
121
|
-
const dirs =
|
|
134
|
+
const dirs = fg__default.sync(slash(dir), {
|
|
122
135
|
ignore: exclude,
|
|
123
136
|
onlyDirectories: true,
|
|
124
137
|
dot: true,
|
|
@@ -131,7 +144,7 @@ function resolvePageDirs(dir, root, exclude) {
|
|
|
131
144
|
function getPageFiles(path, options) {
|
|
132
145
|
const { exclude } = options;
|
|
133
146
|
const ext = extsToGlob(FILE_EXTENSIONS);
|
|
134
|
-
const files =
|
|
147
|
+
const files = fg__default.sync(`**/*.${ext}`, {
|
|
135
148
|
ignore: exclude,
|
|
136
149
|
onlyFiles: true,
|
|
137
150
|
cwd: path
|
|
@@ -139,11 +152,14 @@ function getPageFiles(path, options) {
|
|
|
139
152
|
return files;
|
|
140
153
|
}
|
|
141
154
|
function checkPagesJsonFile(path) {
|
|
142
|
-
if (!
|
|
155
|
+
if (!fs__default.existsSync(path)) {
|
|
143
156
|
writeFileSync(path, JSON.stringify({ pages: [{ path: "" }] }, null, 2));
|
|
157
|
+
return false;
|
|
158
|
+
}
|
|
159
|
+
return true;
|
|
144
160
|
}
|
|
145
161
|
function writeFileSync(path, content) {
|
|
146
|
-
|
|
162
|
+
fs__default.writeFileSync(path, content, { encoding: "utf-8" });
|
|
147
163
|
}
|
|
148
164
|
|
|
149
165
|
async function parseSFC(code) {
|
|
@@ -169,7 +185,7 @@ function parseCustomBlock(block, filePath, options) {
|
|
|
169
185
|
debug.routeBlock(`use ${lang} parser`);
|
|
170
186
|
if (lang === "json5") {
|
|
171
187
|
try {
|
|
172
|
-
content =
|
|
188
|
+
content = JSON5__default.parse(block.content);
|
|
173
189
|
} catch (err) {
|
|
174
190
|
throw new Error(
|
|
175
191
|
`Invalid JSON5 format of <${block.type}> content in ${filePath}
|
|
@@ -203,7 +219,7 @@ ${err.message}`
|
|
|
203
219
|
}
|
|
204
220
|
}
|
|
205
221
|
async function getRouteBlock(path, options) {
|
|
206
|
-
const content =
|
|
222
|
+
const content = fs__default.readFileSync(path, "utf8");
|
|
207
223
|
const parsedSFC = await parseSFC(content);
|
|
208
224
|
const blockStr = parsedSFC?.customBlocks.find((b) => b.type === "route");
|
|
209
225
|
if (!blockStr)
|
|
@@ -221,7 +237,7 @@ class PageContext {
|
|
|
221
237
|
this.root = slash(viteRoot);
|
|
222
238
|
debug.options("root", this.root);
|
|
223
239
|
this.options = resolveOptions(userOptions, this.root);
|
|
224
|
-
this.resolvedPagesJSONPath =
|
|
240
|
+
this.resolvedPagesJSONPath = path__default.join(this.root, this.options.outDir, OUTPUT_NAME);
|
|
225
241
|
debug.options(this.options);
|
|
226
242
|
}
|
|
227
243
|
setLogger(logger) {
|
|
@@ -238,13 +254,13 @@ class PageContext {
|
|
|
238
254
|
}
|
|
239
255
|
async scanPages() {
|
|
240
256
|
const pageDirFiles = this.options.dirs.map((dir) => {
|
|
241
|
-
const pagesDirPath = slash(
|
|
242
|
-
const basePath = slash(
|
|
257
|
+
const pagesDirPath = slash(path__default.resolve(this.options.root, dir));
|
|
258
|
+
const basePath = slash(path__default.join(this.options.root, this.options.outDir));
|
|
243
259
|
const files = getPageFiles(pagesDirPath, this.options);
|
|
244
260
|
debug.pages(dir, files);
|
|
245
261
|
const pagePaths = files.map((file) => slash(file)).map((file) => ({
|
|
246
|
-
relativePath:
|
|
247
|
-
absolutePath: slash(
|
|
262
|
+
relativePath: path__default.relative(basePath, slash(path__default.resolve(pagesDirPath, file))),
|
|
263
|
+
absolutePath: slash(path__default.resolve(pagesDirPath, file))
|
|
248
264
|
}));
|
|
249
265
|
return { dir, files: pagePaths };
|
|
250
266
|
});
|
|
@@ -299,7 +315,7 @@ class PageContext {
|
|
|
299
315
|
async parsePage(page) {
|
|
300
316
|
const { relativePath, absolutePath } = page;
|
|
301
317
|
const routeBlock = await getRouteBlock(absolutePath, this.options);
|
|
302
|
-
const relativePathWithFileName = relativePath.replace(
|
|
318
|
+
const relativePathWithFileName = relativePath.replace(path__default.extname(relativePath), "");
|
|
303
319
|
const pageMetaDatum = { path: vite.normalizePath(relativePathWithFileName), type: routeBlock?.attr.type ?? "page" };
|
|
304
320
|
if (routeBlock)
|
|
305
321
|
Object.assign(pageMetaDatum, routeBlock.content);
|
|
@@ -335,7 +351,7 @@ class PageContext {
|
|
|
335
351
|
await this.mergePageMetaData();
|
|
336
352
|
this.options.onAfterMergePageMetaData(this);
|
|
337
353
|
this.options.onBeforeWriteFile(this);
|
|
338
|
-
const pagesJson = JSON.stringify({ ...this.pagesGlobConfig, pages: this.pageMetaData }, null, 2);
|
|
354
|
+
const pagesJson = JSON.stringify({ ...this.pagesGlobConfig, pages: this.pageMetaData }, null, this.options.minify ? void 0 : 2);
|
|
339
355
|
writeFileSync(this.resolvedPagesJSONPath, pagesJson);
|
|
340
356
|
this.options.onAfterWriteFile(this);
|
|
341
357
|
}
|
|
@@ -351,25 +367,57 @@ function defineUniPages(config) {
|
|
|
351
367
|
return config;
|
|
352
368
|
}
|
|
353
369
|
|
|
370
|
+
async function restart() {
|
|
371
|
+
return new Promise((resolve) => {
|
|
372
|
+
const build = child_process.spawn(process.argv.shift(), process.argv, {
|
|
373
|
+
cwd: process.cwd(),
|
|
374
|
+
detached: true,
|
|
375
|
+
env: process.env
|
|
376
|
+
});
|
|
377
|
+
build.stdout?.pipe(process.stdout);
|
|
378
|
+
build.stderr?.pipe(process.stderr);
|
|
379
|
+
build.on("close", (code) => {
|
|
380
|
+
resolve(process.exit(code));
|
|
381
|
+
});
|
|
382
|
+
});
|
|
383
|
+
}
|
|
354
384
|
const VitePluginUniPages = (userOptions = {}) => {
|
|
355
385
|
let ctx;
|
|
356
|
-
const resolvedPagesJSONPath =
|
|
357
|
-
|
|
386
|
+
const resolvedPagesJSONPath = path__default.join(
|
|
387
|
+
process.cwd(),
|
|
388
|
+
userOptions.outDir ?? "src",
|
|
389
|
+
OUTPUT_NAME
|
|
390
|
+
);
|
|
391
|
+
const isValidated = checkPagesJsonFile(resolvedPagesJSONPath);
|
|
358
392
|
return {
|
|
359
393
|
name: "vite-plugin-uni-pages",
|
|
360
394
|
enforce: "pre",
|
|
361
395
|
async configResolved(config) {
|
|
362
396
|
ctx = new PageContext(userOptions, config.root);
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
397
|
+
const logger = vite.createLogger(void 0, {
|
|
398
|
+
prefix: "[vite-plugin-uni-pages]"
|
|
399
|
+
});
|
|
400
|
+
ctx.setLogger(logger);
|
|
401
|
+
await ctx.updatePagesJSON();
|
|
402
|
+
if (config.command === "build") {
|
|
403
|
+
if (!isValidated) {
|
|
404
|
+
ctx.logger?.warn("In build mode, if `pages.json` does not exist, the plugin cannot create the complete `pages.json` before the uni-app, so it restarts the build.", {
|
|
405
|
+
timestamp: true
|
|
406
|
+
});
|
|
407
|
+
await restart();
|
|
408
|
+
}
|
|
409
|
+
if (config.build.watch)
|
|
410
|
+
ctx.setupWatcher(chokidar__default.watch(ctx.options.dirs));
|
|
411
|
+
}
|
|
367
412
|
},
|
|
413
|
+
// Applet do not support custom route block, so we need to remove the route block here
|
|
368
414
|
async transform(code, id) {
|
|
369
415
|
if (!/\.vue$/.test(id))
|
|
370
416
|
return null;
|
|
371
|
-
const s = new
|
|
372
|
-
const routeBlockMatches = s.original.matchAll(
|
|
417
|
+
const s = new MagicString__default(code.toString());
|
|
418
|
+
const routeBlockMatches = s.original.matchAll(
|
|
419
|
+
/<route[^>]*>([\s\S]*?)<\/route>/g
|
|
420
|
+
);
|
|
373
421
|
for (const match of routeBlockMatches) {
|
|
374
422
|
const index = match.index;
|
|
375
423
|
const length = match[0].length;
|
package/dist/index.d.ts
CHANGED
|
@@ -8,7 +8,11 @@ interface EasyCom {
|
|
|
8
8
|
custom: Record<string, string>;
|
|
9
9
|
}
|
|
10
10
|
|
|
11
|
+
type RGBColor = `rgb(${number}, ${number}, ${number})`;
|
|
12
|
+
type RGBAColor = `rgba(${number}, ${number}, ${number}, ${number})`;
|
|
11
13
|
type HEXColor = `#${string}`;
|
|
14
|
+
type ThemeColor = `@${string}`;
|
|
15
|
+
type Color = RGBColor | RGBAColor | HEXColor;
|
|
12
16
|
|
|
13
17
|
interface AppPlus {
|
|
14
18
|
/**
|
|
@@ -123,7 +127,7 @@ interface GlobalStyle {
|
|
|
123
127
|
* @default "#F7F7F7"
|
|
124
128
|
* @desc APP与H5为#F7F7F7,小程序平台请参考相应小程序文档
|
|
125
129
|
*/
|
|
126
|
-
navigationBarBackgroundColor?: HEXColor;
|
|
130
|
+
navigationBarBackgroundColor?: HEXColor | ThemeColor;
|
|
127
131
|
/**
|
|
128
132
|
* 导航栏标题颜色及状态栏前景颜色,仅支持 black/white
|
|
129
133
|
* @default "white"
|
|
@@ -146,7 +150,7 @@ interface GlobalStyle {
|
|
|
146
150
|
* @default "#ffffff"
|
|
147
151
|
* @desc 微信小程序
|
|
148
152
|
*/
|
|
149
|
-
backgroundColor?: HEXColor;
|
|
153
|
+
backgroundColor?: HEXColor | ThemeColor;
|
|
150
154
|
/**
|
|
151
155
|
* 下拉 loading 的样式,仅支持 dark / light
|
|
152
156
|
* @default "dark"
|
|
@@ -170,13 +174,13 @@ interface GlobalStyle {
|
|
|
170
174
|
* @default "#ffffff"
|
|
171
175
|
* @desc 仅 iOS 平台
|
|
172
176
|
*/
|
|
173
|
-
backgroundColorTop?: HEXColor;
|
|
177
|
+
backgroundColorTop?: HEXColor | ThemeColor;
|
|
174
178
|
/**
|
|
175
179
|
* 底部窗口的背景色(bounce回弹区域)
|
|
176
180
|
* @default "#ffffff"
|
|
177
181
|
* @desc 仅 iOS 平台
|
|
178
182
|
*/
|
|
179
|
-
backgroundColorBottom?: HEXColor;
|
|
183
|
+
backgroundColorBottom?: HEXColor | ThemeColor;
|
|
180
184
|
/**
|
|
181
185
|
* 导航栏图片地址(替换当前文字标题),支付宝小程序内必须使用https的图片链接地址
|
|
182
186
|
* @desc 支付宝小程序、H5、APP
|
|
@@ -413,15 +417,15 @@ interface TabBar {
|
|
|
413
417
|
/**
|
|
414
418
|
* tab 上的文字默认颜色
|
|
415
419
|
*/
|
|
416
|
-
color?:
|
|
420
|
+
color?: Color;
|
|
417
421
|
/**
|
|
418
422
|
* tab 上的文字选中时的颜色
|
|
419
423
|
*/
|
|
420
|
-
selectedColor?:
|
|
424
|
+
selectedColor?: Color;
|
|
421
425
|
/**
|
|
422
426
|
* tab 的背景色
|
|
423
427
|
*/
|
|
424
|
-
backgroundColor?:
|
|
428
|
+
backgroundColor?: Color;
|
|
425
429
|
/**
|
|
426
430
|
* tabbar 上边框的颜色,可选值 black/white,也支持其他颜色值
|
|
427
431
|
* @default "black"
|
|
@@ -513,6 +517,22 @@ interface PreloadRule {
|
|
|
513
517
|
*/
|
|
514
518
|
network?: string;
|
|
515
519
|
}
|
|
520
|
+
interface UniIdRouter {
|
|
521
|
+
/**
|
|
522
|
+
* 登录页面路径
|
|
523
|
+
*/
|
|
524
|
+
loginPage: string;
|
|
525
|
+
/**
|
|
526
|
+
* 需要登录才可以访问的页面列表,可以使用正则语法
|
|
527
|
+
*/
|
|
528
|
+
needLogin: string[];
|
|
529
|
+
/**
|
|
530
|
+
* 是否自动解析云对象及 clientDB 的错误码,如果是客户端 token 不正确或 token 过期则自动跳转配置的登录页面
|
|
531
|
+
*
|
|
532
|
+
* @default true
|
|
533
|
+
*/
|
|
534
|
+
resToLogin?: boolean;
|
|
535
|
+
}
|
|
516
536
|
interface SubPackage {
|
|
517
537
|
/**
|
|
518
538
|
* 子包的根目录
|
|
@@ -577,7 +597,9 @@ interface PagesConfig {
|
|
|
577
597
|
* 分包预下载规则
|
|
578
598
|
* @desc 微信小程序
|
|
579
599
|
*/
|
|
580
|
-
preloadRule?:
|
|
600
|
+
preloadRule?: {
|
|
601
|
+
[path: string]: PreloadRule;
|
|
602
|
+
};
|
|
581
603
|
/**
|
|
582
604
|
* `Worker` 代码放置的目录
|
|
583
605
|
* @desc 微信小程序
|
|
@@ -599,9 +621,13 @@ interface PagesConfig {
|
|
|
599
621
|
*/
|
|
600
622
|
rightWindow?: TheWindow;
|
|
601
623
|
/**
|
|
602
|
-
* 自动跳转相关配置,新增于HBuilderX 3.5.0
|
|
624
|
+
* 自动跳转相关配置,新增于 HBuilderX 3.5.0
|
|
625
|
+
*/
|
|
626
|
+
uniIdRouter?: UniIdRouter;
|
|
627
|
+
/**
|
|
628
|
+
* 默认启动首页,新增于 HBuilderX 3.7.0
|
|
603
629
|
*/
|
|
604
|
-
|
|
630
|
+
entryPagePath?: string;
|
|
605
631
|
[x: string]: any;
|
|
606
632
|
}
|
|
607
633
|
interface UserPagesConfig extends PagesConfig {
|
|
@@ -663,6 +689,11 @@ interface Options {
|
|
|
663
689
|
* @default 'json5'
|
|
664
690
|
*/
|
|
665
691
|
routeBlockLang: 'json5' | 'json' | 'yaml' | 'yml';
|
|
692
|
+
/**
|
|
693
|
+
* minify the `pages.json`
|
|
694
|
+
* @default false
|
|
695
|
+
*/
|
|
696
|
+
minify: boolean;
|
|
666
697
|
onBeforeLoadUserConfig: (ctx: PageContext) => void;
|
|
667
698
|
onAfterLoadUserConfig: (ctx: PageContext) => void;
|
|
668
699
|
onBeforeScanPages: (ctx: PageContext) => void;
|
|
@@ -698,6 +729,10 @@ interface PageMetaDatum {
|
|
|
698
729
|
* 配置页面窗口表现,配置项参考下方 pageStyle
|
|
699
730
|
*/
|
|
700
731
|
style?: GlobalStyle;
|
|
732
|
+
/**
|
|
733
|
+
* 当前页面是否需要登录才可以访问,此配置优先级高于 uniIdRouter 下的 needLogin
|
|
734
|
+
*/
|
|
735
|
+
needLogin?: boolean;
|
|
701
736
|
[x: string]: any;
|
|
702
737
|
}
|
|
703
738
|
|
|
@@ -729,7 +764,7 @@ declare const getPagesConfigSourcePaths: () => Promise<string[]>;
|
|
|
729
764
|
* Resolves the files that are valid pages for the given context.
|
|
730
765
|
*/
|
|
731
766
|
declare function getPageFiles(path: string, options: ResolvedOptions): string[];
|
|
732
|
-
declare function checkPagesJsonFile(path: string):
|
|
767
|
+
declare function checkPagesJsonFile(path: string): boolean;
|
|
733
768
|
declare function writeFileSync(path: string, content: string): void;
|
|
734
769
|
|
|
735
770
|
declare function resolveOptions(userOptions: UserOptions, viteRoot?: string): ResolvedOptions;
|
|
@@ -744,4 +779,4 @@ declare function getRouteBlock(path: string, options: ResolvedOptions): Promise<
|
|
|
744
779
|
|
|
745
780
|
declare const VitePluginUniPages: (userOptions?: UserOptions) => Plugin;
|
|
746
781
|
|
|
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 };
|
|
782
|
+
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, UniIdRouter, 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
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import path from 'path';
|
|
2
|
+
import { spawn } from 'child_process';
|
|
3
|
+
import { normalizePath, createLogger } from 'vite';
|
|
2
4
|
import MagicString from 'magic-string';
|
|
3
5
|
import chokidar from 'chokidar';
|
|
4
|
-
import { normalizePath } from 'vite';
|
|
5
6
|
import { loadConfig } from 'unconfig';
|
|
6
7
|
import Debug from 'debug';
|
|
7
8
|
import { groupBy } from 'lodash';
|
|
@@ -76,6 +77,7 @@ function resolveOptions(userOptions, viteRoot) {
|
|
|
76
77
|
outDir = "src",
|
|
77
78
|
exclude = ["node_modules", ".git", "**/__*__/**"],
|
|
78
79
|
routeBlockLang = "json5",
|
|
80
|
+
minify = false,
|
|
79
81
|
onBeforeLoadUserConfig = () => {
|
|
80
82
|
},
|
|
81
83
|
onAfterLoadUserConfig = () => {
|
|
@@ -102,6 +104,7 @@ function resolveOptions(userOptions, viteRoot) {
|
|
|
102
104
|
exclude,
|
|
103
105
|
routeBlockLang,
|
|
104
106
|
root,
|
|
107
|
+
minify,
|
|
105
108
|
onBeforeLoadUserConfig,
|
|
106
109
|
onAfterLoadUserConfig,
|
|
107
110
|
onBeforeScanPages,
|
|
@@ -135,8 +138,11 @@ function getPageFiles(path, options) {
|
|
|
135
138
|
return files;
|
|
136
139
|
}
|
|
137
140
|
function checkPagesJsonFile(path) {
|
|
138
|
-
if (!fs.existsSync(path))
|
|
141
|
+
if (!fs.existsSync(path)) {
|
|
139
142
|
writeFileSync(path, JSON.stringify({ pages: [{ path: "" }] }, null, 2));
|
|
143
|
+
return false;
|
|
144
|
+
}
|
|
145
|
+
return true;
|
|
140
146
|
}
|
|
141
147
|
function writeFileSync(path, content) {
|
|
142
148
|
fs.writeFileSync(path, content, { encoding: "utf-8" });
|
|
@@ -331,7 +337,7 @@ class PageContext {
|
|
|
331
337
|
await this.mergePageMetaData();
|
|
332
338
|
this.options.onAfterMergePageMetaData(this);
|
|
333
339
|
this.options.onBeforeWriteFile(this);
|
|
334
|
-
const pagesJson = JSON.stringify({ ...this.pagesGlobConfig, pages: this.pageMetaData }, null, 2);
|
|
340
|
+
const pagesJson = JSON.stringify({ ...this.pagesGlobConfig, pages: this.pageMetaData }, null, this.options.minify ? void 0 : 2);
|
|
335
341
|
writeFileSync(this.resolvedPagesJSONPath, pagesJson);
|
|
336
342
|
this.options.onAfterWriteFile(this);
|
|
337
343
|
}
|
|
@@ -347,25 +353,57 @@ function defineUniPages(config) {
|
|
|
347
353
|
return config;
|
|
348
354
|
}
|
|
349
355
|
|
|
356
|
+
async function restart() {
|
|
357
|
+
return new Promise((resolve) => {
|
|
358
|
+
const build = spawn(process.argv.shift(), process.argv, {
|
|
359
|
+
cwd: process.cwd(),
|
|
360
|
+
detached: true,
|
|
361
|
+
env: process.env
|
|
362
|
+
});
|
|
363
|
+
build.stdout?.pipe(process.stdout);
|
|
364
|
+
build.stderr?.pipe(process.stderr);
|
|
365
|
+
build.on("close", (code) => {
|
|
366
|
+
resolve(process.exit(code));
|
|
367
|
+
});
|
|
368
|
+
});
|
|
369
|
+
}
|
|
350
370
|
const VitePluginUniPages = (userOptions = {}) => {
|
|
351
371
|
let ctx;
|
|
352
|
-
const resolvedPagesJSONPath = path.join(
|
|
353
|
-
|
|
372
|
+
const resolvedPagesJSONPath = path.join(
|
|
373
|
+
process.cwd(),
|
|
374
|
+
userOptions.outDir ?? "src",
|
|
375
|
+
OUTPUT_NAME
|
|
376
|
+
);
|
|
377
|
+
const isValidated = checkPagesJsonFile(resolvedPagesJSONPath);
|
|
354
378
|
return {
|
|
355
379
|
name: "vite-plugin-uni-pages",
|
|
356
380
|
enforce: "pre",
|
|
357
381
|
async configResolved(config) {
|
|
358
382
|
ctx = new PageContext(userOptions, config.root);
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
383
|
+
const logger = createLogger(void 0, {
|
|
384
|
+
prefix: "[vite-plugin-uni-pages]"
|
|
385
|
+
});
|
|
386
|
+
ctx.setLogger(logger);
|
|
387
|
+
await ctx.updatePagesJSON();
|
|
388
|
+
if (config.command === "build") {
|
|
389
|
+
if (!isValidated) {
|
|
390
|
+
ctx.logger?.warn("In build mode, if `pages.json` does not exist, the plugin cannot create the complete `pages.json` before the uni-app, so it restarts the build.", {
|
|
391
|
+
timestamp: true
|
|
392
|
+
});
|
|
393
|
+
await restart();
|
|
394
|
+
}
|
|
395
|
+
if (config.build.watch)
|
|
396
|
+
ctx.setupWatcher(chokidar.watch(ctx.options.dirs));
|
|
397
|
+
}
|
|
363
398
|
},
|
|
399
|
+
// Applet do not support custom route block, so we need to remove the route block here
|
|
364
400
|
async transform(code, id) {
|
|
365
401
|
if (!/\.vue$/.test(id))
|
|
366
402
|
return null;
|
|
367
403
|
const s = new MagicString(code.toString());
|
|
368
|
-
const routeBlockMatches = s.original.matchAll(
|
|
404
|
+
const routeBlockMatches = s.original.matchAll(
|
|
405
|
+
/<route[^>]*>([\s\S]*?)<\/route>/g
|
|
406
|
+
);
|
|
369
407
|
for (const match of routeBlockMatches) {
|
|
370
408
|
const index = match.index;
|
|
371
409
|
const length = match[0].length;
|
package/package.json
CHANGED