@uni-helper/vite-plugin-uni-pages 0.0.6 → 0.1.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/README.md +77 -43
- package/dist/index.cjs +345 -244
- package/dist/index.d.ts +713 -49
- package/dist/index.mjs +366 -0
- package/package.json +20 -33
- package/README.Zh-CN.md +0 -94
- package/dist/index.cjs.map +0 -1
- package/dist/index.js +0 -258
- package/dist/index.js.map +0 -1
package/dist/index.cjs
CHANGED
|
@@ -1,290 +1,391 @@
|
|
|
1
|
-
|
|
2
|
-
var __create = Object.create;
|
|
3
|
-
var __defProp = Object.defineProperty;
|
|
4
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
-
var __export = (target, all) => {
|
|
9
|
-
for (var name in all)
|
|
10
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
-
};
|
|
12
|
-
var __copyProps = (to, from, except, desc) => {
|
|
13
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
-
for (let key of __getOwnPropNames(from))
|
|
15
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
-
}
|
|
18
|
-
return to;
|
|
19
|
-
};
|
|
20
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
22
|
-
mod
|
|
23
|
-
));
|
|
24
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
1
|
+
'use strict';
|
|
25
2
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
const path = require('path');
|
|
6
|
+
const MagicString = require('magic-string');
|
|
7
|
+
const chokidar = require('chokidar');
|
|
8
|
+
const unconfig = require('unconfig');
|
|
9
|
+
const Debug = require('debug');
|
|
10
|
+
const lodashEs = require('lodash-es');
|
|
11
|
+
const fg = require('fast-glob');
|
|
12
|
+
const fs = require('fs');
|
|
13
|
+
const JSON5 = require('json5');
|
|
14
|
+
const yaml = require('yaml');
|
|
15
|
+
const compilerSfc = require('@vue/compiler-sfc');
|
|
16
|
+
|
|
17
|
+
function slash(str) {
|
|
18
|
+
return str.replace(/\\/g, "/");
|
|
19
|
+
}
|
|
35
20
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
const
|
|
43
|
-
|
|
21
|
+
const MODULE_ID_VIRTUAL = "virtual:uni-pages";
|
|
22
|
+
const RESOLVED_MODULE_ID_VIRTUAL = `\0${MODULE_ID_VIRTUAL}`;
|
|
23
|
+
const OUTPUT_NAME = "pages.json";
|
|
24
|
+
const FILE_EXTENSIONS = ["vue", "nvue", "uvue"];
|
|
25
|
+
|
|
26
|
+
function invalidatePagesModule(server) {
|
|
27
|
+
const { moduleGraph } = server;
|
|
28
|
+
const mods = moduleGraph.getModulesByFile(RESOLVED_MODULE_ID_VIRTUAL);
|
|
29
|
+
if (mods) {
|
|
30
|
+
const seen = /* @__PURE__ */ new Set();
|
|
31
|
+
mods.forEach((mod) => {
|
|
32
|
+
moduleGraph.invalidateModule(mod, seen);
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
const debug = {
|
|
37
|
+
hmr: Debug("vite-plugin-uni-pages:hmr"),
|
|
38
|
+
routeBlock: Debug("vite-plugin-uni-pages:routeBlock"),
|
|
39
|
+
options: Debug("vite-plugin-uni-pages:options"),
|
|
40
|
+
pages: Debug("vite-plugin-uni-pages:pages"),
|
|
41
|
+
error: Debug("vite-plugin-uni-pages:error")
|
|
44
42
|
};
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
43
|
+
function extsToGlob(extensions) {
|
|
44
|
+
return extensions.length > 1 ? `{${extensions.join(",")}}` : extensions[0] || "";
|
|
45
|
+
}
|
|
46
|
+
function isTargetFile(path) {
|
|
47
|
+
const ext = path.split(".").pop();
|
|
48
|
+
return FILE_EXTENSIONS.includes(ext);
|
|
49
|
+
}
|
|
50
|
+
function isConfigFile(path) {
|
|
51
|
+
return path.includes("pages.config");
|
|
52
|
+
}
|
|
53
|
+
function mergePageMetaDataArray(pageMetaData) {
|
|
54
|
+
const pageMetaDataObj = lodashEs.groupBy(pageMetaData, "path");
|
|
55
|
+
const result = [];
|
|
56
|
+
for (const path in pageMetaDataObj) {
|
|
57
|
+
const _pageMetaData = pageMetaDataObj[path];
|
|
58
|
+
const options = _pageMetaData[0];
|
|
59
|
+
for (const page of _pageMetaData) {
|
|
60
|
+
options.style = Object.assign(options.style ?? {}, page.style ?? {});
|
|
61
|
+
Object.assign(options, page);
|
|
62
|
+
}
|
|
63
|
+
result.push(options);
|
|
48
64
|
}
|
|
49
|
-
|
|
65
|
+
return result;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function resolveOptions(userOptions, viteRoot) {
|
|
69
|
+
const {
|
|
70
|
+
mergePages = true,
|
|
71
|
+
dir = "src/pages",
|
|
72
|
+
outDir = "src",
|
|
73
|
+
exclude = ["node_modules", ".git", "**/__*__/**"],
|
|
74
|
+
routeBlockLang = "json5",
|
|
75
|
+
onBeforeLoadUserConfig = () => {
|
|
76
|
+
},
|
|
77
|
+
onAfterLoadUserConfig = () => {
|
|
78
|
+
},
|
|
79
|
+
onBeforeScanPages = () => {
|
|
80
|
+
},
|
|
81
|
+
onAfterScanPages = () => {
|
|
82
|
+
},
|
|
83
|
+
onBeforeMergePageMetaData = () => {
|
|
84
|
+
},
|
|
85
|
+
onAfterMergePageMetaData = () => {
|
|
86
|
+
},
|
|
87
|
+
onBeforeWriteFile = () => {
|
|
88
|
+
},
|
|
89
|
+
onAfterWriteFile = () => {
|
|
90
|
+
}
|
|
91
|
+
} = userOptions;
|
|
92
|
+
const root = viteRoot || slash(process.cwd());
|
|
93
|
+
const resolvedDirs = resolvePageDirs(dir, root, exclude);
|
|
94
|
+
const resolvedOptions = {
|
|
95
|
+
mergePages,
|
|
96
|
+
dirs: resolvedDirs,
|
|
97
|
+
outDir,
|
|
98
|
+
exclude,
|
|
99
|
+
routeBlockLang,
|
|
100
|
+
root,
|
|
101
|
+
onBeforeLoadUserConfig,
|
|
102
|
+
onAfterLoadUserConfig,
|
|
103
|
+
onBeforeScanPages,
|
|
104
|
+
onAfterScanPages,
|
|
105
|
+
onBeforeMergePageMetaData,
|
|
106
|
+
onAfterMergePageMetaData,
|
|
107
|
+
onBeforeWriteFile,
|
|
108
|
+
onAfterWriteFile
|
|
109
|
+
};
|
|
110
|
+
return resolvedOptions;
|
|
111
|
+
}
|
|
112
|
+
function resolvePageDirs(dir, root, exclude) {
|
|
113
|
+
const dirs = fg.sync(slash(dir), {
|
|
114
|
+
ignore: exclude,
|
|
115
|
+
onlyDirectories: true,
|
|
116
|
+
dot: true,
|
|
117
|
+
unique: true,
|
|
118
|
+
cwd: root
|
|
119
|
+
});
|
|
120
|
+
return dirs;
|
|
121
|
+
}
|
|
50
122
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
123
|
+
function getPageFiles(path, options) {
|
|
124
|
+
const { exclude } = options;
|
|
125
|
+
const ext = extsToGlob(FILE_EXTENSIONS);
|
|
126
|
+
const files = fg.sync(`**/*.${ext}`, {
|
|
127
|
+
ignore: exclude,
|
|
128
|
+
onlyFiles: true,
|
|
129
|
+
cwd: path
|
|
130
|
+
});
|
|
131
|
+
return files;
|
|
132
|
+
}
|
|
133
|
+
function checkPagesJsonFile(path) {
|
|
134
|
+
if (!fs.existsSync(path))
|
|
135
|
+
writeFileSync(path, JSON.stringify({ pages: [{ path: "" }] }, null, 2));
|
|
136
|
+
}
|
|
137
|
+
function writeFileSync(path, content) {
|
|
138
|
+
fs.writeFileSync(path, content, { encoding: "utf-8" });
|
|
139
|
+
}
|
|
54
140
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
141
|
+
async function parseSFC(code) {
|
|
142
|
+
try {
|
|
143
|
+
return compilerSfc.parse(code, {
|
|
144
|
+
pad: "space"
|
|
145
|
+
}).descriptor || compilerSfc.parse({
|
|
146
|
+
source: code
|
|
147
|
+
});
|
|
148
|
+
} catch {
|
|
149
|
+
throw new Error(
|
|
150
|
+
`[vite-plugin-uni-pages] Vue3's "@vue/compiler-sfc" is required.`
|
|
151
|
+
);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
function parseCustomBlock(block, filePath, options) {
|
|
155
|
+
const lang = block.lang ?? options.routeBlockLang;
|
|
156
|
+
debug.routeBlock(`use ${lang} parser`);
|
|
157
|
+
if (lang === "json5") {
|
|
158
|
+
try {
|
|
159
|
+
return JSON5.parse(block.content);
|
|
160
|
+
} catch (err) {
|
|
161
|
+
throw new Error(
|
|
162
|
+
`Invalid JSON5 format of <${block.type}> content in ${filePath}
|
|
163
|
+
${err.message}`
|
|
164
|
+
);
|
|
165
|
+
}
|
|
166
|
+
} else if (lang === "json") {
|
|
167
|
+
try {
|
|
168
|
+
return JSON.parse(block.content);
|
|
169
|
+
} catch (err) {
|
|
170
|
+
throw new Error(
|
|
171
|
+
`Invalid JSON format of <${block.type}> content in ${filePath}
|
|
172
|
+
${err.message}`
|
|
173
|
+
);
|
|
174
|
+
}
|
|
175
|
+
} else if (lang === "yaml" || lang === "yml") {
|
|
176
|
+
try {
|
|
177
|
+
return yaml.parse(block.content);
|
|
178
|
+
} catch (err) {
|
|
179
|
+
throw new Error(
|
|
180
|
+
`Invalid YAML format of <${block.type}> content in ${filePath}
|
|
181
|
+
${err.message}`
|
|
182
|
+
);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
async function getRouteBlock(path, options) {
|
|
187
|
+
const content = fs.readFileSync(path, "utf8");
|
|
188
|
+
const parsedSFC = await parseSFC(content);
|
|
189
|
+
const blockStr = parsedSFC?.customBlocks.find((b) => b.type === "route");
|
|
190
|
+
if (!blockStr)
|
|
191
|
+
return;
|
|
192
|
+
const result = parseCustomBlock(blockStr, path, options);
|
|
193
|
+
return result;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
class PageContext {
|
|
197
|
+
constructor(userOptions, viteRoot = process.cwd()) {
|
|
198
|
+
this.pagesPath = [];
|
|
199
|
+
this.pageMetaData = [];
|
|
200
|
+
this.resolvedPagesJSONPath = "";
|
|
201
|
+
this.rawOptions = userOptions;
|
|
202
|
+
this.root = slash(viteRoot);
|
|
203
|
+
debug.options("root", this.root);
|
|
204
|
+
this.options = resolveOptions(userOptions, this.root);
|
|
205
|
+
this.resolvedPagesJSONPath = path.join(this.root, this.options.outDir, OUTPUT_NAME);
|
|
206
|
+
debug.options(this.options);
|
|
207
|
+
}
|
|
208
|
+
setLogger(logger) {
|
|
209
|
+
this.logger = logger;
|
|
70
210
|
}
|
|
71
211
|
async loadUserPagesConfig() {
|
|
72
|
-
const { config } = await (
|
|
73
|
-
sources: [
|
|
74
|
-
{
|
|
75
|
-
files: "pages.config"
|
|
76
|
-
}
|
|
77
|
-
],
|
|
78
|
-
merge: false
|
|
79
|
-
});
|
|
212
|
+
const { config } = await unconfig.loadConfig({ sources: [{ files: "pages.config" }] });
|
|
80
213
|
if (!config) {
|
|
81
|
-
logger.
|
|
82
|
-
"Can't found pages.config, please create pages.config.(ts|mts|cts|js|cjs|mjs|json)"
|
|
83
|
-
);
|
|
214
|
+
this.logger?.warn("Can't found pages.config, please create pages.config.(ts|mts|cts|js|cjs|mjs|json)");
|
|
84
215
|
process.exit(-1);
|
|
85
216
|
}
|
|
86
|
-
|
|
87
|
-
|
|
217
|
+
this.pagesGlobConfig = config;
|
|
218
|
+
debug.options(config);
|
|
88
219
|
}
|
|
89
220
|
async scanPages() {
|
|
90
|
-
const
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
221
|
+
const pageDirFiles = this.options.dirs.map((dir) => {
|
|
222
|
+
const pagesDirPath = slash(path.resolve(this.options.root, dir));
|
|
223
|
+
const basePath = slash(path.join(this.options.root, this.options.outDir));
|
|
224
|
+
const files = getPageFiles(pagesDirPath, this.options);
|
|
225
|
+
debug.pages(dir, files);
|
|
226
|
+
const pagePaths = files.map((file) => slash(file)).map((file) => ({
|
|
227
|
+
relativePath: path.relative(basePath, slash(path.resolve(pagesDirPath, file))),
|
|
228
|
+
absolutePath: slash(path.resolve(pagesDirPath, file))
|
|
229
|
+
}));
|
|
230
|
+
return { dir, files: pagePaths };
|
|
94
231
|
});
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
this.
|
|
100
|
-
return
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
(0, import_path2.resolve)(process.cwd(), this.options.pagesDir, file)
|
|
104
|
-
)
|
|
105
|
-
};
|
|
106
|
-
});
|
|
107
|
-
logger.debug("Scanned pages dir", this.pagesPathInfo);
|
|
232
|
+
this.pagesPath = pageDirFiles.map((page) => page.files).flat();
|
|
233
|
+
debug.pages(this.pagesPath);
|
|
234
|
+
}
|
|
235
|
+
setupViteServer(server) {
|
|
236
|
+
if (this._server === server)
|
|
237
|
+
return;
|
|
238
|
+
this._server = server;
|
|
239
|
+
this.setupWatcher(server.watcher);
|
|
108
240
|
}
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
241
|
+
setupWatcher(watcher) {
|
|
242
|
+
watcher.on("add", async (path2) => {
|
|
243
|
+
path2 = slash(path2);
|
|
244
|
+
if (!isTargetFile(path2))
|
|
245
|
+
return;
|
|
246
|
+
debug.pages(`File added: ${path2}`);
|
|
247
|
+
this.updatePagesJSON();
|
|
248
|
+
this.onUpdate();
|
|
112
249
|
});
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
path,
|
|
117
|
-
type: "page"
|
|
118
|
-
};
|
|
119
|
-
t == null ? void 0 : t.children.forEach((v) => {
|
|
120
|
-
var _a;
|
|
121
|
-
if (v.type !== 1) {
|
|
250
|
+
watcher.on("change", async (path2) => {
|
|
251
|
+
path2 = slash(path2);
|
|
252
|
+
if (!isTargetFile(path2) && !isConfigFile(path2))
|
|
122
253
|
return;
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
Object.keys(_pageMeta).forEach((key) => {
|
|
135
|
-
pageMeta[key] = _pageMeta[key];
|
|
136
|
-
});
|
|
137
|
-
pageMeta["path"] = path;
|
|
138
|
-
}
|
|
139
|
-
}
|
|
254
|
+
debug.pages(`File changed: ${path2}`);
|
|
255
|
+
this.updatePagesJSON();
|
|
256
|
+
this.onUpdate();
|
|
257
|
+
});
|
|
258
|
+
watcher.on("unlink", async (path2) => {
|
|
259
|
+
path2 = slash(path2);
|
|
260
|
+
if (!isTargetFile(path2))
|
|
261
|
+
return;
|
|
262
|
+
debug.pages(`File removed: ${path2}`);
|
|
263
|
+
this.updatePagesJSON();
|
|
264
|
+
this.onUpdate();
|
|
140
265
|
});
|
|
141
|
-
return pageMeta;
|
|
142
266
|
}
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
);
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
267
|
+
onUpdate() {
|
|
268
|
+
if (!this._server)
|
|
269
|
+
return;
|
|
270
|
+
invalidatePagesModule(this._server);
|
|
271
|
+
debug.hmr("Reload generated pages.");
|
|
272
|
+
this._server.ws.send({
|
|
273
|
+
type: "full-reload"
|
|
274
|
+
});
|
|
275
|
+
}
|
|
276
|
+
async parsePage(page) {
|
|
277
|
+
const { relativePath, absolutePath } = page;
|
|
278
|
+
const routeBlock = await getRouteBlock(absolutePath, this.options);
|
|
279
|
+
const relativePathWithFileName = relativePath.replace(path.extname(relativePath), "");
|
|
280
|
+
const pageMetaDatum = { path: relativePathWithFileName, type: "page" };
|
|
281
|
+
if (routeBlock)
|
|
282
|
+
Object.assign(pageMetaDatum, routeBlock);
|
|
283
|
+
return pageMetaDatum;
|
|
152
284
|
}
|
|
153
|
-
async
|
|
154
|
-
const
|
|
155
|
-
|
|
156
|
-
(0, import_path2.resolve)(process.cwd(), this.options.outDir, outputName)
|
|
285
|
+
async mergePageMetaData() {
|
|
286
|
+
const generatedPageMetaData = await Promise.all(
|
|
287
|
+
this.pagesPath.map(async (page) => await this.parsePage(page))
|
|
157
288
|
);
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
289
|
+
const customPageMetaData = this.pagesGlobConfig?.pages || [];
|
|
290
|
+
if (!this.options.mergePages) {
|
|
291
|
+
this.pageMetaData = customPageMetaData;
|
|
292
|
+
} else {
|
|
293
|
+
if (this.pagesGlobConfig?.pages)
|
|
294
|
+
this.pageMetaData = mergePageMetaDataArray(generatedPageMetaData.concat(customPageMetaData));
|
|
295
|
+
else
|
|
296
|
+
this.pageMetaData = generatedPageMetaData;
|
|
162
297
|
}
|
|
298
|
+
debug.pages(generatedPageMetaData);
|
|
299
|
+
}
|
|
300
|
+
async updatePagesJSON() {
|
|
301
|
+
checkPagesJsonFile(this.resolvedPagesJSONPath);
|
|
163
302
|
this.options.onBeforeLoadUserConfig(this);
|
|
164
303
|
await this.loadUserPagesConfig();
|
|
165
304
|
this.options.onAfterLoadUserConfig(this);
|
|
166
|
-
this.options.
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
this.options.
|
|
305
|
+
if (this.options.mergePages) {
|
|
306
|
+
this.options.onBeforeScanPages(this);
|
|
307
|
+
await this.scanPages();
|
|
308
|
+
this.options.onAfterScanPages(this);
|
|
309
|
+
}
|
|
310
|
+
this.options.onBeforeMergePageMetaData(this);
|
|
311
|
+
await this.mergePageMetaData();
|
|
312
|
+
this.options.onAfterMergePageMetaData(this);
|
|
172
313
|
this.options.onBeforeWriteFile(this);
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
});
|
|
314
|
+
const pagesJson = JSON.stringify({ ...this.pagesGlobConfig, pages: this.pageMetaData }, null, 2);
|
|
315
|
+
writeFileSync(this.resolvedPagesJSONPath, pagesJson);
|
|
176
316
|
this.options.onAfterWriteFile(this);
|
|
177
317
|
}
|
|
178
318
|
async virtualModule() {
|
|
179
|
-
return `export const pages = ${
|
|
319
|
+
return `export const pages = ${this.resolveRoutes()};`;
|
|
180
320
|
}
|
|
181
|
-
|
|
182
|
-
return
|
|
183
|
-
deep: 0,
|
|
184
|
-
onlyFiles: true,
|
|
185
|
-
absolute: true
|
|
186
|
-
});
|
|
321
|
+
resolveRoutes() {
|
|
322
|
+
return JSON.stringify(this.pageMetaData, null, 2);
|
|
187
323
|
}
|
|
188
|
-
|
|
189
|
-
return JSON.stringify({
|
|
190
|
-
...this.pagesConfig,
|
|
191
|
-
pages: this.pagesMeta
|
|
192
|
-
});
|
|
193
|
-
}
|
|
194
|
-
};
|
|
324
|
+
}
|
|
195
325
|
|
|
196
|
-
|
|
197
|
-
function definePages(config) {
|
|
326
|
+
function defineUniPages(config) {
|
|
198
327
|
return config;
|
|
199
328
|
}
|
|
200
329
|
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
outDir: "src",
|
|
206
|
-
exclude: [],
|
|
207
|
-
onBeforeLoadUserConfig: () => {
|
|
208
|
-
},
|
|
209
|
-
onAfterLoadUserConfig: () => {
|
|
210
|
-
},
|
|
211
|
-
onBeforeScanPages: () => {
|
|
212
|
-
},
|
|
213
|
-
onAfterScanPages: () => {
|
|
214
|
-
},
|
|
215
|
-
onBeforeMergePagesMeta: () => {
|
|
216
|
-
},
|
|
217
|
-
onAfterMergePagesMeta: () => {
|
|
218
|
-
},
|
|
219
|
-
onBeforeWriteFile: () => {
|
|
220
|
-
},
|
|
221
|
-
onAfterWriteFile: () => {
|
|
222
|
-
},
|
|
223
|
-
...userOptions
|
|
224
|
-
};
|
|
225
|
-
}
|
|
226
|
-
var VitePluginUniPages = async (userOptions = {}) => {
|
|
227
|
-
const options = resolveOptions(userOptions);
|
|
228
|
-
logger.debug("Create uni-pages context with", options);
|
|
229
|
-
const ctx = new Context(options);
|
|
330
|
+
const VitePluginUniPages = (userOptions = {}) => {
|
|
331
|
+
let ctx;
|
|
332
|
+
const resolvedPagesJSONPath = path.join(process.cwd(), userOptions.outDir ?? "src", OUTPUT_NAME);
|
|
333
|
+
checkPagesJsonFile(resolvedPagesJSONPath);
|
|
230
334
|
return {
|
|
231
335
|
name: "vite-plugin-uni-pages",
|
|
232
336
|
enforce: "pre",
|
|
233
|
-
async
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
moduleGraph.invalidateModule(module2);
|
|
240
|
-
if (ws) {
|
|
241
|
-
ws.send({
|
|
242
|
-
path,
|
|
243
|
-
type: "full-reload"
|
|
244
|
-
});
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
};
|
|
248
|
-
const updateVirtualModule = () => {
|
|
249
|
-
logger.debug("Update virtualModule");
|
|
250
|
-
const module2 = moduleGraph.getModuleById(resolvedVirtualModuleId);
|
|
251
|
-
reloadModule(module2);
|
|
252
|
-
};
|
|
253
|
-
watcher.on("change", async (path) => {
|
|
254
|
-
if (pagesConfigSourcePaths.includes((0, import_vite3.normalizePath)(path)) || isPagePath(path, options)) {
|
|
255
|
-
await ctx.createOrUpdatePagesJSON();
|
|
256
|
-
updateVirtualModule();
|
|
257
|
-
}
|
|
258
|
-
});
|
|
259
|
-
watcher.on("add", async (path) => {
|
|
260
|
-
if (isPagePath(path, options)) {
|
|
261
|
-
await ctx.createOrUpdatePagesJSON();
|
|
262
|
-
updateVirtualModule();
|
|
263
|
-
}
|
|
264
|
-
});
|
|
265
|
-
watcher.on("unlink", async (path) => {
|
|
266
|
-
if (isPagePath(path, options)) {
|
|
267
|
-
await ctx.createOrUpdatePagesJSON();
|
|
268
|
-
updateVirtualModule();
|
|
269
|
-
}
|
|
270
|
-
});
|
|
337
|
+
async configResolved(config) {
|
|
338
|
+
ctx = new PageContext(userOptions, config.root);
|
|
339
|
+
ctx.setLogger(config.logger);
|
|
340
|
+
ctx.updatePagesJSON();
|
|
341
|
+
if (config.build.watch && config.command === "build")
|
|
342
|
+
ctx.setupWatcher(chokidar.watch(ctx.options.dirs));
|
|
271
343
|
},
|
|
272
|
-
|
|
273
|
-
if (id
|
|
274
|
-
return
|
|
344
|
+
async transform(code, id) {
|
|
345
|
+
if (!/\.vue$/.test(id))
|
|
346
|
+
return null;
|
|
347
|
+
const s = new MagicString(code.toString());
|
|
348
|
+
const routeBlockMatches = s.original.matchAll(/<route[^>]*>([\s\S]*?)<\/route>/g);
|
|
349
|
+
for (const match of routeBlockMatches) {
|
|
350
|
+
const index = match.index;
|
|
351
|
+
const length = match[0].length;
|
|
352
|
+
s.remove(index, index + length);
|
|
275
353
|
}
|
|
354
|
+
return s.toString();
|
|
355
|
+
},
|
|
356
|
+
configureServer(server) {
|
|
357
|
+
ctx.setupViteServer(server);
|
|
358
|
+
},
|
|
359
|
+
resolveId(id) {
|
|
360
|
+
if (id === MODULE_ID_VIRTUAL)
|
|
361
|
+
return RESOLVED_MODULE_ID_VIRTUAL;
|
|
276
362
|
},
|
|
277
363
|
load(id) {
|
|
278
|
-
if (id ===
|
|
364
|
+
if (id === RESOLVED_MODULE_ID_VIRTUAL)
|
|
279
365
|
return ctx.virtualModule();
|
|
280
|
-
}
|
|
281
366
|
}
|
|
282
367
|
};
|
|
283
368
|
};
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
369
|
+
|
|
370
|
+
exports.FILE_EXTENSIONS = FILE_EXTENSIONS;
|
|
371
|
+
exports.MODULE_ID_VIRTUAL = MODULE_ID_VIRTUAL;
|
|
372
|
+
exports.OUTPUT_NAME = OUTPUT_NAME;
|
|
373
|
+
exports.PageContext = PageContext;
|
|
374
|
+
exports.RESOLVED_MODULE_ID_VIRTUAL = RESOLVED_MODULE_ID_VIRTUAL;
|
|
375
|
+
exports.VitePluginUniPages = VitePluginUniPages;
|
|
376
|
+
exports.checkPagesJsonFile = checkPagesJsonFile;
|
|
377
|
+
exports.debug = debug;
|
|
378
|
+
exports.default = VitePluginUniPages;
|
|
379
|
+
exports.defineUniPages = defineUniPages;
|
|
380
|
+
exports.extsToGlob = extsToGlob;
|
|
381
|
+
exports.getPageFiles = getPageFiles;
|
|
382
|
+
exports.getRouteBlock = getRouteBlock;
|
|
383
|
+
exports.invalidatePagesModule = invalidatePagesModule;
|
|
384
|
+
exports.isConfigFile = isConfigFile;
|
|
385
|
+
exports.isTargetFile = isTargetFile;
|
|
386
|
+
exports.mergePageMetaDataArray = mergePageMetaDataArray;
|
|
387
|
+
exports.parseCustomBlock = parseCustomBlock;
|
|
388
|
+
exports.parseSFC = parseSFC;
|
|
389
|
+
exports.resolveOptions = resolveOptions;
|
|
390
|
+
exports.resolvePageDirs = resolvePageDirs;
|
|
391
|
+
exports.writeFileSync = writeFileSync;
|