From f8f57682948a76cad81eef579235ee059578fe91 Mon Sep 17 00:00:00 2001 From: Bjorn Lu Date: Wed, 28 Dec 2022 00:12:03 +0800 Subject: [PATCH] Fix code generation quotes handling (#5678) --- .changeset/purple-hounds-decide.md | 5 +++++ packages/astro/src/core/build/vite-plugin-pages.ts | 4 ++-- packages/astro/src/core/create-vite.ts | 4 +++- packages/astro/src/core/dev/container.ts | 2 +- packages/astro/src/vite-plugin-env/index.ts | 4 ++-- packages/astro/src/vite-plugin-markdown-legacy/index.ts | 6 +++--- packages/astro/src/vite-plugin-markdown/index.ts | 2 +- packages/astro/test/astro-pages.test.js | 7 +++++++ .../fixtures/astro pages/src/pages/quotes'-work-too.md | 1 + 9 files changed, 25 insertions(+), 10 deletions(-) create mode 100644 .changeset/purple-hounds-decide.md create mode 100644 packages/astro/test/fixtures/astro pages/src/pages/quotes'-work-too.md diff --git a/.changeset/purple-hounds-decide.md b/.changeset/purple-hounds-decide.md new file mode 100644 index 000000000..cf330ee50 --- /dev/null +++ b/.changeset/purple-hounds-decide.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fix code generation quotes handling diff --git a/packages/astro/src/core/build/vite-plugin-pages.ts b/packages/astro/src/core/build/vite-plugin-pages.ts index 98d10b2c8..1d661865c 100644 --- a/packages/astro/src/core/build/vite-plugin-pages.ts +++ b/packages/astro/src/core/build/vite-plugin-pages.ts @@ -27,8 +27,8 @@ export function vitePluginPages(opts: StaticBuildOptions, internals: BuildIntern let i = 0; for (const pageData of eachPageData(internals)) { const variable = `_page${i}`; - imports.push(`import * as ${variable} from '${pageData.moduleSpecifier}';`); - importMap += `['${pageData.component}', ${variable}],`; + imports.push(`import * as ${variable} from ${JSON.stringify(pageData.moduleSpecifier)};`); + importMap += `[${JSON.stringify(pageData.component)}, ${variable}],`; i++; } diff --git a/packages/astro/src/core/create-vite.ts b/packages/astro/src/core/create-vite.ts index efa1251e5..942566c88 100644 --- a/packages/astro/src/core/create-vite.ts +++ b/packages/astro/src/core/create-vite.ts @@ -133,7 +133,9 @@ export async function createVite( root: fileURLToPath(settings.config.root), envPrefix: 'PUBLIC_', define: { - 'import.meta.env.SITE': settings.config.site ? `'${settings.config.site}'` : 'undefined', + 'import.meta.env.SITE': settings.config.site + ? JSON.stringify(settings.config.site) + : 'undefined', }, server: { hmr: diff --git a/packages/astro/src/core/dev/container.ts b/packages/astro/src/core/dev/container.ts index 87cd2b7ad..e059b1d06 100644 --- a/packages/astro/src/core/dev/container.ts +++ b/packages/astro/src/core/dev/container.ts @@ -82,7 +82,7 @@ export async function createContainer(params: CreateContainerParams = {}): Promi }, define: { 'import.meta.env.BASE_URL': settings.config.base - ? `'${settings.config.base}'` + ? JSON.stringify(settings.config.base) : 'undefined', }, }, diff --git a/packages/astro/src/vite-plugin-env/index.ts b/packages/astro/src/vite-plugin-env/index.ts index fdde4b59e..2540299fa 100644 --- a/packages/astro/src/vite-plugin-env/index.ts +++ b/packages/astro/src/vite-plugin-env/index.ts @@ -37,9 +37,9 @@ function getPrivateEnv( } } } - privateEnv.SITE = astroConfig.site ? `'${astroConfig.site}'` : 'undefined'; + privateEnv.SITE = astroConfig.site ? JSON.stringify(astroConfig.site) : 'undefined'; privateEnv.SSR = JSON.stringify(true); - privateEnv.BASE_URL = astroConfig.base ? `'${astroConfig.base}'` : 'undefined'; + privateEnv.BASE_URL = astroConfig.base ? JSON.stringify(astroConfig.base) : 'undefined'; return privateEnv; } diff --git a/packages/astro/src/vite-plugin-markdown-legacy/index.ts b/packages/astro/src/vite-plugin-markdown-legacy/index.ts index 5203a2b3f..4691957f5 100644 --- a/packages/astro/src/vite-plugin-markdown-legacy/index.ts +++ b/packages/astro/src/vite-plugin-markdown-legacy/index.ts @@ -174,8 +174,8 @@ export default function markdown({ settings }: AstroPluginOptions): Plugin { const prelude = `--- import Slugger from 'github-slugger'; -${layout ? `import Layout from '${layout}';` : ''} -${components ? `import * from '${components}';` : ''} +${layout ? `import Layout from ${JSON.stringify(layout)};` : ''} +${components ? `import * from ${JSON.stringify(components)};` : ''} ${setup} const slugger = new Slugger(); @@ -193,7 +193,7 @@ Object.defineProperty($$content.astro, 'headers', { }); ---`; - const imports = `${layout ? `import Layout from '${layout}';` : ''} + const imports = `${layout ? `import Layout from ${JSON.stringify(layout)};` : ''} ${setup}`.trim(); // If the user imported "Layout", wrap the content in a Layout diff --git a/packages/astro/src/vite-plugin-markdown/index.ts b/packages/astro/src/vite-plugin-markdown/index.ts index 67ad4c1e0..b879be70b 100644 --- a/packages/astro/src/vite-plugin-markdown/index.ts +++ b/packages/astro/src/vite-plugin-markdown/index.ts @@ -95,7 +95,7 @@ export default function markdown({ settings, logging }: AstroPluginOptions): Plu } const code = escapeViteEnvReferences(` - import { Fragment, jsx as h } from '${astroJsxRuntimeModulePath}'; + import { Fragment, jsx as h } from ${JSON.stringify(astroJsxRuntimeModulePath)}; ${layout ? `import Layout from ${JSON.stringify(layout)};` : ''} const html = ${JSON.stringify(html)}; diff --git a/packages/astro/test/astro-pages.test.js b/packages/astro/test/astro-pages.test.js index 9d365adb7..624560cc9 100644 --- a/packages/astro/test/astro-pages.test.js +++ b/packages/astro/test/astro-pages.test.js @@ -21,6 +21,13 @@ describe('Pages', () => { expect($('h1').text()).to.equal('Name with index'); }); + + it('Can find page with quotes in file name', async () => { + const html = await fixture.readFile("/quotes'-work-too/index.html"); + const $ = cheerio.load(html); + + expect($('h1').text()).to.equal("Quotes work too"); + }); }); if (isWindows) return; diff --git a/packages/astro/test/fixtures/astro pages/src/pages/quotes'-work-too.md b/packages/astro/test/fixtures/astro pages/src/pages/quotes'-work-too.md new file mode 100644 index 000000000..9cfd878a7 --- /dev/null +++ b/packages/astro/test/fixtures/astro pages/src/pages/quotes'-work-too.md @@ -0,0 +1 @@ +# Quotes work too