From 9db2f8d224e7f9a8578a55732c95f9a8a9a172d4 Mon Sep 17 00:00:00 2001
From: Inesh Bose <2504266b@student.gla.ac.uk>
Date: Sun, 14 May 2023 14:40:56 +0100
Subject: [PATCH 1/2] fix(dev-only): split regex
---
docs/2.guide/2.directory-structure/1.components.md | 1 +
packages/nuxt/src/core/plugins/dev-only.ts | 6 ++++--
2 files changed, 5 insertions(+), 2 deletions(-)
diff --git a/docs/2.guide/2.directory-structure/1.components.md b/docs/2.guide/2.directory-structure/1.components.md
index f9000e45fdba..f4d5168f8560 100644
--- a/docs/2.guide/2.directory-structure/1.components.md
+++ b/docs/2.guide/2.directory-structure/1.components.md
@@ -338,6 +338,7 @@ The content will not be included in production builds and tree-shaken.
+
diff --git a/packages/nuxt/src/core/plugins/dev-only.ts b/packages/nuxt/src/core/plugins/dev-only.ts
index ce6207c14c32..92e94c085b2f 100644
--- a/packages/nuxt/src/core/plugins/dev-only.ts
+++ b/packages/nuxt/src/core/plugins/dev-only.ts
@@ -9,7 +9,8 @@ interface DevOnlyPluginOptions {
}
export const DevOnlyPlugin = createUnplugin((options: DevOnlyPluginOptions) => {
- const DEVONLY_COMP_RE = /<(?:dev-only|DevOnly)>[^<]*(?:(?[\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 = /(?[\s\S]*?)<\/template>/
return {
name: 'nuxt:server-devonly:transform',
@@ -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()) {
From f29bc14b3ddbad7efec90fae0cb7e88db5069f7e Mon Sep 17 00:00:00 2001
From: Inesh Bose <2504266b@student.gla.ac.uk>
Date: Sun, 14 May 2023 22:23:56 +0100
Subject: [PATCH 2/2] refactor: use parser
---
packages/nuxt/package.json | 1 +
packages/nuxt/src/core/plugins/dev-only.ts | 7 +++++--
pnpm-lock.yaml | 7 +++++++
3 files changed, 13 insertions(+), 2 deletions(-)
diff --git a/packages/nuxt/package.json b/packages/nuxt/package.json
index 1e1661289bb3..5e3eeb346694 100644
--- a/packages/nuxt/package.json
+++ b/packages/nuxt/package.json
@@ -90,6 +90,7 @@
"scule": "^1.0.0",
"strip-literal": "^1.0.1",
"ufo": "^1.1.2",
+ "ultrahtml": "^1.2.0",
"unctx": "^2.3.0",
"unenv": "^1.4.1",
"unimport": "^3.0.6",
diff --git a/packages/nuxt/src/core/plugins/dev-only.ts b/packages/nuxt/src/core/plugins/dev-only.ts
index 92e94c085b2f..b14b4f6a7405 100644
--- a/packages/nuxt/src/core/plugins/dev-only.ts
+++ b/packages/nuxt/src/core/plugins/dev-only.ts
@@ -3,6 +3,7 @@ import { stripLiteral } from 'strip-literal'
import { parseQuery, parseURL } from 'ufo'
import MagicString from 'magic-string'
import { createUnplugin } from 'unplugin'
+import { type Node, parse } from 'ultrahtml'
interface DevOnlyPluginOptions {
sourcemap?: boolean
@@ -10,7 +11,6 @@ interface DevOnlyPluginOptions {
export const DevOnlyPlugin = createUnplugin((options: DevOnlyPluginOptions) => {
const DEVONLY_COMP_RE = /<(?:dev-only|DevOnly)>[\s\S]*?<\/(?:dev-only|DevOnly)>/g
- const FALLBACK_SLOT_RE = /(?[\s\S]*?)<\/template>/
return {
name: 'nuxt:server-devonly:transform',
@@ -30,7 +30,10 @@ export const DevOnlyPlugin = createUnplugin((options: DevOnlyPluginOptions) => {
const s = new MagicString(code)
const strippedCode = stripLiteral(code)
for (const match of strippedCode.matchAll(DEVONLY_COMP_RE) || []) {
- const replacement = match[0].match(FALLBACK_SLOT_RE)?.groups?.fallback || ''
+ const ast: Node = parse(match[0]).children[0]
+ const fallback: Node | undefined = ast.children?.find((n: Node) => n.name === 'template' && Object.values(n.attributes).includes('#fallback'))
+ const replacement = fallback ? match[0].slice(fallback.loc[0].end, fallback.loc[fallback.loc.length - 1].start) : ''
+
s.overwrite(match.index!, match.index! + match[0].length, replacement)
}
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index a7638afa62ef..e4dd6ced005e 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -435,6 +435,9 @@ importers:
ufo:
specifier: ^1.1.2
version: 1.1.2
+ ultrahtml:
+ specifier: ^1.2.0
+ version: 1.2.0
unctx:
specifier: ^2.3.0
version: 2.3.0
@@ -8277,6 +8280,10 @@ packages:
/ufo@1.1.2:
resolution: {integrity: sha512-TrY6DsjTQQgyS3E3dBaOXf0TpPD8u9FVrVYmKVegJuFw51n/YB9XPt+U6ydzFG5ZIN7+DIjPbNmXoBj9esYhgQ==}
+ /ultrahtml@1.2.0:
+ resolution: {integrity: sha512-vxZM2yNvajRmCj/SknRYGNXk2tqiy6kRNvZjJLaleG3zJbSh/aNkOqD1/CVzypw8tyHyhpzYuwQgMMhUB4ZVNQ==}
+ dev: false
+
/unbox-primitive@1.0.2:
resolution: {integrity: sha512-61pPlCD9h51VoreyJ0BReideM3MDKMKnh6+V9L08331ipq6Q8OFXZYiqP6n/tbHx4s5I9uRhcye6BrbkizkBDw==}
dependencies: