From bec8c4e4e916f94ca01b9265c9ab806998df2194 Mon Sep 17 00:00:00 2001 From: Jonathan Neal Date: Tue, 7 Dec 2021 11:59:11 -0500 Subject: [PATCH] Convert import assertions to query parameters --- packages/astro/src/vite-plugin-astro/assertions.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 packages/astro/src/vite-plugin-astro/assertions.ts diff --git a/packages/astro/src/vite-plugin-astro/assertions.ts b/packages/astro/src/vite-plugin-astro/assertions.ts new file mode 100644 index 0000000000..6401dcb42e --- /dev/null +++ b/packages/astro/src/vite-plugin-astro/assertions.ts @@ -0,0 +1,11 @@ +export const convertAssertionsToQueryParams = (code: string) => code.replace( + /import\s+(?:[\W\w]*?\sfrom\s+(?:"(?:[^"]|\\")*"|'(?:[^']|\\')*')(\s+assert\s+([^}]+\})))/gm, + (statement, assertions, assertionType) => { + return assertions + ? statement.slice(0, -assertions.length - 1) + + '?' + + new URLSearchParams(JSON.parse(assertionType.replace(/(['"])?([a-z0-9A-Z_]+)(['"])?\s*:/g, '"$2": '))).toString() + + statement[statement.length - assertions.length - 1] + : statement; + } + ) \ No newline at end of file