@vixt/react 0.7.0 → 0.7.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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@vixt/react",
3
3
  "type": "module",
4
- "version": "0.7.0",
4
+ "version": "0.7.1",
5
5
  "author": "SoulLyoko<https://github.com/SoulLyoko>",
6
6
  "license": "MIT",
7
7
  "homepage": "https://soullyoko.github.io/vixt/",
@@ -20,6 +20,9 @@
20
20
  },
21
21
  "main": "./dist/index.mjs",
22
22
  "types": "./dist/types/index.d.ts",
23
+ "files": [
24
+ "dist"
25
+ ],
23
26
  "dependencies": {
24
27
  "@types/react": "^19.2.7",
25
28
  "@types/react-dom": "^19.2.3",
@@ -36,6 +39,6 @@
36
39
  "vite-plugin-pages": "^0.33.2",
37
40
  "vite-plugin-vue-layouts": "^0.11.0",
38
41
  "yaml": "^2.8.2",
39
- "@vixt/core": "0.7.0"
42
+ "@vixt/core": "0.7.1"
40
43
  }
41
44
  }
@@ -1,17 +0,0 @@
1
- // @ts-nocheck
2
- import routes from '~react-pages'
3
- import { BrowserRouter, useRoutes } from 'react-router'
4
- import appConfig from 'virtual:vixt:app-config'
5
- import { setupLayouts } from 'virtual:vixt:setup-layouts'
6
-
7
- function App() {
8
- const routesWithLayouts = setupLayouts(routes)
9
- const Routes = () => useRoutes(routesWithLayouts)
10
- return (
11
- <BrowserRouter basename={appConfig.baseURL}>
12
- <Routes />
13
- </BrowserRouter>
14
- )
15
- }
16
-
17
- export default App
@@ -1,31 +0,0 @@
1
- import 'virtual:vixt:css'
2
- import 'virtual:uno.css'
3
- import { applyPlugins, createVixtApp } from '@vixt/core/client'
4
- import { createElement } from 'react'
5
- import { createRoot } from 'react-dom/client'
6
- // @ts-expect-error virtual file
7
- import appConfig from 'virtual:vixt:app-config'
8
- // @ts-expect-error virtual file
9
- import plugins from 'virtual:vixt:plugins'
10
- // @ts-expect-error virtual file
11
- import RootComponent from 'virtual:vixt:root-component.tsx'
12
-
13
- function entry() {
14
- const app = createRoot(document.getElementById(appConfig.rootId || 'app')!)
15
- const vixt = createVixtApp({ app, appConfig })
16
-
17
- try {
18
- applyPlugins(vixt, plugins)
19
- }
20
- catch (err) {
21
- console.error(err)
22
- }
23
-
24
- app.render(createElement(RootComponent))
25
-
26
- return app
27
- }
28
-
29
- entry()
30
-
31
- export default entry
package/src/index.ts DELETED
@@ -1,31 +0,0 @@
1
- import { createVixtPlugin } from '@vixt/core'
2
- import fs from 'fs-extra'
3
- import { resolvePathSync } from 'mlly'
4
-
5
- import presetReact from './modules/preset-react'
6
- import virtualRootComponent from './modules/virtual-root-component'
7
- import virtualSetupLayouts from './modules/virtual-setup-layouts'
8
-
9
- export default createVixtPlugin({
10
- defaults: {
11
- modules: [presetReact, virtualRootComponent, virtualSetupLayouts],
12
- app: {
13
- entryFile: 'main.tsx',
14
- entryCode: fs.readFileSync(resolvePathSync('@vixt/react/client/entry'), 'utf-8'),
15
- head: {
16
- meta: [
17
- { charset: 'utf-8' },
18
- { name: 'viewport', content: 'width=device-width, initial-scale=1.0' },
19
- ],
20
- },
21
- },
22
- typescript: {
23
- tsConfig: {
24
- compilerOptions: {
25
- jsxImportSource: 'react',
26
- types: ['@vixt/react/types', 'vite-plugin-pages/client-react'],
27
- },
28
- },
29
- },
30
- },
31
- })
@@ -1,33 +0,0 @@
1
- import type { ImportsMap } from 'unplugin-auto-import/types'
2
-
3
- import { cwd } from 'node:process'
4
-
5
- import fg from 'fast-glob'
6
- import { pascalCase } from 'unplugin-vue-components'
7
-
8
- export interface ComponentResolverOptions {
9
- dirs?: string[]
10
- }
11
-
12
- export function componentsResolver(options: ComponentResolverOptions = {}) {
13
- const { dirs = ['src/components'] } = options
14
-
15
- const files = fg.sync(dirs.map(c => `${c}/**/*.(t|j)sx`), {
16
- ignore: ['node_modules'],
17
- onlyFiles: true,
18
- cwd: cwd(),
19
- absolute: true,
20
- })
21
-
22
- const imports: ImportsMap = {}
23
- files.forEach((componentPath) => {
24
- let componentName = componentPath.replace(/\/index\.(t|j)sx$/, '').replace(/\.(t|j)sx$/, '')
25
- for (const dir of dirs) {
26
- componentName = componentName.replace(dir, '')
27
- }
28
- componentName = pascalCase(componentName.replace(/\//g, '-'))
29
- imports[componentPath] = [['default', componentName]]
30
- })
31
-
32
- return imports
33
- }
@@ -1,55 +0,0 @@
1
- import type { VixtOptions } from '@vixt/core'
2
-
3
- import React from '@vitejs/plugin-react'
4
- import { defineVixtModule, resolveLayersDirs, VixtClientAutoImports } from '@vixt/core'
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
-
11
- import { componentsResolver } from './components'
12
- import { extendRoute } from './route-block'
13
-
14
- const name = 'vixt:preset-react'
15
- export default defineVixtModule<VixtOptions>({
16
- meta: { name },
17
- setup(_, vixt) {
18
- const { components = [], constants = [], hooks = [], layouts = [], pages = [], stores = [], utils = [] } = resolveLayersDirs([...vixt._layers].reverse())
19
- const { buildTypesDir } = vixt.options
20
-
21
- const defaultOptions: VixtOptions = {
22
- react: {},
23
- pages: {
24
- dirs: pages,
25
- extendRoute,
26
- },
27
- layouts: {
28
- layoutsDirs: layouts,
29
- pagesDirs: pages,
30
- extensions: ['jsx', 'tsx'],
31
- importMode: () => 'sync',
32
- },
33
- imports: {
34
- imports: ['react', 'react-router', 'ahooks', componentsResolver({ dirs: components }), VixtClientAutoImports],
35
- dts: `${buildTypesDir}/auto-imports.d.ts`,
36
- dirs: [constants, hooks, stores, utils].flat(),
37
- exclude: [],
38
- include: [],
39
- },
40
- unocss: {},
41
- }
42
-
43
- const options = vixt.options = defu(vixt.options, defaultOptions)
44
-
45
- const plugins = [
46
- UnoCSS(options.unocss),
47
- React(options.react),
48
- Pages(options.pages),
49
- Layouts(options.layouts),
50
- AutoImport(options.imports!),
51
- ]
52
-
53
- return plugins
54
- },
55
- })
@@ -1,52 +0,0 @@
1
- /**
2
- * Support route block in react
3
- * @link https://github.com/hannoeru/vite-plugin-pages?tab=readme-ov-file#jsxtsx-yaml-format-comments-for-route-datain-vue
4
- * @link https://github.com/hannoeru/vite-plugin-pages/blob/main/src/customBlock.ts
5
- */
6
- import type { CustomBlock, ParsedJSX, ReactRoute } from 'vite-plugin-pages'
7
-
8
- // @ts-expect-error
9
- import extractComments from 'extract-comments'
10
- import fs from 'fs-extra'
11
- import { parse as YAMLParser } from 'yaml'
12
-
13
- const routeJSXReg = /^\s+(route)\s+/gm
14
-
15
- function parseJSX(code: string): ParsedJSX[] {
16
- return extractComments(code).slice(0, 1).filter((comment: ParsedJSX) => routeJSXReg.test(comment.value) && comment.value.includes(':') && comment.loc.start.line === 1)
17
- }
18
-
19
- function parseYamlComment(code: ParsedJSX[], path: string): CustomBlock {
20
- return code.reduce((memo, item) => {
21
- const { value } = item
22
- const v = value.replace(routeJSXReg, '')
23
- try {
24
- const yamlResult = YAMLParser(v)
25
-
26
- return {
27
- ...memo,
28
- ...yamlResult,
29
- }
30
- }
31
- catch (err: any) {
32
- throw new Error(`Invalid YAML format of comment in ${path}\n${err.message}`)
33
- }
34
- }, {})
35
- }
36
-
37
- function getRouteBlock(path: string) {
38
- const code = fs.readFileSync(path, 'utf-8')
39
- const parsedJSX = parseJSX(code)
40
- const block = parseYamlComment(parsedJSX, path)
41
- return block
42
- }
43
-
44
- export function extendRoute(route: ReactRoute) {
45
- if (!route.element)
46
- return
47
-
48
- const codePath = route.element.startsWith('/') ? route.element.slice(1) : route.element
49
- const block = getRouteBlock(codePath)
50
-
51
- return { ...route, ...block }
52
- }
@@ -1,60 +0,0 @@
1
- import type { Vixt } from '@vixt/core'
2
-
3
- import { defineVixtModule } from '@vixt/core'
4
- import fs from 'fs-extra'
5
- import { resolvePathSync } from 'mlly'
6
- import path from 'pathe'
7
- import { parseAst } from 'vite'
8
-
9
- function resolveRootComponent(vixt: Vixt) {
10
- for (const layer of vixt._layers) {
11
- const layerRootComponentPath = path.resolve(layer.config!.srcDir!, 'App.tsx')
12
- const isExists = fs.existsSync(layerRootComponentPath)
13
- const layerRootComponentCode = (isExists && fs.readFileSync(layerRootComponentPath, 'utf-8')) || ''
14
- if (!isEmptyCode(layerRootComponentCode)) {
15
- return {
16
- path: layerRootComponentPath,
17
- code: layerRootComponentCode,
18
- }
19
- }
20
- }
21
-
22
- const defaultRootComponentPath = resolvePathSync('@vixt/react/client/App')
23
- return {
24
- path: defaultRootComponentPath,
25
- code: fs.readFileSync(defaultRootComponentPath, 'utf-8'),
26
- }
27
- }
28
-
29
- export function isEmptyCode(code?: string) {
30
- if (!code)
31
- return true
32
- try {
33
- return !parseAst(code, { jsx: true }).body.length
34
- }
35
- catch {
36
- return false
37
- }
38
- }
39
-
40
- const name = 'virtual:vixt:root-component.tsx'
41
- export default defineVixtModule({
42
- meta: { name },
43
- setup(_, vixt) {
44
- return {
45
- name,
46
- resolveId(id) {
47
- if (id === name) {
48
- return name
49
- }
50
- },
51
- load(id) {
52
- if (id === name) {
53
- const { path, code } = resolveRootComponent(vixt)
54
- this.addWatchFile(path)
55
- return code
56
- }
57
- },
58
- }
59
- },
60
- })
@@ -1,42 +0,0 @@
1
- import { defineVixtModule } from '@vixt/core'
2
-
3
- const name = 'virtual:vixt:setup-layouts'
4
- const virtualModuleId = name
5
- const resolvedVirtualModuleId = `\0${virtualModuleId}`
6
- export default defineVixtModule({
7
- meta: { name },
8
- setup(_, vixt) {
9
- return {
10
- name,
11
- resolveId(id) {
12
- if (id === virtualModuleId) {
13
- return resolvedVirtualModuleId
14
- }
15
- },
16
- load(id) {
17
- if (id === resolvedVirtualModuleId) {
18
- const { defaultLayout = 'default' } = vixt.options.layouts ?? {}
19
- const setupLayoutCode = `import { createElement } from 'react'
20
- import { layouts } from 'virtual:generated-layouts'
21
-
22
- export function setupLayouts(routes) {
23
- return routes.map(route => {
24
- if (route.children?.length > 0) {
25
- route.children = setupLayouts(route.children)
26
- }
27
- if(route.element && route.meta?.layout !== false) {
28
- return {
29
- path: route.path,
30
- element: createElement(layouts[route.meta?.layout ?? '${defaultLayout}']),
31
- children: [{ ...route, path: '' }]
32
- }
33
- }
34
- return route
35
- });
36
- }`
37
- return setupLayoutCode
38
- }
39
- },
40
- }
41
- },
42
- })
@@ -1,12 +0,0 @@
1
- import type { Root } from 'react-dom/client'
2
- import type { RouteObject } from 'react-router'
3
-
4
- declare module '@vixt/core/client' {
5
- interface VixtAppConfig { }
6
-
7
- interface VixtApp {
8
- app: Root
9
- routes: RouteObject[]
10
- appConfig: VixtAppConfig
11
- }
12
- }
@@ -1,2 +0,0 @@
1
- import './client.d'
2
- import './node.d'
@@ -1,20 +0,0 @@
1
- import type React from '@vitejs/plugin-react'
2
- import type { ExtractPluginOptions } from '@vixt/core'
3
- import type UnoCSS from 'unocss/vite'
4
- import type AutoImport from 'unplugin-auto-import/vite'
5
- import type Pages from 'vite-plugin-pages'
6
- import type Layouts from 'vite-plugin-vue-layouts'
7
-
8
- declare module '@vixt/core' {
9
- interface VixtOptions {
10
- react?: ExtractPluginOptions<typeof React>
11
- /** https://github.com/hannoeru/vite-plugin-pages */
12
- pages?: ExtractPluginOptions<typeof Pages>
13
- /** https://github.com/JohnCampionJr/vite-plugin-vue-layouts */
14
- layouts?: ExtractPluginOptions<typeof Layouts>
15
- /** https://github.com/unplugin/unplugin-auto-import */
16
- imports?: ExtractPluginOptions<typeof AutoImport>
17
- /** https://github.com/unocss/unocss */
18
- unocss?: ExtractPluginOptions<typeof UnoCSS>
19
- }
20
- }
package/tsdown.config.ts DELETED
@@ -1,17 +0,0 @@
1
- import { defineConfig } from 'tsdown'
2
-
3
- export default defineConfig([
4
- {
5
- entry: 'src/index.ts',
6
- platform: 'node',
7
- copy: ['src/types'],
8
- external: ['vite'],
9
- },
10
- {
11
- entry: ['src/client/**/*', '!src/client/App.tsx'],
12
- platform: 'browser',
13
- outDir: 'dist/client',
14
- external: [/virtual:/],
15
- copy: ['src/client/App.tsx'],
16
- },
17
- ])