Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/lib/string.ts
Original file line number Diff line number Diff line change
@@ -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. `</script>` 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[][],
Expand Down
28 changes: 27 additions & 1 deletion test/gen-setup.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { writeFile } from "node:fs/promises";
import { mkdir, writeFile } from "node:fs/promises";

import { it } from "./it";

Expand Down Expand Up @@ -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 <script>
// block is generated (the simulator page).
await writeFile(
new URL("package.json", project),
JSON.stringify({ dependencies: { "@sveltejs/kit": "latest", svelte: "latest" } }),
);
await mkdir(new URL("node_modules/svelte/", project), { recursive: true });
await writeFile(
new URL("node_modules/svelte/package.json", project),
JSON.stringify({ version: "5.0.0" }),
);

const { exitCode } = await prismic("gen", ["setup", "--no-install"]);
expect(exitCode).toBe(0);

// The closing tag must be "</script>", not the bundler-escaped "<\/script>".
await expect(project).toHaveFile("src/routes/slice-simulator/+page.svelte", {
contains: "</script>",
});
});

it("skips installation with --no-install", async ({ expect, project, prismic }) => {
const { exitCode, stdout } = await prismic("gen", ["setup", "--no-install"]);
expect(exitCode).toBe(0);
Expand Down