@univa/core 0.0.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/README.md ADDED
@@ -0,0 +1,87 @@
1
+ # @univa/core
2
+
3
+ > 🚀 开箱即用的 uni-app Vite 插件集,提供完整的开发体验
4
+
5
+ ## 特性
6
+
7
+ - 📦 **开箱即用** - 零配置即可开始开发
8
+ - 🎯 **自动导入** - 自动导入 Vue、uni-app API
9
+ - 🧩 **组件自动注册** - 自动注册组件,无需手动 import
10
+ - 📄 **页面管理** - 增强的页面配置和管理
11
+ - 🎨 **布局系统** - 灵活的布局系统
12
+ - 🎭 **UnoCSS** - 即时按需原子化 CSS 引擎
13
+ - 📱 **自定义 TabBar** - 支持自定义 tabBar 配置
14
+ - 🌳 **根组件** - 自动创建和管理根组件
15
+
16
+ ## 安装
17
+
18
+ ```bash
19
+ # npm
20
+ npm install @univa/core
21
+
22
+ # pnpm
23
+ pnpm add @univa/core
24
+
25
+ # yarn
26
+ yarn add @univa/core
27
+ ```
28
+
29
+ ## 快速开始
30
+
31
+ ### 1. 配置 Vite
32
+
33
+ 在 `vite.config.ts` 中使用:
34
+
35
+ ```typescript
36
+ import { defineConfig } from 'vite'
37
+ import { Univa } from '@univa/core'
38
+
39
+ export default defineConfig({
40
+ plugins: [
41
+ Univa(),
42
+ ],
43
+ })
44
+ ```
45
+
46
+ ### 2. 配置 TypeScript
47
+
48
+ 在 `tsconfig.json` 中添加类型支持:
49
+
50
+ ```json
51
+ {
52
+ "compilerOptions": {
53
+ "types": [
54
+ "@univa/core/client"
55
+ ]
56
+ }
57
+ }
58
+ ```
59
+
60
+ <br />
61
+
62
+ ## 功能详解
63
+
64
+ 待补充...
65
+
66
+ ## 依赖
67
+
68
+ 本包集成了以下优秀的插件:
69
+
70
+ - `@uni-helper/plugin-uni` - uni-app Vite 插件
71
+ - `@uni-helper/vite-plugin-uni-components` - 组件自动注册
72
+ - `@uni-helper/vite-plugin-uni-pages` - 页面管理
73
+ - `@uni-helper/vite-plugin-uni-layouts` - 布局系统
74
+ - `unplugin-auto-import` - 自动导入
75
+ - `unocss` - 原子化 CSS 引擎
76
+ - `@univa/root` - 根组件管理
77
+ - `@univa/manifest` - manifest 配置
78
+ - `@uni-ku/bundle-optimizer` - 打包优化
79
+ - `vite-plugin-uni-polyfill` - Polyfill
80
+
81
+ ## License
82
+
83
+ MIT
84
+
85
+ ## 贡献
86
+
87
+ 欢迎提交 Issue 和 Pull Request!
package/dist/index.cjs ADDED
@@ -0,0 +1,372 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/index.ts
31
+ var index_exports = {};
32
+ __export(index_exports, {
33
+ COMPONENT_ASYNC_ROOT: () => COMPONENT_ASYNC_ROOT,
34
+ DTS_DIR: () => DTS_DIR,
35
+ Univa: () => Univa,
36
+ definePagesConfig: () => definePagesConfig,
37
+ presetUniva: () => presetUniva
38
+ });
39
+ module.exports = __toCommonJS(index_exports);
40
+
41
+ // src/plugins/index.ts
42
+ var import_plugin_uni = __toESM(require("@uni-helper/plugin-uni"), 1);
43
+ var import_vite_plugin_uni_layouts = __toESM(require("@uni-helper/vite-plugin-uni-layouts"), 1);
44
+ var import_bundle_optimizer = __toESM(require("@uni-ku/bundle-optimizer"), 1);
45
+ var import_root = __toESM(require("@univa/root"), 1);
46
+ var import_vite_plugin_uni_polyfill = __toESM(require("vite-plugin-uni-polyfill"), 1);
47
+
48
+ // src/constant.ts
49
+ var DTS_DIR = "./.univa";
50
+ var COMPONENT_ASYNC_ROOT = "components-async";
51
+
52
+ // src/plugins/autoImport.ts
53
+ var import_vite = __toESM(require("unplugin-auto-import/vite"), 1);
54
+ function createAutoImportPlugin(options) {
55
+ const defaultAutoImportOptions = {
56
+ imports: [
57
+ "vue",
58
+ "pinia",
59
+ "uni-app"
60
+ ],
61
+ dirs: [
62
+ "src/hooks/**",
63
+ "!src/hooks/**/_*/**",
64
+ "src/store/**",
65
+ "!src/store/**/_*/**",
66
+ "src/constants/**"
67
+ ],
68
+ vueTemplate: true,
69
+ dts: `${DTS_DIR}/auto-imports.d.ts`
70
+ };
71
+ const userAutoImportOptions = typeof options.autoImport === "object" ? options.autoImport : {};
72
+ const mergedOptions = {
73
+ ...defaultAutoImportOptions,
74
+ ...userAutoImportOptions
75
+ };
76
+ if (userAutoImportOptions.imports) {
77
+ const userImports = Array.isArray(userAutoImportOptions.imports) ? userAutoImportOptions.imports : [userAutoImportOptions.imports];
78
+ mergedOptions.imports = [...defaultAutoImportOptions.imports, ...userImports];
79
+ }
80
+ if (userAutoImportOptions.dirs) {
81
+ const userDirs = Array.isArray(userAutoImportOptions.dirs) ? userAutoImportOptions.dirs : [userAutoImportOptions.dirs];
82
+ mergedOptions.dirs = [...defaultAutoImportOptions.dirs, ...userDirs];
83
+ }
84
+ return (0, import_vite.default)(mergedOptions);
85
+ }
86
+
87
+ // src/plugins/components.ts
88
+ var import_vite_plugin_uni_components = __toESM(require("@uni-helper/vite-plugin-uni-components"), 1);
89
+ function createComponentsPlugin(options) {
90
+ const defaultComponentsOptions = {
91
+ dirs: ["src/components", "src/components-biz"],
92
+ exclude: ["**/components/**/*.*"],
93
+ directoryAsNamespace: true,
94
+ globalNamespaces: ["components", "common"],
95
+ dts: `${DTS_DIR}/components.d.ts`
96
+ };
97
+ const userComponentsOptions = typeof options.components === "object" ? options.components : {};
98
+ const mergedOptions = {
99
+ ...defaultComponentsOptions,
100
+ ...userComponentsOptions
101
+ };
102
+ if (userComponentsOptions.dirs) {
103
+ const userDirs = Array.isArray(userComponentsOptions.dirs) ? userComponentsOptions.dirs : [userComponentsOptions.dirs];
104
+ mergedOptions.dirs = [...defaultComponentsOptions.dirs, ...userDirs];
105
+ }
106
+ if (userComponentsOptions.exclude) {
107
+ const userExclude = Array.isArray(userComponentsOptions.exclude) ? userComponentsOptions.exclude : [userComponentsOptions.exclude];
108
+ mergedOptions.exclude = [...defaultComponentsOptions.exclude, ...userExclude];
109
+ }
110
+ if (userComponentsOptions.globalNamespaces) {
111
+ const userGlobalNamespaces = Array.isArray(userComponentsOptions.globalNamespaces) ? userComponentsOptions.globalNamespaces : [userComponentsOptions.globalNamespaces];
112
+ mergedOptions.globalNamespaces = [...defaultComponentsOptions.globalNamespaces, ...userGlobalNamespaces];
113
+ }
114
+ return (0, import_vite_plugin_uni_components.default)(mergedOptions);
115
+ }
116
+
117
+ // src/plugins/manifest.ts
118
+ var import_manifest = require("@univa/manifest");
119
+ function createManifestPlugin(options) {
120
+ return (0, import_manifest.VitePluginUniManifest)({
121
+ rewrite: (config) => {
122
+ return options?.manifest || config;
123
+ }
124
+ });
125
+ }
126
+
127
+ // src/plugins/pages.ts
128
+ var import_vite_plugin_uni_pages = __toESM(require("@uni-helper/vite-plugin-uni-pages"), 1);
129
+ function _pathToName(path, partToReplace = "", prefix = "") {
130
+ const parts = path.replace(/\.\w+$/, "").split("/").filter(Boolean);
131
+ const toPascalCase = (parts2) => {
132
+ return parts2.map(
133
+ (part) => part.split("-").map((subPart) => subPart.charAt(0).toUpperCase() + subPart.slice(1)).join("")
134
+ ).join("");
135
+ };
136
+ if (!partToReplace) {
137
+ return toPascalCase(parts);
138
+ }
139
+ const indexToReplace = parts.findIndex((part) => part.toLowerCase() === partToReplace.toLowerCase());
140
+ if (indexToReplace === -1) {
141
+ return prefix + toPascalCase(parts);
142
+ }
143
+ parts[indexToReplace] = prefix;
144
+ return toPascalCase(parts);
145
+ }
146
+ function processPageName(ctx) {
147
+ ctx.pageMetaData.forEach((page) => {
148
+ if (page.name) {
149
+ return;
150
+ }
151
+ page.name = _pathToName(page.path, "pages");
152
+ });
153
+ ctx.subPageMetaData.forEach((subPackage) => {
154
+ const root = subPackage.root;
155
+ const isAsyncComponent = root.startsWith(COMPONENT_ASYNC_ROOT);
156
+ subPackage.pages.forEach((page) => {
157
+ if (page.name) {
158
+ return;
159
+ }
160
+ if (!page.path) {
161
+ console.warn(`[vite-plugin-uni-pages] Page path is missing in subPackage: ${root}`);
162
+ return;
163
+ }
164
+ if (isAsyncComponent) {
165
+ page.layout = false;
166
+ }
167
+ page.name = _pathToName(`${root}/${page.path}`, "pages-sub", "Sub");
168
+ });
169
+ });
170
+ }
171
+ function resolveUserPagesConfig(config) {
172
+ if (!config.tabBar) {
173
+ return config;
174
+ }
175
+ const tabBar = config.tabBar || {};
176
+ if (config.tabBarMode === "CUSTOM") {
177
+ tabBar.custom = true;
178
+ if (tabBar.list && tabBar.list.length > 0) {
179
+ tabBar.list = tabBar.list.map((item) => {
180
+ const filtered = { ...item };
181
+ filtered.customIconPath = filtered.iconPath;
182
+ filtered.customSelectedIconPath = filtered.selectedIconPath;
183
+ if (!filtered.iconPath?.startsWith("static/")) {
184
+ delete filtered.iconPath;
185
+ }
186
+ if (!filtered.selectedIconPath?.startsWith("static/")) {
187
+ delete filtered.selectedIconPath;
188
+ }
189
+ return filtered;
190
+ });
191
+ }
192
+ }
193
+ return {
194
+ ...config,
195
+ tabBar
196
+ };
197
+ }
198
+ function createPagesPlugin(options) {
199
+ const pages = options.pages || {};
200
+ let tabBarConfig = null;
201
+ const moduleId = "\0virtual:univa-tabbar";
202
+ return [
203
+ {
204
+ name: "vite-plugin-univa-pages-tabbar",
205
+ enforce: "pre",
206
+ resolveId(id) {
207
+ if (id === "virtual:univa-tabbar") {
208
+ return moduleId;
209
+ }
210
+ },
211
+ load(id) {
212
+ if (id === moduleId) {
213
+ return `export default ${JSON.stringify(tabBarConfig)}`;
214
+ }
215
+ }
216
+ },
217
+ (0, import_vite_plugin_uni_pages.default)({
218
+ configSource: pages.config ? {
219
+ files: "vite.config",
220
+ rewrite: () => resolveUserPagesConfig(pages.config)
221
+ } : {
222
+ files: "pages.config",
223
+ extensions: ["ts", "mts", "cts", "js", "cjs", "mjs", "json"]
224
+ },
225
+ subPackages: [
226
+ ...pages.subPackages || []
227
+ ],
228
+ exclude: [
229
+ ...pages?.exclude || [],
230
+ "**/components/**/*.*"
231
+ ],
232
+ onAfterMergePageMetaData(ctx) {
233
+ processPageName(ctx);
234
+ const tabBar = ctx.pagesGlobConfig?.tabBar;
235
+ if (!tabBar) {
236
+ tabBarConfig = null;
237
+ return;
238
+ }
239
+ const clonedTabBar = { ...tabBar };
240
+ if (clonedTabBar.list) {
241
+ clonedTabBar.list = clonedTabBar.list.map((item) => {
242
+ const filtered = { ...item };
243
+ if (filtered.customIconPath) {
244
+ filtered.iconPath = filtered.customIconPath;
245
+ delete filtered.customIconPath;
246
+ }
247
+ if (filtered.customSelectedIconPath) {
248
+ filtered.selectedIconPath = filtered.customSelectedIconPath;
249
+ delete filtered.customSelectedIconPath;
250
+ }
251
+ return filtered;
252
+ });
253
+ }
254
+ tabBarConfig = clonedTabBar;
255
+ },
256
+ dts: `${DTS_DIR}/uni-pages.d.ts`
257
+ })
258
+ ];
259
+ }
260
+ function definePagesConfig(config) {
261
+ return config;
262
+ }
263
+
264
+ // src/plugins/unocss.ts
265
+ var import_unocss_preset_uni = require("@uni-helper/unocss-preset-uni");
266
+ var import_preset_legacy_compat = __toESM(require("@unocss/preset-legacy-compat"), 1);
267
+ var import_unocss = require("unocss");
268
+ var import_vite2 = __toESM(require("unocss/vite"), 1);
269
+ function createUnocssPlugin() {
270
+ return (0, import_vite2.default)({
271
+ presets: [
272
+ presetUniva()
273
+ ]
274
+ });
275
+ }
276
+ function presetUniva() {
277
+ return {
278
+ name: "univa-preset",
279
+ presets: [
280
+ (0, import_unocss_preset_uni.presetUni)({
281
+ attributify: false
282
+ }),
283
+ (0, import_unocss.presetIcons)({
284
+ scale: 1.2,
285
+ warn: true,
286
+ extraProperties: {
287
+ "display": "inline-block",
288
+ "vertical-align": "middle"
289
+ }
290
+ }),
291
+ // 处理低端安卓机的样式问题,eg: `rgb(255 0 0)` -> `rgb(255, 0, 0)`
292
+ (0, import_preset_legacy_compat.default)({
293
+ commaStyleColorFunction: true,
294
+ legacyColorSpace: true
295
+ })
296
+ ],
297
+ shortcuts: [
298
+ ["border-s", "border border-solid"],
299
+ ["wh-full", "w-full h-full"],
300
+ ["f-c-c", "flex justify-center items-center"],
301
+ ["f-col-c", "flex-col justify-center items-center"],
302
+ ["flex-items", "flex items-center"],
303
+ ["flex-justify", "flex justify-center"],
304
+ ["flex-col", "flex flex-col"]
305
+ ],
306
+ transformers: [
307
+ (0, import_unocss.transformerDirectives)(),
308
+ (0, import_unocss.transformerVariantGroup)()
309
+ ],
310
+ rules: [
311
+ [
312
+ "p-safe",
313
+ {
314
+ padding: "env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left)"
315
+ }
316
+ ],
317
+ ["pt-safe", { "padding-top": "env(safe-area-inset-top)" }],
318
+ ["pb-safe", { "padding-bottom": "env(safe-area-inset-bottom)" }]
319
+ ]
320
+ };
321
+ }
322
+
323
+ // src/plugins/index.ts
324
+ function createUnivaPlugins(options) {
325
+ const pluginFactory = {
326
+ plugins: [
327
+ ...createPagesPlugin(options),
328
+ (0, import_vite_plugin_uni_layouts.default)()
329
+ ],
330
+ register(condition, plugin) {
331
+ if (condition) {
332
+ this.plugins.push(plugin);
333
+ }
334
+ }
335
+ };
336
+ pluginFactory.register(options.appRoot !== false, (0, import_root.default)({
337
+ enabledVirtualHost: true,
338
+ rootFileName: "App.univa",
339
+ enabledGlobalRef: true,
340
+ autoCreateRoot: true,
341
+ excludePages: [
342
+ `${COMPONENT_ASYNC_ROOT}/**/*.*`
343
+ ]
344
+ }));
345
+ pluginFactory.register(true, createManifestPlugin(options));
346
+ pluginFactory.register(options.components !== false, createComponentsPlugin(options));
347
+ pluginFactory.register(true, (0, import_bundle_optimizer.default)());
348
+ pluginFactory.register(true, (0, import_plugin_uni.default)());
349
+ pluginFactory.register(true, createUnocssPlugin());
350
+ pluginFactory.register(options.autoImport !== false, createAutoImportPlugin(options));
351
+ pluginFactory.register(true, (0, import_vite_plugin_uni_polyfill.default)());
352
+ return pluginFactory.plugins;
353
+ }
354
+
355
+ // src/index.ts
356
+ function Univa(config) {
357
+ return createUnivaPlugins({
358
+ components: config?.components ?? true,
359
+ autoImport: config?.autoImport ?? true,
360
+ appRoot: config?.appRoot ?? true,
361
+ pages: config?.pages,
362
+ manifest: config?.manifest
363
+ });
364
+ }
365
+ // Annotate the CommonJS export names for ESM import in node:
366
+ 0 && (module.exports = {
367
+ COMPONENT_ASYNC_ROOT,
368
+ DTS_DIR,
369
+ Univa,
370
+ definePagesConfig,
371
+ presetUniva
372
+ });
@@ -0,0 +1,43 @@
1
+ import { PluginOption } from 'vite';
2
+ import { Options as Options$1 } from '@uni-helper/vite-plugin-uni-components';
3
+ import { UserPagesConfig as UserPagesConfig$1 } from '@uni-helper/vite-plugin-uni-pages';
4
+ import { UserManifestConfig } from '@univa/manifest';
5
+ import { Options } from 'unplugin-auto-import/types';
6
+ import { Preset } from 'unocss';
7
+
8
+ /**
9
+ * NONE: 不需要tabbar
10
+ * NATIVE: 原生tabbar
11
+ * CUSTOM: 自定义tabbar
12
+ */
13
+ type TabBarMode = 'NONE' | 'NATIVE' | 'CUSTOM';
14
+ interface UserPagesConfig extends UserPagesConfig$1 {
15
+ tabBarMode?: TabBarMode;
16
+ }
17
+ interface DefinePagesConfig extends UserPagesConfig {
18
+ }
19
+ interface PagesOptions {
20
+ exclude?: string[];
21
+ subPackages?: string[];
22
+ config?: UserPagesConfig;
23
+ }
24
+ interface UnivaOptions {
25
+ autoImport?: boolean | Options;
26
+ components?: boolean | Options$1;
27
+ pages?: PagesOptions;
28
+ appRoot?: boolean;
29
+ manifest?: UserManifestConfig;
30
+ }
31
+ interface UnivaUserConfig extends UnivaOptions {
32
+ }
33
+
34
+ declare function definePagesConfig(config: DefinePagesConfig): DefinePagesConfig;
35
+
36
+ declare function presetUniva(): Preset;
37
+
38
+ declare const DTS_DIR = "./.univa";
39
+ declare const COMPONENT_ASYNC_ROOT = "components-async";
40
+
41
+ declare function Univa(config?: UnivaUserConfig): PluginOption[];
42
+
43
+ export { COMPONENT_ASYNC_ROOT, DTS_DIR, Univa, type UnivaOptions, type UnivaUserConfig, definePagesConfig, presetUniva };
@@ -0,0 +1,43 @@
1
+ import { PluginOption } from 'vite';
2
+ import { Options as Options$1 } from '@uni-helper/vite-plugin-uni-components';
3
+ import { UserPagesConfig as UserPagesConfig$1 } from '@uni-helper/vite-plugin-uni-pages';
4
+ import { UserManifestConfig } from '@univa/manifest';
5
+ import { Options } from 'unplugin-auto-import/types';
6
+ import { Preset } from 'unocss';
7
+
8
+ /**
9
+ * NONE: 不需要tabbar
10
+ * NATIVE: 原生tabbar
11
+ * CUSTOM: 自定义tabbar
12
+ */
13
+ type TabBarMode = 'NONE' | 'NATIVE' | 'CUSTOM';
14
+ interface UserPagesConfig extends UserPagesConfig$1 {
15
+ tabBarMode?: TabBarMode;
16
+ }
17
+ interface DefinePagesConfig extends UserPagesConfig {
18
+ }
19
+ interface PagesOptions {
20
+ exclude?: string[];
21
+ subPackages?: string[];
22
+ config?: UserPagesConfig;
23
+ }
24
+ interface UnivaOptions {
25
+ autoImport?: boolean | Options;
26
+ components?: boolean | Options$1;
27
+ pages?: PagesOptions;
28
+ appRoot?: boolean;
29
+ manifest?: UserManifestConfig;
30
+ }
31
+ interface UnivaUserConfig extends UnivaOptions {
32
+ }
33
+
34
+ declare function definePagesConfig(config: DefinePagesConfig): DefinePagesConfig;
35
+
36
+ declare function presetUniva(): Preset;
37
+
38
+ declare const DTS_DIR = "./.univa";
39
+ declare const COMPONENT_ASYNC_ROOT = "components-async";
40
+
41
+ declare function Univa(config?: UnivaUserConfig): PluginOption[];
42
+
43
+ export { COMPONENT_ASYNC_ROOT, DTS_DIR, Univa, type UnivaOptions, type UnivaUserConfig, definePagesConfig, presetUniva };
package/dist/index.js ADDED
@@ -0,0 +1,331 @@
1
+ // src/plugins/index.ts
2
+ import Uni from "@uni-helper/plugin-uni";
3
+ import UniHelperLayouts from "@uni-helper/vite-plugin-uni-layouts";
4
+ import UniOptimization from "@uni-ku/bundle-optimizer";
5
+ import UnivaRoot from "@univa/root";
6
+ import UniPolyfill from "vite-plugin-uni-polyfill";
7
+
8
+ // src/constant.ts
9
+ var DTS_DIR = "./.univa";
10
+ var COMPONENT_ASYNC_ROOT = "components-async";
11
+
12
+ // src/plugins/autoImport.ts
13
+ import AutoImport from "unplugin-auto-import/vite";
14
+ function createAutoImportPlugin(options) {
15
+ const defaultAutoImportOptions = {
16
+ imports: [
17
+ "vue",
18
+ "pinia",
19
+ "uni-app"
20
+ ],
21
+ dirs: [
22
+ "src/hooks/**",
23
+ "!src/hooks/**/_*/**",
24
+ "src/store/**",
25
+ "!src/store/**/_*/**",
26
+ "src/constants/**"
27
+ ],
28
+ vueTemplate: true,
29
+ dts: `${DTS_DIR}/auto-imports.d.ts`
30
+ };
31
+ const userAutoImportOptions = typeof options.autoImport === "object" ? options.autoImport : {};
32
+ const mergedOptions = {
33
+ ...defaultAutoImportOptions,
34
+ ...userAutoImportOptions
35
+ };
36
+ if (userAutoImportOptions.imports) {
37
+ const userImports = Array.isArray(userAutoImportOptions.imports) ? userAutoImportOptions.imports : [userAutoImportOptions.imports];
38
+ mergedOptions.imports = [...defaultAutoImportOptions.imports, ...userImports];
39
+ }
40
+ if (userAutoImportOptions.dirs) {
41
+ const userDirs = Array.isArray(userAutoImportOptions.dirs) ? userAutoImportOptions.dirs : [userAutoImportOptions.dirs];
42
+ mergedOptions.dirs = [...defaultAutoImportOptions.dirs, ...userDirs];
43
+ }
44
+ return AutoImport(mergedOptions);
45
+ }
46
+
47
+ // src/plugins/components.ts
48
+ import UniHelperComponents from "@uni-helper/vite-plugin-uni-components";
49
+ function createComponentsPlugin(options) {
50
+ const defaultComponentsOptions = {
51
+ dirs: ["src/components", "src/components-biz"],
52
+ exclude: ["**/components/**/*.*"],
53
+ directoryAsNamespace: true,
54
+ globalNamespaces: ["components", "common"],
55
+ dts: `${DTS_DIR}/components.d.ts`
56
+ };
57
+ const userComponentsOptions = typeof options.components === "object" ? options.components : {};
58
+ const mergedOptions = {
59
+ ...defaultComponentsOptions,
60
+ ...userComponentsOptions
61
+ };
62
+ if (userComponentsOptions.dirs) {
63
+ const userDirs = Array.isArray(userComponentsOptions.dirs) ? userComponentsOptions.dirs : [userComponentsOptions.dirs];
64
+ mergedOptions.dirs = [...defaultComponentsOptions.dirs, ...userDirs];
65
+ }
66
+ if (userComponentsOptions.exclude) {
67
+ const userExclude = Array.isArray(userComponentsOptions.exclude) ? userComponentsOptions.exclude : [userComponentsOptions.exclude];
68
+ mergedOptions.exclude = [...defaultComponentsOptions.exclude, ...userExclude];
69
+ }
70
+ if (userComponentsOptions.globalNamespaces) {
71
+ const userGlobalNamespaces = Array.isArray(userComponentsOptions.globalNamespaces) ? userComponentsOptions.globalNamespaces : [userComponentsOptions.globalNamespaces];
72
+ mergedOptions.globalNamespaces = [...defaultComponentsOptions.globalNamespaces, ...userGlobalNamespaces];
73
+ }
74
+ return UniHelperComponents(mergedOptions);
75
+ }
76
+
77
+ // src/plugins/manifest.ts
78
+ import { VitePluginUniManifest } from "@univa/manifest";
79
+ function createManifestPlugin(options) {
80
+ return VitePluginUniManifest({
81
+ rewrite: (config) => {
82
+ return options?.manifest || config;
83
+ }
84
+ });
85
+ }
86
+
87
+ // src/plugins/pages.ts
88
+ import UniHelperPages from "@uni-helper/vite-plugin-uni-pages";
89
+ function _pathToName(path, partToReplace = "", prefix = "") {
90
+ const parts = path.replace(/\.\w+$/, "").split("/").filter(Boolean);
91
+ const toPascalCase = (parts2) => {
92
+ return parts2.map(
93
+ (part) => part.split("-").map((subPart) => subPart.charAt(0).toUpperCase() + subPart.slice(1)).join("")
94
+ ).join("");
95
+ };
96
+ if (!partToReplace) {
97
+ return toPascalCase(parts);
98
+ }
99
+ const indexToReplace = parts.findIndex((part) => part.toLowerCase() === partToReplace.toLowerCase());
100
+ if (indexToReplace === -1) {
101
+ return prefix + toPascalCase(parts);
102
+ }
103
+ parts[indexToReplace] = prefix;
104
+ return toPascalCase(parts);
105
+ }
106
+ function processPageName(ctx) {
107
+ ctx.pageMetaData.forEach((page) => {
108
+ if (page.name) {
109
+ return;
110
+ }
111
+ page.name = _pathToName(page.path, "pages");
112
+ });
113
+ ctx.subPageMetaData.forEach((subPackage) => {
114
+ const root = subPackage.root;
115
+ const isAsyncComponent = root.startsWith(COMPONENT_ASYNC_ROOT);
116
+ subPackage.pages.forEach((page) => {
117
+ if (page.name) {
118
+ return;
119
+ }
120
+ if (!page.path) {
121
+ console.warn(`[vite-plugin-uni-pages] Page path is missing in subPackage: ${root}`);
122
+ return;
123
+ }
124
+ if (isAsyncComponent) {
125
+ page.layout = false;
126
+ }
127
+ page.name = _pathToName(`${root}/${page.path}`, "pages-sub", "Sub");
128
+ });
129
+ });
130
+ }
131
+ function resolveUserPagesConfig(config) {
132
+ if (!config.tabBar) {
133
+ return config;
134
+ }
135
+ const tabBar = config.tabBar || {};
136
+ if (config.tabBarMode === "CUSTOM") {
137
+ tabBar.custom = true;
138
+ if (tabBar.list && tabBar.list.length > 0) {
139
+ tabBar.list = tabBar.list.map((item) => {
140
+ const filtered = { ...item };
141
+ filtered.customIconPath = filtered.iconPath;
142
+ filtered.customSelectedIconPath = filtered.selectedIconPath;
143
+ if (!filtered.iconPath?.startsWith("static/")) {
144
+ delete filtered.iconPath;
145
+ }
146
+ if (!filtered.selectedIconPath?.startsWith("static/")) {
147
+ delete filtered.selectedIconPath;
148
+ }
149
+ return filtered;
150
+ });
151
+ }
152
+ }
153
+ return {
154
+ ...config,
155
+ tabBar
156
+ };
157
+ }
158
+ function createPagesPlugin(options) {
159
+ const pages = options.pages || {};
160
+ let tabBarConfig = null;
161
+ const moduleId = "\0virtual:univa-tabbar";
162
+ return [
163
+ {
164
+ name: "vite-plugin-univa-pages-tabbar",
165
+ enforce: "pre",
166
+ resolveId(id) {
167
+ if (id === "virtual:univa-tabbar") {
168
+ return moduleId;
169
+ }
170
+ },
171
+ load(id) {
172
+ if (id === moduleId) {
173
+ return `export default ${JSON.stringify(tabBarConfig)}`;
174
+ }
175
+ }
176
+ },
177
+ UniHelperPages({
178
+ configSource: pages.config ? {
179
+ files: "vite.config",
180
+ rewrite: () => resolveUserPagesConfig(pages.config)
181
+ } : {
182
+ files: "pages.config",
183
+ extensions: ["ts", "mts", "cts", "js", "cjs", "mjs", "json"]
184
+ },
185
+ subPackages: [
186
+ ...pages.subPackages || []
187
+ ],
188
+ exclude: [
189
+ ...pages?.exclude || [],
190
+ "**/components/**/*.*"
191
+ ],
192
+ onAfterMergePageMetaData(ctx) {
193
+ processPageName(ctx);
194
+ const tabBar = ctx.pagesGlobConfig?.tabBar;
195
+ if (!tabBar) {
196
+ tabBarConfig = null;
197
+ return;
198
+ }
199
+ const clonedTabBar = { ...tabBar };
200
+ if (clonedTabBar.list) {
201
+ clonedTabBar.list = clonedTabBar.list.map((item) => {
202
+ const filtered = { ...item };
203
+ if (filtered.customIconPath) {
204
+ filtered.iconPath = filtered.customIconPath;
205
+ delete filtered.customIconPath;
206
+ }
207
+ if (filtered.customSelectedIconPath) {
208
+ filtered.selectedIconPath = filtered.customSelectedIconPath;
209
+ delete filtered.customSelectedIconPath;
210
+ }
211
+ return filtered;
212
+ });
213
+ }
214
+ tabBarConfig = clonedTabBar;
215
+ },
216
+ dts: `${DTS_DIR}/uni-pages.d.ts`
217
+ })
218
+ ];
219
+ }
220
+ function definePagesConfig(config) {
221
+ return config;
222
+ }
223
+
224
+ // src/plugins/unocss.ts
225
+ import { presetUni } from "@uni-helper/unocss-preset-uni";
226
+ import presetLegacyCompat from "@unocss/preset-legacy-compat";
227
+ import { presetIcons, transformerDirectives, transformerVariantGroup } from "unocss";
228
+ import UnoCSS from "unocss/vite";
229
+ function createUnocssPlugin() {
230
+ return UnoCSS({
231
+ presets: [
232
+ presetUniva()
233
+ ]
234
+ });
235
+ }
236
+ function presetUniva() {
237
+ return {
238
+ name: "univa-preset",
239
+ presets: [
240
+ presetUni({
241
+ attributify: false
242
+ }),
243
+ presetIcons({
244
+ scale: 1.2,
245
+ warn: true,
246
+ extraProperties: {
247
+ "display": "inline-block",
248
+ "vertical-align": "middle"
249
+ }
250
+ }),
251
+ // 处理低端安卓机的样式问题,eg: `rgb(255 0 0)` -> `rgb(255, 0, 0)`
252
+ presetLegacyCompat({
253
+ commaStyleColorFunction: true,
254
+ legacyColorSpace: true
255
+ })
256
+ ],
257
+ shortcuts: [
258
+ ["border-s", "border border-solid"],
259
+ ["wh-full", "w-full h-full"],
260
+ ["f-c-c", "flex justify-center items-center"],
261
+ ["f-col-c", "flex-col justify-center items-center"],
262
+ ["flex-items", "flex items-center"],
263
+ ["flex-justify", "flex justify-center"],
264
+ ["flex-col", "flex flex-col"]
265
+ ],
266
+ transformers: [
267
+ transformerDirectives(),
268
+ transformerVariantGroup()
269
+ ],
270
+ rules: [
271
+ [
272
+ "p-safe",
273
+ {
274
+ padding: "env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left)"
275
+ }
276
+ ],
277
+ ["pt-safe", { "padding-top": "env(safe-area-inset-top)" }],
278
+ ["pb-safe", { "padding-bottom": "env(safe-area-inset-bottom)" }]
279
+ ]
280
+ };
281
+ }
282
+
283
+ // src/plugins/index.ts
284
+ function createUnivaPlugins(options) {
285
+ const pluginFactory = {
286
+ plugins: [
287
+ ...createPagesPlugin(options),
288
+ UniHelperLayouts()
289
+ ],
290
+ register(condition, plugin) {
291
+ if (condition) {
292
+ this.plugins.push(plugin);
293
+ }
294
+ }
295
+ };
296
+ pluginFactory.register(options.appRoot !== false, UnivaRoot({
297
+ enabledVirtualHost: true,
298
+ rootFileName: "App.univa",
299
+ enabledGlobalRef: true,
300
+ autoCreateRoot: true,
301
+ excludePages: [
302
+ `${COMPONENT_ASYNC_ROOT}/**/*.*`
303
+ ]
304
+ }));
305
+ pluginFactory.register(true, createManifestPlugin(options));
306
+ pluginFactory.register(options.components !== false, createComponentsPlugin(options));
307
+ pluginFactory.register(true, UniOptimization());
308
+ pluginFactory.register(true, Uni());
309
+ pluginFactory.register(true, createUnocssPlugin());
310
+ pluginFactory.register(options.autoImport !== false, createAutoImportPlugin(options));
311
+ pluginFactory.register(true, UniPolyfill());
312
+ return pluginFactory.plugins;
313
+ }
314
+
315
+ // src/index.ts
316
+ function Univa(config) {
317
+ return createUnivaPlugins({
318
+ components: config?.components ?? true,
319
+ autoImport: config?.autoImport ?? true,
320
+ appRoot: config?.appRoot ?? true,
321
+ pages: config?.pages,
322
+ manifest: config?.manifest
323
+ });
324
+ }
325
+ export {
326
+ COMPONENT_ASYNC_ROOT,
327
+ DTS_DIR,
328
+ Univa,
329
+ definePagesConfig,
330
+ presetUniva
331
+ };
package/package.json ADDED
@@ -0,0 +1,64 @@
1
+ {
2
+ "name": "@univa/core",
3
+ "type": "module",
4
+ "version": "0.0.1",
5
+ "description": "",
6
+ "author": "",
7
+ "license": "MIT",
8
+ "keywords": [],
9
+ "exports": {
10
+ ".": {
11
+ "import": {
12
+ "types": "./dist/index.d.ts",
13
+ "default": "./dist/index.js"
14
+ },
15
+ "require": {
16
+ "types": "./dist/index.d.cts",
17
+ "default": "./dist/index.cjs"
18
+ }
19
+ },
20
+ "./client": {
21
+ "types": "./src/client.d.ts"
22
+ }
23
+ },
24
+ "main": "./dist/index.cjs",
25
+ "module": "./dist/index.js",
26
+ "types": "./dist/index.d.ts",
27
+ "files": [
28
+ "README.md",
29
+ "dist",
30
+ "src/client.d.ts"
31
+ ],
32
+ "publishConfig": {
33
+ "access": "public",
34
+ "registry": "https://registry.npmjs.org/"
35
+ },
36
+ "scripts": {
37
+ "build": "tsup",
38
+ "dev": "tsup --watch"
39
+ },
40
+ "dependencies": {
41
+ "@uni-helper/eslint-config": "0.7.3",
42
+ "@uni-helper/plugin-uni": "0.1.0",
43
+ "@uni-helper/unocss-preset-uni": "0.2.11",
44
+ "@uni-helper/vite-plugin-uni-components": "0.2.10",
45
+ "@uni-helper/vite-plugin-uni-layouts": "0.1.11",
46
+ "@uni-helper/vite-plugin-uni-pages": "0.3.24",
47
+ "@uni-ku/bundle-optimizer": "2.1.2",
48
+ "@univa/manifest": "^0.0.1",
49
+ "@univa/root": "^0.0.1",
50
+ "@unocss/eslint-plugin": "66.6.7",
51
+ "@unocss/preset-legacy-compat": "66.0.0",
52
+ "eslint": "10.3.0",
53
+ "sass": "1.99.0",
54
+ "unocss": "66.0.0",
55
+ "unplugin-auto-import": "21.0.0",
56
+ "vite-plugin-uni-polyfill": "0.1.0"
57
+ },
58
+ "devDependencies": {
59
+ "c12": "4.0.0-beta.4",
60
+ "tsup": "^8.5.1",
61
+ "typescript": "^5.4.0",
62
+ "vite": "5.2.8"
63
+ }
64
+ }
@@ -0,0 +1,10 @@
1
+ /// <reference path="./global.d.ts" />
2
+ /// <reference types="@uni-helper/vite-plugin-uni-pages/client" />
3
+ /// <reference types="vite/client" />
4
+
5
+ declare module 'virtual:univa-tabbar' {
6
+ import type { TabBar } from '@uni-helper/vite-plugin-uni-pages'
7
+
8
+ const tabBar: TabBar
9
+ export default tabBar
10
+ }