Skip to content

Commit

Permalink
feat(prerender): support quoted true / false values and 0 / 1 (#6772)
Browse files Browse the repository at this point in the history
  • Loading branch information
natemoo-re authored Apr 6, 2023
1 parent 9b497a6 commit 45bff6f
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/cuddly-eels-melt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Allow `import.meta.env` values of `0`, `1`, `true`, and `false` to be used for `export const prerender` statements
23 changes: 21 additions & 2 deletions packages/astro/src/vite-plugin-scanner/scan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,25 @@ function includesExport(code: string) {
return false;
}

// Support quoted values to allow statically known `import.meta.env` variables to be used
function isQuoted(value: string) {
return (value[0] === '"' || value[0] === "'") && value[value.length - 1] === value[0]
}

function isTruthy(value: string) {
if (isQuoted(value)) {
value = value.slice(1, -1);
}
return value === 'true' || value === '1';
}

function isFalsy(value: string) {
if (isQuoted(value)) {
value = value.slice(1, -1);
}
return value === 'false' || value === '0';
}

let didInit = false;

export async function scan(code: string, id: string): Promise<PageOptions> {
Expand All @@ -39,14 +58,14 @@ export async function scan(code: string, id: string): Promise<PageOptions> {
// For a given export, check the value of the first non-whitespace token.
// Basically extract the `true` from the statement `export const prerender = true`
const suffix = code.slice(endOfLocalName).trim().replace(/\=/, '').trim().split(/[;\n]/)[0];
if (prefix !== 'const' || !(suffix === 'true' || suffix === 'false')) {
if (prefix !== 'const' || !(isTruthy(suffix) || isFalsy(suffix))) {
throw new AstroError({
...AstroErrorData.InvalidPrerenderExport,
message: AstroErrorData.InvalidPrerenderExport.message(prefix, suffix),
location: { file: id },
});
} else {
pageOptions[name as keyof PageOptions] = suffix === 'true';
pageOptions[name as keyof PageOptions] = isTruthy(suffix);
}
}
}
Expand Down
30 changes: 30 additions & 0 deletions packages/astro/test/units/vite-plugin-scanner/scan.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,36 @@ describe('astro scan', () => {
expect(result.prerender).to.equal(false);
});

it('recognizes single quoted boolean (\'true\')', async () => {
const result = await scan(`export const prerender = 'true';`, '/src/components/index.astro');
expect(result.prerender).to.equal(true);
});

it('recognizes double quoted boolean ("true")', async () => {
const result = await scan(`export const prerender = "true";`, '/src/components/index.astro');
expect(result.prerender).to.equal(true);
});

it('recognizes double quoted boolean ("false")', async () => {
const result = await scan(`export const prerender = "false";`, '/src/components/index.astro');
expect(result.prerender).to.equal(false);
});

it('recognizes single quoted boolean (\'false\')', async () => {
const result = await scan(`export const prerender = 'false';`, '/src/components/index.astro');
expect(result.prerender).to.equal(false);
});

it('recognizes number (1)', async () => {
const result = await scan(`export const prerender = 1;`, '/src/components/index.astro');
expect(result.prerender).to.equal(true);
});

it('recognizes number (0)', async () => {
const result = await scan(`export const prerender = 0;`, '/src/components/index.astro');
expect(result.prerender).to.equal(false);
});

it('throws on let boolean literal', async () => {
try {
const result = await scan(`export let prerender = true;`, '/src/components/index.astro');
Expand Down

0 comments on commit 45bff6f

Please sign in to comment.