@univa/core 0.1.0 → 0.1.1
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 +207 -156
- package/dist/index.d.cts +7 -3
- package/dist/index.d.ts +7 -3
- package/dist/index.js +201 -150
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -31,7 +31,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
31
31
|
var index_exports = {};
|
|
32
32
|
__export(index_exports, {
|
|
33
33
|
AutoImport: () => import_vite4.default,
|
|
34
|
-
UniKuRoot: () =>
|
|
34
|
+
UniKuRoot: () => import_root3.default,
|
|
35
35
|
Univa: () => plugins_default,
|
|
36
36
|
VitePluginUniComponents: () => import_vite_plugin_uni_components3.default,
|
|
37
37
|
VitePluginUniLayouts: () => import_vite_plugin_uni_layouts2.VitePluginUniLayouts,
|
|
@@ -55,7 +55,6 @@ var import_plugin_uni = __toESM(require("@uni-helper/plugin-uni"), 1);
|
|
|
55
55
|
var import_vite_plugin_uni_components = __toESM(require("@uni-helper/vite-plugin-uni-components"), 1);
|
|
56
56
|
var import_vite_plugin_uni_layouts = require("@uni-helper/vite-plugin-uni-layouts");
|
|
57
57
|
var import_bundle_optimizer = __toESM(require("@uni-ku/bundle-optimizer"), 1);
|
|
58
|
-
var import_root = __toESM(require("@uni-ku/root"), 1);
|
|
59
58
|
var import_vite3 = __toESM(require("unplugin-auto-import/vite"), 1);
|
|
60
59
|
var import_vite_plugin_uni_polyfill = __toESM(require("vite-plugin-uni-polyfill"), 1);
|
|
61
60
|
|
|
@@ -65,6 +64,7 @@ var UNIVA_DIR_NAME = `.${ID}`;
|
|
|
65
64
|
var ROOT_NAME = `${ID}.root`;
|
|
66
65
|
var COMPONENTS_ASYNC_DIR_NAME = "components-async";
|
|
67
66
|
var SUB_PACKAGE_DIR_NAME = "app";
|
|
67
|
+
var PAGES_JSON_FILE_NAME = "pages.json";
|
|
68
68
|
|
|
69
69
|
// src/utils/fs.ts
|
|
70
70
|
var import_node_fs = require("fs");
|
|
@@ -110,8 +110,7 @@ function log(message, ...args) {
|
|
|
110
110
|
}
|
|
111
111
|
|
|
112
112
|
// src/plugins/context.ts
|
|
113
|
-
var
|
|
114
|
-
var import_node_path4 = require("path");
|
|
113
|
+
var import_node_path5 = require("path");
|
|
115
114
|
var import_utils = require("@antfu/utils");
|
|
116
115
|
var import_c122 = require("c12");
|
|
117
116
|
var import_jiti = require("jiti");
|
|
@@ -174,7 +173,6 @@ var ManifestContext = class {
|
|
|
174
173
|
* Must be called after construction.
|
|
175
174
|
*/
|
|
176
175
|
async setup() {
|
|
177
|
-
log(`manifest cwd: ${this.options.cwd}`);
|
|
178
176
|
const { config, unwatch } = await (0, import_c12.watchConfig)({
|
|
179
177
|
cwd: this.options.cwd,
|
|
180
178
|
name: "manifest",
|
|
@@ -203,7 +201,10 @@ function VitePluginUniManifest(userOptions = {}) {
|
|
|
203
201
|
};
|
|
204
202
|
}
|
|
205
203
|
function generateManifestConfigContent(content = {}) {
|
|
206
|
-
return
|
|
204
|
+
return `/* eslint-disable */
|
|
205
|
+
/* prettier-ignore */
|
|
206
|
+
// @ts-nocheck
|
|
207
|
+
// Auto-generated by @univa/core
|
|
207
208
|
export default ${JSON.stringify(content, null, 2)}
|
|
208
209
|
`;
|
|
209
210
|
}
|
|
@@ -217,6 +218,7 @@ function generateManifestConfigFile(content = {}, root) {
|
|
|
217
218
|
}
|
|
218
219
|
}
|
|
219
220
|
(0, import_node_fs2.writeFileSync)(configPath, newContent, "utf-8");
|
|
221
|
+
log(`Generated manifest config file: ${configPath}`);
|
|
220
222
|
}
|
|
221
223
|
function getManifestConfigPath(root) {
|
|
222
224
|
return (0, import_node_path2.join)(getUnivaDir(root), "manifest.config.ts");
|
|
@@ -228,33 +230,96 @@ function createManifestPlugin(userOptions = {}) {
|
|
|
228
230
|
});
|
|
229
231
|
}
|
|
230
232
|
|
|
233
|
+
// src/plugins/options.ts
|
|
234
|
+
var import_node_path3 = require("path");
|
|
235
|
+
var DEFAULT_DIRS = {
|
|
236
|
+
pages: "pages",
|
|
237
|
+
subPackages: [],
|
|
238
|
+
layouts: "layouts"
|
|
239
|
+
};
|
|
240
|
+
var DEFAULT_IMPORTS = {
|
|
241
|
+
apis: ["vue", "uni-app", "composables/**", "store/**", "hooks/**", "constants/**"],
|
|
242
|
+
components: ["components", "components-biz"]
|
|
243
|
+
};
|
|
244
|
+
var DEFAULT_UNI_PRESET = {
|
|
245
|
+
attributify: false
|
|
246
|
+
};
|
|
247
|
+
function resolveOptions2(userOptions, root) {
|
|
248
|
+
const srcDir = userOptions.srcDir || "src";
|
|
249
|
+
const srcDirAbs = (0, import_node_path3.resolve)(root, srcDir);
|
|
250
|
+
const autoScanPackages = scanSubPackages(srcDirAbs, SUB_PACKAGE_DIR_NAME);
|
|
251
|
+
const autoScanCompAsyncPackages = scanSubPackages(srcDirAbs, COMPONENTS_ASYNC_DIR_NAME);
|
|
252
|
+
const userSubPackages = userOptions.dirs?.subPackages ?? [];
|
|
253
|
+
const subPackages = [...userSubPackages, ...autoScanPackages, ...autoScanCompAsyncPackages];
|
|
254
|
+
log(`\u81EA\u52A8\u626B\u63CF\u5206\u5305\u76EE\u5F55\uFF1A${JSON.stringify({ subPackages })}`);
|
|
255
|
+
const dirs = {
|
|
256
|
+
pages: userOptions.dirs?.pages ?? DEFAULT_DIRS.pages,
|
|
257
|
+
subPackages,
|
|
258
|
+
layouts: userOptions.dirs?.layouts ?? DEFAULT_DIRS.layouts
|
|
259
|
+
};
|
|
260
|
+
const imports = {
|
|
261
|
+
apis: [...userOptions.imports?.apis ?? [], ...DEFAULT_IMPORTS.apis],
|
|
262
|
+
components: [...userOptions.imports?.components ?? [], ...DEFAULT_IMPORTS.components]
|
|
263
|
+
};
|
|
264
|
+
const unocss = {
|
|
265
|
+
presetLegacyCompat: userOptions.unocss?.presetLegacyCompat ?? true,
|
|
266
|
+
uniPreset: { ...DEFAULT_UNI_PRESET, ...userOptions.unocss?.uniPreset },
|
|
267
|
+
safelist: userOptions.unocss?.safelist ?? [],
|
|
268
|
+
themeColors: userOptions.unocss?.themeColors ?? {}
|
|
269
|
+
};
|
|
270
|
+
return {
|
|
271
|
+
// 先展开用户配置(所有属性)
|
|
272
|
+
...userOptions,
|
|
273
|
+
// 解析后的属性覆盖原始属性(必填、已应用默认值)
|
|
274
|
+
srcDir,
|
|
275
|
+
srcDirAbs,
|
|
276
|
+
dirs,
|
|
277
|
+
imports,
|
|
278
|
+
unocss
|
|
279
|
+
};
|
|
280
|
+
}
|
|
281
|
+
|
|
231
282
|
// src/plugins/pages.ts
|
|
232
283
|
var import_node_fs3 = require("fs");
|
|
233
|
-
var
|
|
284
|
+
var import_node_path4 = require("path");
|
|
234
285
|
var import_vite_plugin_uni_pages = require("@uni-helper/vite-plugin-uni-pages");
|
|
235
286
|
function getPagesConfigPath(root) {
|
|
236
|
-
return (0,
|
|
287
|
+
return (0, import_node_path4.join)(getUnivaDir(root), "pages-config.ts");
|
|
237
288
|
}
|
|
238
289
|
function generatePagesConfigContent(content) {
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
export default ${JSON.stringify(
|
|
290
|
+
return `/* eslint-disable */
|
|
291
|
+
/* prettier-ignore */
|
|
292
|
+
// @ts-nocheck
|
|
293
|
+
// Auto-generated by @univa/core
|
|
294
|
+
export default ${JSON.stringify(content, null, 2)}
|
|
244
295
|
`;
|
|
245
296
|
}
|
|
246
|
-
function generatePagesConfigFile(content, root) {
|
|
297
|
+
function generatePagesConfigFile(content, root, config) {
|
|
247
298
|
const pagesConfigPath = getPagesConfigPath(root);
|
|
248
299
|
const newContent = generatePagesConfigContent(content);
|
|
300
|
+
let needWrite = true;
|
|
249
301
|
if ((0, import_node_fs3.existsSync)(pagesConfigPath)) {
|
|
250
302
|
const existingContent = (0, import_node_fs3.readFileSync)(pagesConfigPath, "utf-8");
|
|
251
303
|
if (existingContent === newContent) {
|
|
252
|
-
|
|
304
|
+
log(`existingContent: ${existingContent}`);
|
|
305
|
+
log(`newContent: ${newContent}`);
|
|
306
|
+
log(`Pages config file ${pagesConfigPath} is up-to-date`);
|
|
307
|
+
needWrite = false;
|
|
253
308
|
}
|
|
254
309
|
}
|
|
255
|
-
(
|
|
310
|
+
if (needWrite) {
|
|
311
|
+
(0, import_node_fs3.writeFileSync)(pagesConfigPath, newContent, "utf-8");
|
|
312
|
+
log(`Generated pages config file: ${pagesConfigPath}`);
|
|
313
|
+
}
|
|
314
|
+
const resolvedPagesJSONPath = (0, import_node_path4.join)(config.srcDirAbs, PAGES_JSON_FILE_NAME);
|
|
315
|
+
log(`Generated pages JSON file: ${resolvedPagesJSONPath}`);
|
|
316
|
+
if (!(0, import_node_fs3.existsSync)(resolvedPagesJSONPath)) {
|
|
317
|
+
(0, import_node_fs3.writeFileSync)(resolvedPagesJSONPath, JSON.stringify(content, null, 2), "utf-8");
|
|
318
|
+
} else {
|
|
319
|
+
log(`Pages JSON file ${resolvedPagesJSONPath} is up-to-date`);
|
|
320
|
+
}
|
|
256
321
|
}
|
|
257
|
-
function createPagesPlugin(pluginOptions) {
|
|
322
|
+
function createPagesPlugin(pluginOptions, options) {
|
|
258
323
|
const result = (0, import_vite_plugin_uni_pages.VitePluginUniPages)({
|
|
259
324
|
dts: `${UNIVA_DIR_NAME}/uni-pages.d.ts`,
|
|
260
325
|
exclude: ["**/components/**/*.*"],
|
|
@@ -294,68 +359,80 @@ function normalizePageNames(pageContext) {
|
|
|
294
359
|
}
|
|
295
360
|
|
|
296
361
|
// src/plugins/context.ts
|
|
297
|
-
var APP_KU_VUE_TEMPLATE = `<!-- Auto-generated by @univa/core -->
|
|
298
|
-
<template>
|
|
299
|
-
<KuRootView />
|
|
300
|
-
</template>
|
|
301
|
-
`;
|
|
302
362
|
var UnivaContext = class {
|
|
303
363
|
_server;
|
|
304
|
-
|
|
305
|
-
|
|
364
|
+
_config;
|
|
365
|
+
_configPath;
|
|
366
|
+
_jiti;
|
|
367
|
+
_resolveUserOptions;
|
|
368
|
+
/** 项目根目录(Vite root) */
|
|
306
369
|
root;
|
|
307
370
|
constructor(options) {
|
|
308
371
|
this.root = options.root;
|
|
372
|
+
this._jiti = (0, import_jiti.createJiti)(this.root);
|
|
309
373
|
}
|
|
310
374
|
/**
|
|
311
|
-
*
|
|
312
|
-
*
|
|
313
|
-
* UniKuRoot 插件依赖 src 下的虚拟根组件文件(默认 App.ku.vue)。
|
|
314
|
-
* 若不存在则生成最小模板,避免插件注入 import 时解析失败。
|
|
375
|
+
* 异步加载配置(c12)
|
|
315
376
|
*
|
|
316
|
-
*
|
|
317
|
-
*
|
|
318
|
-
* 导致 Vite 重编译与 VitePluginUniPages 的 pages.json 写入冲突(EPERM)。
|
|
377
|
+
* 支持更多格式:JSON/YAML/远程配置等
|
|
378
|
+
* 用于配置文件变化时重新加载
|
|
319
379
|
*/
|
|
320
|
-
|
|
321
|
-
const
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
}
|
|
327
|
-
|
|
328
|
-
log(`\u5DF2\u751F\u6210\u865A\u62DF\u6839\u7EC4\u4EF6\uFF1A${(0, import_utils.slash)(rootComponentPath)}`);
|
|
380
|
+
async loadUserConfig() {
|
|
381
|
+
const { config: userConfig, configFile } = await (0, import_c122.loadConfig)({
|
|
382
|
+
name: ID,
|
|
383
|
+
cwd: this.root,
|
|
384
|
+
defaults: this._config || {},
|
|
385
|
+
merger: mergeWithArrayOverride
|
|
386
|
+
});
|
|
387
|
+
this.resolveConfig(userConfig, configFile);
|
|
329
388
|
}
|
|
330
389
|
/**
|
|
331
|
-
*
|
|
390
|
+
* 同步加载配置(jiti)
|
|
391
|
+
*
|
|
392
|
+
* 用于插件初始化阶段,确保配置在插件创建前可用
|
|
332
393
|
*/
|
|
333
394
|
loadUserConfigSync() {
|
|
334
|
-
|
|
335
|
-
|
|
395
|
+
if (!this._jiti) {
|
|
396
|
+
log("Jiti \u672A\u521D\u59CB\u5316\uFF0C\u65E0\u6CD5\u52A0\u8F7D\u914D\u7F6E");
|
|
397
|
+
return;
|
|
398
|
+
}
|
|
399
|
+
const configFile = (0, import_node_path5.join)(this.root, `${ID}.config`);
|
|
336
400
|
try {
|
|
337
|
-
const configModule =
|
|
401
|
+
const configModule = this._jiti(configFile);
|
|
338
402
|
const userConfig = configModule?.default || configModule;
|
|
339
|
-
this.
|
|
340
|
-
|
|
341
|
-
|
|
403
|
+
this.resolveConfig(mergeWithArrayOverride({}, userConfig), configFile);
|
|
404
|
+
} catch (e) {
|
|
405
|
+
log(`\u52A0\u8F7D\u914D\u7F6E\u6587\u4EF6\u5931\u8D25\uFF1A${configFile}`, e);
|
|
342
406
|
}
|
|
343
407
|
}
|
|
344
408
|
/**
|
|
345
|
-
*
|
|
409
|
+
* 解析配置
|
|
410
|
+
*
|
|
411
|
+
* 统一处理配置加载后的逻辑:
|
|
412
|
+
* - 存储 raw config
|
|
413
|
+
* - 解析为 resolved options(应用默认值、扫描目录)
|
|
346
414
|
*/
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
this.
|
|
355
|
-
|
|
415
|
+
resolveConfig(userConfig, configFile) {
|
|
416
|
+
this._config = userConfig;
|
|
417
|
+
this._configPath = configFile || "";
|
|
418
|
+
this._resolveUserOptions = resolveOptions2(userConfig, this.root);
|
|
419
|
+
}
|
|
420
|
+
/** 获取解析后的配置 */
|
|
421
|
+
get resolveUserOptions() {
|
|
422
|
+
return this._resolveUserOptions;
|
|
423
|
+
}
|
|
424
|
+
/** 获取原始配置 */
|
|
425
|
+
get config() {
|
|
426
|
+
return this._config;
|
|
427
|
+
}
|
|
428
|
+
/** 获取配置文件路径 */
|
|
429
|
+
get configPath() {
|
|
430
|
+
return this._configPath;
|
|
356
431
|
}
|
|
357
432
|
/**
|
|
358
433
|
* 设置 Vite 开发服务器
|
|
434
|
+
*
|
|
435
|
+
* 监听配置文件变化,触发重新加载
|
|
359
436
|
*/
|
|
360
437
|
setupViteServer(server) {
|
|
361
438
|
if (this._server === server)
|
|
@@ -366,117 +443,104 @@ var UnivaContext = class {
|
|
|
366
443
|
/**
|
|
367
444
|
* 设置文件监听器
|
|
368
445
|
*
|
|
369
|
-
* 监听 univa.config.ts
|
|
446
|
+
* 监听 univa.config.ts 变化:
|
|
447
|
+
* - 重新加载配置
|
|
448
|
+
* - 更新生成文件
|
|
449
|
+
* - 触发 HMR
|
|
370
450
|
*/
|
|
371
451
|
setupWatcher(watcher) {
|
|
372
|
-
if (!this.
|
|
452
|
+
if (!this._configPath)
|
|
373
453
|
return;
|
|
374
|
-
|
|
375
|
-
watcher.add(this.configPath);
|
|
454
|
+
watcher.add(this._configPath);
|
|
376
455
|
watcher.on("change", async (filePath) => {
|
|
377
|
-
filePath
|
|
378
|
-
const configPath = (0, import_utils.slash)(this.configPath || "");
|
|
379
|
-
if (filePath !== configPath) {
|
|
456
|
+
if ((0, import_utils.slash)(filePath) !== (0, import_utils.slash)(this._configPath || ""))
|
|
380
457
|
return;
|
|
381
|
-
}
|
|
382
458
|
await this.updateConfig();
|
|
383
459
|
this.onUpdate();
|
|
384
460
|
});
|
|
385
461
|
}
|
|
462
|
+
/**
|
|
463
|
+
* 更新配置
|
|
464
|
+
*
|
|
465
|
+
* 配置文件变化时:
|
|
466
|
+
* - 异步加载新配置
|
|
467
|
+
* - 更新生成文件
|
|
468
|
+
*/
|
|
386
469
|
async updateConfig() {
|
|
387
|
-
await this.
|
|
470
|
+
await this.loadUserConfig();
|
|
388
471
|
this.updateGeneratedFiles();
|
|
389
472
|
}
|
|
390
473
|
/**
|
|
391
|
-
*
|
|
392
|
-
*
|
|
393
|
-
* 读取 univa.config.ts 的 pages 字段(应用级配置),
|
|
394
|
-
* 生成 .univa/pages-config.ts 供 VitePluginUniPages 消费。
|
|
474
|
+
* 更新生成文件
|
|
395
475
|
*
|
|
396
|
-
*
|
|
397
|
-
*
|
|
398
|
-
*
|
|
399
|
-
* App.ku.vue 的生成在 Univa() 初始化阶段完成。
|
|
476
|
+
* 根据配置生成:
|
|
477
|
+
* - .univa/pages-config.ts(VitePluginUniPages 依赖)
|
|
478
|
+
* - .univa/manifest-config.ts(可选)
|
|
400
479
|
*/
|
|
401
480
|
updateGeneratedFiles() {
|
|
402
|
-
const
|
|
403
|
-
if (
|
|
404
|
-
|
|
481
|
+
const resolved = this._resolveUserOptions;
|
|
482
|
+
if (!resolved)
|
|
483
|
+
return;
|
|
484
|
+
const pagesConfig = resolved.pages || {};
|
|
485
|
+
if (resolved.homePage) {
|
|
486
|
+
pagesConfig.pages = pagesConfig.pages || [];
|
|
487
|
+
pagesConfig.pages.unshift(resolved.homePage);
|
|
488
|
+
log(`\u5DF2\u63D2\u5165\u9996\u9875\u914D\u7F6E\uFF1A${JSON.stringify(resolved.homePage)}`);
|
|
405
489
|
}
|
|
406
|
-
|
|
490
|
+
log(`\u751F\u6210 pages \u914D\u7F6E\uFF1A${JSON.stringify(pagesConfig)}`);
|
|
491
|
+
generatePagesConfigFile(pagesConfig, this.root, resolved);
|
|
492
|
+
const manifestConfig = resolved.manifest;
|
|
407
493
|
if (manifestConfig) {
|
|
408
494
|
generateManifestConfigFile(manifestConfig, this.root);
|
|
409
495
|
}
|
|
410
|
-
const pagesTsPath = getPagesConfigPath(this.root);
|
|
411
|
-
const emptyContent = `// Auto-generated by @univa/core
|
|
412
|
-
export default {}
|
|
413
|
-
`;
|
|
414
|
-
if (!(0, import_node_fs4.existsSync)(pagesTsPath)) {
|
|
415
|
-
(0, import_node_fs4.writeFileSync)(pagesTsPath, emptyContent, "utf-8");
|
|
416
|
-
} else if (!pagesConfig) {
|
|
417
|
-
if ((0, import_node_fs4.readFileSync)(pagesTsPath, "utf-8") !== emptyContent) {
|
|
418
|
-
(0, import_node_fs4.writeFileSync)(pagesTsPath, emptyContent, "utf-8");
|
|
419
|
-
}
|
|
420
|
-
}
|
|
421
496
|
}
|
|
422
497
|
/**
|
|
423
|
-
* 更新回调
|
|
424
498
|
* 触发 HMR
|
|
499
|
+
*
|
|
500
|
+
* 配置变化后通知浏览器刷新
|
|
425
501
|
*/
|
|
426
502
|
onUpdate() {
|
|
427
|
-
if (!this._server)
|
|
503
|
+
if (!this._server)
|
|
428
504
|
return;
|
|
429
|
-
}
|
|
430
505
|
this._server.ws.send({
|
|
431
506
|
type: "full-reload"
|
|
432
507
|
});
|
|
433
508
|
}
|
|
434
509
|
};
|
|
435
510
|
|
|
436
|
-
// src/plugins/
|
|
437
|
-
var
|
|
438
|
-
var
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
uniPreset: userOptions.unocss?.uniPreset ?? DEFAULT_UNI_PRESET,
|
|
470
|
-
safelist: userOptions.unocss?.safelist ?? [],
|
|
471
|
-
themeColors: userOptions.unocss?.themeColors ?? {}
|
|
472
|
-
};
|
|
473
|
-
return {
|
|
474
|
-
srcDir,
|
|
475
|
-
dirs,
|
|
476
|
-
unocss,
|
|
477
|
-
imports,
|
|
478
|
-
overrides: userOptions.overrides
|
|
479
|
-
};
|
|
511
|
+
// src/plugins/root.ts
|
|
512
|
+
var import_node_fs4 = require("fs");
|
|
513
|
+
var import_node_path6 = require("path");
|
|
514
|
+
var import_utils2 = require("@antfu/utils");
|
|
515
|
+
var import_root = __toESM(require("@uni-ku/root"), 1);
|
|
516
|
+
var APP_KU_VUE_TEMPLATE = `<!-- Auto-generated by @univa/core -->
|
|
517
|
+
<template>
|
|
518
|
+
<KuRootView />
|
|
519
|
+
</template>
|
|
520
|
+
`;
|
|
521
|
+
function ensureRootComponent(fileName, srcDirAbs) {
|
|
522
|
+
const rootComponentPath = (0, import_node_path6.join)(srcDirAbs, fileName);
|
|
523
|
+
if ((0, import_node_fs4.existsSync)(rootComponentPath)) {
|
|
524
|
+
return;
|
|
525
|
+
}
|
|
526
|
+
(0, import_node_fs4.writeFileSync)(rootComponentPath, APP_KU_VUE_TEMPLATE, "utf-8");
|
|
527
|
+
log(`\u5DF2\u751F\u6210\u865A\u62DF\u6839\u7EC4\u4EF6\uFF1A${(0, import_utils2.slash)(rootComponentPath)}`);
|
|
528
|
+
}
|
|
529
|
+
function createRootPlugin(options) {
|
|
530
|
+
let rootFileName = `${ROOT_NAME}.vue`;
|
|
531
|
+
if (options.overrides?.root?.rootFileName) {
|
|
532
|
+
rootFileName = `${options.overrides.root.rootFileName}.vue`;
|
|
533
|
+
}
|
|
534
|
+
ensureRootComponent(rootFileName, options.srcDirAbs);
|
|
535
|
+
return (0, import_root.default)({
|
|
536
|
+
enabledVirtualHost: true,
|
|
537
|
+
enabledGlobalRef: true,
|
|
538
|
+
rootFileName: ROOT_NAME,
|
|
539
|
+
excludePages: [
|
|
540
|
+
`${COMPONENTS_ASYNC_DIR_NAME}/**/*.*`
|
|
541
|
+
],
|
|
542
|
+
...options.overrides?.root
|
|
543
|
+
});
|
|
480
544
|
}
|
|
481
545
|
|
|
482
546
|
// src/plugins/unocss.ts
|
|
@@ -547,10 +611,6 @@ function createCorePlugin() {
|
|
|
547
611
|
return {
|
|
548
612
|
name: "vite-plugin-univa",
|
|
549
613
|
enforce: "pre",
|
|
550
|
-
async configResolved() {
|
|
551
|
-
await context.loadUserConfigAsync();
|
|
552
|
-
context.updateGeneratedFiles();
|
|
553
|
-
},
|
|
554
614
|
configureServer(server) {
|
|
555
615
|
context.setupViteServer(server);
|
|
556
616
|
}
|
|
@@ -588,21 +648,14 @@ function createUnivaPlugins(resolved) {
|
|
|
588
648
|
plugins.push(...createPagesPlugin({
|
|
589
649
|
dir: `${srcDir}/${resolved.dirs.pages}`,
|
|
590
650
|
subPackages: resolved.dirs.subPackages.map((p) => `${srcDir}/${p}`),
|
|
651
|
+
debug: resolved.debug,
|
|
591
652
|
...resolved.overrides?.pages
|
|
592
|
-
}));
|
|
653
|
+
}, resolved));
|
|
593
654
|
plugins.push((0, import_vite_plugin_uni_layouts.VitePluginUniLayouts)({
|
|
594
655
|
layoutDir: `${srcDir}/${resolved.dirs.layouts}`,
|
|
595
656
|
...resolved.overrides?.layouts
|
|
596
657
|
}));
|
|
597
|
-
plugins.push((
|
|
598
|
-
enabledVirtualHost: true,
|
|
599
|
-
enabledGlobalRef: true,
|
|
600
|
-
rootFileName: ROOT_NAME,
|
|
601
|
-
excludePages: [
|
|
602
|
-
`${COMPONENTS_ASYNC_DIR_NAME}/**/*.*`
|
|
603
|
-
],
|
|
604
|
-
...resolved.overrides?.root
|
|
605
|
-
}));
|
|
658
|
+
plugins.push(createRootPlugin(resolved));
|
|
606
659
|
plugins.push((0, import_vite_plugin_uni_components.default)({
|
|
607
660
|
dirs: resolved.imports.components.map((c) => `${srcDir}/${c}`),
|
|
608
661
|
dts: `${UNIVA_DIR_NAME}/components.d.ts`,
|
|
@@ -641,11 +694,9 @@ function Univa() {
|
|
|
641
694
|
});
|
|
642
695
|
ensureUnivaDir(context.root);
|
|
643
696
|
context.loadUserConfigSync();
|
|
644
|
-
setDebug(context.
|
|
645
|
-
context.
|
|
646
|
-
|
|
647
|
-
log("\u5DF2\u52A0\u8F7D\u914D\u7F6E\uFF1A", context.configPath || "\u9ED8\u8BA4\u914D\u7F6E");
|
|
648
|
-
return createUnivaPlugins(resolved);
|
|
697
|
+
setDebug(context.resolveUserOptions.debug ?? false);
|
|
698
|
+
context.updateGeneratedFiles();
|
|
699
|
+
return createUnivaPlugins(context.resolveUserOptions);
|
|
649
700
|
}
|
|
650
701
|
var plugins_default = Univa;
|
|
651
702
|
|
|
@@ -655,7 +706,7 @@ var import_vite_plugin_uni_components2 = require("@uni-helper/vite-plugin-uni-co
|
|
|
655
706
|
var import_vite_plugin_uni_components3 = __toESM(require("@uni-helper/vite-plugin-uni-components"), 1);
|
|
656
707
|
var import_vite_plugin_uni_layouts2 = require("@uni-helper/vite-plugin-uni-layouts");
|
|
657
708
|
var import_vite_plugin_uni_pages2 = require("@uni-helper/vite-plugin-uni-pages");
|
|
658
|
-
var
|
|
709
|
+
var import_root3 = __toESM(require("@uni-ku/root"), 1);
|
|
659
710
|
var import_vite4 = __toESM(require("unplugin-auto-import/vite"), 1);
|
|
660
711
|
// Annotate the CommonJS export names for ESM import in node:
|
|
661
712
|
0 && (module.exports = {
|
package/dist/index.d.cts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { PagesConfig } from '@uni-helper/uni-pages-types';
|
|
1
|
+
import { PageMetaDatum, PagesConfig } from '@uni-helper/uni-pages-types';
|
|
2
2
|
export { Condition, EasyCom, GlobalStyle, PageMetaDatum, PagesConfig, SubPackage, TabBar, TabBarItem } from '@uni-helper/uni-pages-types';
|
|
3
3
|
import { UserUniPresetOptions } from '@uni-helper/unocss-preset-uni';
|
|
4
4
|
export { UserUniPresetOptions, presetUni } from '@uni-helper/unocss-preset-uni';
|
|
@@ -7,7 +7,7 @@ export { ComponentInfo, ComponentResolver, Options as ComponentsOptions, Resolve
|
|
|
7
7
|
import { UserManifestConfig } from '@uni-helper/vite-plugin-uni-manifest';
|
|
8
8
|
import { UserOptions } from '@uni-helper/vite-plugin-uni-pages';
|
|
9
9
|
export { PageContext, PagePath, UserOptions as PagesPluginOptions, ResolvedOptions as PagesResolvedOptions, SubPageMetaDatum, VitePluginUniPages, definePage } from '@uni-helper/vite-plugin-uni-pages';
|
|
10
|
-
import {
|
|
10
|
+
import { IOptions } from '@uni-ku/bundle-optimizer';
|
|
11
11
|
import { ConfigBase } from 'unocss';
|
|
12
12
|
export { ConfigBase as UnoCSSConfigBase } from 'unocss';
|
|
13
13
|
import { Options as Options$1 } from 'unplugin-auto-import/types';
|
|
@@ -159,7 +159,7 @@ interface UnivaOverrides {
|
|
|
159
159
|
/** @uni-helper/vite-plugin-uni-manifest 完整选项 */
|
|
160
160
|
manifest?: UserManifestOptions;
|
|
161
161
|
/** @uni-ku/bundle-optimizer 完整选项 */
|
|
162
|
-
optimization?:
|
|
162
|
+
optimization?: IOptions;
|
|
163
163
|
}
|
|
164
164
|
/**
|
|
165
165
|
* Univa 统一配置接口(工程级约定)
|
|
@@ -175,6 +175,8 @@ interface UnivaOptions {
|
|
|
175
175
|
dirs?: UnivaDirs;
|
|
176
176
|
/** 自动导入配置 */
|
|
177
177
|
imports?: UnivaImports;
|
|
178
|
+
/** 首页配置 */
|
|
179
|
+
homePage?: PageMetaDatum;
|
|
178
180
|
/**
|
|
179
181
|
* 应用级 pages.json 配置(globalStyle / tabBar 等)
|
|
180
182
|
*
|
|
@@ -204,6 +206,8 @@ interface ResolvedOptions extends Omit<UnivaOptions, 'dirs' | 'imports' | 'srcDi
|
|
|
204
206
|
/** 自动导入配置(已应用默认值,字段必填) */
|
|
205
207
|
imports: ResolvedUnivaImports;
|
|
206
208
|
unocss: ResolvedUnivaUnocss;
|
|
209
|
+
homePage?: PageMetaDatum;
|
|
210
|
+
srcDirAbs: string;
|
|
207
211
|
}
|
|
208
212
|
|
|
209
213
|
/**
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { PagesConfig } from '@uni-helper/uni-pages-types';
|
|
1
|
+
import { PageMetaDatum, PagesConfig } from '@uni-helper/uni-pages-types';
|
|
2
2
|
export { Condition, EasyCom, GlobalStyle, PageMetaDatum, PagesConfig, SubPackage, TabBar, TabBarItem } from '@uni-helper/uni-pages-types';
|
|
3
3
|
import { UserUniPresetOptions } from '@uni-helper/unocss-preset-uni';
|
|
4
4
|
export { UserUniPresetOptions, presetUni } from '@uni-helper/unocss-preset-uni';
|
|
@@ -7,7 +7,7 @@ export { ComponentInfo, ComponentResolver, Options as ComponentsOptions, Resolve
|
|
|
7
7
|
import { UserManifestConfig } from '@uni-helper/vite-plugin-uni-manifest';
|
|
8
8
|
import { UserOptions } from '@uni-helper/vite-plugin-uni-pages';
|
|
9
9
|
export { PageContext, PagePath, UserOptions as PagesPluginOptions, ResolvedOptions as PagesResolvedOptions, SubPageMetaDatum, VitePluginUniPages, definePage } from '@uni-helper/vite-plugin-uni-pages';
|
|
10
|
-
import {
|
|
10
|
+
import { IOptions } from '@uni-ku/bundle-optimizer';
|
|
11
11
|
import { ConfigBase } from 'unocss';
|
|
12
12
|
export { ConfigBase as UnoCSSConfigBase } from 'unocss';
|
|
13
13
|
import { Options as Options$1 } from 'unplugin-auto-import/types';
|
|
@@ -159,7 +159,7 @@ interface UnivaOverrides {
|
|
|
159
159
|
/** @uni-helper/vite-plugin-uni-manifest 完整选项 */
|
|
160
160
|
manifest?: UserManifestOptions;
|
|
161
161
|
/** @uni-ku/bundle-optimizer 完整选项 */
|
|
162
|
-
optimization?:
|
|
162
|
+
optimization?: IOptions;
|
|
163
163
|
}
|
|
164
164
|
/**
|
|
165
165
|
* Univa 统一配置接口(工程级约定)
|
|
@@ -175,6 +175,8 @@ interface UnivaOptions {
|
|
|
175
175
|
dirs?: UnivaDirs;
|
|
176
176
|
/** 自动导入配置 */
|
|
177
177
|
imports?: UnivaImports;
|
|
178
|
+
/** 首页配置 */
|
|
179
|
+
homePage?: PageMetaDatum;
|
|
178
180
|
/**
|
|
179
181
|
* 应用级 pages.json 配置(globalStyle / tabBar 等)
|
|
180
182
|
*
|
|
@@ -204,6 +206,8 @@ interface ResolvedOptions extends Omit<UnivaOptions, 'dirs' | 'imports' | 'srcDi
|
|
|
204
206
|
/** 自动导入配置(已应用默认值,字段必填) */
|
|
205
207
|
imports: ResolvedUnivaImports;
|
|
206
208
|
unocss: ResolvedUnivaUnocss;
|
|
209
|
+
homePage?: PageMetaDatum;
|
|
210
|
+
srcDirAbs: string;
|
|
207
211
|
}
|
|
208
212
|
|
|
209
213
|
/**
|
package/dist/index.js
CHANGED
|
@@ -8,7 +8,6 @@ import Uni from "@uni-helper/plugin-uni";
|
|
|
8
8
|
import VitePluginUniComponents from "@uni-helper/vite-plugin-uni-components";
|
|
9
9
|
import { VitePluginUniLayouts } from "@uni-helper/vite-plugin-uni-layouts";
|
|
10
10
|
import Optimization from "@uni-ku/bundle-optimizer";
|
|
11
|
-
import UniKuRoot from "@uni-ku/root";
|
|
12
11
|
import AutoImport from "unplugin-auto-import/vite";
|
|
13
12
|
import UniPolyfill from "vite-plugin-uni-polyfill";
|
|
14
13
|
|
|
@@ -18,6 +17,7 @@ var UNIVA_DIR_NAME = `.${ID}`;
|
|
|
18
17
|
var ROOT_NAME = `${ID}.root`;
|
|
19
18
|
var COMPONENTS_ASYNC_DIR_NAME = "components-async";
|
|
20
19
|
var SUB_PACKAGE_DIR_NAME = "app";
|
|
20
|
+
var PAGES_JSON_FILE_NAME = "pages.json";
|
|
21
21
|
|
|
22
22
|
// src/utils/fs.ts
|
|
23
23
|
import { existsSync, mkdirSync, readdirSync, statSync } from "fs";
|
|
@@ -63,7 +63,6 @@ function log(message, ...args) {
|
|
|
63
63
|
}
|
|
64
64
|
|
|
65
65
|
// src/plugins/context.ts
|
|
66
|
-
import { existsSync as existsSync4, readFileSync as readFileSync3, writeFileSync as writeFileSync3 } from "fs";
|
|
67
66
|
import { join as join4 } from "path";
|
|
68
67
|
import { slash } from "@antfu/utils";
|
|
69
68
|
import { loadConfig } from "c12";
|
|
@@ -127,7 +126,6 @@ var ManifestContext = class {
|
|
|
127
126
|
* Must be called after construction.
|
|
128
127
|
*/
|
|
129
128
|
async setup() {
|
|
130
|
-
log(`manifest cwd: ${this.options.cwd}`);
|
|
131
129
|
const { config, unwatch } = await watchConfig({
|
|
132
130
|
cwd: this.options.cwd,
|
|
133
131
|
name: "manifest",
|
|
@@ -156,7 +154,10 @@ function VitePluginUniManifest(userOptions = {}) {
|
|
|
156
154
|
};
|
|
157
155
|
}
|
|
158
156
|
function generateManifestConfigContent(content = {}) {
|
|
159
|
-
return
|
|
157
|
+
return `/* eslint-disable */
|
|
158
|
+
/* prettier-ignore */
|
|
159
|
+
// @ts-nocheck
|
|
160
|
+
// Auto-generated by @univa/core
|
|
160
161
|
export default ${JSON.stringify(content, null, 2)}
|
|
161
162
|
`;
|
|
162
163
|
}
|
|
@@ -170,6 +171,7 @@ function generateManifestConfigFile(content = {}, root) {
|
|
|
170
171
|
}
|
|
171
172
|
}
|
|
172
173
|
writeFileSync(configPath, newContent, "utf-8");
|
|
174
|
+
log(`Generated manifest config file: ${configPath}`);
|
|
173
175
|
}
|
|
174
176
|
function getManifestConfigPath(root) {
|
|
175
177
|
return join2(getUnivaDir(root), "manifest.config.ts");
|
|
@@ -181,6 +183,55 @@ function createManifestPlugin(userOptions = {}) {
|
|
|
181
183
|
});
|
|
182
184
|
}
|
|
183
185
|
|
|
186
|
+
// src/plugins/options.ts
|
|
187
|
+
import { resolve as resolve2 } from "path";
|
|
188
|
+
var DEFAULT_DIRS = {
|
|
189
|
+
pages: "pages",
|
|
190
|
+
subPackages: [],
|
|
191
|
+
layouts: "layouts"
|
|
192
|
+
};
|
|
193
|
+
var DEFAULT_IMPORTS = {
|
|
194
|
+
apis: ["vue", "uni-app", "composables/**", "store/**", "hooks/**", "constants/**"],
|
|
195
|
+
components: ["components", "components-biz"]
|
|
196
|
+
};
|
|
197
|
+
var DEFAULT_UNI_PRESET = {
|
|
198
|
+
attributify: false
|
|
199
|
+
};
|
|
200
|
+
function resolveOptions2(userOptions, root) {
|
|
201
|
+
const srcDir = userOptions.srcDir || "src";
|
|
202
|
+
const srcDirAbs = resolve2(root, srcDir);
|
|
203
|
+
const autoScanPackages = scanSubPackages(srcDirAbs, SUB_PACKAGE_DIR_NAME);
|
|
204
|
+
const autoScanCompAsyncPackages = scanSubPackages(srcDirAbs, COMPONENTS_ASYNC_DIR_NAME);
|
|
205
|
+
const userSubPackages = userOptions.dirs?.subPackages ?? [];
|
|
206
|
+
const subPackages = [...userSubPackages, ...autoScanPackages, ...autoScanCompAsyncPackages];
|
|
207
|
+
log(`\u81EA\u52A8\u626B\u63CF\u5206\u5305\u76EE\u5F55\uFF1A${JSON.stringify({ subPackages })}`);
|
|
208
|
+
const dirs = {
|
|
209
|
+
pages: userOptions.dirs?.pages ?? DEFAULT_DIRS.pages,
|
|
210
|
+
subPackages,
|
|
211
|
+
layouts: userOptions.dirs?.layouts ?? DEFAULT_DIRS.layouts
|
|
212
|
+
};
|
|
213
|
+
const imports = {
|
|
214
|
+
apis: [...userOptions.imports?.apis ?? [], ...DEFAULT_IMPORTS.apis],
|
|
215
|
+
components: [...userOptions.imports?.components ?? [], ...DEFAULT_IMPORTS.components]
|
|
216
|
+
};
|
|
217
|
+
const unocss = {
|
|
218
|
+
presetLegacyCompat: userOptions.unocss?.presetLegacyCompat ?? true,
|
|
219
|
+
uniPreset: { ...DEFAULT_UNI_PRESET, ...userOptions.unocss?.uniPreset },
|
|
220
|
+
safelist: userOptions.unocss?.safelist ?? [],
|
|
221
|
+
themeColors: userOptions.unocss?.themeColors ?? {}
|
|
222
|
+
};
|
|
223
|
+
return {
|
|
224
|
+
// 先展开用户配置(所有属性)
|
|
225
|
+
...userOptions,
|
|
226
|
+
// 解析后的属性覆盖原始属性(必填、已应用默认值)
|
|
227
|
+
srcDir,
|
|
228
|
+
srcDirAbs,
|
|
229
|
+
dirs,
|
|
230
|
+
imports,
|
|
231
|
+
unocss
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
|
|
184
235
|
// src/plugins/pages.ts
|
|
185
236
|
import { existsSync as existsSync3, readFileSync as readFileSync2, writeFileSync as writeFileSync2 } from "fs";
|
|
186
237
|
import { join as join3 } from "path";
|
|
@@ -189,25 +240,39 @@ function getPagesConfigPath(root) {
|
|
|
189
240
|
return join3(getUnivaDir(root), "pages-config.ts");
|
|
190
241
|
}
|
|
191
242
|
function generatePagesConfigContent(content) {
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
export default ${JSON.stringify(
|
|
243
|
+
return `/* eslint-disable */
|
|
244
|
+
/* prettier-ignore */
|
|
245
|
+
// @ts-nocheck
|
|
246
|
+
// Auto-generated by @univa/core
|
|
247
|
+
export default ${JSON.stringify(content, null, 2)}
|
|
197
248
|
`;
|
|
198
249
|
}
|
|
199
|
-
function generatePagesConfigFile(content, root) {
|
|
250
|
+
function generatePagesConfigFile(content, root, config) {
|
|
200
251
|
const pagesConfigPath = getPagesConfigPath(root);
|
|
201
252
|
const newContent = generatePagesConfigContent(content);
|
|
253
|
+
let needWrite = true;
|
|
202
254
|
if (existsSync3(pagesConfigPath)) {
|
|
203
255
|
const existingContent = readFileSync2(pagesConfigPath, "utf-8");
|
|
204
256
|
if (existingContent === newContent) {
|
|
205
|
-
|
|
257
|
+
log(`existingContent: ${existingContent}`);
|
|
258
|
+
log(`newContent: ${newContent}`);
|
|
259
|
+
log(`Pages config file ${pagesConfigPath} is up-to-date`);
|
|
260
|
+
needWrite = false;
|
|
206
261
|
}
|
|
207
262
|
}
|
|
208
|
-
|
|
263
|
+
if (needWrite) {
|
|
264
|
+
writeFileSync2(pagesConfigPath, newContent, "utf-8");
|
|
265
|
+
log(`Generated pages config file: ${pagesConfigPath}`);
|
|
266
|
+
}
|
|
267
|
+
const resolvedPagesJSONPath = join3(config.srcDirAbs, PAGES_JSON_FILE_NAME);
|
|
268
|
+
log(`Generated pages JSON file: ${resolvedPagesJSONPath}`);
|
|
269
|
+
if (!existsSync3(resolvedPagesJSONPath)) {
|
|
270
|
+
writeFileSync2(resolvedPagesJSONPath, JSON.stringify(content, null, 2), "utf-8");
|
|
271
|
+
} else {
|
|
272
|
+
log(`Pages JSON file ${resolvedPagesJSONPath} is up-to-date`);
|
|
273
|
+
}
|
|
209
274
|
}
|
|
210
|
-
function createPagesPlugin(pluginOptions) {
|
|
275
|
+
function createPagesPlugin(pluginOptions, options) {
|
|
211
276
|
const result = VitePluginUniPages({
|
|
212
277
|
dts: `${UNIVA_DIR_NAME}/uni-pages.d.ts`,
|
|
213
278
|
exclude: ["**/components/**/*.*"],
|
|
@@ -247,68 +312,80 @@ function normalizePageNames(pageContext) {
|
|
|
247
312
|
}
|
|
248
313
|
|
|
249
314
|
// src/plugins/context.ts
|
|
250
|
-
var APP_KU_VUE_TEMPLATE = `<!-- Auto-generated by @univa/core -->
|
|
251
|
-
<template>
|
|
252
|
-
<KuRootView />
|
|
253
|
-
</template>
|
|
254
|
-
`;
|
|
255
315
|
var UnivaContext = class {
|
|
256
316
|
_server;
|
|
257
|
-
|
|
258
|
-
|
|
317
|
+
_config;
|
|
318
|
+
_configPath;
|
|
319
|
+
_jiti;
|
|
320
|
+
_resolveUserOptions;
|
|
321
|
+
/** 项目根目录(Vite root) */
|
|
259
322
|
root;
|
|
260
323
|
constructor(options) {
|
|
261
324
|
this.root = options.root;
|
|
325
|
+
this._jiti = createJiti(this.root);
|
|
262
326
|
}
|
|
263
327
|
/**
|
|
264
|
-
*
|
|
328
|
+
* 异步加载配置(c12)
|
|
265
329
|
*
|
|
266
|
-
*
|
|
267
|
-
*
|
|
268
|
-
*
|
|
269
|
-
* 必须在 Vite 启动前调用(Univa() 初始化阶段),
|
|
270
|
-
* 否则在 configResolved 阶段创建 src 下新文件会触发 watcher add 事件,
|
|
271
|
-
* 导致 Vite 重编译与 VitePluginUniPages 的 pages.json 写入冲突(EPERM)。
|
|
330
|
+
* 支持更多格式:JSON/YAML/远程配置等
|
|
331
|
+
* 用于配置文件变化时重新加载
|
|
272
332
|
*/
|
|
273
|
-
|
|
274
|
-
const
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
log(`\u5DF2\u751F\u6210\u865A\u62DF\u6839\u7EC4\u4EF6\uFF1A${slash(rootComponentPath)}`);
|
|
333
|
+
async loadUserConfig() {
|
|
334
|
+
const { config: userConfig, configFile } = await loadConfig({
|
|
335
|
+
name: ID,
|
|
336
|
+
cwd: this.root,
|
|
337
|
+
defaults: this._config || {},
|
|
338
|
+
merger: mergeWithArrayOverride
|
|
339
|
+
});
|
|
340
|
+
this.resolveConfig(userConfig, configFile);
|
|
282
341
|
}
|
|
283
342
|
/**
|
|
284
|
-
*
|
|
343
|
+
* 同步加载配置(jiti)
|
|
344
|
+
*
|
|
345
|
+
* 用于插件初始化阶段,确保配置在插件创建前可用
|
|
285
346
|
*/
|
|
286
347
|
loadUserConfigSync() {
|
|
287
|
-
|
|
348
|
+
if (!this._jiti) {
|
|
349
|
+
log("Jiti \u672A\u521D\u59CB\u5316\uFF0C\u65E0\u6CD5\u52A0\u8F7D\u914D\u7F6E");
|
|
350
|
+
return;
|
|
351
|
+
}
|
|
288
352
|
const configFile = join4(this.root, `${ID}.config`);
|
|
289
353
|
try {
|
|
290
|
-
const configModule =
|
|
354
|
+
const configModule = this._jiti(configFile);
|
|
291
355
|
const userConfig = configModule?.default || configModule;
|
|
292
|
-
this.
|
|
293
|
-
|
|
294
|
-
|
|
356
|
+
this.resolveConfig(mergeWithArrayOverride({}, userConfig), configFile);
|
|
357
|
+
} catch (e) {
|
|
358
|
+
log(`\u52A0\u8F7D\u914D\u7F6E\u6587\u4EF6\u5931\u8D25\uFF1A${configFile}`, e);
|
|
295
359
|
}
|
|
296
360
|
}
|
|
297
361
|
/**
|
|
298
|
-
*
|
|
362
|
+
* 解析配置
|
|
363
|
+
*
|
|
364
|
+
* 统一处理配置加载后的逻辑:
|
|
365
|
+
* - 存储 raw config
|
|
366
|
+
* - 解析为 resolved options(应用默认值、扫描目录)
|
|
299
367
|
*/
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
this.
|
|
308
|
-
|
|
368
|
+
resolveConfig(userConfig, configFile) {
|
|
369
|
+
this._config = userConfig;
|
|
370
|
+
this._configPath = configFile || "";
|
|
371
|
+
this._resolveUserOptions = resolveOptions2(userConfig, this.root);
|
|
372
|
+
}
|
|
373
|
+
/** 获取解析后的配置 */
|
|
374
|
+
get resolveUserOptions() {
|
|
375
|
+
return this._resolveUserOptions;
|
|
376
|
+
}
|
|
377
|
+
/** 获取原始配置 */
|
|
378
|
+
get config() {
|
|
379
|
+
return this._config;
|
|
380
|
+
}
|
|
381
|
+
/** 获取配置文件路径 */
|
|
382
|
+
get configPath() {
|
|
383
|
+
return this._configPath;
|
|
309
384
|
}
|
|
310
385
|
/**
|
|
311
386
|
* 设置 Vite 开发服务器
|
|
387
|
+
*
|
|
388
|
+
* 监听配置文件变化,触发重新加载
|
|
312
389
|
*/
|
|
313
390
|
setupViteServer(server) {
|
|
314
391
|
if (this._server === server)
|
|
@@ -319,117 +396,104 @@ var UnivaContext = class {
|
|
|
319
396
|
/**
|
|
320
397
|
* 设置文件监听器
|
|
321
398
|
*
|
|
322
|
-
* 监听 univa.config.ts
|
|
399
|
+
* 监听 univa.config.ts 变化:
|
|
400
|
+
* - 重新加载配置
|
|
401
|
+
* - 更新生成文件
|
|
402
|
+
* - 触发 HMR
|
|
323
403
|
*/
|
|
324
404
|
setupWatcher(watcher) {
|
|
325
|
-
if (!this.
|
|
405
|
+
if (!this._configPath)
|
|
326
406
|
return;
|
|
327
|
-
|
|
328
|
-
watcher.add(this.configPath);
|
|
407
|
+
watcher.add(this._configPath);
|
|
329
408
|
watcher.on("change", async (filePath) => {
|
|
330
|
-
filePath
|
|
331
|
-
const configPath = slash(this.configPath || "");
|
|
332
|
-
if (filePath !== configPath) {
|
|
409
|
+
if (slash(filePath) !== slash(this._configPath || ""))
|
|
333
410
|
return;
|
|
334
|
-
}
|
|
335
411
|
await this.updateConfig();
|
|
336
412
|
this.onUpdate();
|
|
337
413
|
});
|
|
338
414
|
}
|
|
415
|
+
/**
|
|
416
|
+
* 更新配置
|
|
417
|
+
*
|
|
418
|
+
* 配置文件变化时:
|
|
419
|
+
* - 异步加载新配置
|
|
420
|
+
* - 更新生成文件
|
|
421
|
+
*/
|
|
339
422
|
async updateConfig() {
|
|
340
|
-
await this.
|
|
423
|
+
await this.loadUserConfig();
|
|
341
424
|
this.updateGeneratedFiles();
|
|
342
425
|
}
|
|
343
426
|
/**
|
|
344
|
-
*
|
|
345
|
-
*
|
|
346
|
-
* 读取 univa.config.ts 的 pages 字段(应用级配置),
|
|
347
|
-
* 生成 .univa/pages-config.ts 供 VitePluginUniPages 消费。
|
|
427
|
+
* 更新生成文件
|
|
348
428
|
*
|
|
349
|
-
*
|
|
350
|
-
*
|
|
351
|
-
*
|
|
352
|
-
* App.ku.vue 的生成在 Univa() 初始化阶段完成。
|
|
429
|
+
* 根据配置生成:
|
|
430
|
+
* - .univa/pages-config.ts(VitePluginUniPages 依赖)
|
|
431
|
+
* - .univa/manifest-config.ts(可选)
|
|
353
432
|
*/
|
|
354
433
|
updateGeneratedFiles() {
|
|
355
|
-
const
|
|
356
|
-
if (
|
|
357
|
-
|
|
434
|
+
const resolved = this._resolveUserOptions;
|
|
435
|
+
if (!resolved)
|
|
436
|
+
return;
|
|
437
|
+
const pagesConfig = resolved.pages || {};
|
|
438
|
+
if (resolved.homePage) {
|
|
439
|
+
pagesConfig.pages = pagesConfig.pages || [];
|
|
440
|
+
pagesConfig.pages.unshift(resolved.homePage);
|
|
441
|
+
log(`\u5DF2\u63D2\u5165\u9996\u9875\u914D\u7F6E\uFF1A${JSON.stringify(resolved.homePage)}`);
|
|
358
442
|
}
|
|
359
|
-
|
|
443
|
+
log(`\u751F\u6210 pages \u914D\u7F6E\uFF1A${JSON.stringify(pagesConfig)}`);
|
|
444
|
+
generatePagesConfigFile(pagesConfig, this.root, resolved);
|
|
445
|
+
const manifestConfig = resolved.manifest;
|
|
360
446
|
if (manifestConfig) {
|
|
361
447
|
generateManifestConfigFile(manifestConfig, this.root);
|
|
362
448
|
}
|
|
363
|
-
const pagesTsPath = getPagesConfigPath(this.root);
|
|
364
|
-
const emptyContent = `// Auto-generated by @univa/core
|
|
365
|
-
export default {}
|
|
366
|
-
`;
|
|
367
|
-
if (!existsSync4(pagesTsPath)) {
|
|
368
|
-
writeFileSync3(pagesTsPath, emptyContent, "utf-8");
|
|
369
|
-
} else if (!pagesConfig) {
|
|
370
|
-
if (readFileSync3(pagesTsPath, "utf-8") !== emptyContent) {
|
|
371
|
-
writeFileSync3(pagesTsPath, emptyContent, "utf-8");
|
|
372
|
-
}
|
|
373
|
-
}
|
|
374
449
|
}
|
|
375
450
|
/**
|
|
376
|
-
* 更新回调
|
|
377
451
|
* 触发 HMR
|
|
452
|
+
*
|
|
453
|
+
* 配置变化后通知浏览器刷新
|
|
378
454
|
*/
|
|
379
455
|
onUpdate() {
|
|
380
|
-
if (!this._server)
|
|
456
|
+
if (!this._server)
|
|
381
457
|
return;
|
|
382
|
-
}
|
|
383
458
|
this._server.ws.send({
|
|
384
459
|
type: "full-reload"
|
|
385
460
|
});
|
|
386
461
|
}
|
|
387
462
|
};
|
|
388
463
|
|
|
389
|
-
// src/plugins/
|
|
390
|
-
import {
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
uniPreset: userOptions.unocss?.uniPreset ?? DEFAULT_UNI_PRESET,
|
|
423
|
-
safelist: userOptions.unocss?.safelist ?? [],
|
|
424
|
-
themeColors: userOptions.unocss?.themeColors ?? {}
|
|
425
|
-
};
|
|
426
|
-
return {
|
|
427
|
-
srcDir,
|
|
428
|
-
dirs,
|
|
429
|
-
unocss,
|
|
430
|
-
imports,
|
|
431
|
-
overrides: userOptions.overrides
|
|
432
|
-
};
|
|
464
|
+
// src/plugins/root.ts
|
|
465
|
+
import { existsSync as existsSync4, writeFileSync as writeFileSync3 } from "fs";
|
|
466
|
+
import { join as join5 } from "path";
|
|
467
|
+
import { slash as slash2 } from "@antfu/utils";
|
|
468
|
+
import UniKuRoot from "@uni-ku/root";
|
|
469
|
+
var APP_KU_VUE_TEMPLATE = `<!-- Auto-generated by @univa/core -->
|
|
470
|
+
<template>
|
|
471
|
+
<KuRootView />
|
|
472
|
+
</template>
|
|
473
|
+
`;
|
|
474
|
+
function ensureRootComponent(fileName, srcDirAbs) {
|
|
475
|
+
const rootComponentPath = join5(srcDirAbs, fileName);
|
|
476
|
+
if (existsSync4(rootComponentPath)) {
|
|
477
|
+
return;
|
|
478
|
+
}
|
|
479
|
+
writeFileSync3(rootComponentPath, APP_KU_VUE_TEMPLATE, "utf-8");
|
|
480
|
+
log(`\u5DF2\u751F\u6210\u865A\u62DF\u6839\u7EC4\u4EF6\uFF1A${slash2(rootComponentPath)}`);
|
|
481
|
+
}
|
|
482
|
+
function createRootPlugin(options) {
|
|
483
|
+
let rootFileName = `${ROOT_NAME}.vue`;
|
|
484
|
+
if (options.overrides?.root?.rootFileName) {
|
|
485
|
+
rootFileName = `${options.overrides.root.rootFileName}.vue`;
|
|
486
|
+
}
|
|
487
|
+
ensureRootComponent(rootFileName, options.srcDirAbs);
|
|
488
|
+
return UniKuRoot({
|
|
489
|
+
enabledVirtualHost: true,
|
|
490
|
+
enabledGlobalRef: true,
|
|
491
|
+
rootFileName: ROOT_NAME,
|
|
492
|
+
excludePages: [
|
|
493
|
+
`${COMPONENTS_ASYNC_DIR_NAME}/**/*.*`
|
|
494
|
+
],
|
|
495
|
+
...options.overrides?.root
|
|
496
|
+
});
|
|
433
497
|
}
|
|
434
498
|
|
|
435
499
|
// src/plugins/unocss.ts
|
|
@@ -500,10 +564,6 @@ function createCorePlugin() {
|
|
|
500
564
|
return {
|
|
501
565
|
name: "vite-plugin-univa",
|
|
502
566
|
enforce: "pre",
|
|
503
|
-
async configResolved() {
|
|
504
|
-
await context.loadUserConfigAsync();
|
|
505
|
-
context.updateGeneratedFiles();
|
|
506
|
-
},
|
|
507
567
|
configureServer(server) {
|
|
508
568
|
context.setupViteServer(server);
|
|
509
569
|
}
|
|
@@ -541,21 +601,14 @@ function createUnivaPlugins(resolved) {
|
|
|
541
601
|
plugins.push(...createPagesPlugin({
|
|
542
602
|
dir: `${srcDir}/${resolved.dirs.pages}`,
|
|
543
603
|
subPackages: resolved.dirs.subPackages.map((p) => `${srcDir}/${p}`),
|
|
604
|
+
debug: resolved.debug,
|
|
544
605
|
...resolved.overrides?.pages
|
|
545
|
-
}));
|
|
606
|
+
}, resolved));
|
|
546
607
|
plugins.push(VitePluginUniLayouts({
|
|
547
608
|
layoutDir: `${srcDir}/${resolved.dirs.layouts}`,
|
|
548
609
|
...resolved.overrides?.layouts
|
|
549
610
|
}));
|
|
550
|
-
plugins.push(
|
|
551
|
-
enabledVirtualHost: true,
|
|
552
|
-
enabledGlobalRef: true,
|
|
553
|
-
rootFileName: ROOT_NAME,
|
|
554
|
-
excludePages: [
|
|
555
|
-
`${COMPONENTS_ASYNC_DIR_NAME}/**/*.*`
|
|
556
|
-
],
|
|
557
|
-
...resolved.overrides?.root
|
|
558
|
-
}));
|
|
611
|
+
plugins.push(createRootPlugin(resolved));
|
|
559
612
|
plugins.push(VitePluginUniComponents({
|
|
560
613
|
dirs: resolved.imports.components.map((c) => `${srcDir}/${c}`),
|
|
561
614
|
dts: `${UNIVA_DIR_NAME}/components.d.ts`,
|
|
@@ -594,11 +647,9 @@ function Univa() {
|
|
|
594
647
|
});
|
|
595
648
|
ensureUnivaDir(context.root);
|
|
596
649
|
context.loadUserConfigSync();
|
|
597
|
-
setDebug(context.
|
|
598
|
-
context.
|
|
599
|
-
|
|
600
|
-
log("\u5DF2\u52A0\u8F7D\u914D\u7F6E\uFF1A", context.configPath || "\u9ED8\u8BA4\u914D\u7F6E");
|
|
601
|
-
return createUnivaPlugins(resolved);
|
|
650
|
+
setDebug(context.resolveUserOptions.debug ?? false);
|
|
651
|
+
context.updateGeneratedFiles();
|
|
652
|
+
return createUnivaPlugins(context.resolveUserOptions);
|
|
602
653
|
}
|
|
603
654
|
var plugins_default = Univa;
|
|
604
655
|
|