Skip to content

Commit

Permalink
fix(dev-only): split regex
Browse files Browse the repository at this point in the history
  • Loading branch information
ineshbose committed May 14, 2023
1 parent d077c10 commit 9db2f8d
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
1 change: 1 addition & 0 deletions docs/2.guide/2.directory-structure/1.components.md
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,7 @@ The content will not be included in production builds and tree-shaken.
<LazyDebugBar />

<!-- if you ever require to have a replacement during production -->
<!-- be sure to test these using `nuxt preview` -->
<template #fallback>
<div><!-- empty div for flex.justify-between --></div>
</template>
Expand Down
6 changes: 4 additions & 2 deletions packages/nuxt/src/core/plugins/dev-only.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ interface DevOnlyPluginOptions {
}

export const DevOnlyPlugin = createUnplugin((options: DevOnlyPluginOptions) => {
const DEVONLY_COMP_RE = /<(?:dev-only|DevOnly)>[^<]*(?:<template\s+#fallback>(?<fallback>[\s\S]*?)<\/template>)?[\s\S]*?<\/(?:dev-only|DevOnly)>/g
const DEVONLY_COMP_RE = /<(?:dev-only|DevOnly)>[\s\S]*?<\/(?:dev-only|DevOnly)>/g
const FALLBACK_SLOT_RE = /<template\s+#fallback>(?<fallback>[\s\S]*?)<\/template>/

return {
name: 'nuxt:server-devonly:transform',
Expand All @@ -29,7 +30,8 @@ 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.overwrite(match.index!, match.index! + match[0].length, match.groups?.fallback || '')
const replacement = match[0].match(FALLBACK_SLOT_RE)?.groups?.fallback || ''
s.overwrite(match.index!, match.index! + match[0].length, replacement)
}

if (s.hasChanged()) {
Expand Down

0 comments on commit 9db2f8d

Please sign in to comment.