From c1f7cb5ca6aba84f7533688b03ae6592cd5a70a4 Mon Sep 17 00:00:00 2001 From: Simon Lydell Date: Fri, 25 Oct 2024 22:18:49 +0200 Subject: [PATCH] Fix syntax error when using quotes or escapes in target name --- src/Inject.ts | 6 +++--- tests/Hot.test.ts | 15 +++++++++++++++ tests/fixtures/hot/basic/elm-watch.json | 6 ++++++ 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/src/Inject.ts b/src/Inject.ts index 9a0ca634..99c494ab 100644 --- a/src/Inject.ts +++ b/src/Inject.ts @@ -870,10 +870,10 @@ export function clientCode( return ( versionedIdentifier(outputPath.targetName, webSocketPort) + ClientCode.client.replace( - new RegExp(`%(${join(Object.keys(replacements), "|")})%`, "g"), - (match: string, name: string) => + new RegExp(`"%(${join(Object.keys(replacements), "|")})%"`, "g"), + (_, name: string) => /* v8 ignore next */ - replacements[name] ?? match, + Codec.JSON.stringify(Codec.string, replacements[name] ?? name), ) ); } diff --git a/tests/Hot.test.ts b/tests/Hot.test.ts index 47926776..fdd3cb41 100644 --- a/tests/Hot.test.ts +++ b/tests/Hot.test.ts @@ -325,6 +325,20 @@ describe("hot", () => { expect(div.outerHTML).toMatchInlineSnapshot(`
main
`); }); + test("successful connect (target name with escapes)", async () => { + const { div } = await run({ + fixture: "basic", + args: ['"Esc\\apes"'], + scripts: ["Escapes.js"], + init: (node) => { + window.Elm?.["HtmlMain"]?.init({ node }); + }, + onIdle: () => "Stop", + }); + + expect(div.outerHTML).toMatchInlineSnapshot(`
Hello, World!
`); + }); + test("connect with elm.json error", async () => { const fixture = "connect-with-elm-json-error"; const dir = path.join(FIXTURES_DIR, fixture); @@ -979,6 +993,7 @@ describe("hot", () => { SendBadJson Reconnect HttpCaching + "Esc\\apes" If you want to have this target compiled, restart elm-watch either with more CLI arguments or no CLI arguments at all! ▲ ❌ 13:10:05 TargetDisabled diff --git a/tests/fixtures/hot/basic/elm-watch.json b/tests/fixtures/hot/basic/elm-watch.json index f2ad90a7..4b3e4054 100644 --- a/tests/fixtures/hot/basic/elm-watch.json +++ b/tests/fixtures/hot/basic/elm-watch.json @@ -71,6 +71,12 @@ "src/HtmlMain.elm" ], "output": "build/HttpCaching.js" + }, + "\"Esc\\apes\"": { + "inputs": [ + "src/HtmlMain.elm" + ], + "output": "build/Escapes.js" } } }