import { describe, expect, it, vi } from 'vitest' import type { Plugin } from 'vite' import type { Component } from '@nuxt/schema' import type { UnpluginOptions } from 'unplugin' import { islandsTransform } from '../src/components/islandsTransform' import { normalizeLineEndings } from './utils' const getComponents = () => [{ filePath: '/root/hello.server.vue', mode: 'server', pascalName: 'HelloWorld', island: true, kebabName: 'hello-world', chunkName: 'components/hello-world', export: 'default', shortPath: '', prefetch: false, preload: false }] as Component[] const pluginWebpack = islandsTransform.raw({ getComponents, selectiveClient: true }, { framework: 'webpack', webpack: { compiler: {} as any } }) const viteTransform = async (source: string, id: string, isDev = false, selectiveClient = false) => { const vitePlugin = islandsTransform.raw({ getComponents, rootDir: '/root', isDev, selectiveClient }, { framework: 'vite' }) as Plugin const result = await (vitePlugin.transform! as Function)(source, id) return typeof result === 'string' ? result : result?.code } const webpackTransform = async (source: string, id: string) => { const result = await ((pluginWebpack as UnpluginOptions).transform! as Function)(source, id) return typeof result === 'string' ? result : result?.code } describe('islandTransform - server and island components', () => { describe('slots', () => { it('expect slot transform to match inline snapshot', async () => { const result = await viteTransform(` ` , 'hello.server.vue') expect(normalizeLineEndings(result)).toMatchInlineSnapshot(` " " `) }) it('expect slot fallback transform to match inline snapshot', async () => { const result = await viteTransform(` ` , 'hello.server.vue') expect(normalizeLineEndings(result)).toMatchInlineSnapshot(` " " `) }) it('expect slot transform with fallback and no name to match inline snapshot #23209', async () => { const result = await viteTransform(` ` , 'hello.server.vue') expect(normalizeLineEndings(result)).toMatchInlineSnapshot(` " " `) }) }) describe('nuxt-client', () => { describe('vite', () => { it('test transform with vite in dev', async () => { const result = await viteTransform(` `, 'hello.server.vue', true, true) expect(normalizeLineEndings(result)).toMatchInlineSnapshot(` " " `) // root-dir prop should never be used in production expect(result).toContain('root-dir="/root"') }) it('test transform with vite in prod', async () => { const result = await viteTransform(` `, 'hello.server.vue', false, true) expect(normalizeLineEndings(result)).toMatchInlineSnapshot(` " " `) // root-dir prop should never be used in production expect(result).not.toContain('root-dir="') }) it('test dynamic nuxt-client', async () => { const result = await viteTransform(` `, 'hello.server.vue', false, true) expect(normalizeLineEndings(result)).toMatchInlineSnapshot(` " " `) }) it('should not transform if disabled', async () => { const result = await viteTransform(` `, 'hello.server.vue', false, false) expect(normalizeLineEndings(result)).toMatchInlineSnapshot(` " " `) }) it('should add import if there is no scripts in the SFC', async () => { const result = await viteTransform(` `, 'hello.server.vue', false, true) expect(result).toMatchInlineSnapshot(` " " `) expect(result).toContain(`import NuxtTeleportSsrClient from '#app/components/nuxt-teleport-ssr-client'`) }) }) describe('webpack', () => { it('test transform with webpack', async () => { const spyOnWarn = vi.spyOn(console, 'warn') const result = await webpackTransform(` `, 'hello.server.vue') expect(normalizeLineEndings(result)).toMatchInlineSnapshot(` " " `) expect(spyOnWarn).toHaveBeenCalledWith('nuxt-client attribute and client components within islands is only supported with Vite. file: hello.server.vue') }) }) }) })