diff --git a/docs/2.guide/2.directory-structure/1.components.md b/docs/2.guide/2.directory-structure/1.components.md index 7a39da99f66f..f9000e45fdba 100644 --- a/docs/2.guide/2.directory-structure/1.components.md +++ b/docs/2.guide/2.directory-structure/1.components.md @@ -336,6 +336,11 @@ The content will not be included in production builds and tree-shaken. + + + diff --git a/packages/nuxt/src/app/components/dev-only.ts b/packages/nuxt/src/app/components/dev-only.ts index ba7cd22deaf5..914ba6f6e445 100644 --- a/packages/nuxt/src/app/components/dev-only.ts +++ b/packages/nuxt/src/app/components/dev-only.ts @@ -6,6 +6,6 @@ export default defineComponent({ if (process.dev) { return () => props.slots.default?.() } - return () => null + return () => props.slots.fallback?.() } }) diff --git a/packages/nuxt/src/core/plugins/dev-only.ts b/packages/nuxt/src/core/plugins/dev-only.ts index b0c6226cc3bf..ce6207c14c32 100644 --- a/packages/nuxt/src/core/plugins/dev-only.ts +++ b/packages/nuxt/src/core/plugins/dev-only.ts @@ -9,7 +9,7 @@ interface DevOnlyPluginOptions { } export const DevOnlyPlugin = createUnplugin((options: DevOnlyPluginOptions) => { - const DEVONLY_COMP_RE = /<(dev-only|DevOnly)>[\s\S]*?<\/(dev-only|DevOnly)>/g + const DEVONLY_COMP_RE = /<(?:dev-only|DevOnly)>[^<]*(?:(?[\s\S]*?)<\/template>)?[\s\S]*?<\/(?:dev-only|DevOnly)>/g return { name: 'nuxt:server-devonly:transform', @@ -29,7 +29,7 @@ export const DevOnlyPlugin = createUnplugin((options: DevOnlyPluginOptions) => { const s = new MagicString(code) const strippedCode = stripLiteral(code) for (const match of strippedCode.matchAll(DEVONLY_COMP_RE) || []) { - s.remove(match.index!, match.index! + match[0].length) + s.overwrite(match.index!, match.index! + match[0].length, match.groups?.fallback || '') } if (s.hasChanged()) { diff --git a/test/basic.test.ts b/test/basic.test.ts index ea4ce77ed94e..cc1c913fd40b 100644 --- a/test/basic.test.ts +++ b/test/basic.test.ts @@ -83,6 +83,8 @@ describe('pages', () => { expect(html).toContain('Composable | star: auto imported from ~/composables/nested/bar.ts via star export') // should import components expect(html).toContain('This is a custom component with a named export.') + // should remove dev-only and replace with any fallback content + expect(html).toContain(isDev() ? 'Some dev-only info' : 'Some prod-only info') // should apply attributes to client-only components expect(html).toContain('
') // should render server-only components diff --git a/test/fixtures/basic/pages/index.vue b/test/fixtures/basic/pages/index.vue index 7b6c4bb7349d..2b31da638015 100644 --- a/test/fixtures/basic/pages/index.vue +++ b/test/fixtures/basic/pages/index.vue @@ -11,6 +11,14 @@
Composable | star: {{ useNestedBar() }}
Some dev-only info
Some dev-only info
+
+ + Some dev-only info + + +
Path: {{ $route.fullPath }}
Link