fix: update lazyLoadComponent fn name to __tuono__internal__lazyLoadComponent (#222)

This commit is contained in:
Valerio Ageno
2024-12-15 09:22:19 +01:00
committed by GitHub
parent 4abc163a59
commit 14bea36356
22 changed files with 83 additions and 79 deletions
+6 -1
View File
@@ -1 +1,6 @@
pnpm-lock.yaml pnpm-lock.yaml
dist
.tuono
packages/lazy-fn-vite-plugin/tests/sources/*
+1 -1
View File
@@ -6,7 +6,7 @@
"type": "module", "type": "module",
"scripts": { "scripts": {
"lint": "eslint .", "lint": "eslint .",
"format": "prettier -u --write --ignore-unknown .", "format": "prettier --write --ignore-unknown .",
"format:check": "prettier --check --ignore-unknown .", "format:check": "prettier --check --ignore-unknown .",
"types": "tsc --noEmit" "types": "tsc --noEmit"
}, },
+1
View File
@@ -14,6 +14,7 @@ export default tseslint.config(
// #region package-specific // #region package-specific
'packages/fs-router-vite-plugin/tests/generator/**', 'packages/fs-router-vite-plugin/tests/generator/**',
'packages/lazy-fn-vite-plugin/tests/sources/**',
'packages/tuono/bin/**', 'packages/tuono/bin/**',
// #endregion package-specific // #endregion package-specific
@@ -1,2 +0,0 @@
dist
pnpm-lock.yaml
+2 -2
View File
@@ -7,8 +7,8 @@
"dev": "vite build --watch", "dev": "vite build --watch",
"build": "vite build", "build": "vite build",
"lint": "eslint .", "lint": "eslint .",
"format": "prettier -u --write --ignore-unknown '**/*'", "format": "prettier --write --ignore-unknown --ignore-path ../../.prettierignore .",
"format:check": "prettier --check --ignore-unknown '**/*'", "format:check": "prettier --check --ignore-unknown --ignore-path ../../.prettierignore .",
"types": "tsc --noEmit", "types": "tsc --noEmit",
"test:watch": "vitest", "test:watch": "vitest",
"test": "vitest run" "test": "vitest run"
@@ -1,3 +1,3 @@
export const TUONO_DYNAMIC_FN_ID = 'dynamic' export const TUONO_DYNAMIC_FN_ID = 'dynamic'
export const TUONO_LAZY_FN_ID = 'lazyLoadComponent' export const TUONO_LAZY_FN_ID = '__tuono__internal__lazyLoadComponent'
export const TUONO_MAIN_PACKAGE = 'tuono' export const TUONO_MAIN_PACKAGE = 'tuono'
+1 -1
View File
@@ -33,7 +33,7 @@ const RemoveTuonoLazyImport: PluginItem = {
/** /**
* [CLIENT build] * [CLIENT build]
* This plugin replace the `dynamic` function with the `lazyLoadComponent` one * This plugin replace the `dynamic` function with the `__tuono__internal__lazyLoadComponent` one
*/ */
const ReplaceTuonoLazyImport: PluginItem = { const ReplaceTuonoLazyImport: PluginItem = {
name: 'remove-tuono-lazy-import-plugin', name: 'remove-tuono-lazy-import-plugin',
@@ -0,0 +1,4 @@
import { createRoute } from 'tuono';
import { dynamic } from 'external-lib';
const IndexImport = dynamic(() => import('./../src/routes/index'));
const PokemonspokemonImport = dynamic(() => import('./../src/routes/pokemons/[pokemon]'));
@@ -0,0 +1,4 @@
import { createRoute } from 'tuono';
import { dynamic } from 'external-lib';
const IndexImport = dynamic(() => import('./../src/routes/index'));
const PokemonspokemonImport = dynamic(() => import('./../src/routes/pokemons/[pokemon]'));
@@ -0,0 +1,7 @@
import { createRoute } from 'tuono'
import { dynamic } from 'external-lib'
const IndexImport = dynamic(() => import('./../src/routes/index'))
const PokemonspokemonImport = dynamic(
() => import('./../src/routes/pokemons/[pokemon]'),
)
@@ -0,0 +1,3 @@
import { createRoute, __tuono__internal__lazyLoadComponent as dynamic } from 'tuono';
const IndexImport = dynamic(() => import('./../src/routes/index'));
const PokemonspokemonImport = dynamic(() => import('./../src/routes/pokemons/[pokemon]'));
@@ -0,0 +1,3 @@
import { createRoute } from 'tuono';
import IndexImport from "./../src/routes/index";
import PokemonspokemonImport from "./../src/routes/pokemons/[pokemon]";
@@ -0,0 +1,6 @@
import { createRoute, dynamic } from 'tuono'
const IndexImport = dynamic(() => import('./../src/routes/index'))
const PokemonspokemonImport = dynamic(
() => import('./../src/routes/pokemons/[pokemon]'),
)
@@ -1,27 +1,11 @@
import fs from 'node:fs/promises'
import path from 'node:path'
import { it, expect, describe } from 'vitest' import { it, expect, describe } from 'vitest'
import type { Plugin } from 'vite' import type { Plugin } from 'vite'
import { LazyLoadingPlugin } from '../src' import { LazyLoadingPlugin } from '../src'
const SOURCE_CODE = `
import { createRoute, dynamic } from 'tuono'
const IndexImport = dynamic(() => import('./../src/routes/index'))
const PokemonspokemonImport = dynamic(
() => import('./../src/routes/pokemons/[pokemon]'),
)
`
const NON_DYNAMIC_SOURCE = `
import { createRoute } from 'tuono'
import {dynamic} from 'external-lib'
const IndexImport = dynamic(() => import('./../src/routes/index'))
const PokemonspokemonImport = dynamic(
() => import('./../src/routes/pokemons/[pokemon]'),
)
`
type ViteTransformHandler = Exclude< type ViteTransformHandler = Exclude<
Plugin['transform'], Plugin['transform'],
// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type // eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
@@ -33,45 +17,35 @@ function getTransform(): (...args: Parameters<ViteTransformHandler>) => string {
return LazyLoadingPlugin().transform as never return LazyLoadingPlugin().transform as never
} }
describe('Transpile tuono source', () => { describe('"dynamic" fn', async () => {
it('Into the client bundle', () => { const folderNames = await fs.readdir(`${process.cwd()}/tests/sources`)
const pluginTransform = getTransform()
const bundle = pluginTransform(SOURCE_CODE, 'id')
expect(bundle)
.toBe(`import { createRoute, lazyLoadComponent as dynamic } from 'tuono';
const IndexImport = dynamic(() => import('./../src/routes/index'));
const PokemonspokemonImport = dynamic(() => import('./../src/routes/pokemons/[pokemon]'));`)
})
it('Into the server bundle', () => { it.each(folderNames)(
const pluginTransform = getTransform() 'should correctly build the "%s" dynamic fn',
const bundle = pluginTransform(SOURCE_CODE, 'id', { async (folderName) => {
ssr: true, const testDirPath = `${process.cwd()}/tests/sources/${folderName}`
})
expect(bundle).toBe(`import { createRoute } from 'tuono'; const source = await fs.readFile(
import IndexImport from "./../src/routes/index"; path.join(testDirPath, 'source.tsx'),
import PokemonspokemonImport from "./../src/routes/pokemons/[pokemon]";`) 'utf-8',
}) )
})
const pluginTransform = getTransform()
describe('Non tuono dynamic function', () => { const clientBundle = pluginTransform(source, 'id')
it('Into the client bundle', () => { const serverBundle = pluginTransform(source, 'id', { ssr: true })
const pluginTransform = getTransform()
const bundle = pluginTransform(NON_DYNAMIC_SOURCE, 'id') const expectedClientSrc = `${testDirPath}/client.expected.tsx`
expect(bundle).toBe(`import { createRoute } from 'tuono'; const expectedServerSrc = `${testDirPath}/server.expected.tsx`
import { dynamic } from 'external-lib';
const IndexImport = dynamic(() => import('./../src/routes/index')); await expect(clientBundle).toMatchFileSnapshot(
const PokemonspokemonImport = dynamic(() => import('./../src/routes/pokemons/[pokemon]'));`) expectedClientSrc,
}) `${testDirPath} client build should be equal to ${expectedClientSrc}`,
)
it('Into the server bundle', () => {
const pluginTransform = getTransform() await expect(serverBundle).toMatchFileSnapshot(
const bundle = pluginTransform(NON_DYNAMIC_SOURCE, 'id', { expectedServerSrc,
ssr: true, `${testDirPath} server build should be equal to ${expectedServerSrc}`,
}) )
expect(bundle).toBe(`import { createRoute } from 'tuono'; },
import { dynamic } from 'external-lib'; )
const IndexImport = dynamic(() => import('./../src/routes/index'));
const PokemonspokemonImport = dynamic(() => import('./../src/routes/pokemons/[pokemon]'));`)
})
}) })
+2 -1
View File
@@ -1,4 +1,5 @@
{ {
"extends": "../../tsconfig.json", "extends": "../../tsconfig.json",
"include": ["src", "tests", "vite.config.ts"] "include": ["src", "tests", "vite.config.ts"],
"exclude": ["tests/sources"]
} }
-2
View File
@@ -1,2 +0,0 @@
dist
pnpm-lock.yaml
+2 -2
View File
@@ -7,8 +7,8 @@
"dev": "vite build --watch", "dev": "vite build --watch",
"build": "vite build", "build": "vite build",
"lint": "eslint .", "lint": "eslint .",
"format": "prettier -u --write --ignore-unknown '**/*'", "format": "prettier --write --ignore-unknown --ignore-path ../../.prettierignore .",
"format:check": "prettier --check --ignore-unknown '**/*'", "format:check": "prettier --check --ignore-unknown --ignore-path ../../.prettierignore .",
"types": "tsc --noEmit", "types": "tsc --noEmit",
"test:watch": "vitest", "test:watch": "vitest",
"test": "vitest run" "test": "vitest run"
+3 -1
View File
@@ -41,7 +41,9 @@ export const dynamic = (importFn: ImportFn): React.JSX.Element => {
return <></> return <></>
} }
export const lazyLoadComponent = (factory: ImportFn): RouteComponent => { export const __tuono__internal__lazyLoadComponent = (
factory: ImportFn,
): RouteComponent => {
let LoadedComponent: ComponentType<any> | undefined let LoadedComponent: ComponentType<any> | undefined
const LazyComponent = React.lazy(factory) as unknown as RouteComponent const LazyComponent = React.lazy(factory) as unknown as RouteComponent
+1 -1
View File
@@ -2,5 +2,5 @@ export { RouterProvider } from './components/RouterProvider'
export { default as Link } from './components/Link' export { default as Link } from './components/Link'
export { createRouter } from './router' export { createRouter } from './router'
export { createRoute, createRootRoute } from './route' export { createRoute, createRootRoute } from './route'
export { dynamic, lazyLoadComponent } from './dynamic' export { dynamic, __tuono__internal__lazyLoadComponent } from './dynamic'
export { useRouter } from './hooks/useRouter' export { useRouter } from './hooks/useRouter'
-2
View File
@@ -1,2 +0,0 @@
dist
pnpm-lock.yaml
+2 -2
View File
@@ -7,8 +7,8 @@
"dev": "vite build --watch", "dev": "vite build --watch",
"build": "vite build", "build": "vite build",
"lint": "eslint .", "lint": "eslint .",
"format": "prettier -u --write --ignore-unknown '**/*'", "format": "prettier --write --ignore-unknown --ignore-path ../../.prettierignore .",
"format:check": "prettier --check --ignore-unknown '**/*'", "format:check": "prettier --check --ignore-unknown --ignore-path ../../.prettierignore .",
"types": "tsc --noEmit", "types": "tsc --noEmit",
"test:watch": "vitest", "test:watch": "vitest",
"test": "vitest run" "test": "vitest run"
+1 -1
View File
@@ -7,8 +7,8 @@ export {
Link, Link,
RouterProvider, RouterProvider,
dynamic, dynamic,
lazyLoadComponent,
useRouter, useRouter,
__tuono__internal__lazyLoadComponent,
} from 'tuono-router' } from 'tuono-router'
export type { TuonoProps } from './types' export type { TuonoProps } from './types'