From ba917cb2cfb59991e7beb08b8da396a6e0f39277 Mon Sep 17 00:00:00 2001 From: julien huang Date: Sat, 19 Aug 2023 11:10:31 +0200 Subject: [PATCH] fix: send rootDir only in dev --- packages/nuxt/src/components/islandsTransform.ts | 16 ++++++++++++---- packages/nuxt/src/components/module.ts | 8 ++++---- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/packages/nuxt/src/components/islandsTransform.ts b/packages/nuxt/src/components/islandsTransform.ts index c6da341a77..a76a54089a 100644 --- a/packages/nuxt/src/components/islandsTransform.ts +++ b/packages/nuxt/src/components/islandsTransform.ts @@ -1,7 +1,7 @@ import { pathToFileURL } from 'node:url' import { basename, join } from 'node:path' import fs from 'node:fs' -import type { Component, Nuxt } from '@nuxt/schema' +import type { Component } from '@nuxt/schema' import { parseURL } from 'ufo' import { createUnplugin } from 'unplugin' import MagicString from 'magic-string' @@ -16,6 +16,12 @@ interface ServerOnlyComponentTransformPluginOptions { * should be done only in dev mode as we use build:manifest result in production */ rootDir?: string + isDev?: boolean +} + +interface ComponentChunkOptions { + getComponents: () => Component[] + buildDir: string } const SCRIPT_RE = /]*>/g @@ -26,6 +32,7 @@ const NUXTCLIENT_ATTR_RE = /\snuxt-client(="[^"]*")?/g export const islandsTransform = createUnplugin((options: ServerOnlyComponentTransformPluginOptions, meta) => { const components = options.getComponents() const isVite = meta.framework === 'vite' + const { isDev, rootDir } = options return { name: 'server-only-component-transform', enforce: 'pre', @@ -98,7 +105,7 @@ export const islandsTransform = createUnplugin((options: ServerOnlyComponentTran 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, '')}`) } } } @@ -133,7 +140,8 @@ function getBindings (bindings: Record, vfor?: [string, string]) } } -export const componentsChunkPlugin = createUnplugin((options: ServerOnlyComponentTransformPluginOptions & {nuxt: Nuxt}) => { +export const componentsChunkPlugin = createUnplugin((options: ComponentChunkOptions) => { + const { buildDir } = options return { name: 'componentsChunkPlugin', vite: { @@ -174,7 +182,7 @@ export const componentsChunkPlugin = createUnplugin((options: ServerOnlyComponen }) }) - fs.writeFileSync(join(options.nuxt.options.buildDir, 'components-chunk.mjs'), `export const paths = ${JSON.stringify(componentsChunks.reduce((acc, [chunkPath, chunkInfo]) => { + fs.writeFileSync(join(buildDir, 'components-chunk.mjs'), `export const paths = ${JSON.stringify(componentsChunks.reduce((acc, [chunkPath, chunkInfo]) => { if (chunkInfo.type === 'chunk' && chunkInfo.name && chunkInfo.exports.length > 0) { return Object.assign(acc, { [withoutClientSuffixAndExtension(chunkInfo.name)]: chunkPath }) } return acc }, {}))}`) diff --git a/packages/nuxt/src/components/module.ts b/packages/nuxt/src/components/module.ts index 9c0239b3c5..91a6c72764 100644 --- a/packages/nuxt/src/components/module.ts +++ b/packages/nuxt/src/components/module.ts @@ -233,13 +233,13 @@ export default defineNuxtModule({ if (isServer) { config.plugins.push(islandsTransform.vite({ getComponents, - rootDir: nuxt.options.rootDir + rootDir: nuxt.options.rootDir, + isDev: nuxt.options.dev })) - } else { + } else if (!nuxt.options.dev) { config.plugins.push(componentsChunkPlugin.vite({ getComponents, - rootDir: nuxt.options.rootDir, - nuxt + buildDir: nuxt.options.buildDir })) } }