Fix code generation quotes handling (#5678)

This commit is contained in:
Bjorn Lu 2022-12-28 00:12:03 +08:00 committed by GitHub
parent 376f67011d
commit f8f5768294
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 25 additions and 10 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Fix code generation quotes handling

View file

@ -27,8 +27,8 @@ export function vitePluginPages(opts: StaticBuildOptions, internals: BuildIntern
let i = 0; let i = 0;
for (const pageData of eachPageData(internals)) { for (const pageData of eachPageData(internals)) {
const variable = `_page${i}`; const variable = `_page${i}`;
imports.push(`import * as ${variable} from '${pageData.moduleSpecifier}';`); imports.push(`import * as ${variable} from ${JSON.stringify(pageData.moduleSpecifier)};`);
importMap += `['${pageData.component}', ${variable}],`; importMap += `[${JSON.stringify(pageData.component)}, ${variable}],`;
i++; i++;
} }

View file

@ -133,7 +133,9 @@ export async function createVite(
root: fileURLToPath(settings.config.root), root: fileURLToPath(settings.config.root),
envPrefix: 'PUBLIC_', envPrefix: 'PUBLIC_',
define: { 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: { server: {
hmr: hmr:

View file

@ -82,7 +82,7 @@ export async function createContainer(params: CreateContainerParams = {}): Promi
}, },
define: { define: {
'import.meta.env.BASE_URL': settings.config.base 'import.meta.env.BASE_URL': settings.config.base
? `'${settings.config.base}'` ? JSON.stringify(settings.config.base)
: 'undefined', : 'undefined',
}, },
}, },

View file

@ -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.SSR = JSON.stringify(true);
privateEnv.BASE_URL = astroConfig.base ? `'${astroConfig.base}'` : 'undefined'; privateEnv.BASE_URL = astroConfig.base ? JSON.stringify(astroConfig.base) : 'undefined';
return privateEnv; return privateEnv;
} }

View file

@ -174,8 +174,8 @@ export default function markdown({ settings }: AstroPluginOptions): Plugin {
const prelude = `--- const prelude = `---
import Slugger from 'github-slugger'; import Slugger from 'github-slugger';
${layout ? `import Layout from '${layout}';` : ''} ${layout ? `import Layout from ${JSON.stringify(layout)};` : ''}
${components ? `import * from '${components}';` : ''} ${components ? `import * from ${JSON.stringify(components)};` : ''}
${setup} ${setup}
const slugger = new Slugger(); 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(); ${setup}`.trim();
// If the user imported "Layout", wrap the content in a Layout // If the user imported "Layout", wrap the content in a Layout

View file

@ -95,7 +95,7 @@ export default function markdown({ settings, logging }: AstroPluginOptions): Plu
} }
const code = escapeViteEnvReferences(` 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)};` : ''} ${layout ? `import Layout from ${JSON.stringify(layout)};` : ''}
const html = ${JSON.stringify(html)}; const html = ${JSON.stringify(html)};

View file

@ -21,6 +21,13 @@ describe('Pages', () => {
expect($('h1').text()).to.equal('Name with index'); 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; if (isWindows) return;

View file

@ -0,0 +1 @@
# Quotes work too