2024-12-15 09:22:19 +01:00
|
|
|
import fs from 'node:fs/promises'
|
2024-12-16 08:03:37 +01:00
|
|
|
import os from 'node:os'
|
2024-12-15 09:22:19 +01:00
|
|
|
|
2024-06-20 19:40:14 +02:00
|
|
|
import { it, expect, describe } from 'vitest'
|
2024-12-01 10:07:35 +01:00
|
|
|
import type { Plugin } from 'vite'
|
|
|
|
|
|
2024-06-20 19:40:14 +02:00
|
|
|
import { LazyLoadingPlugin } from '../src'
|
|
|
|
|
|
2024-12-01 10:07:35 +01:00
|
|
|
type ViteTransformHandler = Exclude<
|
|
|
|
|
Plugin['transform'],
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
|
|
|
|
|
Function | undefined
|
|
|
|
|
>['handler']
|
|
|
|
|
|
|
|
|
|
// Create a type-safe transform method
|
|
|
|
|
function getTransform(): (...args: Parameters<ViteTransformHandler>) => string {
|
2024-12-18 09:11:30 +01:00
|
|
|
/** @warning Keep in sync with {@link createBaseViteConfigFromTuonoConfig} */
|
|
|
|
|
const pluginFilesInclude = /\.(jsx|js|mdx|md|tsx|ts)$/
|
|
|
|
|
|
|
|
|
|
return LazyLoadingPlugin({ include: pluginFilesInclude }).transform as never
|
2024-12-01 10:07:35 +01:00
|
|
|
}
|
|
|
|
|
|
2024-12-16 08:03:37 +01:00
|
|
|
describe('"dynamic" sources', async () => {
|
2024-12-15 09:22:19 +01:00
|
|
|
const folderNames = await fs.readdir(`${process.cwd()}/tests/sources`)
|
|
|
|
|
|
2024-12-16 08:03:37 +01:00
|
|
|
describe.each(folderNames)('%s', async (folderName) => {
|
|
|
|
|
const testDirPath = `${process.cwd()}/tests/sources/${folderName}`
|
2024-12-15 09:22:19 +01:00
|
|
|
|
2024-12-18 09:11:30 +01:00
|
|
|
const sourceFilePath = `${testDirPath}/source.tsx`
|
|
|
|
|
|
|
|
|
|
const sourceRaw = await fs.readFile(sourceFilePath, 'utf-8')
|
2024-12-16 08:03:37 +01:00
|
|
|
/**
|
|
|
|
|
* When adding `packages/lazy-fn-vite-plugin/tests/sources/dynamic-only` only
|
|
|
|
|
* the test involving that fixture were broken on Windows... but not the one in the other fixtures:
|
|
|
|
|
* - packages/lazy-fn-vite-plugin/tests/sources/vanilla
|
|
|
|
|
* - packages/lazy-fn-vite-plugin/tests/sources/external-dynamic
|
|
|
|
|
*
|
|
|
|
|
* Awkwardly this doesn't happen on `packages/fs-router-vite-plugin/tests/generator.spec.ts`
|
|
|
|
|
*
|
|
|
|
|
* Too much pain and sadness to investigate this right now.
|
|
|
|
|
* Might worth creating an utility function in the future if this happens again
|
|
|
|
|
*/
|
|
|
|
|
const source = sourceRaw.replace(new RegExp(os.EOL, 'g'), '\n')
|
2024-12-15 09:22:19 +01:00
|
|
|
|
2024-12-16 08:03:37 +01:00
|
|
|
it('should generate file for client', async () => {
|
2024-12-15 09:22:19 +01:00
|
|
|
const pluginTransform = getTransform()
|
2024-12-18 09:11:30 +01:00
|
|
|
const clientBundle = pluginTransform(source, sourceFilePath)
|
2024-12-15 09:22:19 +01:00
|
|
|
|
|
|
|
|
const expectedClientSrc = `${testDirPath}/client.expected.tsx`
|
|
|
|
|
|
|
|
|
|
await expect(clientBundle).toMatchFileSnapshot(
|
|
|
|
|
expectedClientSrc,
|
|
|
|
|
`${testDirPath} client build should be equal to ${expectedClientSrc}`,
|
|
|
|
|
)
|
2024-12-16 08:03:37 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('should generate file for server', async () => {
|
|
|
|
|
const pluginTransform = getTransform()
|
2024-12-18 09:11:30 +01:00
|
|
|
const serverBundle = pluginTransform(source, sourceFilePath, {
|
|
|
|
|
ssr: true,
|
|
|
|
|
})
|
2024-12-16 08:03:37 +01:00
|
|
|
|
|
|
|
|
const expectedServerSrc = `${testDirPath}/server.expected.tsx`
|
2024-12-15 09:22:19 +01:00
|
|
|
|
|
|
|
|
await expect(serverBundle).toMatchFileSnapshot(
|
|
|
|
|
expectedServerSrc,
|
|
|
|
|
`${testDirPath} server build should be equal to ${expectedServerSrc}`,
|
|
|
|
|
)
|
2024-12-16 08:03:37 +01:00
|
|
|
})
|
|
|
|
|
})
|
2024-06-20 19:40:14 +02:00
|
|
|
})
|
2024-12-18 09:11:30 +01:00
|
|
|
|
|
|
|
|
describe('not valid file should not be processed', () => {
|
|
|
|
|
const notValidFiles: Array<string> = [
|
|
|
|
|
'src/styles/module.css',
|
|
|
|
|
'src/pages/file-without-ext',
|
|
|
|
|
'src/pages/file-with-invalid-ext',
|
|
|
|
|
'src/components/fileWithInvalidExt',
|
|
|
|
|
'src/components/sidebar/sidebar-link.module.css',
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
it.each(notValidFiles)('"%s"', (fileName) => {
|
|
|
|
|
const pluginTransform = getTransform()
|
|
|
|
|
|
|
|
|
|
const code = [
|
|
|
|
|
"import { createRoute, dynamic } from 'tuono'",
|
|
|
|
|
"const IndexImport = dynamic(() => import('./../src/routes/index'))",
|
|
|
|
|
].join('\n')
|
|
|
|
|
|
|
|
|
|
const result = pluginTransform(code, fileName)
|
|
|
|
|
|
|
|
|
|
expect(result).toBeUndefined()
|
|
|
|
|
})
|
|
|
|
|
})
|