feat(prerender): support quoted true / false values and 0 / 1 (#6772)
This commit is contained in:
parent
9b497a6d98
commit
45bff6fccb
3 changed files with 56 additions and 2 deletions
5
.changeset/cuddly-eels-melt.md
Normal file
5
.changeset/cuddly-eels-melt.md
Normal file
|
@ -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
|
|
@ -13,6 +13,25 @@ function includesExport(code: string) {
|
||||||
return false;
|
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;
|
let didInit = false;
|
||||||
|
|
||||||
export async function scan(code: string, id: string): Promise<PageOptions> {
|
export async function scan(code: string, id: string): Promise<PageOptions> {
|
||||||
|
@ -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.
|
// For a given export, check the value of the first non-whitespace token.
|
||||||
// Basically extract the `true` from the statement `export const prerender = true`
|
// Basically extract the `true` from the statement `export const prerender = true`
|
||||||
const suffix = code.slice(endOfLocalName).trim().replace(/\=/, '').trim().split(/[;\n]/)[0];
|
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({
|
throw new AstroError({
|
||||||
...AstroErrorData.InvalidPrerenderExport,
|
...AstroErrorData.InvalidPrerenderExport,
|
||||||
message: AstroErrorData.InvalidPrerenderExport.message(prefix, suffix),
|
message: AstroErrorData.InvalidPrerenderExport.message(prefix, suffix),
|
||||||
location: { file: id },
|
location: { file: id },
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
pageOptions[name as keyof PageOptions] = suffix === 'true';
|
pageOptions[name as keyof PageOptions] = isTruthy(suffix);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,6 +17,36 @@ describe('astro scan', () => {
|
||||||
expect(result.prerender).to.equal(false);
|
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 () => {
|
it('throws on let boolean literal', async () => {
|
||||||
try {
|
try {
|
||||||
const result = await scan(`export let prerender = true;`, '/src/components/index.astro');
|
const result = await scan(`export let prerender = true;`, '/src/components/index.astro');
|
||||||
|
|
Loading…
Reference in a new issue