From 79fc8a19dac81f9a89c8d28f5950576f467abfe3 Mon Sep 17 00:00:00 2001 From: julien huang Date: Fri, 18 Aug 2023 21:55:29 +0200 Subject: [PATCH] fix: make it only vite compatible for now --- .../nuxt/src/components/islandsTransform.ts | 23 ++++++++++++------- packages/nuxt/src/components/module.ts | 3 +-- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/packages/nuxt/src/components/islandsTransform.ts b/packages/nuxt/src/components/islandsTransform.ts index cab6b981ae..c6da341a77 100644 --- a/packages/nuxt/src/components/islandsTransform.ts +++ b/packages/nuxt/src/components/islandsTransform.ts @@ -23,14 +23,12 @@ const HAS_SLOT_RE = /([\s\S]*)<\/template>/ const NUXTCLIENT_ATTR_RE = /\snuxt-client(="[^"]*")?/g -export const islandsTransform = createUnplugin((options: ServerOnlyComponentTransformPluginOptions & {nuxt: Nuxt}) => { +export const islandsTransform = createUnplugin((options: ServerOnlyComponentTransformPluginOptions, meta) => { const components = options.getComponents() + const isVite = meta.framework === 'vite' return { name: 'server-only-component-transform', enforce: 'pre', - vite: { - - }, transformInclude (id) { if (!isVue(id)) { return false } @@ -51,6 +49,8 @@ export const islandsTransform = createUnplugin((options: ServerOnlyComponentTran return full + '\nimport { vforToArray as __vforToArray } from \'#app/components/utils\'' + '\nimport TeleportIfClient from \'#app/components/TeleportIfClient\'' }) + let hasNuxtClient = false + const ast = parse(template[0]) await walk(ast, (node) => { if (node.type === ELEMENT_NODE) { @@ -92,15 +92,22 @@ export const islandsTransform = createUnplugin((options: ServerOnlyComponentTran } } } else if ('nuxt-client' in node.attributes) { - // handle granular interactivity - const htmlCode = code.slice(startingIndex + node.loc[0].start, startingIndex + node.loc[1].end) - const uid = hash(id + node.loc[0].start + node.loc[0].end) + hasNuxtClient = true + if (isVite) { + // handle granular interactivity + const htmlCode = code.slice(startingIndex + node.loc[0].start, startingIndex + node.loc[1].end) + const uid = hash(id + node.loc[0].start + node.loc[0].end) - s.overwrite(node.loc[0].start, node.loc[1].end, `${htmlCode.replaceAll(NUXTCLIENT_ATTR_RE, '')}`) + s.overwrite(node.loc[0].start, node.loc[1].end, `${htmlCode.replaceAll(NUXTCLIENT_ATTR_RE, '')}`) + } } } }) + if (!isVite && hasNuxtClient) { + console.warn(`nuxt-client attribute and client components within islands is only supported in vite mode. file: ${id}`) + } + if (s.hasChanged()) { return { code: s.toString(), diff --git a/packages/nuxt/src/components/module.ts b/packages/nuxt/src/components/module.ts index 5ffe06e2ed..9c0239b3c5 100644 --- a/packages/nuxt/src/components/module.ts +++ b/packages/nuxt/src/components/module.ts @@ -233,8 +233,7 @@ export default defineNuxtModule({ if (isServer) { config.plugins.push(islandsTransform.vite({ getComponents, - rootDir: nuxt.options.rootDir, - nuxt + rootDir: nuxt.options.rootDir })) } else { config.plugins.push(componentsChunkPlugin.vite({