@uni-helper/vite-plugin-uni-pages 0.0.7 → 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 +76 -42
- package/dist/index.cjs +345 -247
- package/dist/index.d.ts +228 -238
- package/dist/index.mjs +366 -0
- package/package.json +20 -34
- package/README.Zh-CN.md +0 -94
- package/dist/index.cjs.map +0 -1
- package/dist/index.js +0 -261
- package/dist/index.js.map +0 -1
package/dist/index.cjs
CHANGED
|
@@ -1,293 +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
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
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")
|
|
47
42
|
};
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
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);
|
|
51
64
|
}
|
|
52
|
-
|
|
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
|
+
}
|
|
53
122
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
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
|
+
}
|
|
57
140
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
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;
|
|
73
210
|
}
|
|
74
211
|
async loadUserPagesConfig() {
|
|
75
|
-
const { config } = await (
|
|
76
|
-
sources: [
|
|
77
|
-
{
|
|
78
|
-
files: "pages.config"
|
|
79
|
-
}
|
|
80
|
-
],
|
|
81
|
-
merge: false
|
|
82
|
-
});
|
|
212
|
+
const { config } = await unconfig.loadConfig({ sources: [{ files: "pages.config" }] });
|
|
83
213
|
if (!config) {
|
|
84
|
-
logger.
|
|
85
|
-
"Can't found pages.config, please create pages.config.(ts|mts|cts|js|cjs|mjs|json)"
|
|
86
|
-
);
|
|
214
|
+
this.logger?.warn("Can't found pages.config, please create pages.config.(ts|mts|cts|js|cjs|mjs|json)");
|
|
87
215
|
process.exit(-1);
|
|
88
216
|
}
|
|
89
|
-
|
|
90
|
-
|
|
217
|
+
this.pagesGlobConfig = config;
|
|
218
|
+
debug.options(config);
|
|
91
219
|
}
|
|
92
220
|
async scanPages() {
|
|
93
|
-
const
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
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 };
|
|
97
231
|
});
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
this.
|
|
103
|
-
return
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
(0, import_path2.resolve)(process.cwd(), this.options.pagesDir, file)
|
|
107
|
-
)
|
|
108
|
-
};
|
|
109
|
-
});
|
|
110
|
-
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);
|
|
111
240
|
}
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
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();
|
|
115
249
|
});
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
path,
|
|
120
|
-
type: "page"
|
|
121
|
-
};
|
|
122
|
-
t == null ? void 0 : t.children.forEach((v) => {
|
|
123
|
-
var _a;
|
|
124
|
-
if (v.type !== 1) {
|
|
250
|
+
watcher.on("change", async (path2) => {
|
|
251
|
+
path2 = slash(path2);
|
|
252
|
+
if (!isTargetFile(path2) && !isConfigFile(path2))
|
|
125
253
|
return;
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
Object.keys(_pageMeta).forEach((key) => {
|
|
138
|
-
pageMeta[key] = _pageMeta[key];
|
|
139
|
-
});
|
|
140
|
-
pageMeta["path"] = path;
|
|
141
|
-
}
|
|
142
|
-
}
|
|
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();
|
|
143
265
|
});
|
|
144
|
-
return pageMeta;
|
|
145
266
|
}
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
);
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
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;
|
|
155
284
|
}
|
|
156
|
-
async
|
|
157
|
-
const
|
|
158
|
-
|
|
159
|
-
(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))
|
|
160
288
|
);
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
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;
|
|
165
297
|
}
|
|
298
|
+
debug.pages(generatedPageMetaData);
|
|
299
|
+
}
|
|
300
|
+
async updatePagesJSON() {
|
|
301
|
+
checkPagesJsonFile(this.resolvedPagesJSONPath);
|
|
166
302
|
this.options.onBeforeLoadUserConfig(this);
|
|
167
303
|
await this.loadUserPagesConfig();
|
|
168
304
|
this.options.onAfterLoadUserConfig(this);
|
|
169
|
-
this.options.
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
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);
|
|
175
313
|
this.options.onBeforeWriteFile(this);
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
});
|
|
314
|
+
const pagesJson = JSON.stringify({ ...this.pagesGlobConfig, pages: this.pageMetaData }, null, 2);
|
|
315
|
+
writeFileSync(this.resolvedPagesJSONPath, pagesJson);
|
|
179
316
|
this.options.onAfterWriteFile(this);
|
|
180
317
|
}
|
|
181
318
|
async virtualModule() {
|
|
182
|
-
return `export const pages = ${
|
|
319
|
+
return `export const pages = ${this.resolveRoutes()};`;
|
|
183
320
|
}
|
|
184
|
-
|
|
185
|
-
return
|
|
186
|
-
deep: 0,
|
|
187
|
-
onlyFiles: true,
|
|
188
|
-
absolute: true
|
|
189
|
-
});
|
|
321
|
+
resolveRoutes() {
|
|
322
|
+
return JSON.stringify(this.pageMetaData, null, 2);
|
|
190
323
|
}
|
|
191
|
-
|
|
192
|
-
return JSON.stringify({
|
|
193
|
-
...this.pagesConfig,
|
|
194
|
-
pages: this.pagesMeta
|
|
195
|
-
});
|
|
196
|
-
}
|
|
197
|
-
};
|
|
324
|
+
}
|
|
198
325
|
|
|
199
|
-
|
|
200
|
-
function definePages(config) {
|
|
326
|
+
function defineUniPages(config) {
|
|
201
327
|
return config;
|
|
202
328
|
}
|
|
203
329
|
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
outDir: "src",
|
|
209
|
-
exclude: [],
|
|
210
|
-
onBeforeLoadUserConfig: () => {
|
|
211
|
-
},
|
|
212
|
-
onAfterLoadUserConfig: () => {
|
|
213
|
-
},
|
|
214
|
-
onBeforeScanPages: () => {
|
|
215
|
-
},
|
|
216
|
-
onAfterScanPages: () => {
|
|
217
|
-
},
|
|
218
|
-
onBeforeMergePagesMeta: () => {
|
|
219
|
-
},
|
|
220
|
-
onAfterMergePagesMeta: () => {
|
|
221
|
-
},
|
|
222
|
-
onBeforeWriteFile: () => {
|
|
223
|
-
},
|
|
224
|
-
onAfterWriteFile: () => {
|
|
225
|
-
},
|
|
226
|
-
...userOptions
|
|
227
|
-
};
|
|
228
|
-
}
|
|
229
|
-
var VitePluginUniPages = async (userOptions = {}) => {
|
|
230
|
-
const options = resolveOptions(userOptions);
|
|
231
|
-
logger.debug("Create uni-pages context with", options);
|
|
232
|
-
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);
|
|
233
334
|
return {
|
|
234
335
|
name: "vite-plugin-uni-pages",
|
|
235
336
|
enforce: "pre",
|
|
236
|
-
async
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
moduleGraph.invalidateModule(module2);
|
|
243
|
-
if (ws) {
|
|
244
|
-
ws.send({
|
|
245
|
-
path,
|
|
246
|
-
type: "full-reload"
|
|
247
|
-
});
|
|
248
|
-
}
|
|
249
|
-
}
|
|
250
|
-
};
|
|
251
|
-
const updateVirtualModule = () => {
|
|
252
|
-
logger.debug("Update virtualModule");
|
|
253
|
-
const module2 = moduleGraph.getModuleById(resolvedVirtualModuleId);
|
|
254
|
-
reloadModule(module2);
|
|
255
|
-
};
|
|
256
|
-
watcher.on("change", async (path) => {
|
|
257
|
-
if (pagesConfigSourcePaths.includes((0, import_vite3.normalizePath)(path)) || isPagePath(path, options)) {
|
|
258
|
-
await ctx.createOrUpdatePagesJSON();
|
|
259
|
-
updateVirtualModule();
|
|
260
|
-
}
|
|
261
|
-
});
|
|
262
|
-
watcher.on("add", async (path) => {
|
|
263
|
-
if (isPagePath(path, options)) {
|
|
264
|
-
await ctx.createOrUpdatePagesJSON();
|
|
265
|
-
updateVirtualModule();
|
|
266
|
-
}
|
|
267
|
-
});
|
|
268
|
-
watcher.on("unlink", async (path) => {
|
|
269
|
-
if (isPagePath(path, options)) {
|
|
270
|
-
await ctx.createOrUpdatePagesJSON();
|
|
271
|
-
updateVirtualModule();
|
|
272
|
-
}
|
|
273
|
-
});
|
|
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));
|
|
274
343
|
},
|
|
275
|
-
|
|
276
|
-
if (id
|
|
277
|
-
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);
|
|
278
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;
|
|
279
362
|
},
|
|
280
363
|
load(id) {
|
|
281
|
-
if (id ===
|
|
364
|
+
if (id === RESOLVED_MODULE_ID_VIRTUAL)
|
|
282
365
|
return ctx.virtualModule();
|
|
283
|
-
}
|
|
284
366
|
}
|
|
285
367
|
};
|
|
286
368
|
};
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
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;
|