@vixt/uni 0.4.0 → 0.4.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.
@@ -0,0 +1,78 @@
1
+ import * as vite from 'vite';
2
+ import * as _vixt_core from '@vixt/core';
3
+ import { AppOptions, Vixt, PluginOptions as PluginOptions$1, VixtOptions } from '@vixt/core';
4
+ import Pages, { PageMetaDatum } from '@uni-helper/vite-plugin-uni-pages';
5
+ import { Pinia } from 'pinia';
6
+ import { PluginOptions } from 'pinia-plugin-persistedstate';
7
+ import { App } from 'vue';
8
+ import Uni from '@dcloudio/vite-plugin-uni';
9
+ import Components from '@uni-helper/vite-plugin-uni-components';
10
+ import Layouts from '@uni-helper/vite-plugin-uni-layouts';
11
+ import UnoCSS from 'unocss/vite';
12
+ import AutoImport from 'unplugin-auto-import/vite';
13
+ import { ImportsMap } from 'unplugin-auto-import/types';
14
+
15
+ declare module '@vixt/core/client' {
16
+ interface VixtAppConfig {
17
+ /** https://github.com/prazdevs/pinia-plugin-persistedstate */
18
+ piniaPersistedState?: PluginOptions
19
+ }
20
+
21
+ interface VixtApp {
22
+ app: App
23
+ routes: PageMetaDatum[]
24
+ pinia: Pinia
25
+ appConfig: VixtAppConfig
26
+ }
27
+ }
28
+
29
+ declare const appUni: _vixt_core.VixtModule<AppOptions>;
30
+
31
+ declare function generateMainTs(options: AppOptions, vixt: Vixt): string;
32
+
33
+ declare module '@vixt/core' {
34
+ interface VixtOptions {
35
+ uni?: PluginOptions$1<typeof Uni>;
36
+ /** https://github.com/uni-helper/vite-plugin-uni-pages */
37
+ uniPages?: PluginOptions$1<typeof Pages>;
38
+ /** https://github.com/uni-helper/vite-plugin-uni-layouts */
39
+ uniLayouts?: PluginOptions$1<typeof Layouts>;
40
+ /** https://github.com/uni-helper/vite-plugin-uni-components */
41
+ uniComponents?: PluginOptions$1<typeof Components>;
42
+ /** https://github.com/antfu/unplugin-auto-import */
43
+ imports?: PluginOptions$1<typeof AutoImport>;
44
+ /** https://github.com/antfu/unocss */
45
+ unocss?: PluginOptions$1<typeof UnoCSS>;
46
+ }
47
+ }
48
+ declare module '@vixt/core/client' {
49
+ interface VixtAppConfig {
50
+ /** https://github.com/prazdevs/pinia-plugin-persistedstate */
51
+ piniaPersistedState?: PluginOptions;
52
+ }
53
+ }
54
+ declare const presetUni: _vixt_core.VixtModule<VixtOptions>;
55
+
56
+ declare module '@vixt/core' {
57
+ interface VixtOptions {
58
+ uniModules?: ModuleOptions;
59
+ }
60
+ }
61
+ interface ModuleOptions {
62
+ include?: string[];
63
+ exclude?: string[];
64
+ }
65
+ declare const uniModules: _vixt_core.VixtModule<_vixt_core.ModuleOptions>;
66
+
67
+ /** 增加小程序中vueuse的运行所需 */
68
+ declare function vueusePolyfill(code: string, id: string): string;
69
+ /** 移除路径中的'../' */
70
+ declare function patchNormalizeNodeModules(): void;
71
+ declare const uniPatch: (options?: any) => vite.PluginOption;
72
+
73
+ declare function useImports(): ImportsMap;
74
+
75
+ declare const _default: (options?: _vixt_core.VixtOptions | undefined) => vite.PluginOption;
76
+
77
+ export { appUni, _default as default, generateMainTs, patchNormalizeNodeModules, presetUni, uniModules, uniPatch, useImports, vueusePolyfill };
78
+ export type { ModuleOptions };
@@ -0,0 +1,78 @@
1
+ import * as vite from 'vite';
2
+ import * as _vixt_core from '@vixt/core';
3
+ import { AppOptions, Vixt, PluginOptions as PluginOptions$1, VixtOptions } from '@vixt/core';
4
+ import Pages, { PageMetaDatum } from '@uni-helper/vite-plugin-uni-pages';
5
+ import { Pinia } from 'pinia';
6
+ import { PluginOptions } from 'pinia-plugin-persistedstate';
7
+ import { App } from 'vue';
8
+ import Uni from '@dcloudio/vite-plugin-uni';
9
+ import Components from '@uni-helper/vite-plugin-uni-components';
10
+ import Layouts from '@uni-helper/vite-plugin-uni-layouts';
11
+ import UnoCSS from 'unocss/vite';
12
+ import AutoImport from 'unplugin-auto-import/vite';
13
+ import { ImportsMap } from 'unplugin-auto-import/types';
14
+
15
+ declare module '@vixt/core/client' {
16
+ interface VixtAppConfig {
17
+ /** https://github.com/prazdevs/pinia-plugin-persistedstate */
18
+ piniaPersistedState?: PluginOptions
19
+ }
20
+
21
+ interface VixtApp {
22
+ app: App
23
+ routes: PageMetaDatum[]
24
+ pinia: Pinia
25
+ appConfig: VixtAppConfig
26
+ }
27
+ }
28
+
29
+ declare const appUni: _vixt_core.VixtModule<AppOptions>;
30
+
31
+ declare function generateMainTs(options: AppOptions, vixt: Vixt): string;
32
+
33
+ declare module '@vixt/core' {
34
+ interface VixtOptions {
35
+ uni?: PluginOptions$1<typeof Uni>;
36
+ /** https://github.com/uni-helper/vite-plugin-uni-pages */
37
+ uniPages?: PluginOptions$1<typeof Pages>;
38
+ /** https://github.com/uni-helper/vite-plugin-uni-layouts */
39
+ uniLayouts?: PluginOptions$1<typeof Layouts>;
40
+ /** https://github.com/uni-helper/vite-plugin-uni-components */
41
+ uniComponents?: PluginOptions$1<typeof Components>;
42
+ /** https://github.com/antfu/unplugin-auto-import */
43
+ imports?: PluginOptions$1<typeof AutoImport>;
44
+ /** https://github.com/antfu/unocss */
45
+ unocss?: PluginOptions$1<typeof UnoCSS>;
46
+ }
47
+ }
48
+ declare module '@vixt/core/client' {
49
+ interface VixtAppConfig {
50
+ /** https://github.com/prazdevs/pinia-plugin-persistedstate */
51
+ piniaPersistedState?: PluginOptions;
52
+ }
53
+ }
54
+ declare const presetUni: _vixt_core.VixtModule<VixtOptions>;
55
+
56
+ declare module '@vixt/core' {
57
+ interface VixtOptions {
58
+ uniModules?: ModuleOptions;
59
+ }
60
+ }
61
+ interface ModuleOptions {
62
+ include?: string[];
63
+ exclude?: string[];
64
+ }
65
+ declare const uniModules: _vixt_core.VixtModule<_vixt_core.ModuleOptions>;
66
+
67
+ /** 增加小程序中vueuse的运行所需 */
68
+ declare function vueusePolyfill(code: string, id: string): string;
69
+ /** 移除路径中的'../' */
70
+ declare function patchNormalizeNodeModules(): void;
71
+ declare const uniPatch: (options?: any) => vite.PluginOption;
72
+
73
+ declare function useImports(): ImportsMap;
74
+
75
+ declare const _default: (options?: _vixt_core.VixtOptions | undefined) => vite.PluginOption;
76
+
77
+ export { appUni, _default as default, generateMainTs, patchNormalizeNodeModules, presetUni, uniModules, uniPatch, useImports, vueusePolyfill };
78
+ export type { ModuleOptions };
package/dist/index.mjs ADDED
@@ -0,0 +1,274 @@
1
+ import { generateClient, generateCss, generateAppConfig, generatePlugins, defineVixtModule, generateIndexHtml, resolveLayersDirs, defineVitePlugin, createVixtPlugin } from '@vixt/core';
2
+ import path from 'pathe';
3
+ import { genarateAppComponent } from '@vixt/vue';
4
+ import fs from 'fs-extra';
5
+ import Uni from '@dcloudio/vite-plugin-uni';
6
+ import Components from '@uni-helper/vite-plugin-uni-components';
7
+ import Layouts from '@uni-helper/vite-plugin-uni-layouts';
8
+ import Pages from '@uni-helper/vite-plugin-uni-pages';
9
+ import defu from 'defu';
10
+ import UnoCSS from 'unocss/vite';
11
+ import AutoImport from 'unplugin-auto-import/vite';
12
+ import { resolvePathSync } from 'mlly';
13
+ import { readFileSync } from 'node:fs';
14
+ import process from 'node:process';
15
+ import { uniuseAutoImports } from '@uni-helper/uni-use';
16
+ import { resolveModule } from 'local-pkg';
17
+
18
+ function generateMainTs(options, vixt) {
19
+ const { buildDir, srcDir } = vixt.options;
20
+ const mainTsPath = path.resolve(srcDir, "main.ts");
21
+ if (!fs.existsSync(mainTsPath))
22
+ fs.outputFileSync(mainTsPath, `// Generated by Vixt
23
+ // This file transform from '${path.basename(buildDir)}/main.ts'
24
+ `);
25
+ generateClient(vixt);
26
+ const appComponentTemplate = genarateAppComponent(vixt);
27
+ const cssTemplate = generateCss(options);
28
+ const appConfigTemplate = generateAppConfig(vixt);
29
+ const pluginsTemplate = generatePlugins(vixt);
30
+ const code = `// Generated by Vixt
31
+ // This file transform to '${srcDir}/main.ts'
32
+ // @ts-nocheck
33
+ ${appComponentTemplate}
34
+ ${cssTemplate}
35
+ ${appConfigTemplate}
36
+ ${pluginsTemplate}
37
+
38
+ import 'virtual:uno.css'
39
+ import { createSSRApp } from 'vue'
40
+ import * as Pinia from 'pinia'
41
+ import { createPersistedState } from 'pinia-plugin-persistedstate'
42
+ import { pages as routes } from 'virtual:uni-pages'
43
+
44
+ export function createApp() {
45
+ const app = createSSRApp(App)
46
+ const pinia = Pinia.createPinia()
47
+ pinia.use(createPersistedState({
48
+ storage: {
49
+ getItem: uni.getStorageSync,
50
+ setItem: uni.setStorageSync,
51
+ },
52
+ ...appConfig.piniaPersistedState,
53
+ }))
54
+ app.use(pinia)
55
+ usePlugins({ app, routes, pinia, appConfig })
56
+
57
+ return { app, Pinia }
58
+ }
59
+ `;
60
+ fs.outputFileSync(path.resolve(buildDir, "main.ts"), code);
61
+ return code;
62
+ }
63
+
64
+ const name$2 = "vixt:app-uni";
65
+ const defaults$1 = {
66
+ rootId: "app",
67
+ rootTag: "div",
68
+ baseURL: "/",
69
+ entryFile: "main.ts",
70
+ css: [],
71
+ head: {
72
+ meta: [
73
+ { charset: "utf-8" },
74
+ { name: "viewport", content: "width=device-width, initial-scale=1" }
75
+ ]
76
+ }
77
+ };
78
+ const appUni = defineVixtModule({
79
+ meta: { name: name$2, configKey: "app" },
80
+ defaults: defaults$1,
81
+ setup(options, vixt) {
82
+ let indexHtmlCode;
83
+ let mainTsCode;
84
+ const mainTsPath = path.resolve(vixt.options.srcDir, "main.ts");
85
+ return {
86
+ name: name$2,
87
+ configResolved() {
88
+ indexHtmlCode = generateIndexHtml(options, vixt);
89
+ mainTsCode = generateMainTs(options, vixt);
90
+ },
91
+ transformIndexHtml: {
92
+ order: "pre",
93
+ handler() {
94
+ return indexHtmlCode;
95
+ }
96
+ },
97
+ transform: {
98
+ order: "pre",
99
+ handler(_, id) {
100
+ if (id !== mainTsPath)
101
+ return;
102
+ return mainTsCode;
103
+ }
104
+ }
105
+ };
106
+ }
107
+ });
108
+
109
+ const name$1 = "vixt:preset-uni";
110
+ const presetUni = defineVixtModule({
111
+ meta: { name: name$1 },
112
+ setup(_, vixt) {
113
+ const { components, composables = [], constants = [], utils = [], stores = [] } = resolveLayersDirs(vixt._layers);
114
+ const { buildTypesDir, buildImportsDir } = vixt.options;
115
+ const defaultOptions = {
116
+ uni: {},
117
+ uniPages: { dts: `${buildTypesDir}/uni-pages.d.ts` },
118
+ uniLayouts: {},
119
+ uniComponents: {
120
+ dts: `${buildTypesDir}/components.d.ts`,
121
+ dirs: components,
122
+ directoryAsNamespace: true,
123
+ collapseSamePrefixes: true
124
+ },
125
+ imports: {
126
+ imports: ["vue", "uni-app", "pinia", useImports()],
127
+ dts: `${buildTypesDir}/auto-imports.d.ts`,
128
+ dirs: [...composables, ...constants, ...stores, ...utils, buildImportsDir],
129
+ vueTemplate: true
130
+ },
131
+ unocss: {}
132
+ };
133
+ const options = vixt.options = defu(vixt.options, defaultOptions);
134
+ const modules = [
135
+ Pages(options.uniPages),
136
+ Layouts(options.uniLayouts),
137
+ Components(options.uniComponents),
138
+ AutoImport(options.imports),
139
+ UnoCSS(options.unocss),
140
+ // @ts-expect-error
141
+ Uni.default(options.uni),
142
+ uniPatch()
143
+ ];
144
+ return modules;
145
+ }
146
+ });
147
+
148
+ function copyUniModules(options, vixt) {
149
+ const { srcDir } = vixt.options;
150
+ const { include, exclude } = options ?? {};
151
+ const { uni_modules = [] } = resolveLayersDirs(vixt._layers);
152
+ const srcUniModulesPath = path.join(srcDir, "uni_modules");
153
+ uni_modules.forEach((uniModulesPath) => {
154
+ fs.readdirSync(uniModulesPath).filter((dir) => {
155
+ if (include?.length)
156
+ return include.includes(dir);
157
+ if (exclude?.length)
158
+ return !exclude.includes(dir);
159
+ return true;
160
+ }).forEach((dir) => {
161
+ const srcPath = path.join(uniModulesPath, dir);
162
+ const destPath = path.join(srcUniModulesPath, dir);
163
+ if (srcPath !== destPath) {
164
+ try {
165
+ fs.copySync(srcPath, destPath);
166
+ fs.writeFileSync(path.join(destPath, ".gitignore"), "*", "utf-8");
167
+ } catch (e) {
168
+ console.error(`Error copying ${srcPath} to ${destPath}:
169
+ `, e);
170
+ }
171
+ }
172
+ });
173
+ });
174
+ }
175
+ const name = "vixt:uni-modules";
176
+ const uniModules = defineVixtModule({
177
+ meta: { name, configKey: "uniModules" },
178
+ setup(options, vixt) {
179
+ copyUniModules(options, vixt);
180
+ return {
181
+ name,
182
+ configureServer(server) {
183
+ const { uni_modules = [] } = resolveLayersDirs(vixt._layers);
184
+ server.watcher.add(uni_modules);
185
+ server.watcher.on("all", (_, file) => {
186
+ const match = uni_modules.some((e) => path.normalize(file).match(e));
187
+ if (match)
188
+ copyUniModules(options, vixt);
189
+ });
190
+ }
191
+ };
192
+ }
193
+ });
194
+
195
+ function vueusePolyfill(code, id) {
196
+ if (!id.endsWith("@dcloudio/uni-mp-vue/dist/vue.runtime.esm.js"))
197
+ return code;
198
+ code += `
199
+ export const render = () => {}
200
+ export const TransitionGroup = {}
201
+ `;
202
+ return code;
203
+ }
204
+ function patchNormalizeNodeModules() {
205
+ const matched = `str = normalizePath(str).replace(NODE_MODULES_REGEX, 'node-modules');`;
206
+ const replaced = `str = normalizePath(str).replace(NODE_MODULES_REGEX, 'node-modules').replace(/\\.\\.\\//g, '');`;
207
+ const codePath = resolvePathSync("@dcloudio/uni-cli-shared/dist/utils.js");
208
+ let code = fs.readFileSync(codePath, "utf8");
209
+ if (code.includes(matched)) {
210
+ code = code.replace(matched, replaced);
211
+ fs.writeFileSync(codePath, code);
212
+ }
213
+ }
214
+ const uniPatch = defineVitePlugin(() => {
215
+ patchNormalizeNodeModules();
216
+ return {
217
+ name: "vixt:uni-patch",
218
+ transform(code, id) {
219
+ code = vueusePolyfill(code, id);
220
+ return code;
221
+ }
222
+ };
223
+ });
224
+
225
+ let _cache;
226
+ function useImports() {
227
+ const excluded = [
228
+ "toRefs",
229
+ "utils",
230
+ "toRef",
231
+ "toValue",
232
+ "tryOnScopeDispose",
233
+ "useNetwork",
234
+ "useOnline",
235
+ "usePreferredDark",
236
+ "useStorage",
237
+ "useStorageAsync"
238
+ ];
239
+ if (!_cache) {
240
+ let indexesJson;
241
+ try {
242
+ const corePath = resolveModule("@vueuse/core") || process.cwd();
243
+ const path = resolveModule("@vueuse/core/indexes.json") || resolveModule("@vueuse/metadata/index.json") || resolveModule("@vueuse/metadata/index.json", { paths: [corePath] });
244
+ indexesJson = JSON.parse(readFileSync(path, "utf-8"));
245
+ } catch (error) {
246
+ console.error(error);
247
+ throw new Error("[auto-import] failed to load @vueuse/core, have you installed it?");
248
+ }
249
+ if (indexesJson) {
250
+ _cache = {
251
+ "@vueuse/core": indexesJson.functions.filter((i) => ["core", "shared"].includes(i.package)).flatMap((i) => [i.name, ...i.alias || []]).filter((i) => i && i.length >= 4 && !excluded.includes(i)),
252
+ ...uniuseAutoImports()
253
+ };
254
+ }
255
+ }
256
+ return _cache || {};
257
+ }
258
+
259
+ const defaults = {
260
+ modules: [uniModules, appUni, presetUni],
261
+ typescript: {
262
+ tsConfig: {
263
+ compilerOptions: {
264
+ types: ["@dcloudio/types", "@uni-helper/uni-app-types", "@uni-helper/vite-plugin-uni-pages/client"]
265
+ },
266
+ vueCompilerOptions: {
267
+ plugins: ["@uni-helper/uni-app-types/volar-plugin"]
268
+ }
269
+ }
270
+ }
271
+ };
272
+ const index = createVixtPlugin({ defaults });
273
+
274
+ export { appUni, index as default, generateMainTs, patchNormalizeNodeModules, presetUni, uniModules, uniPatch, useImports, vueusePolyfill };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@vixt/uni",
3
3
  "type": "module",
4
- "version": "0.4.0",
4
+ "version": "0.4.1",
5
5
  "author": "SoulLyoko<https://github.com/SoulLyoko>",
6
6
  "license": "MIT",
7
7
  "homepage": "https://github.com/SoulLyoko/vixt#readme",
@@ -26,8 +26,8 @@
26
26
  "@uni-helper/vite-plugin-uni-layouts": "^0.1.10",
27
27
  "@uni-helper/vite-plugin-uni-pages": "^0.2.28",
28
28
  "unocss-applet": "^0.10.0",
29
- "@vixt/core": "0.4.0",
30
- "@vixt/vue": "0.4.0"
29
+ "@vixt/core": "0.4.1",
30
+ "@vixt/vue": "0.4.1"
31
31
  },
32
32
  "scripts": {
33
33
  "build": "unbuild",