From 6f16cfe7611b53dea4bd57aab51c9d7e01f05345 Mon Sep 17 00:00:00 2001 From: Angelo Ashmore Date: Wed, 17 Jun 2026 02:00:24 +0000 Subject: [PATCH] fix: resolve escaped in generated framework files The bundler escapes `` to `<\/script>` in string literals, and dedent reads the raw template strings, so the backslash leaked into generated Nuxt and SvelteKit files. Point dedent at the resolved string segments instead. Co-Authored-By: Claude Opus 4.8 (1M context) --- src/lib/string.ts | 15 ++++++++++++++- test/gen-setup.test.ts | 28 +++++++++++++++++++++++++++- 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/src/lib/string.ts b/src/lib/string.ts index bd8890d..b4bb75b 100644 --- a/src/lib/string.ts +++ b/src/lib/string.ts @@ -1,6 +1,19 @@ import baseDedent from "dedent"; -export const dedent = baseDedent.withOptions({ alignValues: true }); +const baseDedentWithOptions = baseDedent.withOptions({ alignValues: true }); + +export function dedent(strings: TemplateStringsArray | string, ...values: unknown[]): string { + if (typeof strings === "string") { + return baseDedentWithOptions(strings); + } + + // dedent reads `strings.raw` by default, which keeps escapes the bundler adds + // to string literals (e.g. `` becomes `<\/script>`). Point `raw` at + // the regular string segments instead, where escapes are already resolved, so + // they don't leak into generated files. + const resolved = Array.from(strings); + return baseDedentWithOptions(Object.assign(resolved, { raw: resolved }), ...values); +} export function formatTable( rows: string[][], diff --git a/test/gen-setup.test.ts b/test/gen-setup.test.ts index 1fb6451..113e0a5 100644 --- a/test/gen-setup.test.ts +++ b/test/gen-setup.test.ts @@ -1,4 +1,4 @@ -import { writeFile } from "node:fs/promises"; +import { mkdir, writeFile } from "node:fs/promises"; import { it } from "./it"; @@ -35,6 +35,32 @@ it("skips existing files", { timeout: 30_000 }, async ({ expect, project, prismi await expect(project).toHaveFile("prismicio.js", { contains: "// custom client file" }); }); +it("generates valid script tags for SvelteKit", { timeout: 30_000 }, async ({ + expect, + project, + prismic, +}) => { + // Reconfigure the fixture as a SvelteKit project so a file with a ", not the bundler-escaped "<\/script>". + await expect(project).toHaveFile("src/routes/slice-simulator/+page.svelte", { + contains: "", + }); +}); + it("skips installation with --no-install", async ({ expect, project, prismic }) => { const { exitCode, stdout } = await prismic("gen", ["setup", "--no-install"]); expect(exitCode).toBe(0);