@univa/core 0.0.9 → 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.js CHANGED
@@ -7,18 +7,21 @@ function defineConfig(config) {
7
7
  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
- import UniKuRoot from "@uni-ku/root";
11
- import UnoCSS from "unocss/vite";
10
+ import Optimization from "@uni-ku/bundle-optimizer";
12
11
  import AutoImport from "unplugin-auto-import/vite";
12
+ import UniPolyfill from "vite-plugin-uni-polyfill";
13
13
 
14
14
  // src/constant.ts
15
15
  var ID = "univa";
16
16
  var UNIVA_DIR_NAME = `.${ID}`;
17
17
  var ROOT_NAME = `${ID}.root`;
18
+ var COMPONENTS_ASYNC_DIR_NAME = "components-async";
19
+ var SUB_PACKAGE_DIR_NAME = "app";
20
+ var PAGES_JSON_FILE_NAME = "pages.json";
18
21
 
19
22
  // src/utils/fs.ts
20
- import { existsSync, mkdirSync } from "fs";
21
- import { join } from "path";
23
+ import { existsSync, mkdirSync, readdirSync, statSync } from "fs";
24
+ import { join, posix } from "path";
22
25
  function ensureDir(dir) {
23
26
  if (!existsSync(dir)) {
24
27
  mkdirSync(dir, { recursive: true });
@@ -32,6 +35,21 @@ function ensureUnivaDir(cwd) {
32
35
  ensureDir(dir);
33
36
  return dir;
34
37
  }
38
+ function getSubDirectories(dirPath) {
39
+ if (!existsSync(dirPath)) {
40
+ return [];
41
+ }
42
+ const entries = readdirSync(dirPath);
43
+ return entries.filter((entry) => {
44
+ const fullPath = join(dirPath, entry);
45
+ return statSync(fullPath).isDirectory();
46
+ });
47
+ }
48
+ function scanSubPackages(srcDir, subPackageRoot) {
49
+ const rootPath = join(srcDir, subPackageRoot);
50
+ const subDirs = getSubDirectories(rootPath);
51
+ return subDirs.map((name) => posix.join(subPackageRoot, name));
52
+ }
35
53
 
36
54
  // src/utils/logger.ts
37
55
  var debugMode = false;
@@ -45,7 +63,6 @@ function log(message, ...args) {
45
63
  }
46
64
 
47
65
  // src/plugins/context.ts
48
- import { existsSync as existsSync4, readFileSync as readFileSync3, writeFileSync as writeFileSync3 } from "fs";
49
66
  import { join as join4 } from "path";
50
67
  import { slash } from "@antfu/utils";
51
68
  import { loadConfig } from "c12";
@@ -109,7 +126,6 @@ var ManifestContext = class {
109
126
  * Must be called after construction.
110
127
  */
111
128
  async setup() {
112
- log(`manifest cwd: ${this.options.cwd}`);
113
129
  const { config, unwatch } = await watchConfig({
114
130
  cwd: this.options.cwd,
115
131
  name: "manifest",
@@ -138,7 +154,10 @@ function VitePluginUniManifest(userOptions = {}) {
138
154
  };
139
155
  }
140
156
  function generateManifestConfigContent(content = {}) {
141
- return `// Auto-generated by @univa/core
157
+ return `/* eslint-disable */
158
+ /* prettier-ignore */
159
+ // @ts-nocheck
160
+ // Auto-generated by @univa/core
142
161
  export default ${JSON.stringify(content, null, 2)}
143
162
  `;
144
163
  }
@@ -152,6 +171,7 @@ function generateManifestConfigFile(content = {}, root) {
152
171
  }
153
172
  }
154
173
  writeFileSync(configPath, newContent, "utf-8");
174
+ log(`Generated manifest config file: ${configPath}`);
155
175
  }
156
176
  function getManifestConfigPath(root) {
157
177
  return join2(getUnivaDir(root), "manifest.config.ts");
@@ -163,6 +183,55 @@ function createManifestPlugin(userOptions = {}) {
163
183
  });
164
184
  }
165
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
+
166
235
  // src/plugins/pages.ts
167
236
  import { existsSync as existsSync3, readFileSync as readFileSync2, writeFileSync as writeFileSync2 } from "fs";
168
237
  import { join as join3 } from "path";
@@ -171,98 +240,152 @@ function getPagesConfigPath(root) {
171
240
  return join3(getUnivaDir(root), "pages-config.ts");
172
241
  }
173
242
  function generatePagesConfigContent(content) {
174
- const { pages, subPackages, ...rest } = content;
175
- void pages;
176
- void subPackages;
177
- return `// Auto-generated by @univa/core
178
- // \u6765\u6E90\uFF1Auniva.config.ts \u7684 pages \u5B57\u6BB5
179
- export default ${JSON.stringify(rest, null, 2)}
243
+ return `/* eslint-disable */
244
+ /* prettier-ignore */
245
+ // @ts-nocheck
246
+ // Auto-generated by @univa/core
247
+ export default ${JSON.stringify(content, null, 2)}
180
248
  `;
181
249
  }
182
- function generatePagesConfigFile(content, root) {
250
+ function generatePagesConfigFile(content, root, config) {
183
251
  const pagesConfigPath = getPagesConfigPath(root);
184
252
  const newContent = generatePagesConfigContent(content);
253
+ let needWrite = true;
185
254
  if (existsSync3(pagesConfigPath)) {
186
255
  const existingContent = readFileSync2(pagesConfigPath, "utf-8");
187
256
  if (existingContent === newContent) {
188
- return;
257
+ log(`existingContent: ${existingContent}`);
258
+ log(`newContent: ${newContent}`);
259
+ log(`Pages config file ${pagesConfigPath} is up-to-date`);
260
+ needWrite = false;
189
261
  }
190
262
  }
191
- writeFileSync2(pagesConfigPath, newContent, "utf-8");
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
+ }
192
274
  }
193
- function createPagesPlugin(pluginOptions) {
275
+ function createPagesPlugin(pluginOptions, options) {
194
276
  const result = VitePluginUniPages({
195
277
  dts: `${UNIVA_DIR_NAME}/uni-pages.d.ts`,
196
278
  exclude: ["**/components/**/*.*"],
197
279
  configSource: `${UNIVA_DIR_NAME}/pages-config.ts`,
280
+ onAfterMergePageMetaData: (ctx) => {
281
+ normalizePageNames(ctx);
282
+ },
198
283
  ...pluginOptions
199
284
  });
200
285
  return Array.isArray(result) ? result : [result];
201
286
  }
287
+ function pathToPascalCase(path) {
288
+ if (!path) {
289
+ return "";
290
+ }
291
+ return path.split(/[/.-]/).filter(Boolean).map((str) => str.charAt(0).toUpperCase() + str.slice(1)).join("");
292
+ }
293
+ function normalizePageNames(pageContext) {
294
+ pageContext.pageMetaData.forEach((page) => {
295
+ if (page.name) {
296
+ return;
297
+ }
298
+ page.name = pathToPascalCase(page.path);
299
+ });
300
+ pageContext.subPageMetaData.forEach((subPage) => {
301
+ subPage.pages.forEach((page) => {
302
+ if (subPage.root.startsWith(COMPONENTS_ASYNC_DIR_NAME)) {
303
+ page.layout = false;
304
+ }
305
+ if (page.name) {
306
+ return;
307
+ }
308
+ const fullPath = [subPage.root, page.path].filter(Boolean).join("/");
309
+ page.name = pathToPascalCase(fullPath);
310
+ });
311
+ });
312
+ }
202
313
 
203
314
  // src/plugins/context.ts
204
- var APP_KU_VUE_TEMPLATE = `<!-- Auto-generated by @univa/core -->
205
- <template>
206
- <KuRootView />
207
- </template>
208
- `;
209
315
  var UnivaContext = class {
210
316
  _server;
211
- config;
212
- configPath;
317
+ _config;
318
+ _configPath;
319
+ _jiti;
320
+ _resolveUserOptions;
321
+ /** 项目根目录(Vite root) */
213
322
  root;
214
323
  constructor(options) {
215
324
  this.root = options.root;
325
+ this._jiti = createJiti(this.root);
216
326
  }
217
327
  /**
218
- * 确保虚拟根组件文件存在
219
- *
220
- * UniKuRoot 插件依赖 src 下的虚拟根组件文件(默认 App.ku.vue)。
221
- * 若不存在则生成最小模板,避免插件注入 import 时解析失败。
328
+ * 异步加载配置(c12)
222
329
  *
223
- * 必须在 Vite 启动前调用(Univa() 初始化阶段),
224
- * 否则在 configResolved 阶段创建 src 下新文件会触发 watcher add 事件,
225
- * 导致 Vite 重编译与 VitePluginUniPages 的 pages.json 写入冲突(EPERM)。
330
+ * 支持更多格式:JSON/YAML/远程配置等
331
+ * 用于配置文件变化时重新加载
226
332
  */
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)}`);
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);
236
341
  }
237
342
  /**
238
- * 同步加载用户配置(univa.config.ts
343
+ * 同步加载配置(jiti
344
+ *
345
+ * 用于插件初始化阶段,确保配置在插件创建前可用
239
346
  */
240
347
  loadUserConfigSync() {
241
- const jiti = createJiti(this.root);
348
+ if (!this._jiti) {
349
+ log("Jiti \u672A\u521D\u59CB\u5316\uFF0C\u65E0\u6CD5\u52A0\u8F7D\u914D\u7F6E");
350
+ return;
351
+ }
242
352
  const configFile = join4(this.root, `${ID}.config`);
243
353
  try {
244
- const configModule = jiti(configFile);
354
+ const configModule = this._jiti(configFile);
245
355
  const userConfig = configModule?.default || configModule;
246
- this.config = mergeWithArrayOverride({}, userConfig);
247
- this.configPath = jiti.resolve(configFile);
248
- } catch {
356
+ this.resolveConfig(mergeWithArrayOverride({}, userConfig), configFile);
357
+ } catch (e) {
358
+ log(`\u52A0\u8F7D\u914D\u7F6E\u6587\u4EF6\u5931\u8D25\uFF1A${configFile}`, e);
249
359
  }
250
360
  }
251
361
  /**
252
- * 异步加载用户配置(c12 支持更多格式与合并能力)
362
+ * 解析配置
363
+ *
364
+ * 统一处理配置加载后的逻辑:
365
+ * - 存储 raw config
366
+ * - 解析为 resolved options(应用默认值、扫描目录)
253
367
  */
254
- async loadUserConfigAsync() {
255
- const { config: userConfig, configFile } = await loadConfig({
256
- name: ID,
257
- cwd: this.root,
258
- defaults: this.config || {},
259
- merger: mergeWithArrayOverride
260
- });
261
- this.config = userConfig;
262
- this.configPath = configFile || "";
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;
263
384
  }
264
385
  /**
265
386
  * 设置 Vite 开发服务器
387
+ *
388
+ * 监听配置文件变化,触发重新加载
266
389
  */
267
390
  setupViteServer(server) {
268
391
  if (this._server === server)
@@ -273,101 +396,167 @@ var UnivaContext = class {
273
396
  /**
274
397
  * 设置文件监听器
275
398
  *
276
- * 监听 univa.config.ts 变化,重新生成 .univa/pages.ts
399
+ * 监听 univa.config.ts 变化:
400
+ * - 重新加载配置
401
+ * - 更新生成文件
402
+ * - 触发 HMR
277
403
  */
278
404
  setupWatcher(watcher) {
279
- if (!this.configPath) {
405
+ if (!this._configPath)
280
406
  return;
281
- }
282
- watcher.add(this.configPath);
407
+ watcher.add(this._configPath);
283
408
  watcher.on("change", async (filePath) => {
284
- filePath = slash(filePath);
285
- const configPath = slash(this.configPath || "");
286
- if (filePath !== configPath) {
409
+ if (slash(filePath) !== slash(this._configPath || ""))
287
410
  return;
288
- }
289
411
  await this.updateConfig();
290
412
  this.onUpdate();
291
413
  });
292
414
  }
415
+ /**
416
+ * 更新配置
417
+ *
418
+ * 配置文件变化时:
419
+ * - 异步加载新配置
420
+ * - 更新生成文件
421
+ */
293
422
  async updateConfig() {
294
- await this.loadUserConfigAsync();
423
+ await this.loadUserConfig();
295
424
  this.updateGeneratedFiles();
296
425
  }
297
426
  /**
298
- * 更新生成的文件
299
- *
300
- * 读取 univa.config.ts 的 pages 字段(应用级配置),
301
- * 生成 .univa/pages-config.ts 供 VitePluginUniPages 消费。
427
+ * 更新生成文件
302
428
  *
303
- * 注意:不在此处生成 src/App.ku.vue,避免在 configResolved 阶段
304
- * 创建 src 下新文件触发 watcher add 事件,导致 Vite 重编译
305
- * VitePluginUniPages 的 pages.json 写入冲突(EPERM)。
306
- * App.ku.vue 的生成在 Univa() 初始化阶段完成。
429
+ * 根据配置生成:
430
+ * - .univa/pages-config.ts(VitePluginUniPages 依赖)
431
+ * - .univa/manifest-config.ts(可选)
307
432
  */
308
433
  updateGeneratedFiles() {
309
- const pagesConfig = this.config?.pages;
310
- if (pagesConfig) {
311
- generatePagesConfigFile(pagesConfig, this.root);
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)}`);
312
442
  }
313
- const manifestConfig = this.config?.manifest;
443
+ log(`\u751F\u6210 pages \u914D\u7F6E\uFF1A${JSON.stringify(pagesConfig)}`);
444
+ generatePagesConfigFile(pagesConfig, this.root, resolved);
445
+ const manifestConfig = resolved.manifest;
314
446
  if (manifestConfig) {
315
447
  generateManifestConfigFile(manifestConfig, this.root);
316
448
  }
317
- const pagesTsPath = getPagesConfigPath(this.root);
318
- const emptyContent = `// Auto-generated by @univa/core
319
- export default {}
320
- `;
321
- if (!existsSync4(pagesTsPath)) {
322
- writeFileSync3(pagesTsPath, emptyContent, "utf-8");
323
- } else if (!pagesConfig) {
324
- if (readFileSync3(pagesTsPath, "utf-8") !== emptyContent) {
325
- writeFileSync3(pagesTsPath, emptyContent, "utf-8");
326
- }
327
- }
328
449
  }
329
450
  /**
330
- * 更新回调
331
451
  * 触发 HMR
452
+ *
453
+ * 配置变化后通知浏览器刷新
332
454
  */
333
455
  onUpdate() {
334
- if (!this._server) {
456
+ if (!this._server)
335
457
  return;
336
- }
337
458
  this._server.ws.send({
338
459
  type: "full-reload"
339
460
  });
340
461
  }
341
462
  };
342
463
 
343
- // src/plugins/options.ts
344
- var DEFAULT_DIRS = {
345
- pages: "pages",
346
- subPackages: ["pages-sub/*"],
347
- layouts: "layouts"
348
- };
349
- var DEFAULT_IMPORTS = {
350
- apis: ["vue", "uni-app", "composables/**", "stores/**", "hooks/**", "constants/**"],
351
- components: ["components/**", "components-biz/**"]
352
- };
353
- function resolveOptions2(userOptions) {
354
- const srcDir = userOptions.srcDir || "src";
355
- const dirs = {
356
- pages: userOptions.dirs?.pages ?? DEFAULT_DIRS.pages,
357
- subPackages: userOptions.dirs?.subPackages ?? DEFAULT_DIRS.subPackages,
358
- layouts: userOptions.dirs?.layouts ?? DEFAULT_DIRS.layouts
359
- };
360
- const imports = {
361
- apis: userOptions.imports?.apis ?? DEFAULT_IMPORTS.apis,
362
- components: userOptions.imports?.components ?? DEFAULT_IMPORTS.components
363
- };
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
+ });
497
+ }
498
+
499
+ // src/plugins/unocss.ts
500
+ import { presetUni } from "@uni-helper/unocss-preset-uni";
501
+ import presetLegacyCompat from "@unocss/preset-legacy-compat";
502
+ import { presetIcons, transformerDirectives, transformerVariantGroup } from "unocss";
503
+ import UnoCSS from "unocss/vite";
504
+ function presetUniva(userOptions) {
505
+ const presets = [];
506
+ if (userOptions.presetLegacyCompat) {
507
+ presets.push(presetLegacyCompat({
508
+ commaStyleColorFunction: true,
509
+ legacyColorSpace: true
510
+ }));
511
+ }
364
512
  return {
365
- srcDir,
366
- dirs,
367
- imports,
368
- overrides: userOptions.overrides
513
+ name: `${ID}-preset`,
514
+ presets: [
515
+ presetUni(userOptions.uniPreset),
516
+ presetIcons({
517
+ scale: 1.2,
518
+ warn: true,
519
+ extraProperties: {
520
+ "display": "inline-block",
521
+ "vertical-align": "middle"
522
+ }
523
+ }),
524
+ ...presets
525
+ ],
526
+ shortcuts: [
527
+ ["border-s", "border border-solid"],
528
+ ["wh-full", "w-full h-full"],
529
+ ["f-c-c", "flex justify-center items-center"],
530
+ ["f-col-c", "flex-col justify-center items-center"],
531
+ ["flex-items", "flex items-center"],
532
+ ["flex-justify", "flex justify-center"],
533
+ ["flex-col", "flex flex-col"]
534
+ ],
535
+ transformers: [transformerDirectives(), transformerVariantGroup()],
536
+ rules: [
537
+ [
538
+ "p-safe",
539
+ {
540
+ padding: "env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left)"
541
+ }
542
+ ],
543
+ ["pt-safe", { "padding-top": "env(safe-area-inset-top)" }],
544
+ ["pb-safe", { "padding-bottom": "env(safe-area-inset-bottom)" }]
545
+ ],
546
+ safelist: userOptions.safelist,
547
+ theme: {
548
+ colors: userOptions.themeColors
549
+ }
369
550
  };
370
551
  }
552
+ function createUnocssPlugin(univaOptions, unocssOptions = {}) {
553
+ return UnoCSS({
554
+ presets: [
555
+ presetUniva(univaOptions)
556
+ ],
557
+ ...unocssOptions
558
+ });
559
+ }
371
560
 
372
561
  // src/plugins/index.ts
373
562
  var context;
@@ -375,10 +564,6 @@ function createCorePlugin() {
375
564
  return {
376
565
  name: "vite-plugin-univa",
377
566
  enforce: "pre",
378
- async configResolved() {
379
- await context.loadUserConfigAsync();
380
- context.updateGeneratedFiles();
381
- },
382
567
  configureServer(server) {
383
568
  context.setupViteServer(server);
384
569
  }
@@ -416,21 +601,14 @@ function createUnivaPlugins(resolved) {
416
601
  plugins.push(...createPagesPlugin({
417
602
  dir: `${srcDir}/${resolved.dirs.pages}`,
418
603
  subPackages: resolved.dirs.subPackages.map((p) => `${srcDir}/${p}`),
604
+ debug: resolved.debug,
419
605
  ...resolved.overrides?.pages
420
- }));
606
+ }, resolved));
421
607
  plugins.push(VitePluginUniLayouts({
422
608
  layoutDir: `${srcDir}/${resolved.dirs.layouts}`,
423
609
  ...resolved.overrides?.layouts
424
610
  }));
425
- plugins.push(UniKuRoot({
426
- enabledVirtualHost: true,
427
- enabledGlobalRef: true,
428
- rootFileName: ROOT_NAME,
429
- excludePages: [
430
- "components-async/**/*.*"
431
- ],
432
- ...resolved.overrides?.root
433
- }));
611
+ plugins.push(createRootPlugin(resolved));
434
612
  plugins.push(VitePluginUniComponents({
435
613
  dirs: resolved.imports.components.map((c) => `${srcDir}/${c}`),
436
614
  dts: `${UNIVA_DIR_NAME}/components.d.ts`,
@@ -442,7 +620,13 @@ function createUnivaPlugins(resolved) {
442
620
  ...resolved.overrides?.manifest
443
621
  }));
444
622
  plugins.push(...Uni());
445
- plugins.push(...UnoCSS());
623
+ plugins.push(UniPolyfill());
624
+ plugins.push(createUnocssPlugin(resolved.unocss, resolved.overrides?.unocss));
625
+ plugins.push(Optimization({
626
+ enable: true,
627
+ logger: resolved.debug,
628
+ ...resolved.overrides?.optimization
629
+ }));
446
630
  const { presets, dirs } = resolveApiSources(resolved.imports.apis, srcDir);
447
631
  const autoImportPlugin = AutoImport({
448
632
  imports: presets,
@@ -463,25 +647,22 @@ function Univa() {
463
647
  });
464
648
  ensureUnivaDir(context.root);
465
649
  context.loadUserConfigSync();
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");
470
- return createUnivaPlugins(resolved);
650
+ setDebug(context.resolveUserOptions.debug ?? false);
651
+ context.updateGeneratedFiles();
652
+ return createUnivaPlugins(context.resolveUserOptions);
471
653
  }
472
654
  var plugins_default = Univa;
473
655
 
474
656
  // src/index.ts
475
- import { presetUni } from "@uni-helper/unocss-preset-uni";
657
+ import { presetUni as presetUni2 } from "@uni-helper/unocss-preset-uni";
476
658
  import { camelCase, kebabCase, pascalCase } from "@uni-helper/vite-plugin-uni-components";
477
659
  import { default as default2 } from "@uni-helper/vite-plugin-uni-components";
478
660
  import { VitePluginUniLayouts as VitePluginUniLayouts2 } from "@uni-helper/vite-plugin-uni-layouts";
479
- import { PageContext, VitePluginUniPages as VitePluginUniPages2 } from "@uni-helper/vite-plugin-uni-pages";
661
+ import { VitePluginUniPages as VitePluginUniPages2 } from "@uni-helper/vite-plugin-uni-pages";
480
662
  import { default as default3 } from "@uni-ku/root";
481
663
  import { default as default4 } from "unplugin-auto-import/vite";
482
664
  export {
483
665
  default4 as AutoImport,
484
- PageContext,
485
666
  default3 as UniKuRoot,
486
667
  plugins_default as Univa,
487
668
  default2 as VitePluginUniComponents,
@@ -492,5 +673,5 @@ export {
492
673
  defineConfig,
493
674
  kebabCase,
494
675
  pascalCase,
495
- presetUni
676
+ presetUni2 as presetUni
496
677
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@univa/core",
3
3
  "type": "module",
4
- "version": "0.0.9",
4
+ "version": "0.1.1",
5
5
  "description": "🚀 开箱即用的 uni-app Vite 插件集,提供完整的开发体验",
6
6
  "author": "lianghang <libre1103@163.com>",
7
7
  "license": "MIT",
@@ -74,6 +74,7 @@
74
74
  "@uni-helper/vite-plugin-uni-layouts": "0.1.11",
75
75
  "@uni-helper/vite-plugin-uni-manifest": "0.4.1",
76
76
  "@uni-helper/vite-plugin-uni-pages": "0.4.6",
77
+ "@uni-ku/bundle-optimizer": "2.2.0",
77
78
  "@uni-ku/root": "1.4.1",
78
79
  "@unocss/eslint-plugin": "66.6.7",
79
80
  "@unocss/preset-legacy-compat": "66.0.0",