@vixt/react 0.4.0 → 0.4.2

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,47 @@
1
+ import * as vite from 'vite';
2
+ import * as _vixt_core from '@vixt/core';
3
+ import { AppOptions, Vixt, PluginOptions, VixtOptions } from '@vixt/core';
4
+ import { Root } from 'react-dom/client';
5
+ import { RouteObject } from 'react-router';
6
+ import React from '@vitejs/plugin-react';
7
+ import UnoCSS from 'unocss/vite';
8
+ import AutoImport from 'unplugin-auto-import/vite';
9
+ import Pages from 'vite-plugin-pages';
10
+ import Layouts from 'vite-plugin-vue-layouts';
11
+
12
+ declare module '@vixt/core/client' {
13
+ interface VixtAppConfig { }
14
+
15
+ interface VixtApp {
16
+ app: Root
17
+ routes: RouteObject[]
18
+ appConfig: VixtAppConfig
19
+ }
20
+ }
21
+
22
+ declare const app: _vixt_core.VixtModule<AppOptions>;
23
+
24
+ declare function getAppComponentPath(vixt: Vixt): string | undefined;
25
+ declare function generateSetupLayouts(vixt: Vixt): void;
26
+ declare function genarateAppComponent(vixt: Vixt): string;
27
+
28
+ declare function generateMainFile(options: AppOptions, vixt: Vixt): string;
29
+
30
+ declare module '@vixt/core' {
31
+ interface VixtOptions {
32
+ react?: PluginOptions<typeof React>;
33
+ /** https://github.com/hannoeru/vite-plugin-pages */
34
+ pages?: PluginOptions<typeof Pages>;
35
+ /** https://github.com/JohnCampionJr/vite-plugin-vue-layouts */
36
+ layouts?: PluginOptions<typeof Layouts>;
37
+ /** https://github.com/unplugin/unplugin-auto-import */
38
+ imports?: PluginOptions<typeof AutoImport>;
39
+ /** https://github.com/unocss/unocss */
40
+ unocss?: PluginOptions<typeof UnoCSS>;
41
+ }
42
+ }
43
+ declare const presetReact: _vixt_core.VixtModule<VixtOptions>;
44
+
45
+ declare const _default: (options?: VixtOptions | undefined) => vite.PluginOption;
46
+
47
+ export { app, _default as default, genarateAppComponent, generateMainFile, generateSetupLayouts, getAppComponentPath, presetReact };
@@ -0,0 +1,47 @@
1
+ import * as vite from 'vite';
2
+ import * as _vixt_core from '@vixt/core';
3
+ import { AppOptions, Vixt, PluginOptions, VixtOptions } from '@vixt/core';
4
+ import { Root } from 'react-dom/client';
5
+ import { RouteObject } from 'react-router';
6
+ import React from '@vitejs/plugin-react';
7
+ import UnoCSS from 'unocss/vite';
8
+ import AutoImport from 'unplugin-auto-import/vite';
9
+ import Pages from 'vite-plugin-pages';
10
+ import Layouts from 'vite-plugin-vue-layouts';
11
+
12
+ declare module '@vixt/core/client' {
13
+ interface VixtAppConfig { }
14
+
15
+ interface VixtApp {
16
+ app: Root
17
+ routes: RouteObject[]
18
+ appConfig: VixtAppConfig
19
+ }
20
+ }
21
+
22
+ declare const app: _vixt_core.VixtModule<AppOptions>;
23
+
24
+ declare function getAppComponentPath(vixt: Vixt): string | undefined;
25
+ declare function generateSetupLayouts(vixt: Vixt): void;
26
+ declare function genarateAppComponent(vixt: Vixt): string;
27
+
28
+ declare function generateMainFile(options: AppOptions, vixt: Vixt): string;
29
+
30
+ declare module '@vixt/core' {
31
+ interface VixtOptions {
32
+ react?: PluginOptions<typeof React>;
33
+ /** https://github.com/hannoeru/vite-plugin-pages */
34
+ pages?: PluginOptions<typeof Pages>;
35
+ /** https://github.com/JohnCampionJr/vite-plugin-vue-layouts */
36
+ layouts?: PluginOptions<typeof Layouts>;
37
+ /** https://github.com/unplugin/unplugin-auto-import */
38
+ imports?: PluginOptions<typeof AutoImport>;
39
+ /** https://github.com/unocss/unocss */
40
+ unocss?: PluginOptions<typeof UnoCSS>;
41
+ }
42
+ }
43
+ declare const presetReact: _vixt_core.VixtModule<VixtOptions>;
44
+
45
+ declare const _default: (options?: VixtOptions | undefined) => vite.PluginOption;
46
+
47
+ export { app, _default as default, genarateAppComponent, generateMainFile, generateSetupLayouts, getAppComponentPath, presetReact };
package/dist/index.mjs ADDED
@@ -0,0 +1,263 @@
1
+ import { generateClient, generateCss, generateAppConfig, generatePlugins, defineVixtModule, generateIndexHtml, resolveLayersDirs, createVixtPlugin } from '@vixt/core';
2
+ import path from 'pathe';
3
+ import fs from 'fs-extra';
4
+ import React from '@vitejs/plugin-react';
5
+ import defu from 'defu';
6
+ import UnoCSS from 'unocss/vite';
7
+ import AutoImport from 'unplugin-auto-import/vite';
8
+ import Pages from 'vite-plugin-pages';
9
+ import Layouts from 'vite-plugin-vue-layouts';
10
+ import { cwd } from 'node:process';
11
+ import fg from 'fast-glob';
12
+ import { pascalCase } from 'unplugin-vue-components';
13
+ import extractComments from 'extract-comments';
14
+ import { parse } from 'yaml';
15
+
16
+ function getAppComponentPath(vixt) {
17
+ for (const layer of vixt._layers) {
18
+ const appComponentPath = path.resolve(layer.config.srcDir, "App.tsx");
19
+ if (fs.existsSync(appComponentPath))
20
+ return appComponentPath;
21
+ }
22
+ }
23
+ function generateSetupLayouts(vixt) {
24
+ const { buildImportsDir } = vixt.options;
25
+ const { defaultLayout = "default" } = vixt.options.layouts ?? {};
26
+ fs.outputFileSync(path.resolve(buildImportsDir, `layouts.ts`), `// Generated by Vixt
27
+ // @ts-nocheck
28
+ import { createElement } from 'react'
29
+ import { layouts } from 'virtual:generated-layouts'
30
+
31
+ export function setupLayouts(routes) {
32
+ return routes.map(route => {
33
+ if (route.children?.length > 0) {
34
+ route.children = setupLayouts(route.children)
35
+ }
36
+ if(route.element && route.meta?.layout !== false) {
37
+ return {
38
+ path: route.path,
39
+ element: createElement(layouts[route.meta?.layout ?? '${defaultLayout}']),
40
+ children: [{ ...route, path: '' }]
41
+ }
42
+ }
43
+ return route
44
+ });
45
+ }
46
+ `);
47
+ }
48
+ function genarateAppComponent(vixt) {
49
+ generateSetupLayouts(vixt);
50
+ const { buildDir } = vixt.options;
51
+ const defaultAppComponentPath = path.resolve(buildDir, `App.tsx`);
52
+ fs.outputFileSync(defaultAppComponentPath, `// Generated by Vixt
53
+ // @ts-nocheck
54
+ import { StrictMode } from 'react'
55
+ import { useRoutes } from 'react-router'
56
+ import generatedRoutes from '~react-pages'
57
+
58
+ const routes = setupLayouts(generatedRoutes)
59
+
60
+ function App() {
61
+ return (
62
+ <StrictMode>
63
+ {useRoutes(routes)}
64
+ </StrictMode>
65
+ )
66
+ }
67
+
68
+ export default App
69
+ `);
70
+ const appComponentPath = getAppComponentPath(vixt) || defaultAppComponentPath;
71
+ const appComponentTemplate = `import App from '${appComponentPath}'`;
72
+ return appComponentTemplate;
73
+ }
74
+
75
+ function generateMainFile(options, vixt) {
76
+ const { buildDir, srcDir } = vixt.options;
77
+ const mainFileName = "main.tsx";
78
+ const mainTsPath = path.resolve(srcDir, mainFileName);
79
+ if (!fs.existsSync(mainTsPath))
80
+ fs.outputFileSync(mainTsPath, `// Generated by Vixt
81
+ // This file transform from '${path.basename(buildDir)}/${mainFileName}'
82
+ `);
83
+ generateClient(vixt);
84
+ const appComponentTemplate = genarateAppComponent(vixt);
85
+ const cssTemplate = generateCss(options);
86
+ const appConfigTemplate = generateAppConfig(vixt);
87
+ const pluginsTemplate = generatePlugins(vixt);
88
+ const code = `// Generated by Vixt
89
+ // This file transform to '${srcDir}/${mainFileName}'
90
+ // @ts-nocheck
91
+ ${appComponentTemplate}
92
+ ${cssTemplate}
93
+ ${appConfigTemplate}
94
+ ${pluginsTemplate}
95
+
96
+ import 'virtual:uno.css'
97
+ import { StrictMode, Suspense } from 'react'
98
+ import { createRoot } from 'react-dom/client'
99
+ import { BrowserRouter } from 'react-router'
100
+ import routes from '~react-pages'
101
+
102
+ const app = createRoot(document.getElementById('${options.rootId}')!)
103
+ app.render(
104
+ <BrowserRouter>
105
+ <App />
106
+ </BrowserRouter>,
107
+ )
108
+
109
+ usePlugins({ app, routes, appConfig })
110
+ `;
111
+ fs.outputFileSync(path.resolve(buildDir, mainFileName), code);
112
+ return code;
113
+ }
114
+
115
+ const name$1 = "vixt:app-react";
116
+ const defaults$1 = {
117
+ rootId: "app",
118
+ rootTag: "div",
119
+ baseURL: "/",
120
+ entryFile: "main.tsx",
121
+ css: [],
122
+ head: {
123
+ meta: [
124
+ { charset: "utf-8" },
125
+ { name: "viewport", content: "width=device-width, initial-scale=1" }
126
+ ]
127
+ }
128
+ };
129
+ const app = defineVixtModule({
130
+ meta: { name: name$1, configKey: "app" },
131
+ defaults: defaults$1,
132
+ setup(options, vixt) {
133
+ let indexHtmlCode;
134
+ let mainTsCode;
135
+ const mainTsPath = path.resolve(vixt.options.srcDir, "main.tsx");
136
+ return {
137
+ name: name$1,
138
+ configResolved() {
139
+ indexHtmlCode = generateIndexHtml(options, vixt);
140
+ mainTsCode = generateMainFile(options, vixt);
141
+ },
142
+ transformIndexHtml: {
143
+ order: "pre",
144
+ handler() {
145
+ return indexHtmlCode;
146
+ }
147
+ },
148
+ transform: {
149
+ order: "pre",
150
+ handler(_, id) {
151
+ if (id !== mainTsPath)
152
+ return;
153
+ return mainTsCode;
154
+ }
155
+ }
156
+ };
157
+ }
158
+ });
159
+
160
+ function componentsResolver(options = {}) {
161
+ const { dirs = ["src/components"] } = options;
162
+ const files = fg.sync(dirs.map((c) => `${c}/**/*.(t|j)sx`), {
163
+ ignore: ["node_modules"],
164
+ onlyFiles: true,
165
+ cwd: cwd(),
166
+ absolute: true
167
+ });
168
+ const imports = {};
169
+ files.forEach((componentPath) => {
170
+ let componentName = componentPath.replace(/\/index\.(t|j)sx$/, "").replace(/\.(t|j)sx$/, "");
171
+ for (const dir of dirs) {
172
+ componentName = componentName.replace(dir, "");
173
+ }
174
+ componentName = pascalCase(componentName.replace(/\//g, "-"));
175
+ imports[componentPath] = [["default", componentName]];
176
+ });
177
+ return imports;
178
+ }
179
+
180
+ const routeJSXReg = /^\s+(route)\s+/gm;
181
+ function parseJSX(code) {
182
+ return extractComments(code).slice(0, 1).filter((comment) => routeJSXReg.test(comment.value) && comment.value.includes(":") && comment.loc.start.line === 1);
183
+ }
184
+ function parseYamlComment(code, path) {
185
+ return code.reduce((memo, item) => {
186
+ const { value } = item;
187
+ const v = value.replace(routeJSXReg, "");
188
+ try {
189
+ const yamlResult = parse(v);
190
+ return {
191
+ ...memo,
192
+ ...yamlResult
193
+ };
194
+ } catch (err) {
195
+ throw new Error(`Invalid YAML format of comment in ${path}
196
+ ${err.message}`);
197
+ }
198
+ }, {});
199
+ }
200
+ function extendRoute(route) {
201
+ if (!route.element)
202
+ return;
203
+ const codePath = route.element.startsWith("/") ? route.element.slice(1) : route.element;
204
+ const code = fs.readFileSync(codePath, "utf-8");
205
+ const jsx = parseJSX(code);
206
+ const block = parseYamlComment(jsx, codePath);
207
+ return {
208
+ ...route,
209
+ ...block
210
+ };
211
+ }
212
+
213
+ const name = "vixt:preset-react";
214
+ const presetReact = defineVixtModule({
215
+ meta: { name },
216
+ async setup(_, vixt) {
217
+ const { components = [], constants = [], hooks = [], layouts = [], pages = [], stores = [], utils = [] } = resolveLayersDirs([...vixt._layers].reverse());
218
+ const { buildTypesDir, buildImportsDir } = vixt.options;
219
+ const defaultOptions = {
220
+ react: {},
221
+ pages: {
222
+ dirs: pages,
223
+ extendRoute
224
+ },
225
+ layouts: {
226
+ layoutsDirs: layouts,
227
+ pagesDirs: pages,
228
+ extensions: ["jsx", "tsx"],
229
+ importMode: () => "sync"
230
+ },
231
+ imports: {
232
+ imports: ["react", "react-router", "ahooks", componentsResolver({ dirs: components })],
233
+ dts: `${buildTypesDir}/auto-imports.d.ts`,
234
+ dirs: [constants, hooks, stores, utils, buildImportsDir].flat()
235
+ },
236
+ unocss: {}
237
+ };
238
+ const options = vixt.options = defu(vixt.options, defaultOptions);
239
+ const plugins = [
240
+ UnoCSS(options.unocss),
241
+ React(options.react),
242
+ Pages(options.pages),
243
+ Layouts(options.layouts),
244
+ AutoImport(options.imports)
245
+ ];
246
+ return plugins;
247
+ }
248
+ });
249
+
250
+ const defaults = {
251
+ modules: [app, presetReact],
252
+ typescript: {
253
+ tsConfig: {
254
+ compilerOptions: {
255
+ jsxImportSource: "react",
256
+ types: ["vite-plugin-pages/client-react"]
257
+ }
258
+ }
259
+ }
260
+ };
261
+ const index = createVixtPlugin({ defaults });
262
+
263
+ export { app, index as default, genarateAppComponent, generateMainFile, generateSetupLayouts, getAppComponentPath, presetReact };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@vixt/react",
3
3
  "type": "module",
4
- "version": "0.4.0",
4
+ "version": "0.4.2",
5
5
  "author": "SoulLyoko<https://github.com/SoulLyoko>",
6
6
  "license": "MIT",
7
7
  "homepage": "https://github.com/SoulLyoko/vixt#readme",
@@ -20,7 +20,7 @@
20
20
  "dependencies": {
21
21
  "@types/react": "^19.1.5",
22
22
  "@types/react-dom": "^19.1.5",
23
- "@vitejs/plugin-react": "^4.4.1",
23
+ "@vitejs/plugin-react": "^4.5.0",
24
24
  "ahooks": "^3.8.5",
25
25
  "extract-comments": "^1.1.0",
26
26
  "fast-glob": "^3.3.3",
@@ -33,7 +33,7 @@
33
33
  "vite-plugin-pages": "^0.33.0",
34
34
  "vite-plugin-vue-layouts": "^0.11.0",
35
35
  "yaml": "^2.8.0",
36
- "@vixt/core": "0.4.0"
36
+ "@vixt/core": "0.4.2"
37
37
  },
38
38
  "scripts": {
39
39
  "build": "unbuild",