Skip to content

Commit

Permalink
refactor(router-vite-plugin): proxy for re-exporting from `@tanstack/…
Browse files Browse the repository at this point in the history
…router-plugin/vite` (#1785)

* chore: this route-tree has not been generated

* chore: remove tests

* refactor(router-plugin): export configSchema

* refactor(start): use the exports from router-plugin

* refactor(router-vite-plugin): just a proxy doing exports

* fix(router-plugin): do not perform code-splitting unless perms are given
  • Loading branch information
SeanCassiere authored Jun 19, 2024
1 parent 38b7488 commit 05d1a1d
Show file tree
Hide file tree
Showing 38 changed files with 96 additions and 2,579 deletions.
57 changes: 57 additions & 0 deletions examples/react/start-trellaux/app/routeTree.gen.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/* prettier-ignore-start */

/* eslint-disable */

// @ts-nocheck

// noinspection JSUnusedGlobalSymbols

// This file is auto-generated by TanStack Router

// Import Routes

import { Route as rootRoute } from './routes/__root'
import { Route as IndexImport } from './routes/index'

// Create/Update Routes

const IndexRoute = IndexImport.update({
path: '/',
getParentRoute: () => rootRoute,
} as any)

// Populate the FileRoutesByPath interface

declare module '@tanstack/react-router' {
interface FileRoutesByPath {
'/': {
id: '/'
path: '/'
fullPath: '/'
preLoaderRoute: typeof IndexImport
parentRoute: typeof rootRoute
}
}
}

// Create and export the route tree

export const routeTree = rootRoute.addChildren({ IndexRoute })

/* prettier-ignore-end */

/* ROUTE_MANIFEST_START
{
"routes": {
"__root__": {
"filePath": "__root.tsx",
"children": [
"/"
]
},
"/": {
"filePath": "index.tsx"
}
}
}
ROUTE_MANIFEST_END */
15 changes: 10 additions & 5 deletions packages/router-plugin/src/code-splitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { compileFile, splitFile } from './compilers'
import { getConfig } from './config'
import { splitPrefix } from './constants'

import type { PluginOptions } from './config'
import type { Config } from './config'
import type { UnpluginFactory } from 'unplugin'

function capitalizeFirst(str: string): string {
Expand Down Expand Up @@ -45,13 +45,14 @@ plugins: [
}
}

export const unpluginFactory: UnpluginFactory<
Partial<PluginOptions> | undefined
> = (options = {}, { framework }) => {
export const unpluginFactory: UnpluginFactory<Partial<Config> | undefined> = (
options = {},
{ framework },
) => {
const debug = Boolean(process.env.TSR_VITE_DEBUG)

let ROOT: string = process.cwd()
let userConfig = options as PluginOptions
let userConfig = options as Config

const handleSplittingFile = async (code: string, id: string) => {
const compiledAst = compileAst({
Expand Down Expand Up @@ -127,6 +128,10 @@ export const unpluginFactory: UnpluginFactory<
return null
},
async transform(code, id) {
if (!userConfig.experimental?.enableCodeSplitting) {
return null
}

const url = pathToFileURL(id)
url.searchParams.delete('v')
id = fileURLToPath(url).replace(/\\/g, '/')
Expand Down
6 changes: 3 additions & 3 deletions packages/router-plugin/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
getConfig as getGeneratorConfig,
} from '@tanstack/router-generator'

const configSchema = generatorConfigSchema.extend({
export const configSchema = generatorConfigSchema.extend({
enableRouteGeneration: z.boolean().optional(),
experimental: z
.object({
Expand All @@ -14,12 +14,12 @@ const configSchema = generatorConfigSchema.extend({
})

export const getConfig = async (
inlineConfig: Partial<PluginOptions>,
inlineConfig: Partial<Config>,
root: string,
) => {
const config = await getGeneratorConfig(inlineConfig, root)

return configSchema.parse({ ...config, ...inlineConfig })
}

export type PluginOptions = z.infer<typeof configSchema>
export type Config = z.infer<typeof configSchema>
2 changes: 2 additions & 0 deletions packages/router-plugin/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
export { unpluginRouterCodeSplitter } from './code-splitter'
export { unpluginRouterGenerator } from './router-generator'
export { configSchema } from './config'
export type { Config } from './config'
6 changes: 3 additions & 3 deletions packages/router-plugin/src/router-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { generator } from '@tanstack/router-generator'

import { getConfig } from './config'
import { CONFIG_FILE_NAME } from './constants'
import type { PluginOptions } from './config'
import type { Config } from './config'
import type { UnpluginFactory } from 'unplugin'

let lock = false
Expand All @@ -13,11 +13,11 @@ const setLock = (bool: boolean) => {
lock = bool
}

const unpluginFactory: UnpluginFactory<Partial<PluginOptions> | undefined> = (
const unpluginFactory: UnpluginFactory<Partial<Config> | undefined> = (
options = {},
) => {
let ROOT: string = process.cwd()
let userConfig = options as PluginOptions
let userConfig = options as Config

const generate = async () => {
if (checkLock()) {
Expand Down
19 changes: 12 additions & 7 deletions packages/router-plugin/src/vite.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
import { unpluginRouterCodeSplitter } from './code-splitter'
import { configSchema } from './config'
import { unpluginRouterGenerator } from './router-generator'

import type { PluginOptions } from './config'
import type { Config } from './config'
import type { VitePlugin } from 'unplugin'

export const TanStackRouterGeneratorVite = unpluginRouterGenerator.vite
export const TanStackRouterCodeSplitterVite = unpluginRouterCodeSplitter.vite
const TanStackRouterGeneratorVite = unpluginRouterGenerator.vite
const TanStackRouterCodeSplitterVite = unpluginRouterCodeSplitter.vite

export function TanStackRouterVite(
inlineConfig?: Partial<PluginOptions>,
): Array<VitePlugin> {
function TanStackRouterVite(inlineConfig?: Partial<Config>): Array<VitePlugin> {
return [
TanStackRouterGeneratorVite(inlineConfig) as VitePlugin,
TanStackRouterCodeSplitterVite(inlineConfig) as VitePlugin,
]
}

export type Config = PluginOptions
export {
configSchema,
TanStackRouterGeneratorVite,
TanStackRouterCodeSplitterVite,
TanStackRouterVite,
}
export type { Config }
20 changes: 2 additions & 18 deletions packages/router-vite-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"scripts": {
"clean": "rimraf ./dist && rimraf ./coverage",
"test": "pnpm test:eslint && pnpm test:types && pnpm test:build && pnpm test:unit",
"test:unit": "vitest --typecheck --watch=false",
"test:unit": "echo 'No unit tests are needed here since we do them in @tanstack/router-plugin!'",
"test:eslint": "eslint --ext .ts,.tsx ./src",
"test:types": "tsc --noEmit",
"test:build": "publint --strict",
Expand All @@ -58,22 +58,6 @@
"src/**"
],
"dependencies": {
"@babel/core": "^7.23.7",
"@babel/generator": "^7.23.6",
"@babel/plugin-syntax-jsx": "^7.24.1",
"@babel/plugin-syntax-typescript": "^7.24.1",
"@babel/plugin-transform-react-jsx": "^7.23.4",
"@babel/plugin-transform-typescript": "^7.24.1",
"@babel/template": "^7.24.0",
"@babel/traverse": "^7.24.1",
"@babel/types": "^7.24.0",
"@tanstack/router-generator": "workspace:*",
"@tanstack/router-plugin": "workspace:^",
"@types/babel__core": "^7.20.5",
"@types/babel__generator": "^7.6.8",
"@types/babel__template": "^7.4.4",
"@types/babel__traverse": "^7.20.5",
"@vitejs/plugin-react": "^4.2.1",
"zod": "^3.22.4"
"@tanstack/router-plugin": "workspace:^"
}
}
Loading

0 comments on commit 05d1a1d

Please sign in to comment.