From e729406805234bb4003f18856bb58fad0314548f Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Thu, 14 Sep 2023 07:06:07 +0100 Subject: [PATCH] fix(nuxt): use original escapes/quotes in page re-exports (#23191) --- packages/nuxt/src/pages/plugins/page-meta.ts | 16 ++++++++++++---- test/fixtures/basic/pages/non-ascii/ç.vue | 8 ++++++++ 2 files changed, 20 insertions(+), 4 deletions(-) create mode 100644 test/fixtures/basic/pages/non-ascii/ç.vue diff --git a/packages/nuxt/src/pages/plugins/page-meta.ts b/packages/nuxt/src/pages/plugins/page-meta.ts index 867098146b..01cce87cf2 100644 --- a/packages/nuxt/src/pages/plugins/page-meta.ts +++ b/packages/nuxt/src/pages/plugins/page-meta.ts @@ -65,8 +65,10 @@ export const PageMetaPlugin = createUnplugin((options: PageMetaPluginOptions) => // [vite] Re-export any script imports const scriptImport = imports.find(i => parseMacroQuery(i.specifier).type === 'script') if (scriptImport) { - const specifier = rewriteQuery(scriptImport.specifier) - s.overwrite(0, code.length, `export { default } from ${JSON.stringify(specifier)}`) + const reorderedQuery = rewriteQuery(scriptImport.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() } @@ -77,8 +79,10 @@ export const PageMetaPlugin = createUnplugin((options: PageMetaPluginOptions) => continue } - const specifier = rewriteQuery(match.specifier) - s.overwrite(0, code.length, `export { default } from ${JSON.stringify(specifier)}`) + const reorderedQuery = rewriteQuery(match.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() } @@ -183,3 +187,7 @@ function parseMacroQuery (id: string) { } return query } + +function getQuotedSpecifier (id: string) { + return id.match(/(["']).*\1/)?.[0] +} diff --git a/test/fixtures/basic/pages/non-ascii/ç.vue b/test/fixtures/basic/pages/non-ascii/ç.vue new file mode 100644 index 0000000000..047335e971 --- /dev/null +++ b/test/fixtures/basic/pages/non-ascii/ç.vue @@ -0,0 +1,8 @@ + + +