Convert import assertions to query parameters
This commit is contained in:
parent
0ef682c924
commit
bec8c4e4e9
1 changed files with 11 additions and 0 deletions
11
packages/astro/src/vite-plugin-astro/assertions.ts
Normal file
11
packages/astro/src/vite-plugin-astro/assertions.ts
Normal file
|
@ -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;
|
||||
}
|
||||
)
|
Loading…
Reference in a new issue