fix(nuxt): use original escapes/quotes in page re-exports (#23191)

This commit is contained in:
Daniel Roe 2023-09-14 07:06:07 +01:00 committed by GitHub
parent 16e40f899e
commit e729406805
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 4 deletions

View File

@ -65,8 +65,10 @@ export const PageMetaPlugin = createUnplugin((options: PageMetaPluginOptions) =>
// [vite] Re-export any script imports // [vite] Re-export any script imports
const scriptImport = imports.find(i => parseMacroQuery(i.specifier).type === 'script') const scriptImport = imports.find(i => parseMacroQuery(i.specifier).type === 'script')
if (scriptImport) { if (scriptImport) {
const specifier = rewriteQuery(scriptImport.specifier) const reorderedQuery = rewriteQuery(scriptImport.specifier)
s.overwrite(0, code.length, `export { default } from ${JSON.stringify(specifier)}`) // Avoid using JSON.stringify which can add extra escapes to paths with non-ASCII characters
const quotedSpecifier = getQuotedSpecifier(scriptImport.code)?.replace(scriptImport.specifier, reorderedQuery) ?? JSON.stringify(reorderedQuery)
s.overwrite(0, code.length, `export { default } from ${quotedSpecifier}`)
return result() return result()
} }
@ -77,8 +79,10 @@ export const PageMetaPlugin = createUnplugin((options: PageMetaPluginOptions) =>
continue continue
} }
const specifier = rewriteQuery(match.specifier) const reorderedQuery = rewriteQuery(match.specifier)
s.overwrite(0, code.length, `export { default } from ${JSON.stringify(specifier)}`) // Avoid using JSON.stringify which can add extra escapes to paths with non-ASCII characters
const quotedSpecifier = getQuotedSpecifier(match.code)?.replace(match.specifier, reorderedQuery) ?? JSON.stringify(reorderedQuery)
s.overwrite(0, code.length, `export { default } from ${quotedSpecifier}`)
return result() return result()
} }
@ -183,3 +187,7 @@ function parseMacroQuery (id: string) {
} }
return query return query
} }
function getQuotedSpecifier (id: string) {
return id.match(/(["']).*\1/)?.[0]
}

View File

@ -0,0 +1,8 @@
<script setup lang="ts">
</script>
<template>
<div>
<!-- -->
</div>
</template>