test: test transform island with dev mode

This commit is contained in:
julien huang 2023-08-19 12:14:34 +02:00
parent 26dff83a84
commit 08079eccef

View File

@ -6,7 +6,7 @@ import { islandsTransform } from '../src/components/islandsTransform'
import { normalizeLineEndings } from './utils' import { normalizeLineEndings } from './utils'
const getComponents = () => [{ const getComponents = () => [{
filePath: 'hello.server.vue', filePath: '/root/hello.server.vue',
mode: 'server', mode: 'server',
pascalName: 'HelloWorld', pascalName: 'HelloWorld',
island: true, island: true,
@ -19,22 +19,26 @@ const getComponents = () => [{
}] as Component[] }] as Component[]
const pluginVite = islandsTransform.raw({ const pluginVite = islandsTransform.raw({
getComponents
}, { framework: 'vite' }) as Plugin
const pluginViteDev = islandsTransform.raw({
getComponents, getComponents,
rootDir: '' rootDir: '/root',
isDev: true
}, { framework: 'vite' }) as Plugin }, { framework: 'vite' }) as Plugin
const pluginWebpack = islandsTransform.raw({ const pluginWebpack = islandsTransform.raw({
getComponents, getComponents
rootDir: ''
}, { framework: 'webpack', webpack: { compiler: {} as any } }) }, { framework: 'webpack', webpack: { compiler: {} as any } })
const viteTransform = async (source: string, id: string) => { const viteTransform = async (source: string, id: string, dev = false) => {
const result = await (pluginVite.transform! as Function).call({ error: null, warn: null } as any, source, id) const result = await ((dev ? pluginViteDev : pluginVite).transform! as Function)(source, id)
return typeof result === 'string' ? result : result?.code return typeof result === 'string' ? result : result?.code
} }
const webpackTransform = async (source: string, id: string) => { const webpackTransform = async (source: string, id: string) => {
const result = await ((pluginWebpack as UnpluginOptions).transform! as Function).call({ error: null, warn: null } as any, source, id) const result = await ((pluginWebpack as UnpluginOptions).transform! as Function)(source, id)
return typeof result === 'string' ? result : result?.code return typeof result === 'string' ? result : result?.code
} }
@ -138,6 +142,40 @@ describe('islandTransform - server and island components', () => {
`) `)
}) })
it('test transform with vite in dev', async () => {
const result = await viteTransform(`<template>
<div>
<!-- should not be wrapped by TeleportIfClient -->
<HelloWorld />
<!-- should be wrapped by TeleportIfClient with a rootDir attr -->
<HelloWorld nuxt-client />
</div>
</template>
<script setup lang="ts">
import HelloWorld from './HelloWorld.vue'
</script>
`, 'hello.server.vue', true)
expect(normalizeLineEndings(result)).toMatchInlineSnapshot(`
"<template>
<div>
<!-- should not be wrapped by TeleportIfClient -->
<HelloWorld />
<!-- should be wrapped by TeleportIfClient with a rootDir attr -->
<TeleportIfClient to=\\"HelloWorld-vxDirZrUwF\\" root-dir=\\"/root\\" :nuxt-client=\\"true\\"><HelloWorld /></TeleportIfClient>
</div>
</template>
<script setup lang=\\"ts\\">
import { vforToArray as __vforToArray } from '#app/components/utils'
import TeleportIfClient from '#app/components/TeleportIfClient'
import HelloWorld from './HelloWorld.vue'
</script>
"
`)
})
it('test transform with webpack', async () => { it('test transform with webpack', async () => {
const spyOnWarn = vi.spyOn(console, 'warn') const spyOnWarn = vi.spyOn(console, 'warn')
const result = await webpackTransform(`<template> const result = await webpackTransform(`<template>