@univa/core 0.0.7 → 0.0.8

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.
@@ -0,0 +1,4 @@
1
+ /// <reference types="@dcloudio/types" />
2
+ /// <reference types="vite/client" />
3
+ /// <reference types="@uni-helper/vite-plugin-uni-pages/client" />
4
+ /// <reference types="@uni-helper/uni-types" />
package/dist/index.cjs CHANGED
@@ -30,7 +30,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
30
30
  // src/index.ts
31
31
  var index_exports = {};
32
32
  __export(index_exports, {
33
- AutoImport: () => import_vite3.default,
33
+ AutoImport: () => import_vite4.default,
34
34
  PageContext: () => import_vite_plugin_uni_pages2.PageContext,
35
35
  UniKuRoot: () => import_root2.default,
36
36
  Univa: () => plugins_default,
@@ -56,12 +56,13 @@ var import_plugin_uni = __toESM(require("@uni-helper/plugin-uni"), 1);
56
56
  var import_vite_plugin_uni_components = __toESM(require("@uni-helper/vite-plugin-uni-components"), 1);
57
57
  var import_vite_plugin_uni_layouts = require("@uni-helper/vite-plugin-uni-layouts");
58
58
  var import_root = __toESM(require("@uni-ku/root"), 1);
59
- var import_vite = __toESM(require("unocss/vite"), 1);
60
- var import_vite2 = __toESM(require("unplugin-auto-import/vite"), 1);
59
+ var import_vite2 = __toESM(require("unocss/vite"), 1);
60
+ var import_vite3 = __toESM(require("unplugin-auto-import/vite"), 1);
61
61
 
62
62
  // src/constant.ts
63
63
  var ID = "univa";
64
64
  var UNIVA_DIR_NAME = `.${ID}`;
65
+ var ROOT_NAME = `${ID}.root`;
65
66
 
66
67
  // src/utils/fs.ts
67
68
  var import_node_fs = require("fs");
@@ -80,11 +81,22 @@ function ensureUnivaDir(cwd) {
80
81
  return dir;
81
82
  }
82
83
 
84
+ // src/utils/logger.ts
85
+ var debugMode = false;
86
+ function setDebug(enabled) {
87
+ debugMode = enabled;
88
+ }
89
+ function log(message, ...args) {
90
+ if (debugMode) {
91
+ console.log(`[${ID}]`, message, ...args);
92
+ }
93
+ }
94
+
83
95
  // src/plugins/context.ts
84
- var import_node_fs3 = require("fs");
85
- var import_node_path3 = require("path");
96
+ var import_node_fs4 = require("fs");
97
+ var import_node_path4 = require("path");
86
98
  var import_utils = require("@antfu/utils");
87
- var import_c12 = require("c12");
99
+ var import_c122 = require("c12");
88
100
  var import_jiti = require("jiti");
89
101
 
90
102
  // src/utils/merge.ts
@@ -96,12 +108,115 @@ var mergeWithArrayOverride = (0, import_defu.createDefu)((originObj, key, update
96
108
  }
97
109
  });
98
110
 
99
- // src/plugins/pages.ts
111
+ // src/plugins/manifest.ts
100
112
  var import_node_fs2 = require("fs");
101
113
  var import_node_path2 = require("path");
114
+ var import_node_process = __toESM(require("process"), 1);
115
+ var import_vite_plugin_uni_manifest = require("@uni-helper/vite-plugin-uni-manifest");
116
+ var import_c12 = require("c12");
117
+ var import_vite = require("vite");
118
+ function resolveOptions(userOptions) {
119
+ return {
120
+ minify: false,
121
+ insertFinalNewline: false,
122
+ cwd: import_node_process.default.env.VITE_ROOT_DIR,
123
+ merge: true,
124
+ ...userOptions
125
+ };
126
+ }
127
+ function writeManifestJson(config = {}, opts) {
128
+ const path = resolveManifestJsonPath();
129
+ let mergeContent = {};
130
+ if (opts?.merge && (0, import_node_fs2.existsSync)(path)) {
131
+ try {
132
+ const content2 = (0, import_node_fs2.readFileSync)(path, "utf-8");
133
+ mergeContent = JSON.parse(content2);
134
+ } catch (error) {
135
+ log(`manifest.json parse error: ${error}`);
136
+ }
137
+ }
138
+ const content = JSON.stringify(opts?.merge ? mergeWithArrayOverride(config, mergeContent) : config, null, opts?.minify ? 0 : 2) + (opts?.insertFinalNewline ? "\n" : "");
139
+ if ((0, import_node_fs2.existsSync)(path) && (0, import_node_fs2.readFileSync)(path, "utf-8") === content) {
140
+ return;
141
+ }
142
+ (0, import_node_fs2.writeFileSync)(path, content);
143
+ }
144
+ function resolveManifestJsonPath() {
145
+ return (0, import_vite.normalizePath)(
146
+ (0, import_node_path2.resolve)(import_node_process.default.env.UNI_INPUT_DIR || `${import_node_process.default.cwd()}/src`, "manifest.json")
147
+ );
148
+ }
149
+ var ManifestContext = class {
150
+ options;
151
+ unwatch;
152
+ constructor(options) {
153
+ this.options = resolveOptions(options);
154
+ }
155
+ /**
156
+ * Start watching config sources and perform initial write.
157
+ * Must be called after construction.
158
+ */
159
+ async setup() {
160
+ log(`manifest cwd: ${this.options.cwd}`);
161
+ const { config, unwatch } = await (0, import_c12.watchConfig)({
162
+ cwd: this.options.cwd,
163
+ name: "manifest",
164
+ rcFile: false,
165
+ packageJson: false,
166
+ onUpdate: (config2) => {
167
+ writeManifestJson(config2.newConfig.config, this.options);
168
+ }
169
+ });
170
+ writeManifestJson(config, this.options);
171
+ this.unwatch = unwatch;
172
+ }
173
+ };
174
+ function VitePluginUniManifest(userOptions = {}) {
175
+ let ctx;
176
+ return {
177
+ name: "vite-plugin-uni-manifest",
178
+ // Run before other plugins to ensure manifest.json is ready
179
+ enforce: "pre",
180
+ async configResolved() {
181
+ (0, import_vite_plugin_uni_manifest.ensureManifestJsonExists)();
182
+ ctx = new ManifestContext(userOptions);
183
+ await ctx.setup();
184
+ },
185
+ buildEnd: () => ctx?.unwatch()
186
+ };
187
+ }
188
+ function generateManifestConfigContent(content = {}) {
189
+ return `// Auto-generated by @univa/core
190
+ export default ${JSON.stringify(content, null, 2)}
191
+ `;
192
+ }
193
+ function generateManifestConfigFile(content = {}, root) {
194
+ const configPath = getManifestConfigPath(root);
195
+ const newContent = generateManifestConfigContent(content);
196
+ if ((0, import_node_fs2.existsSync)(configPath)) {
197
+ const existingContent = (0, import_node_fs2.readFileSync)(configPath, "utf-8");
198
+ if (existingContent === newContent) {
199
+ return;
200
+ }
201
+ }
202
+ (0, import_node_fs2.writeFileSync)(configPath, newContent, "utf-8");
203
+ }
204
+ function getManifestConfigPath(root) {
205
+ return (0, import_node_path2.join)(getUnivaDir(root), "manifest.config.ts");
206
+ }
207
+ function createManifestPlugin(userOptions = {}) {
208
+ return VitePluginUniManifest({
209
+ cwd: (0, import_node_path2.join)(import_node_process.default.env.VITE_ROOT_DIR || "", UNIVA_DIR_NAME),
210
+ ...userOptions
211
+ });
212
+ }
213
+
214
+ // src/plugins/pages.ts
215
+ var import_node_fs3 = require("fs");
216
+ var import_node_path3 = require("path");
102
217
  var import_vite_plugin_uni_pages = require("@uni-helper/vite-plugin-uni-pages");
103
218
  function getPagesConfigPath(root) {
104
- return (0, import_node_path2.join)(getUnivaDir(root), "pages-config.ts");
219
+ return (0, import_node_path3.join)(getUnivaDir(root), "pages-config.ts");
105
220
  }
106
221
  function generatePagesConfigContent(content) {
107
222
  const { pages, subPackages, ...rest } = content;
@@ -115,13 +230,13 @@ export default ${JSON.stringify(rest, null, 2)}
115
230
  function generatePagesConfigFile(content, root) {
116
231
  const pagesConfigPath = getPagesConfigPath(root);
117
232
  const newContent = generatePagesConfigContent(content);
118
- if ((0, import_node_fs2.existsSync)(pagesConfigPath)) {
119
- const existingContent = (0, import_node_fs2.readFileSync)(pagesConfigPath, "utf-8");
233
+ if ((0, import_node_fs3.existsSync)(pagesConfigPath)) {
234
+ const existingContent = (0, import_node_fs3.readFileSync)(pagesConfigPath, "utf-8");
120
235
  if (existingContent === newContent) {
121
236
  return;
122
237
  }
123
238
  }
124
- (0, import_node_fs2.writeFileSync)(pagesConfigPath, newContent, "utf-8");
239
+ (0, import_node_fs3.writeFileSync)(pagesConfigPath, newContent, "utf-8");
125
240
  }
126
241
  function createPagesPlugin(pluginOptions) {
127
242
  const result = (0, import_vite_plugin_uni_pages.VitePluginUniPages)({
@@ -134,23 +249,11 @@ function createPagesPlugin(pluginOptions) {
134
249
  }
135
250
 
136
251
  // src/plugins/context.ts
137
- var DEFAULT_ROOT_FILE_NAME = "App.ku.vue";
138
252
  var APP_KU_VUE_TEMPLATE = `<!-- Auto-generated by @univa/core -->
139
- <!-- UniKuRoot \u865A\u62DF\u6839\u7EC4\u4EF6\uFF0C\u53EF\u5728\u6B64\u6DFB\u52A0\u5168\u5C40\u7EC4\u4EF6\uFF08\u5982 Toast\u3001ConfigProvider \u7B49\uFF09 -->
140
253
  <template>
141
254
  <KuRootView />
142
255
  </template>
143
256
  `;
144
- function ensureRootComponent(config, root) {
145
- const srcDir = config?.srcDir || "src";
146
- const rootFileName = config?.overrides?.root?.rootFileName || DEFAULT_ROOT_FILE_NAME;
147
- const rootComponentPath = (0, import_node_path3.join)(root, srcDir, rootFileName);
148
- if ((0, import_node_fs3.existsSync)(rootComponentPath)) {
149
- return;
150
- }
151
- (0, import_node_fs3.writeFileSync)(rootComponentPath, APP_KU_VUE_TEMPLATE, "utf-8");
152
- console.log(`[univa] \u5DF2\u751F\u6210\u865A\u62DF\u6839\u7EC4\u4EF6\uFF1A${(0, import_utils.slash)(rootComponentPath)}`);
153
- }
154
257
  var UnivaContext = class {
155
258
  _server;
156
259
  config;
@@ -159,12 +262,32 @@ var UnivaContext = class {
159
262
  constructor(options) {
160
263
  this.root = options.root;
161
264
  }
265
+ /**
266
+ * 确保虚拟根组件文件存在
267
+ *
268
+ * UniKuRoot 插件依赖 src 下的虚拟根组件文件(默认 App.ku.vue)。
269
+ * 若不存在则生成最小模板,避免插件注入 import 时解析失败。
270
+ *
271
+ * 必须在 Vite 启动前调用(Univa() 初始化阶段),
272
+ * 否则在 configResolved 阶段创建 src 下新文件会触发 watcher add 事件,
273
+ * 导致 Vite 重编译与 VitePluginUniPages 的 pages.json 写入冲突(EPERM)。
274
+ */
275
+ ensureRootComponent(fileName) {
276
+ const srcDir = this.config?.srcDir || "src";
277
+ const rootFileName = this.config?.overrides?.root?.rootFileName || fileName;
278
+ const rootComponentPath = (0, import_node_path4.join)(this.root, srcDir, rootFileName);
279
+ if ((0, import_node_fs4.existsSync)(rootComponentPath)) {
280
+ return;
281
+ }
282
+ (0, import_node_fs4.writeFileSync)(rootComponentPath, APP_KU_VUE_TEMPLATE, "utf-8");
283
+ log(`\u5DF2\u751F\u6210\u865A\u62DF\u6839\u7EC4\u4EF6\uFF1A${(0, import_utils.slash)(rootComponentPath)}`);
284
+ }
162
285
  /**
163
286
  * 同步加载用户配置(univa.config.ts)
164
287
  */
165
288
  loadUserConfigSync() {
166
289
  const jiti = (0, import_jiti.createJiti)(this.root);
167
- const configFile = (0, import_node_path3.join)(this.root, `${ID}.config`);
290
+ const configFile = (0, import_node_path4.join)(this.root, `${ID}.config`);
168
291
  try {
169
292
  const configModule = jiti(configFile);
170
293
  const userConfig = configModule?.default || configModule;
@@ -177,7 +300,7 @@ var UnivaContext = class {
177
300
  * 异步加载用户配置(c12 支持更多格式与合并能力)
178
301
  */
179
302
  async loadUserConfigAsync() {
180
- const { config: userConfig, configFile } = await (0, import_c12.loadConfig)({
303
+ const { config: userConfig, configFile } = await (0, import_c122.loadConfig)({
181
304
  name: ID,
182
305
  cwd: this.root,
183
306
  defaults: this.config || {},
@@ -223,26 +346,33 @@ var UnivaContext = class {
223
346
  * 更新生成的文件
224
347
  *
225
348
  * 读取 univa.config.ts 的 pages 字段(应用级配置),
226
- * 生成 .univa/pages.ts 供 VitePluginUniPages 消费。
227
- * 同时确保 UniKuRoot 虚拟根组件文件存在。
349
+ * 生成 .univa/pages-config.ts 供 VitePluginUniPages 消费。
350
+ *
351
+ * 注意:不在此处生成 src/App.ku.vue,避免在 configResolved 阶段
352
+ * 创建 src 下新文件触发 watcher add 事件,导致 Vite 重编译
353
+ * 与 VitePluginUniPages 的 pages.json 写入冲突(EPERM)。
354
+ * App.ku.vue 的生成在 Univa() 初始化阶段完成。
228
355
  */
229
356
  updateGeneratedFiles() {
230
357
  const pagesConfig = this.config?.pages;
231
358
  if (pagesConfig) {
232
359
  generatePagesConfigFile(pagesConfig, this.root);
233
360
  }
361
+ const manifestConfig = this.config?.manifest;
362
+ if (manifestConfig) {
363
+ generateManifestConfigFile(manifestConfig, this.root);
364
+ }
234
365
  const pagesTsPath = getPagesConfigPath(this.root);
235
366
  const emptyContent = `// Auto-generated by @univa/core
236
367
  export default {}
237
368
  `;
238
- if (!(0, import_node_fs3.existsSync)(pagesTsPath)) {
239
- (0, import_node_fs3.writeFileSync)(pagesTsPath, emptyContent, "utf-8");
369
+ if (!(0, import_node_fs4.existsSync)(pagesTsPath)) {
370
+ (0, import_node_fs4.writeFileSync)(pagesTsPath, emptyContent, "utf-8");
240
371
  } else if (!pagesConfig) {
241
- if ((0, import_node_fs3.readFileSync)(pagesTsPath, "utf-8") !== emptyContent) {
242
- (0, import_node_fs3.writeFileSync)(pagesTsPath, emptyContent, "utf-8");
372
+ if ((0, import_node_fs4.readFileSync)(pagesTsPath, "utf-8") !== emptyContent) {
373
+ (0, import_node_fs4.writeFileSync)(pagesTsPath, emptyContent, "utf-8");
243
374
  }
244
375
  }
245
- ensureRootComponent(this.config, this.root);
246
376
  }
247
377
  /**
248
378
  * 更新回调
@@ -268,7 +398,7 @@ var DEFAULT_IMPORTS = {
268
398
  apis: ["vue", "uni-app", "composables/**", "stores/**", "hooks/**", "constants/**"],
269
399
  components: ["components/**", "components-biz/**"]
270
400
  };
271
- function resolveOptions(userOptions) {
401
+ function resolveOptions2(userOptions) {
272
402
  const srcDir = userOptions.srcDir || "src";
273
403
  const dirs = {
274
404
  pages: userOptions.dirs?.pages ?? DEFAULT_DIRS.pages,
@@ -341,7 +471,9 @@ function createUnivaPlugins(resolved) {
341
471
  ...resolved.overrides?.layouts
342
472
  }));
343
473
  plugins.push((0, import_root.default)({
474
+ enabledVirtualHost: true,
344
475
  enabledGlobalRef: true,
476
+ rootFileName: ROOT_NAME,
345
477
  excludePages: [
346
478
  "components-async/**/*.*"
347
479
  ],
@@ -354,10 +486,13 @@ function createUnivaPlugins(resolved) {
354
486
  directoryAsNamespace: true,
355
487
  ...resolved.overrides?.components
356
488
  }));
489
+ plugins.push(createManifestPlugin({
490
+ ...resolved.overrides?.manifest
491
+ }));
357
492
  plugins.push(...(0, import_plugin_uni.default)());
358
- plugins.push(...(0, import_vite.default)());
493
+ plugins.push(...(0, import_vite2.default)());
359
494
  const { presets, dirs } = resolveApiSources(resolved.imports.apis, srcDir);
360
- const autoImportPlugin = (0, import_vite2.default)({
495
+ const autoImportPlugin = (0, import_vite3.default)({
361
496
  imports: presets,
362
497
  dirs,
363
498
  dts: `${UNIVA_DIR_NAME}/auto-imports.d.ts`,
@@ -376,8 +511,10 @@ function Univa() {
376
511
  });
377
512
  ensureUnivaDir(context.root);
378
513
  context.loadUserConfigSync();
379
- const resolved = resolveOptions(context.config || {});
380
- console.log("[univa] \u5DF2\u52A0\u8F7D\u914D\u7F6E\uFF1A", context.configPath || "\u9ED8\u8BA4\u914D\u7F6E");
514
+ setDebug(context.config?.debug ?? false);
515
+ context.ensureRootComponent(`${ROOT_NAME}.vue`);
516
+ const resolved = resolveOptions2(context.config || {});
517
+ log("\u5DF2\u52A0\u8F7D\u914D\u7F6E\uFF1A", context.configPath || "\u9ED8\u8BA4\u914D\u7F6E");
381
518
  return createUnivaPlugins(resolved);
382
519
  }
383
520
  var plugins_default = Univa;
@@ -389,7 +526,7 @@ var import_vite_plugin_uni_components3 = __toESM(require("@uni-helper/vite-plugi
389
526
  var import_vite_plugin_uni_layouts2 = require("@uni-helper/vite-plugin-uni-layouts");
390
527
  var import_vite_plugin_uni_pages2 = require("@uni-helper/vite-plugin-uni-pages");
391
528
  var import_root2 = __toESM(require("@uni-ku/root"), 1);
392
- var import_vite3 = __toESM(require("unplugin-auto-import/vite"), 1);
529
+ var import_vite4 = __toESM(require("unplugin-auto-import/vite"), 1);
393
530
  // Annotate the CommonJS export names for ESM import in node:
394
531
  0 && (module.exports = {
395
532
  AutoImport,
package/dist/index.d.cts CHANGED
@@ -2,6 +2,7 @@ import { 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 { Options } from '@uni-helper/vite-plugin-uni-components';
4
4
  export { ComponentInfo, ComponentResolver, Options as ComponentsOptions, ResolvedOptions as ComponentsResolvedOptions, ImportInfo, default as VitePluginUniComponents, camelCase, kebabCase, pascalCase } from '@uni-helper/vite-plugin-uni-components';
5
+ import { UserManifestConfig } from '@uni-helper/vite-plugin-uni-manifest';
5
6
  import { UserOptions } from '@uni-helper/vite-plugin-uni-pages';
6
7
  export { PageContext, PagePath, UserOptions as PagesPluginOptions, ResolvedOptions as PagesResolvedOptions, SubPageMetaDatum, VitePluginUniPages, definePage } from '@uni-helper/vite-plugin-uni-pages';
7
8
  import { Options as Options$1 } from 'unplugin-auto-import/types';
@@ -12,6 +13,32 @@ export { VitePluginUniLayouts } from '@uni-helper/vite-plugin-uni-layouts';
12
13
  export { default as UniKuRoot } from '@uni-ku/root';
13
14
  export { default as AutoImport } from 'unplugin-auto-import/vite';
14
15
 
16
+ interface ManifestOptions {
17
+ /**
18
+ * minify the `manifest.json`
19
+ * @default false
20
+ */
21
+ minify: boolean;
22
+ /**
23
+ * insert newline at the end of the `manifest.json`
24
+ * @default false
25
+ */
26
+ insertFinalNewline: boolean;
27
+ /**
28
+ * Resolve configuration from this working directory.
29
+ * @default process.env.VITE_ROOT_DIR
30
+ */
31
+ cwd?: string;
32
+ /**
33
+ * Merge the manifest config with the default config.
34
+ * @default false
35
+ */
36
+ merge?: boolean;
37
+ }
38
+ /** User-facing options — all fields are optional. */
39
+ interface UserManifestOptions extends Partial<ManifestOptions> {
40
+ }
41
+
15
42
  /**
16
43
  * Univa 类型定义统一导出
17
44
  *
@@ -115,6 +142,8 @@ interface UnivaOverrides {
115
142
  autoImport?: Options$1;
116
143
  /** unocss/vite 完整选项 */
117
144
  unocss?: UnoCSSOptions;
145
+ /** @uni-helper/vite-plugin-uni-manifest 完整选项 */
146
+ manifest?: UserManifestOptions;
118
147
  }
119
148
  /**
120
149
  * Univa 统一配置接口(工程级约定)
@@ -124,6 +153,8 @@ interface UnivaOverrides {
124
153
  interface UnivaOptions {
125
154
  /** 源码目录,相对 Vite root,默认 'src' */
126
155
  srcDir?: string;
156
+ /** 开启调试日志,默认 false */
157
+ debug?: boolean;
127
158
  /** 目录约定,相对 srcDir */
128
159
  dirs?: UnivaDirs;
129
160
  /** 自动导入配置 */
@@ -137,6 +168,8 @@ interface UnivaOptions {
137
168
  * 注意:pages / subPackages 由目录扫描自动填充,请勿在此手写。
138
169
  */
139
170
  pages?: Partial<PagesConfig>;
171
+ /** 应用级 manifest.json 配置 */
172
+ manifest?: Partial<UserManifestConfig>;
140
173
  /** 底层插件逃生口(覆盖融合层默认,高级用户使用) */
141
174
  overrides?: UnivaOverrides;
142
175
  }
package/dist/index.d.ts CHANGED
@@ -2,6 +2,7 @@ import { 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 { Options } from '@uni-helper/vite-plugin-uni-components';
4
4
  export { ComponentInfo, ComponentResolver, Options as ComponentsOptions, ResolvedOptions as ComponentsResolvedOptions, ImportInfo, default as VitePluginUniComponents, camelCase, kebabCase, pascalCase } from '@uni-helper/vite-plugin-uni-components';
5
+ import { UserManifestConfig } from '@uni-helper/vite-plugin-uni-manifest';
5
6
  import { UserOptions } from '@uni-helper/vite-plugin-uni-pages';
6
7
  export { PageContext, PagePath, UserOptions as PagesPluginOptions, ResolvedOptions as PagesResolvedOptions, SubPageMetaDatum, VitePluginUniPages, definePage } from '@uni-helper/vite-plugin-uni-pages';
7
8
  import { Options as Options$1 } from 'unplugin-auto-import/types';
@@ -12,6 +13,32 @@ export { VitePluginUniLayouts } from '@uni-helper/vite-plugin-uni-layouts';
12
13
  export { default as UniKuRoot } from '@uni-ku/root';
13
14
  export { default as AutoImport } from 'unplugin-auto-import/vite';
14
15
 
16
+ interface ManifestOptions {
17
+ /**
18
+ * minify the `manifest.json`
19
+ * @default false
20
+ */
21
+ minify: boolean;
22
+ /**
23
+ * insert newline at the end of the `manifest.json`
24
+ * @default false
25
+ */
26
+ insertFinalNewline: boolean;
27
+ /**
28
+ * Resolve configuration from this working directory.
29
+ * @default process.env.VITE_ROOT_DIR
30
+ */
31
+ cwd?: string;
32
+ /**
33
+ * Merge the manifest config with the default config.
34
+ * @default false
35
+ */
36
+ merge?: boolean;
37
+ }
38
+ /** User-facing options — all fields are optional. */
39
+ interface UserManifestOptions extends Partial<ManifestOptions> {
40
+ }
41
+
15
42
  /**
16
43
  * Univa 类型定义统一导出
17
44
  *
@@ -115,6 +142,8 @@ interface UnivaOverrides {
115
142
  autoImport?: Options$1;
116
143
  /** unocss/vite 完整选项 */
117
144
  unocss?: UnoCSSOptions;
145
+ /** @uni-helper/vite-plugin-uni-manifest 完整选项 */
146
+ manifest?: UserManifestOptions;
118
147
  }
119
148
  /**
120
149
  * Univa 统一配置接口(工程级约定)
@@ -124,6 +153,8 @@ interface UnivaOverrides {
124
153
  interface UnivaOptions {
125
154
  /** 源码目录,相对 Vite root,默认 'src' */
126
155
  srcDir?: string;
156
+ /** 开启调试日志,默认 false */
157
+ debug?: boolean;
127
158
  /** 目录约定,相对 srcDir */
128
159
  dirs?: UnivaDirs;
129
160
  /** 自动导入配置 */
@@ -137,6 +168,8 @@ interface UnivaOptions {
137
168
  * 注意:pages / subPackages 由目录扫描自动填充,请勿在此手写。
138
169
  */
139
170
  pages?: Partial<PagesConfig>;
171
+ /** 应用级 manifest.json 配置 */
172
+ manifest?: Partial<UserManifestConfig>;
140
173
  /** 底层插件逃生口(覆盖融合层默认,高级用户使用) */
141
174
  overrides?: UnivaOverrides;
142
175
  }
package/dist/index.js CHANGED
@@ -14,6 +14,7 @@ import AutoImport from "unplugin-auto-import/vite";
14
14
  // src/constant.ts
15
15
  var ID = "univa";
16
16
  var UNIVA_DIR_NAME = `.${ID}`;
17
+ var ROOT_NAME = `${ID}.root`;
17
18
 
18
19
  // src/utils/fs.ts
19
20
  import { existsSync, mkdirSync } from "fs";
@@ -32,9 +33,20 @@ function ensureUnivaDir(cwd) {
32
33
  return dir;
33
34
  }
34
35
 
36
+ // src/utils/logger.ts
37
+ var debugMode = false;
38
+ function setDebug(enabled) {
39
+ debugMode = enabled;
40
+ }
41
+ function log(message, ...args) {
42
+ if (debugMode) {
43
+ console.log(`[${ID}]`, message, ...args);
44
+ }
45
+ }
46
+
35
47
  // src/plugins/context.ts
36
- import { existsSync as existsSync3, readFileSync as readFileSync2, writeFileSync as writeFileSync2 } from "fs";
37
- import { join as join3 } from "path";
48
+ import { existsSync as existsSync4, readFileSync as readFileSync3, writeFileSync as writeFileSync3 } from "fs";
49
+ import { join as join4 } from "path";
38
50
  import { slash } from "@antfu/utils";
39
51
  import { loadConfig } from "c12";
40
52
  import { createJiti } from "jiti";
@@ -48,12 +60,115 @@ var mergeWithArrayOverride = createDefu((originObj, key, updates) => {
48
60
  }
49
61
  });
50
62
 
51
- // src/plugins/pages.ts
63
+ // src/plugins/manifest.ts
52
64
  import { existsSync as existsSync2, readFileSync, writeFileSync } from "fs";
53
- import { join as join2 } from "path";
65
+ import { join as join2, resolve } from "path";
66
+ import process2 from "process";
67
+ import { ensureManifestJsonExists } from "@uni-helper/vite-plugin-uni-manifest";
68
+ import { watchConfig } from "c12";
69
+ import { normalizePath } from "vite";
70
+ function resolveOptions(userOptions) {
71
+ return {
72
+ minify: false,
73
+ insertFinalNewline: false,
74
+ cwd: process2.env.VITE_ROOT_DIR,
75
+ merge: true,
76
+ ...userOptions
77
+ };
78
+ }
79
+ function writeManifestJson(config = {}, opts) {
80
+ const path = resolveManifestJsonPath();
81
+ let mergeContent = {};
82
+ if (opts?.merge && existsSync2(path)) {
83
+ try {
84
+ const content2 = readFileSync(path, "utf-8");
85
+ mergeContent = JSON.parse(content2);
86
+ } catch (error) {
87
+ log(`manifest.json parse error: ${error}`);
88
+ }
89
+ }
90
+ const content = JSON.stringify(opts?.merge ? mergeWithArrayOverride(config, mergeContent) : config, null, opts?.minify ? 0 : 2) + (opts?.insertFinalNewline ? "\n" : "");
91
+ if (existsSync2(path) && readFileSync(path, "utf-8") === content) {
92
+ return;
93
+ }
94
+ writeFileSync(path, content);
95
+ }
96
+ function resolveManifestJsonPath() {
97
+ return normalizePath(
98
+ resolve(process2.env.UNI_INPUT_DIR || `${process2.cwd()}/src`, "manifest.json")
99
+ );
100
+ }
101
+ var ManifestContext = class {
102
+ options;
103
+ unwatch;
104
+ constructor(options) {
105
+ this.options = resolveOptions(options);
106
+ }
107
+ /**
108
+ * Start watching config sources and perform initial write.
109
+ * Must be called after construction.
110
+ */
111
+ async setup() {
112
+ log(`manifest cwd: ${this.options.cwd}`);
113
+ const { config, unwatch } = await watchConfig({
114
+ cwd: this.options.cwd,
115
+ name: "manifest",
116
+ rcFile: false,
117
+ packageJson: false,
118
+ onUpdate: (config2) => {
119
+ writeManifestJson(config2.newConfig.config, this.options);
120
+ }
121
+ });
122
+ writeManifestJson(config, this.options);
123
+ this.unwatch = unwatch;
124
+ }
125
+ };
126
+ function VitePluginUniManifest(userOptions = {}) {
127
+ let ctx;
128
+ return {
129
+ name: "vite-plugin-uni-manifest",
130
+ // Run before other plugins to ensure manifest.json is ready
131
+ enforce: "pre",
132
+ async configResolved() {
133
+ ensureManifestJsonExists();
134
+ ctx = new ManifestContext(userOptions);
135
+ await ctx.setup();
136
+ },
137
+ buildEnd: () => ctx?.unwatch()
138
+ };
139
+ }
140
+ function generateManifestConfigContent(content = {}) {
141
+ return `// Auto-generated by @univa/core
142
+ export default ${JSON.stringify(content, null, 2)}
143
+ `;
144
+ }
145
+ function generateManifestConfigFile(content = {}, root) {
146
+ const configPath = getManifestConfigPath(root);
147
+ const newContent = generateManifestConfigContent(content);
148
+ if (existsSync2(configPath)) {
149
+ const existingContent = readFileSync(configPath, "utf-8");
150
+ if (existingContent === newContent) {
151
+ return;
152
+ }
153
+ }
154
+ writeFileSync(configPath, newContent, "utf-8");
155
+ }
156
+ function getManifestConfigPath(root) {
157
+ return join2(getUnivaDir(root), "manifest.config.ts");
158
+ }
159
+ function createManifestPlugin(userOptions = {}) {
160
+ return VitePluginUniManifest({
161
+ cwd: join2(process2.env.VITE_ROOT_DIR || "", UNIVA_DIR_NAME),
162
+ ...userOptions
163
+ });
164
+ }
165
+
166
+ // src/plugins/pages.ts
167
+ import { existsSync as existsSync3, readFileSync as readFileSync2, writeFileSync as writeFileSync2 } from "fs";
168
+ import { join as join3 } from "path";
54
169
  import { VitePluginUniPages } from "@uni-helper/vite-plugin-uni-pages";
55
170
  function getPagesConfigPath(root) {
56
- return join2(getUnivaDir(root), "pages-config.ts");
171
+ return join3(getUnivaDir(root), "pages-config.ts");
57
172
  }
58
173
  function generatePagesConfigContent(content) {
59
174
  const { pages, subPackages, ...rest } = content;
@@ -67,13 +182,13 @@ export default ${JSON.stringify(rest, null, 2)}
67
182
  function generatePagesConfigFile(content, root) {
68
183
  const pagesConfigPath = getPagesConfigPath(root);
69
184
  const newContent = generatePagesConfigContent(content);
70
- if (existsSync2(pagesConfigPath)) {
71
- const existingContent = readFileSync(pagesConfigPath, "utf-8");
185
+ if (existsSync3(pagesConfigPath)) {
186
+ const existingContent = readFileSync2(pagesConfigPath, "utf-8");
72
187
  if (existingContent === newContent) {
73
188
  return;
74
189
  }
75
190
  }
76
- writeFileSync(pagesConfigPath, newContent, "utf-8");
191
+ writeFileSync2(pagesConfigPath, newContent, "utf-8");
77
192
  }
78
193
  function createPagesPlugin(pluginOptions) {
79
194
  const result = VitePluginUniPages({
@@ -86,23 +201,11 @@ function createPagesPlugin(pluginOptions) {
86
201
  }
87
202
 
88
203
  // src/plugins/context.ts
89
- var DEFAULT_ROOT_FILE_NAME = "App.ku.vue";
90
204
  var APP_KU_VUE_TEMPLATE = `<!-- Auto-generated by @univa/core -->
91
- <!-- UniKuRoot \u865A\u62DF\u6839\u7EC4\u4EF6\uFF0C\u53EF\u5728\u6B64\u6DFB\u52A0\u5168\u5C40\u7EC4\u4EF6\uFF08\u5982 Toast\u3001ConfigProvider \u7B49\uFF09 -->
92
205
  <template>
93
206
  <KuRootView />
94
207
  </template>
95
208
  `;
96
- function ensureRootComponent(config, root) {
97
- const srcDir = config?.srcDir || "src";
98
- const rootFileName = config?.overrides?.root?.rootFileName || DEFAULT_ROOT_FILE_NAME;
99
- const rootComponentPath = join3(root, srcDir, rootFileName);
100
- if (existsSync3(rootComponentPath)) {
101
- return;
102
- }
103
- writeFileSync2(rootComponentPath, APP_KU_VUE_TEMPLATE, "utf-8");
104
- console.log(`[univa] \u5DF2\u751F\u6210\u865A\u62DF\u6839\u7EC4\u4EF6\uFF1A${slash(rootComponentPath)}`);
105
- }
106
209
  var UnivaContext = class {
107
210
  _server;
108
211
  config;
@@ -111,12 +214,32 @@ var UnivaContext = class {
111
214
  constructor(options) {
112
215
  this.root = options.root;
113
216
  }
217
+ /**
218
+ * 确保虚拟根组件文件存在
219
+ *
220
+ * UniKuRoot 插件依赖 src 下的虚拟根组件文件(默认 App.ku.vue)。
221
+ * 若不存在则生成最小模板,避免插件注入 import 时解析失败。
222
+ *
223
+ * 必须在 Vite 启动前调用(Univa() 初始化阶段),
224
+ * 否则在 configResolved 阶段创建 src 下新文件会触发 watcher add 事件,
225
+ * 导致 Vite 重编译与 VitePluginUniPages 的 pages.json 写入冲突(EPERM)。
226
+ */
227
+ ensureRootComponent(fileName) {
228
+ const srcDir = this.config?.srcDir || "src";
229
+ const rootFileName = this.config?.overrides?.root?.rootFileName || fileName;
230
+ const rootComponentPath = join4(this.root, srcDir, rootFileName);
231
+ if (existsSync4(rootComponentPath)) {
232
+ return;
233
+ }
234
+ writeFileSync3(rootComponentPath, APP_KU_VUE_TEMPLATE, "utf-8");
235
+ log(`\u5DF2\u751F\u6210\u865A\u62DF\u6839\u7EC4\u4EF6\uFF1A${slash(rootComponentPath)}`);
236
+ }
114
237
  /**
115
238
  * 同步加载用户配置(univa.config.ts)
116
239
  */
117
240
  loadUserConfigSync() {
118
241
  const jiti = createJiti(this.root);
119
- const configFile = join3(this.root, `${ID}.config`);
242
+ const configFile = join4(this.root, `${ID}.config`);
120
243
  try {
121
244
  const configModule = jiti(configFile);
122
245
  const userConfig = configModule?.default || configModule;
@@ -175,26 +298,33 @@ var UnivaContext = class {
175
298
  * 更新生成的文件
176
299
  *
177
300
  * 读取 univa.config.ts 的 pages 字段(应用级配置),
178
- * 生成 .univa/pages.ts 供 VitePluginUniPages 消费。
179
- * 同时确保 UniKuRoot 虚拟根组件文件存在。
301
+ * 生成 .univa/pages-config.ts 供 VitePluginUniPages 消费。
302
+ *
303
+ * 注意:不在此处生成 src/App.ku.vue,避免在 configResolved 阶段
304
+ * 创建 src 下新文件触发 watcher add 事件,导致 Vite 重编译
305
+ * 与 VitePluginUniPages 的 pages.json 写入冲突(EPERM)。
306
+ * App.ku.vue 的生成在 Univa() 初始化阶段完成。
180
307
  */
181
308
  updateGeneratedFiles() {
182
309
  const pagesConfig = this.config?.pages;
183
310
  if (pagesConfig) {
184
311
  generatePagesConfigFile(pagesConfig, this.root);
185
312
  }
313
+ const manifestConfig = this.config?.manifest;
314
+ if (manifestConfig) {
315
+ generateManifestConfigFile(manifestConfig, this.root);
316
+ }
186
317
  const pagesTsPath = getPagesConfigPath(this.root);
187
318
  const emptyContent = `// Auto-generated by @univa/core
188
319
  export default {}
189
320
  `;
190
- if (!existsSync3(pagesTsPath)) {
191
- writeFileSync2(pagesTsPath, emptyContent, "utf-8");
321
+ if (!existsSync4(pagesTsPath)) {
322
+ writeFileSync3(pagesTsPath, emptyContent, "utf-8");
192
323
  } else if (!pagesConfig) {
193
- if (readFileSync2(pagesTsPath, "utf-8") !== emptyContent) {
194
- writeFileSync2(pagesTsPath, emptyContent, "utf-8");
324
+ if (readFileSync3(pagesTsPath, "utf-8") !== emptyContent) {
325
+ writeFileSync3(pagesTsPath, emptyContent, "utf-8");
195
326
  }
196
327
  }
197
- ensureRootComponent(this.config, this.root);
198
328
  }
199
329
  /**
200
330
  * 更新回调
@@ -220,7 +350,7 @@ var DEFAULT_IMPORTS = {
220
350
  apis: ["vue", "uni-app", "composables/**", "stores/**", "hooks/**", "constants/**"],
221
351
  components: ["components/**", "components-biz/**"]
222
352
  };
223
- function resolveOptions(userOptions) {
353
+ function resolveOptions2(userOptions) {
224
354
  const srcDir = userOptions.srcDir || "src";
225
355
  const dirs = {
226
356
  pages: userOptions.dirs?.pages ?? DEFAULT_DIRS.pages,
@@ -293,7 +423,9 @@ function createUnivaPlugins(resolved) {
293
423
  ...resolved.overrides?.layouts
294
424
  }));
295
425
  plugins.push(UniKuRoot({
426
+ enabledVirtualHost: true,
296
427
  enabledGlobalRef: true,
428
+ rootFileName: ROOT_NAME,
297
429
  excludePages: [
298
430
  "components-async/**/*.*"
299
431
  ],
@@ -306,6 +438,9 @@ function createUnivaPlugins(resolved) {
306
438
  directoryAsNamespace: true,
307
439
  ...resolved.overrides?.components
308
440
  }));
441
+ plugins.push(createManifestPlugin({
442
+ ...resolved.overrides?.manifest
443
+ }));
309
444
  plugins.push(...Uni());
310
445
  plugins.push(...UnoCSS());
311
446
  const { presets, dirs } = resolveApiSources(resolved.imports.apis, srcDir);
@@ -328,8 +463,10 @@ function Univa() {
328
463
  });
329
464
  ensureUnivaDir(context.root);
330
465
  context.loadUserConfigSync();
331
- const resolved = resolveOptions(context.config || {});
332
- console.log("[univa] \u5DF2\u52A0\u8F7D\u914D\u7F6E\uFF1A", context.configPath || "\u9ED8\u8BA4\u914D\u7F6E");
466
+ setDebug(context.config?.debug ?? false);
467
+ context.ensureRootComponent(`${ROOT_NAME}.vue`);
468
+ const resolved = resolveOptions2(context.config || {});
469
+ log("\u5DF2\u52A0\u8F7D\u914D\u7F6E\uFF1A", context.configPath || "\u9ED8\u8BA4\u914D\u7F6E");
333
470
  return createUnivaPlugins(resolved);
334
471
  }
335
472
  var plugins_default = Univa;
package/dist/use.cjs ADDED
@@ -0,0 +1,46 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/use.ts
21
+ var use_exports = {};
22
+ __export(use_exports, {
23
+ usePageContext: () => usePageContext
24
+ });
25
+ module.exports = __toCommonJS(use_exports);
26
+
27
+ // src/hooks/usePageContext.ts
28
+ var import_vue = require("vue");
29
+ function usePageContext() {
30
+ const rootRef = (0, import_vue.shallowRef)(null);
31
+ (0, import_vue.onMounted)(() => {
32
+ const pagesStack = getCurrentPages();
33
+ const pageStack = pagesStack[pagesStack.length - 1];
34
+ if (!pageStack?.$vm?.$refs?.uniKuRoot) {
35
+ throw new Error("[usePageContext] Root not found.");
36
+ }
37
+ rootRef.value = pageStack.$vm.$refs.uniKuRoot;
38
+ });
39
+ return {
40
+ rootRef
41
+ };
42
+ }
43
+ // Annotate the CommonJS export names for ESM import in node:
44
+ 0 && (module.exports = {
45
+ usePageContext
46
+ });
package/dist/use.d.cts ADDED
@@ -0,0 +1,9 @@
1
+ import { ShallowRef } from 'vue';
2
+
3
+ interface PageContext {
4
+ rootRef: ShallowRef;
5
+ }
6
+
7
+ declare function usePageContext(): PageContext;
8
+
9
+ export { usePageContext };
package/dist/use.d.ts ADDED
@@ -0,0 +1,9 @@
1
+ import { ShallowRef } from 'vue';
2
+
3
+ interface PageContext {
4
+ rootRef: ShallowRef;
5
+ }
6
+
7
+ declare function usePageContext(): PageContext;
8
+
9
+ export { usePageContext };
package/dist/use.js ADDED
@@ -0,0 +1,19 @@
1
+ // src/hooks/usePageContext.ts
2
+ import { onMounted, shallowRef } from "vue";
3
+ function usePageContext() {
4
+ const rootRef = shallowRef(null);
5
+ onMounted(() => {
6
+ const pagesStack = getCurrentPages();
7
+ const pageStack = pagesStack[pagesStack.length - 1];
8
+ if (!pageStack?.$vm?.$refs?.uniKuRoot) {
9
+ throw new Error("[usePageContext] Root not found.");
10
+ }
11
+ rootRef.value = pageStack.$vm.$refs.uniKuRoot;
12
+ });
13
+ return {
14
+ rootRef
15
+ };
16
+ }
17
+ export {
18
+ usePageContext
19
+ };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@univa/core",
3
3
  "type": "module",
4
- "version": "0.0.7",
4
+ "version": "0.0.8",
5
5
  "description": "🚀 开箱即用的 uni-app Vite 插件集,提供完整的开发体验",
6
6
  "author": "lianghang <libre1103@163.com>",
7
7
  "license": "MIT",
@@ -29,8 +29,18 @@
29
29
  "default": "./dist/index.cjs"
30
30
  }
31
31
  },
32
- "./client": {
33
- "types": "./src/client.d.ts"
32
+ "./use": {
33
+ "import": {
34
+ "types": "./dist/use.d.ts",
35
+ "default": "./dist/use.js"
36
+ },
37
+ "require": {
38
+ "types": "./dist/use.d.cts",
39
+ "default": "./dist/use.cjs"
40
+ }
41
+ },
42
+ "./global": {
43
+ "types": "./dist/global.d.ts"
34
44
  }
35
45
  },
36
46
  "main": "./dist/index.cjs",
@@ -39,9 +49,7 @@
39
49
  "files": [
40
50
  "LICENSE",
41
51
  "README.md",
42
- "dist",
43
- "src/client.d.ts",
44
- "src/global.d.ts"
52
+ "dist"
45
53
  ],
46
54
  "engines": {
47
55
  "node": ">=20.0.0"
@@ -51,19 +59,24 @@
51
59
  "registry": "https://registry.npmjs.org/"
52
60
  },
53
61
  "scripts": {
54
- "build": "tsup",
62
+ "build": "node scripts/build.mjs",
55
63
  "dev": "tsup --watch"
56
64
  },
65
+ "peerDependencies": {
66
+ "vite": "^5.2.8"
67
+ },
57
68
  "dependencies": {
58
69
  "@antfu/utils": "catalog:",
59
- "@uni-helper/eslint-config": "0.7.3",
70
+ "@dcloudio/types": "3.4.28",
71
+ "@uni-helper/eslint-config": "0.7.4",
60
72
  "@uni-helper/plugin-uni": "0.1.0",
61
- "@uni-helper/uni-pages-types": "^0.4.4",
62
- "@uni-helper/uni-types": "1.0.0-alpha.8",
73
+ "@uni-helper/uni-pages-types": "0.4.6",
74
+ "@uni-helper/uni-types": "1.1.0",
63
75
  "@uni-helper/unocss-preset-uni": "0.2.11",
64
- "@uni-helper/vite-plugin-uni-components": "0.2.10",
76
+ "@uni-helper/vite-plugin-uni-components": "0.3.2",
65
77
  "@uni-helper/vite-plugin-uni-layouts": "0.1.11",
66
- "@uni-helper/vite-plugin-uni-pages": "0.4.4",
78
+ "@uni-helper/vite-plugin-uni-manifest": "0.4.1",
79
+ "@uni-helper/vite-plugin-uni-pages": "0.4.6",
67
80
  "@uni-ku/root": "1.4.1",
68
81
  "@unocss/eslint-plugin": "66.6.7",
69
82
  "@unocss/preset-legacy-compat": "66.0.0",
@@ -78,6 +91,7 @@
78
91
  },
79
92
  "devDependencies": {
80
93
  "@dcloudio/uni-app": "catalog:",
94
+ "@types/node": "catalog:",
81
95
  "tsup": "catalog:",
82
96
  "typescript": "catalog:",
83
97
  "vite": "catalog:",