@univa/core 0.0.6 → 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.
- package/README.md +330 -235
- package/dist/global.d.ts +4 -0
- package/dist/index.cjs +459 -289
- package/dist/index.d.cts +209 -27
- package/dist/index.d.ts +209 -27
- package/dist/index.js +447 -287
- package/dist/use.cjs +46 -0
- package/dist/use.d.cts +9 -0
- package/dist/use.d.ts +9 -0
- package/dist/use.js +19 -0
- package/package.json +30 -30
- package/dist/hooks.cjs +0 -83
- package/dist/hooks.d.cts +0 -36
- package/dist/hooks.d.ts +0 -36
- package/dist/hooks.js +0 -56
- package/src/client.d.ts +0 -13
- package/src/global.d.ts +0 -1
package/dist/index.cjs
CHANGED
|
@@ -30,346 +30,516 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
30
30
|
// src/index.ts
|
|
31
31
|
var index_exports = {};
|
|
32
32
|
__export(index_exports, {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
33
|
+
AutoImport: () => import_vite4.default,
|
|
34
|
+
PageContext: () => import_vite_plugin_uni_pages2.PageContext,
|
|
35
|
+
UniKuRoot: () => import_root2.default,
|
|
36
|
+
Univa: () => plugins_default,
|
|
37
|
+
VitePluginUniComponents: () => import_vite_plugin_uni_components3.default,
|
|
38
|
+
VitePluginUniLayouts: () => import_vite_plugin_uni_layouts2.VitePluginUniLayouts,
|
|
39
|
+
VitePluginUniPages: () => import_vite_plugin_uni_pages2.VitePluginUniPages,
|
|
40
|
+
camelCase: () => import_vite_plugin_uni_components2.camelCase,
|
|
41
|
+
createUnivaPlugins: () => createUnivaPlugins,
|
|
42
|
+
defineConfig: () => defineConfig,
|
|
43
|
+
kebabCase: () => import_vite_plugin_uni_components2.kebabCase,
|
|
44
|
+
pascalCase: () => import_vite_plugin_uni_components2.pascalCase,
|
|
45
|
+
presetUni: () => import_unocss_preset_uni.presetUni
|
|
36
46
|
});
|
|
37
47
|
module.exports = __toCommonJS(index_exports);
|
|
38
48
|
|
|
49
|
+
// src/config/index.ts
|
|
50
|
+
function defineConfig(config) {
|
|
51
|
+
return config;
|
|
52
|
+
}
|
|
53
|
+
|
|
39
54
|
// src/plugins/index.ts
|
|
40
55
|
var import_plugin_uni = __toESM(require("@uni-helper/plugin-uni"), 1);
|
|
41
|
-
var
|
|
42
|
-
var
|
|
43
|
-
var import_root = __toESM(require("@
|
|
44
|
-
var
|
|
56
|
+
var import_vite_plugin_uni_components = __toESM(require("@uni-helper/vite-plugin-uni-components"), 1);
|
|
57
|
+
var import_vite_plugin_uni_layouts = require("@uni-helper/vite-plugin-uni-layouts");
|
|
58
|
+
var import_root = __toESM(require("@uni-ku/root"), 1);
|
|
59
|
+
var import_vite2 = __toESM(require("unocss/vite"), 1);
|
|
60
|
+
var import_vite3 = __toESM(require("unplugin-auto-import/vite"), 1);
|
|
45
61
|
|
|
46
62
|
// src/constant.ts
|
|
47
|
-
var
|
|
48
|
-
var
|
|
63
|
+
var ID = "univa";
|
|
64
|
+
var UNIVA_DIR_NAME = `.${ID}`;
|
|
65
|
+
var ROOT_NAME = `${ID}.root`;
|
|
49
66
|
|
|
50
|
-
// src/
|
|
51
|
-
var
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
"pinia",
|
|
57
|
-
"uni-app",
|
|
58
|
-
{
|
|
59
|
-
from: "@univa/core/hooks",
|
|
60
|
-
imports: ["usePageContext"]
|
|
61
|
-
}
|
|
62
|
-
],
|
|
63
|
-
dirs: [
|
|
64
|
-
"src/hooks/**",
|
|
65
|
-
"!src/hooks/**/_*/**",
|
|
66
|
-
"src/store/**",
|
|
67
|
-
"!src/store/**/_*/**",
|
|
68
|
-
"src/constants/**"
|
|
69
|
-
],
|
|
70
|
-
vueTemplate: true,
|
|
71
|
-
dts: `${DTS_DIR}/auto-imports.d.ts`
|
|
72
|
-
};
|
|
73
|
-
const userAutoImportOptions = typeof options.autoImport === "object" ? options.autoImport : {};
|
|
74
|
-
const mergedOptions = {
|
|
75
|
-
...defaultAutoImportOptions,
|
|
76
|
-
...userAutoImportOptions
|
|
77
|
-
};
|
|
78
|
-
if (userAutoImportOptions.imports) {
|
|
79
|
-
const userImports = Array.isArray(userAutoImportOptions.imports) ? userAutoImportOptions.imports : [userAutoImportOptions.imports];
|
|
80
|
-
mergedOptions.imports = [...defaultAutoImportOptions.imports, ...userImports];
|
|
67
|
+
// src/utils/fs.ts
|
|
68
|
+
var import_node_fs = require("fs");
|
|
69
|
+
var import_node_path = require("path");
|
|
70
|
+
function ensureDir(dir) {
|
|
71
|
+
if (!(0, import_node_fs.existsSync)(dir)) {
|
|
72
|
+
(0, import_node_fs.mkdirSync)(dir, { recursive: true });
|
|
81
73
|
}
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
74
|
+
}
|
|
75
|
+
function getUnivaDir(cwd) {
|
|
76
|
+
return (0, import_node_path.join)(cwd, UNIVA_DIR_NAME);
|
|
77
|
+
}
|
|
78
|
+
function ensureUnivaDir(cwd) {
|
|
79
|
+
const dir = getUnivaDir(cwd);
|
|
80
|
+
ensureDir(dir);
|
|
81
|
+
return dir;
|
|
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);
|
|
85
92
|
}
|
|
86
|
-
return (0, import_vite.default)(mergedOptions);
|
|
87
93
|
}
|
|
88
94
|
|
|
89
|
-
// src/plugins/
|
|
90
|
-
var
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
95
|
+
// src/plugins/context.ts
|
|
96
|
+
var import_node_fs4 = require("fs");
|
|
97
|
+
var import_node_path4 = require("path");
|
|
98
|
+
var import_utils = require("@antfu/utils");
|
|
99
|
+
var import_c122 = require("c12");
|
|
100
|
+
var import_jiti = require("jiti");
|
|
101
|
+
|
|
102
|
+
// src/utils/merge.ts
|
|
103
|
+
var import_defu = require("defu");
|
|
104
|
+
var mergeWithArrayOverride = (0, import_defu.createDefu)((originObj, key, updates) => {
|
|
105
|
+
if (Array.isArray(originObj[key]) && Array.isArray(updates)) {
|
|
106
|
+
originObj[key] = updates;
|
|
107
|
+
return true;
|
|
108
|
+
}
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
// src/plugins/manifest.ts
|
|
112
|
+
var import_node_fs2 = require("fs");
|
|
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
|
|
103
125
|
};
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
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
|
+
}
|
|
107
137
|
}
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
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;
|
|
111
141
|
}
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
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);
|
|
115
154
|
}
|
|
116
|
-
|
|
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
|
+
};
|
|
117
187
|
}
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
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;
|
|
125
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
|
|
126
211
|
});
|
|
127
212
|
}
|
|
128
213
|
|
|
129
214
|
// src/plugins/pages.ts
|
|
130
|
-
var
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
(part) => part.split("-").map((subPart) => subPart.charAt(0).toUpperCase() + subPart.slice(1)).join("")
|
|
136
|
-
).join("");
|
|
137
|
-
};
|
|
138
|
-
if (!partToReplace) {
|
|
139
|
-
return toPascalCase(parts);
|
|
140
|
-
}
|
|
141
|
-
const indexToReplace = parts.findIndex((part) => part.toLowerCase() === partToReplace.toLowerCase());
|
|
142
|
-
if (indexToReplace === -1) {
|
|
143
|
-
return prefix + toPascalCase(parts);
|
|
144
|
-
}
|
|
145
|
-
parts[indexToReplace] = prefix;
|
|
146
|
-
return toPascalCase(parts);
|
|
215
|
+
var import_node_fs3 = require("fs");
|
|
216
|
+
var import_node_path3 = require("path");
|
|
217
|
+
var import_vite_plugin_uni_pages = require("@uni-helper/vite-plugin-uni-pages");
|
|
218
|
+
function getPagesConfigPath(root) {
|
|
219
|
+
return (0, import_node_path3.join)(getUnivaDir(root), "pages-config.ts");
|
|
147
220
|
}
|
|
148
|
-
function
|
|
149
|
-
|
|
150
|
-
|
|
221
|
+
function generatePagesConfigContent(content) {
|
|
222
|
+
const { pages, subPackages, ...rest } = content;
|
|
223
|
+
void pages;
|
|
224
|
+
void subPackages;
|
|
225
|
+
return `// Auto-generated by @univa/core
|
|
226
|
+
// \u6765\u6E90\uFF1Auniva.config.ts \u7684 pages \u5B57\u6BB5
|
|
227
|
+
export default ${JSON.stringify(rest, null, 2)}
|
|
228
|
+
`;
|
|
229
|
+
}
|
|
230
|
+
function generatePagesConfigFile(content, root) {
|
|
231
|
+
const pagesConfigPath = getPagesConfigPath(root);
|
|
232
|
+
const newContent = generatePagesConfigContent(content);
|
|
233
|
+
if ((0, import_node_fs3.existsSync)(pagesConfigPath)) {
|
|
234
|
+
const existingContent = (0, import_node_fs3.readFileSync)(pagesConfigPath, "utf-8");
|
|
235
|
+
if (existingContent === newContent) {
|
|
151
236
|
return;
|
|
152
237
|
}
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
if (!page.path) {
|
|
163
|
-
console.warn(`[vite-plugin-uni-pages] Page path is missing in subPackage: ${root}`);
|
|
164
|
-
return;
|
|
165
|
-
}
|
|
166
|
-
if (isAsyncComponent) {
|
|
167
|
-
page.layout = false;
|
|
168
|
-
}
|
|
169
|
-
page.name = _pathToName(`${root}/${page.path}`, "pages-sub", "Sub");
|
|
170
|
-
});
|
|
238
|
+
}
|
|
239
|
+
(0, import_node_fs3.writeFileSync)(pagesConfigPath, newContent, "utf-8");
|
|
240
|
+
}
|
|
241
|
+
function createPagesPlugin(pluginOptions) {
|
|
242
|
+
const result = (0, import_vite_plugin_uni_pages.VitePluginUniPages)({
|
|
243
|
+
dts: `${UNIVA_DIR_NAME}/uni-pages.d.ts`,
|
|
244
|
+
exclude: ["**/components/**/*.*"],
|
|
245
|
+
configSource: `${UNIVA_DIR_NAME}/pages-config.ts`,
|
|
246
|
+
...pluginOptions
|
|
171
247
|
});
|
|
248
|
+
return Array.isArray(result) ? result : [result];
|
|
172
249
|
}
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
250
|
+
|
|
251
|
+
// src/plugins/context.ts
|
|
252
|
+
var APP_KU_VUE_TEMPLATE = `<!-- Auto-generated by @univa/core -->
|
|
253
|
+
<template>
|
|
254
|
+
<KuRootView />
|
|
255
|
+
</template>
|
|
256
|
+
`;
|
|
257
|
+
var UnivaContext = class {
|
|
258
|
+
_server;
|
|
259
|
+
config;
|
|
260
|
+
configPath;
|
|
261
|
+
root;
|
|
262
|
+
constructor(options) {
|
|
263
|
+
this.root = options.root;
|
|
177
264
|
}
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
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)}`);
|
|
181
284
|
}
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
}
|
|
285
|
+
/**
|
|
286
|
+
* 同步加载用户配置(univa.config.ts)
|
|
287
|
+
*/
|
|
288
|
+
loadUserConfigSync() {
|
|
289
|
+
const jiti = (0, import_jiti.createJiti)(this.root);
|
|
290
|
+
const configFile = (0, import_node_path4.join)(this.root, `${ID}.config`);
|
|
291
|
+
try {
|
|
292
|
+
const configModule = jiti(configFile);
|
|
293
|
+
const userConfig = configModule?.default || configModule;
|
|
294
|
+
this.config = mergeWithArrayOverride({}, userConfig);
|
|
295
|
+
this.configPath = jiti.resolve(configFile);
|
|
296
|
+
} catch {
|
|
297
|
+
}
|
|
195
298
|
}
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
299
|
+
/**
|
|
300
|
+
* 异步加载用户配置(c12 支持更多格式与合并能力)
|
|
301
|
+
*/
|
|
302
|
+
async loadUserConfigAsync() {
|
|
303
|
+
const { config: userConfig, configFile } = await (0, import_c122.loadConfig)({
|
|
304
|
+
name: ID,
|
|
305
|
+
cwd: this.root,
|
|
306
|
+
defaults: this.config || {},
|
|
307
|
+
merger: mergeWithArrayOverride
|
|
308
|
+
});
|
|
309
|
+
this.config = userConfig;
|
|
310
|
+
this.configPath = configFile || "";
|
|
199
311
|
}
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
312
|
+
/**
|
|
313
|
+
* 设置 Vite 开发服务器
|
|
314
|
+
*/
|
|
315
|
+
setupViteServer(server) {
|
|
316
|
+
if (this._server === server)
|
|
317
|
+
return;
|
|
318
|
+
this._server = server;
|
|
319
|
+
this.setupWatcher(server.watcher);
|
|
205
320
|
}
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
return [pages2, subPackages, tabBar].join("\n");
|
|
222
|
-
};
|
|
223
|
-
const tabbarPlugin = {
|
|
224
|
-
name: "vite-plugin-univa-pages",
|
|
225
|
-
enforce: "pre",
|
|
226
|
-
resolveId(id) {
|
|
227
|
-
if (id === MODULE_ID_VIRTUAL) {
|
|
228
|
-
return RESOLVED_MODULE_ID_VIRTUAL;
|
|
321
|
+
/**
|
|
322
|
+
* 设置文件监听器
|
|
323
|
+
*
|
|
324
|
+
* 监听 univa.config.ts 变化,重新生成 .univa/pages.ts
|
|
325
|
+
*/
|
|
326
|
+
setupWatcher(watcher) {
|
|
327
|
+
if (!this.configPath) {
|
|
328
|
+
return;
|
|
329
|
+
}
|
|
330
|
+
watcher.add(this.configPath);
|
|
331
|
+
watcher.on("change", async (filePath) => {
|
|
332
|
+
filePath = (0, import_utils.slash)(filePath);
|
|
333
|
+
const configPath = (0, import_utils.slash)(this.configPath || "");
|
|
334
|
+
if (filePath !== configPath) {
|
|
335
|
+
return;
|
|
229
336
|
}
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
337
|
+
await this.updateConfig();
|
|
338
|
+
this.onUpdate();
|
|
339
|
+
});
|
|
340
|
+
}
|
|
341
|
+
async updateConfig() {
|
|
342
|
+
await this.loadUserConfigAsync();
|
|
343
|
+
this.updateGeneratedFiles();
|
|
344
|
+
}
|
|
345
|
+
/**
|
|
346
|
+
* 更新生成的文件
|
|
347
|
+
*
|
|
348
|
+
* 读取 univa.config.ts 的 pages 字段(应用级配置),
|
|
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() 初始化阶段完成。
|
|
355
|
+
*/
|
|
356
|
+
updateGeneratedFiles() {
|
|
357
|
+
const pagesConfig = this.config?.pages;
|
|
358
|
+
if (pagesConfig) {
|
|
359
|
+
generatePagesConfigFile(pagesConfig, this.root);
|
|
360
|
+
}
|
|
361
|
+
const manifestConfig = this.config?.manifest;
|
|
362
|
+
if (manifestConfig) {
|
|
363
|
+
generateManifestConfigFile(manifestConfig, this.root);
|
|
364
|
+
}
|
|
365
|
+
const pagesTsPath = getPagesConfigPath(this.root);
|
|
366
|
+
const emptyContent = `// Auto-generated by @univa/core
|
|
367
|
+
export default {}
|
|
368
|
+
`;
|
|
369
|
+
if (!(0, import_node_fs4.existsSync)(pagesTsPath)) {
|
|
370
|
+
(0, import_node_fs4.writeFileSync)(pagesTsPath, emptyContent, "utf-8");
|
|
371
|
+
} else if (!pagesConfig) {
|
|
372
|
+
if ((0, import_node_fs4.readFileSync)(pagesTsPath, "utf-8") !== emptyContent) {
|
|
373
|
+
(0, import_node_fs4.writeFileSync)(pagesTsPath, emptyContent, "utf-8");
|
|
234
374
|
}
|
|
235
375
|
}
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
...pages?.exclude || [],
|
|
251
|
-
"**/components/**/*.*"
|
|
252
|
-
],
|
|
253
|
-
onAfterMergePageMetaData(ctx) {
|
|
254
|
-
processPageName(ctx);
|
|
255
|
-
virtualData.tabBar = processTabBar(ctx);
|
|
256
|
-
},
|
|
257
|
-
onAfterWriteFile(ctx) {
|
|
258
|
-
virtualData.pages = ctx.resolveRoutes();
|
|
259
|
-
virtualData.subPackages = ctx.resolveSubRoutes();
|
|
260
|
-
},
|
|
261
|
-
dts: `${DTS_DIR}/pages.d.ts`
|
|
262
|
-
})
|
|
263
|
-
];
|
|
264
|
-
}
|
|
265
|
-
function definePagesConfig(config) {
|
|
266
|
-
return config;
|
|
267
|
-
}
|
|
376
|
+
}
|
|
377
|
+
/**
|
|
378
|
+
* 更新回调
|
|
379
|
+
* 触发 HMR
|
|
380
|
+
*/
|
|
381
|
+
onUpdate() {
|
|
382
|
+
if (!this._server) {
|
|
383
|
+
return;
|
|
384
|
+
}
|
|
385
|
+
this._server.ws.send({
|
|
386
|
+
type: "full-reload"
|
|
387
|
+
});
|
|
388
|
+
}
|
|
389
|
+
};
|
|
268
390
|
|
|
269
|
-
// src/plugins/
|
|
270
|
-
var
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
391
|
+
// src/plugins/options.ts
|
|
392
|
+
var DEFAULT_DIRS = {
|
|
393
|
+
pages: "pages",
|
|
394
|
+
subPackages: ["pages-sub/*"],
|
|
395
|
+
layouts: "layouts"
|
|
396
|
+
};
|
|
397
|
+
var DEFAULT_IMPORTS = {
|
|
398
|
+
apis: ["vue", "uni-app", "composables/**", "stores/**", "hooks/**", "constants/**"],
|
|
399
|
+
components: ["components/**", "components-biz/**"]
|
|
400
|
+
};
|
|
401
|
+
function resolveOptions2(userOptions) {
|
|
402
|
+
const srcDir = userOptions.srcDir || "src";
|
|
403
|
+
const dirs = {
|
|
404
|
+
pages: userOptions.dirs?.pages ?? DEFAULT_DIRS.pages,
|
|
405
|
+
subPackages: userOptions.dirs?.subPackages ?? DEFAULT_DIRS.subPackages,
|
|
406
|
+
layouts: userOptions.dirs?.layouts ?? DEFAULT_DIRS.layouts
|
|
407
|
+
};
|
|
408
|
+
const imports = {
|
|
409
|
+
apis: userOptions.imports?.apis ?? DEFAULT_IMPORTS.apis,
|
|
410
|
+
components: userOptions.imports?.components ?? DEFAULT_IMPORTS.components
|
|
411
|
+
};
|
|
282
412
|
return {
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
}),
|
|
288
|
-
(0, import_unocss.presetIcons)({
|
|
289
|
-
scale: 1.2,
|
|
290
|
-
warn: true,
|
|
291
|
-
extraProperties: {
|
|
292
|
-
"display": "inline-block",
|
|
293
|
-
"vertical-align": "middle"
|
|
294
|
-
}
|
|
295
|
-
}),
|
|
296
|
-
// 处理低端安卓机的样式问题,eg: `rgb(255 0 0)` -> `rgb(255, 0, 0)`
|
|
297
|
-
(0, import_preset_legacy_compat.default)({
|
|
298
|
-
commaStyleColorFunction: true,
|
|
299
|
-
legacyColorSpace: true
|
|
300
|
-
})
|
|
301
|
-
],
|
|
302
|
-
shortcuts: [
|
|
303
|
-
["border-s", "border border-solid"],
|
|
304
|
-
["wh-full", "w-full h-full"],
|
|
305
|
-
["f-c-c", "flex justify-center items-center"],
|
|
306
|
-
["f-col-c", "flex-col justify-center items-center"],
|
|
307
|
-
["flex-items", "flex items-center"],
|
|
308
|
-
["flex-justify", "flex justify-center"],
|
|
309
|
-
["flex-col", "flex flex-col"]
|
|
310
|
-
],
|
|
311
|
-
transformers: [
|
|
312
|
-
(0, import_unocss.transformerDirectives)(),
|
|
313
|
-
(0, import_unocss.transformerVariantGroup)()
|
|
314
|
-
],
|
|
315
|
-
rules: [
|
|
316
|
-
[
|
|
317
|
-
"p-safe",
|
|
318
|
-
{
|
|
319
|
-
padding: "env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left)"
|
|
320
|
-
}
|
|
321
|
-
],
|
|
322
|
-
["pt-safe", { "padding-top": "env(safe-area-inset-top)" }],
|
|
323
|
-
["pb-safe", { "padding-bottom": "env(safe-area-inset-bottom)" }]
|
|
324
|
-
]
|
|
413
|
+
srcDir,
|
|
414
|
+
dirs,
|
|
415
|
+
imports,
|
|
416
|
+
overrides: userOptions.overrides
|
|
325
417
|
};
|
|
326
418
|
}
|
|
327
419
|
|
|
328
420
|
// src/plugins/index.ts
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
421
|
+
var context;
|
|
422
|
+
function createCorePlugin() {
|
|
423
|
+
return {
|
|
424
|
+
name: "vite-plugin-univa",
|
|
425
|
+
enforce: "pre",
|
|
426
|
+
async configResolved() {
|
|
427
|
+
await context.loadUserConfigAsync();
|
|
428
|
+
context.updateGeneratedFiles();
|
|
429
|
+
},
|
|
430
|
+
configureServer(server) {
|
|
431
|
+
context.setupViteServer(server);
|
|
339
432
|
}
|
|
340
433
|
};
|
|
341
|
-
|
|
434
|
+
}
|
|
435
|
+
function isPresetName(from) {
|
|
436
|
+
if (/[*?[\]{}]/.test(from))
|
|
437
|
+
return false;
|
|
438
|
+
if (/^@[\w.-]+\/[\w.-]+$/.test(from))
|
|
439
|
+
return true;
|
|
440
|
+
if (/[/\\]/.test(from))
|
|
441
|
+
return false;
|
|
442
|
+
return true;
|
|
443
|
+
}
|
|
444
|
+
function resolveApiSources(sources, srcDir) {
|
|
445
|
+
const presets = [];
|
|
446
|
+
const dirs = [];
|
|
447
|
+
for (const source of sources) {
|
|
448
|
+
if (typeof source !== "string") {
|
|
449
|
+
presets.push(source);
|
|
450
|
+
} else if (source.startsWith("!")) {
|
|
451
|
+
dirs.push(`!${srcDir}/${source.slice(1)}`);
|
|
452
|
+
} else if (isPresetName(source)) {
|
|
453
|
+
presets.push(source);
|
|
454
|
+
} else {
|
|
455
|
+
dirs.push(`${srcDir}/${source}`);
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
return { presets, dirs };
|
|
459
|
+
}
|
|
460
|
+
function createUnivaPlugins(resolved) {
|
|
461
|
+
const plugins = [];
|
|
462
|
+
const { srcDir } = resolved;
|
|
463
|
+
plugins.push(createCorePlugin());
|
|
464
|
+
plugins.push(...createPagesPlugin({
|
|
465
|
+
dir: `${srcDir}/${resolved.dirs.pages}`,
|
|
466
|
+
subPackages: resolved.dirs.subPackages.map((p) => `${srcDir}/${p}`),
|
|
467
|
+
...resolved.overrides?.pages
|
|
468
|
+
}));
|
|
469
|
+
plugins.push((0, import_vite_plugin_uni_layouts.VitePluginUniLayouts)({
|
|
470
|
+
layoutDir: `${srcDir}/${resolved.dirs.layouts}`,
|
|
471
|
+
...resolved.overrides?.layouts
|
|
472
|
+
}));
|
|
473
|
+
plugins.push((0, import_root.default)({
|
|
342
474
|
enabledVirtualHost: true,
|
|
343
|
-
rootFileName: "App.univa",
|
|
344
475
|
enabledGlobalRef: true,
|
|
345
|
-
|
|
476
|
+
rootFileName: ROOT_NAME,
|
|
346
477
|
excludePages: [
|
|
347
|
-
|
|
348
|
-
]
|
|
478
|
+
"components-async/**/*.*"
|
|
479
|
+
],
|
|
480
|
+
...resolved.overrides?.root
|
|
481
|
+
}));
|
|
482
|
+
plugins.push((0, import_vite_plugin_uni_components.default)({
|
|
483
|
+
dirs: resolved.imports.components.map((c) => `${srcDir}/${c}`),
|
|
484
|
+
dts: `${UNIVA_DIR_NAME}/components.d.ts`,
|
|
485
|
+
globalNamespaces: ["components", "common"],
|
|
486
|
+
directoryAsNamespace: true,
|
|
487
|
+
...resolved.overrides?.components
|
|
349
488
|
}));
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
489
|
+
plugins.push(createManifestPlugin({
|
|
490
|
+
...resolved.overrides?.manifest
|
|
491
|
+
}));
|
|
492
|
+
plugins.push(...(0, import_plugin_uni.default)());
|
|
493
|
+
plugins.push(...(0, import_vite2.default)());
|
|
494
|
+
const { presets, dirs } = resolveApiSources(resolved.imports.apis, srcDir);
|
|
495
|
+
const autoImportPlugin = (0, import_vite3.default)({
|
|
496
|
+
imports: presets,
|
|
497
|
+
dirs,
|
|
498
|
+
dts: `${UNIVA_DIR_NAME}/auto-imports.d.ts`,
|
|
499
|
+
...resolved.overrides?.autoImport
|
|
500
|
+
});
|
|
501
|
+
if (Array.isArray(autoImportPlugin)) {
|
|
502
|
+
plugins.push(...autoImportPlugin);
|
|
503
|
+
} else {
|
|
504
|
+
plugins.push(autoImportPlugin);
|
|
505
|
+
}
|
|
506
|
+
return plugins;
|
|
358
507
|
}
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
return createUnivaPlugins({
|
|
363
|
-
components: config?.components ?? true,
|
|
364
|
-
autoImport: config?.autoImport ?? true,
|
|
365
|
-
appRoot: config?.appRoot ?? true,
|
|
366
|
-
pages: config?.pages,
|
|
367
|
-
manifest: config?.manifest
|
|
508
|
+
function Univa() {
|
|
509
|
+
context = new UnivaContext({
|
|
510
|
+
root: process.cwd()
|
|
368
511
|
});
|
|
512
|
+
ensureUnivaDir(context.root);
|
|
513
|
+
context.loadUserConfigSync();
|
|
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");
|
|
518
|
+
return createUnivaPlugins(resolved);
|
|
369
519
|
}
|
|
520
|
+
var plugins_default = Univa;
|
|
521
|
+
|
|
522
|
+
// src/index.ts
|
|
523
|
+
var import_unocss_preset_uni = require("@uni-helper/unocss-preset-uni");
|
|
524
|
+
var import_vite_plugin_uni_components2 = require("@uni-helper/vite-plugin-uni-components");
|
|
525
|
+
var import_vite_plugin_uni_components3 = __toESM(require("@uni-helper/vite-plugin-uni-components"), 1);
|
|
526
|
+
var import_vite_plugin_uni_layouts2 = require("@uni-helper/vite-plugin-uni-layouts");
|
|
527
|
+
var import_vite_plugin_uni_pages2 = require("@uni-helper/vite-plugin-uni-pages");
|
|
528
|
+
var import_root2 = __toESM(require("@uni-ku/root"), 1);
|
|
529
|
+
var import_vite4 = __toESM(require("unplugin-auto-import/vite"), 1);
|
|
370
530
|
// Annotate the CommonJS export names for ESM import in node:
|
|
371
531
|
0 && (module.exports = {
|
|
532
|
+
AutoImport,
|
|
533
|
+
PageContext,
|
|
534
|
+
UniKuRoot,
|
|
372
535
|
Univa,
|
|
373
|
-
|
|
374
|
-
|
|
536
|
+
VitePluginUniComponents,
|
|
537
|
+
VitePluginUniLayouts,
|
|
538
|
+
VitePluginUniPages,
|
|
539
|
+
camelCase,
|
|
540
|
+
createUnivaPlugins,
|
|
541
|
+
defineConfig,
|
|
542
|
+
kebabCase,
|
|
543
|
+
pascalCase,
|
|
544
|
+
presetUni
|
|
375
545
|
});
|