@vixt/react 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,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 fg from 'fast-glob';
11
+ import { pascalCase } from 'unplugin-vue-components';
12
+ import extractComments from 'extract-comments';
13
+ import { parse } from 'yaml';
14
+
15
+ function getAppComponentPath(vixt) {
16
+ for (const layer of vixt._layers) {
17
+ const appComponentPath = path.resolve(layer.config.srcDir, "App.tsx");
18
+ if (fs.existsSync(appComponentPath))
19
+ return appComponentPath;
20
+ }
21
+ }
22
+ function generateSetupLayouts(vixt) {
23
+ const { buildImportsDir } = vixt.options;
24
+ const { defaultLayout = "default" } = vixt.options.layouts ?? {};
25
+ fs.outputFileSync(path.resolve(buildImportsDir, `layouts.ts`), `// Generated by Vixt
26
+ // @ts-nocheck
27
+ import { createElement } from 'react'
28
+ import { layouts } from 'virtual:generated-layouts'
29
+
30
+ export function setupLayouts(routes) {
31
+ return routes.map(route => {
32
+ if (route.children?.length > 0) {
33
+ route.children = setupLayouts(route.children)
34
+ }
35
+ if(route.element && route.meta?.layout !== false) {
36
+ return {
37
+ path: route.path,
38
+ element: createElement(layouts[route.meta?.layout ?? '${defaultLayout}']),
39
+ children: [{ ...route, path: '' }]
40
+ }
41
+ }
42
+ return route
43
+ });
44
+ }
45
+ `);
46
+ }
47
+ function genarateAppComponent(vixt) {
48
+ generateSetupLayouts(vixt);
49
+ const { buildDir } = vixt.options;
50
+ const defaultAppComponentPath = path.resolve(buildDir, `App.tsx`);
51
+ fs.outputFileSync(defaultAppComponentPath, `// Generated by Vixt
52
+ // @ts-nocheck
53
+ import { StrictMode } from 'react'
54
+ import { useRoutes } from 'react-router'
55
+ import generatedRoutes from '~react-pages'
56
+
57
+ const routes = setupLayouts(generatedRoutes)
58
+
59
+ function App() {
60
+ return (
61
+ <StrictMode>
62
+ {useRoutes(routes)}
63
+ </StrictMode>
64
+ )
65
+ }
66
+
67
+ export default App
68
+ `);
69
+ const appComponentPath = getAppComponentPath(vixt) || defaultAppComponentPath;
70
+ const appComponentTemplate = `import App from '${appComponentPath}'`;
71
+ return appComponentTemplate;
72
+ }
73
+
74
+ function generateMainFile(options, vixt) {
75
+ const { buildDir, srcDir } = vixt.options;
76
+ const mainFileName = "main.tsx";
77
+ const mainTsPath = path.resolve(srcDir, mainFileName);
78
+ if (!fs.existsSync(mainTsPath))
79
+ fs.outputFileSync(mainTsPath, `// Generated by Vixt
80
+ // This file transform from '${path.basename(buildDir)}/${mainFileName}'
81
+ `);
82
+ generateClient(vixt);
83
+ const appComponentTemplate = genarateAppComponent(vixt);
84
+ const cssTemplate = generateCss(options);
85
+ const appConfigTemplate = generateAppConfig(vixt);
86
+ const pluginsTemplate = generatePlugins(vixt);
87
+ const code = `// Generated by Vixt
88
+ // This file transform to '${srcDir}/${mainFileName}'
89
+ // @ts-nocheck
90
+ ${appComponentTemplate}
91
+ ${cssTemplate}
92
+ ${appConfigTemplate}
93
+ ${pluginsTemplate}
94
+
95
+ import 'virtual:uno.css'
96
+ import { StrictMode, Suspense } from 'react'
97
+ import { createRoot } from 'react-dom/client'
98
+ import { BrowserRouter } from 'react-router'
99
+ import routes from '~react-pages'
100
+
101
+ const app = createRoot(document.getElementById('${options.rootId}')!)
102
+ app.render(
103
+ <BrowserRouter>
104
+ <App />
105
+ </BrowserRouter>,
106
+ )
107
+
108
+ usePlugins({ app, routes, appConfig })
109
+ `;
110
+ fs.outputFileSync(path.resolve(buildDir, mainFileName), code);
111
+ return code;
112
+ }
113
+
114
+ const name$1 = "vixt:app-react";
115
+ const defaults$1 = {
116
+ rootId: "app",
117
+ rootTag: "div",
118
+ baseURL: "/",
119
+ entryFile: "main.tsx",
120
+ css: [],
121
+ head: {
122
+ meta: [
123
+ { charset: "utf-8" },
124
+ { name: "viewport", content: "width=device-width, initial-scale=1" }
125
+ ]
126
+ }
127
+ };
128
+ const app = defineVixtModule({
129
+ meta: { name: name$1, configKey: "app" },
130
+ defaults: defaults$1,
131
+ setup(options, vixt) {
132
+ let indexHtmlCode;
133
+ let mainTsCode;
134
+ const mainTsPath = path.resolve(vixt.options.srcDir, "main.tsx");
135
+ return {
136
+ name: name$1,
137
+ configResolved() {
138
+ indexHtmlCode = generateIndexHtml(options, vixt);
139
+ mainTsCode = generateMainFile(options, vixt);
140
+ },
141
+ transformIndexHtml: {
142
+ order: "pre",
143
+ handler() {
144
+ return indexHtmlCode;
145
+ }
146
+ },
147
+ transform: {
148
+ order: "pre",
149
+ handler(_, id) {
150
+ if (id !== mainTsPath)
151
+ return;
152
+ return mainTsCode;
153
+ }
154
+ }
155
+ };
156
+ }
157
+ });
158
+
159
+ function componentsResolver(vixt) {
160
+ const { components = [] } = resolveLayersDirs([...vixt._layers].reverse());
161
+ const root = vixt.options.rootDir;
162
+ const files = fg.sync(components.map((c) => `${c}/**/*.(t|j)sx`), {
163
+ ignore: ["node_modules"],
164
+ onlyFiles: true,
165
+ cwd: root,
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 components) {
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 { 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(vixt)],
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.1",
5
5
  "author": "SoulLyoko<https://github.com/SoulLyoko>",
6
6
  "license": "MIT",
7
7
  "homepage": "https://github.com/SoulLyoko/vixt#readme",
@@ -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.1"
37
37
  },
38
38
  "scripts": {
39
39
  "build": "unbuild",