diff --git a/packages/astro/src/@types/astro.ts b/packages/astro/src/@types/astro.ts index d5ff981d7..0a1eee62a 100644 --- a/packages/astro/src/@types/astro.ts +++ b/packages/astro/src/@types/astro.ts @@ -896,7 +896,7 @@ export interface AstroUserConfig { astroFlavoredMarkdown?: boolean; }; - /** + /** * @docs * @kind heading * @name Experimental Flags @@ -906,9 +906,9 @@ export interface AstroUserConfig { */ experimental?: { /** - * @hidden - * Turn on experimental support for the new error overlay component. - */ + * @hidden + * Turn on experimental support for the new error overlay component. + */ errorOverlay?: boolean; /** * @docs diff --git a/packages/astro/src/core/build/common.ts b/packages/astro/src/core/build/common.ts index 642db2fe5..74be830f0 100644 --- a/packages/astro/src/core/build/common.ts +++ b/packages/astro/src/core/build/common.ts @@ -1,5 +1,4 @@ import npath from 'path'; -import { createHash } from 'crypto' import { fileURLToPath, pathToFileURL } from 'url'; import type { AstroConfig, RouteType } from '../../@types/astro'; import { appendForwardSlash } from '../../core/path.js'; @@ -46,7 +45,7 @@ export function getOutFile( astroConfig: AstroConfig, outFolder: URL, pathname: string, - routeType: RouteType, + routeType: RouteType ): URL { switch (routeType) { case 'endpoint': diff --git a/packages/astro/src/core/build/generate.ts b/packages/astro/src/core/build/generate.ts index ba95faadc..a20b33e18 100644 --- a/packages/astro/src/core/build/generate.ts +++ b/packages/astro/src/core/build/generate.ts @@ -30,8 +30,8 @@ import { matchRoute } from '../routing/match.js'; import { getOutputFilename } from '../util.js'; import { getOutDirWithinCwd, getOutFile, getOutFolder } from './common.js'; import { - eachPrerenderedPageData, eachPageData, + eachPrerenderedPageData, getPageDataByComponent, sortedCSS, } from './internal.js'; @@ -79,7 +79,12 @@ export async function generatePages(opts: StaticBuildOptions, internals: BuildIn const serverEntry = opts.buildConfig.serverEntry; const outFolder = ssr ? opts.buildConfig.server : getOutDirWithinCwd(opts.settings.config.outDir); - if (opts.settings.config.experimental.prerender && opts.settings.config.output === 'server' && !hasPrerenderedPages(internals)) return; + if ( + opts.settings.config.experimental.prerender && + opts.settings.config.output === 'server' && + !hasPrerenderedPages(internals) + ) + return; const verb = ssr ? 'prerendering' : 'generating'; info(opts.logging, null, `\n${bgGreen(black(` ${verb} static routes `))}`); diff --git a/packages/astro/src/core/build/internal.ts b/packages/astro/src/core/build/internal.ts index 9a7f97326..dc660f494 100644 --- a/packages/astro/src/core/build/internal.ts +++ b/packages/astro/src/core/build/internal.ts @@ -1,9 +1,9 @@ import type { OutputChunk, RenderedChunk } from 'rollup'; -import type { PageBuildData, PageOutput, ViteID } from './types'; +import type { PageBuildData, ViteID } from './types'; +import { PageOptions } from '../../vite-plugin-astro/types'; import { prependForwardSlash, removeFileExtension } from '../path.js'; import { viteID } from '../util.js'; -import { PageOptions } from '../../vite-plugin-astro/types'; export interface BuildInternals { /** @@ -205,10 +205,10 @@ export function* eachPageData(internals: BuildInternals) { export function hasPrerenderedPages(internals: BuildInternals) { for (const id of internals.pagesByViteID.keys()) { if (internals.pageOptionsByPage.get(id)?.prerender) { - return true + return true; } } - return false + return false; } export function* eachPrerenderedPageData(internals: BuildInternals) { diff --git a/packages/astro/src/core/build/static-build.ts b/packages/astro/src/core/build/static-build.ts index 59941f089..c184652bf 100644 --- a/packages/astro/src/core/build/static-build.ts +++ b/packages/astro/src/core/build/static-build.ts @@ -1,3 +1,4 @@ +import * as eslexer from 'es-module-lexer'; import glob from 'fast-glob'; import fs from 'fs'; import { bgGreen, bgMagenta, black, dim } from 'kleur/colors'; @@ -22,13 +23,12 @@ import { trackPageData } from './internal.js'; import type { PageBuildData, StaticBuildOptions } from './types'; import { getTimeStat } from './util.js'; import { vitePluginAnalyzer } from './vite-plugin-analyzer.js'; -import { vitePluginPrerender } from './vite-plugin-prerender.js'; import { rollupPluginAstroBuildCSS } from './vite-plugin-css.js'; import { vitePluginHoistedScripts } from './vite-plugin-hoisted-scripts.js'; import { vitePluginInternals } from './vite-plugin-internals.js'; import { vitePluginPages } from './vite-plugin-pages.js'; +import { vitePluginPrerender } from './vite-plugin-prerender.js'; import { injectManifest, vitePluginSSR } from './vite-plugin-ssr.js'; -import * as eslexer from 'es-module-lexer'; export async function staticBuild(opts: StaticBuildOptions) { const { allPages, settings } = opts; diff --git a/packages/astro/src/core/build/types.ts b/packages/astro/src/core/build/types.ts index fb5860015..57518fb42 100644 --- a/packages/astro/src/core/build/types.ts +++ b/packages/astro/src/core/build/types.ts @@ -14,7 +14,7 @@ import type { RouteCache } from '../render/route-cache'; export type ComponentPath = string; export type ViteID = string; -export type PageOutput = AstroConfig['output'] +export type PageOutput = AstroConfig['output']; export interface PageBuildData { component: ComponentPath; diff --git a/packages/astro/src/core/build/vite-plugin-internals.ts b/packages/astro/src/core/build/vite-plugin-internals.ts index 6e6b1e90c..dfd346e77 100644 --- a/packages/astro/src/core/build/vite-plugin-internals.ts +++ b/packages/astro/src/core/build/vite-plugin-internals.ts @@ -52,7 +52,7 @@ export function vitePluginInternals(input: Set, internals: BuildInternal for (const id of Object.keys(chunk.modules)) { const pageData = internals.pagesByViteID.get(id); if (pageData) { - internals.pageToBundleMap.set(pageData.moduleSpecifier, chunk.fileName) + internals.pageToBundleMap.set(pageData.moduleSpecifier, chunk.fileName); } } } diff --git a/packages/astro/src/core/build/vite-plugin-pages.ts b/packages/astro/src/core/build/vite-plugin-pages.ts index 83e9f4435..98d10b2c8 100644 --- a/packages/astro/src/core/build/vite-plugin-pages.ts +++ b/packages/astro/src/core/build/vite-plugin-pages.ts @@ -1,8 +1,7 @@ import type { Plugin as VitePlugin } from 'vite'; import { pagesVirtualModuleId, resolvedPagesVirtualModuleId } from '../app/index.js'; import { addRollupInput } from './add-rollup-input.js'; -import { BuildInternals, hasPrerenderedPages } from './internal.js'; -import { eachPageData } from './internal.js'; +import { BuildInternals, eachPageData, hasPrerenderedPages } from './internal.js'; import type { StaticBuildOptions } from './types'; export function vitePluginPages(opts: StaticBuildOptions, internals: BuildInternals): VitePlugin { diff --git a/packages/astro/src/core/build/vite-plugin-prerender.ts b/packages/astro/src/core/build/vite-plugin-prerender.ts index 974bdf41f..4ad343200 100644 --- a/packages/astro/src/core/build/vite-plugin-prerender.ts +++ b/packages/astro/src/core/build/vite-plugin-prerender.ts @@ -2,7 +2,10 @@ import type { Plugin as VitePlugin } from 'vite'; import type { BuildInternals } from './internal.js'; import type { StaticBuildOptions } from './types'; -export function vitePluginPrerender(opts: StaticBuildOptions, internals: BuildInternals): VitePlugin { +export function vitePluginPrerender( + opts: StaticBuildOptions, + internals: BuildInternals +): VitePlugin { return { name: 'astro:rollup-plugin-prerender', @@ -39,5 +42,5 @@ export function vitePluginPrerender(opts: StaticBuildOptions, internals: BuildIn } }; }, - } + }; } diff --git a/packages/astro/src/core/build/vite-plugin-ssr.ts b/packages/astro/src/core/build/vite-plugin-ssr.ts index 03a66e126..71a3b513a 100644 --- a/packages/astro/src/core/build/vite-plugin-ssr.ts +++ b/packages/astro/src/core/build/vite-plugin-ssr.ts @@ -13,8 +13,8 @@ import { pagesVirtualModuleId } from '../app/index.js'; import { removeLeadingForwardSlash, removeTrailingForwardSlash } from '../path.js'; import { serializeRouteData } from '../routing/index.js'; import { addRollupInput } from './add-rollup-input.js'; -import { eachServerPageData, eachPrerenderedPageData, sortedCSS } from './internal.js'; import { getOutFile, getOutFolder } from './common.js'; +import { eachPrerenderedPageData, eachServerPageData, sortedCSS } from './internal.js'; export const virtualModuleId = '@astrojs-ssr-virtual-entry'; const resolvedVirtualModuleId = '\0' + virtualModuleId; @@ -140,8 +140,17 @@ function buildManifest( const joinBase = (pth: string) => (bareBase ? bareBase + '/' + pth : pth); for (const pageData of eachPrerenderedPageData(internals)) { - const outFolder = getOutFolder(opts.settings.config, pageData.route.pathname!, pageData.route.type); - const outFile = getOutFile(opts.settings.config, outFolder, pageData.route.pathname!, pageData.route.type); + const outFolder = getOutFolder( + opts.settings.config, + pageData.route.pathname!, + pageData.route.type + ); + const outFile = getOutFile( + opts.settings.config, + outFolder, + pageData.route.pathname!, + pageData.route.type + ); const file = outFile.toString().replace(opts.settings.config.build.client.toString(), ''); routes.push({ file, diff --git a/packages/astro/src/core/config/config.ts b/packages/astro/src/core/config/config.ts index 3524455f6..6616a8213 100644 --- a/packages/astro/src/core/config/config.ts +++ b/packages/astro/src/core/config/config.ts @@ -105,9 +105,7 @@ export function resolveFlags(flags: Partial): CLIFlags { ? flags.experimentalErrorOverlay : undefined, experimentalPrerender: - typeof flags.experimentalPrerender === 'boolean' - ? flags.experimentalPrerender - : undefined, + typeof flags.experimentalPrerender === 'boolean' ? flags.experimentalPrerender : undefined, }; } diff --git a/packages/astro/src/core/create-vite.ts b/packages/astro/src/core/create-vite.ts index 9b6b9b3ab..a54e3ea7e 100644 --- a/packages/astro/src/core/create-vite.ts +++ b/packages/astro/src/core/create-vite.ts @@ -17,10 +17,10 @@ import jsxVitePlugin from '../vite-plugin-jsx/index.js'; import astroLoadFallbackPlugin from '../vite-plugin-load-fallback/index.js'; import legacyMarkdownVitePlugin from '../vite-plugin-markdown-legacy/index.js'; import markdownVitePlugin from '../vite-plugin-markdown/index.js'; +import astroScannerPlugin from '../vite-plugin-scanner/index.js'; import astroScriptsPlugin from '../vite-plugin-scripts/index.js'; import astroScriptsPageSSRPlugin from '../vite-plugin-scripts/page-ssr.js'; import { createCustomViteLogger } from './errors/dev/index.js'; -import astroScannerPlugin from '../vite-plugin-scanner/index.js'; import { resolveDependency } from './util.js'; interface CreateViteOptions { diff --git a/packages/astro/src/core/errors/errors-data.ts b/packages/astro/src/core/errors/errors-data.ts index fb219d876..959bdde28 100644 --- a/packages/astro/src/core/errors/errors-data.ts +++ b/packages/astro/src/core/errors/errors-data.ts @@ -403,8 +403,8 @@ See https://docs.astro.build/en/guides/server-side-rendering/ for more informati code: 3019, message: (prefix: string, suffix: string) => { let msg = `A \`prerender\` export has been detected, but its value cannot be statically analyzed.`; - if (prefix !== 'const') msg += `\nExpected \`const\` declaration but got \`${prefix}\`.` - if (suffix !== 'true') msg += `\nExpected \`true\` value but got \`${suffix}\`.` + if (prefix !== 'const') msg += `\nExpected \`const\` declaration but got \`${prefix}\`.`; + if (suffix !== 'true') msg += `\nExpected \`true\` value but got \`${suffix}\`.`; return msg; }, hint: 'Mutable values declared at runtime are not supported. Please make sure to use exactly `export const prerender = true`.', diff --git a/packages/astro/src/integrations/index.ts b/packages/astro/src/integrations/index.ts index 1769cf90b..e7ce7e51d 100644 --- a/packages/astro/src/integrations/index.ts +++ b/packages/astro/src/integrations/index.ts @@ -1,6 +1,6 @@ -import fs from 'node:fs'; import { bold } from 'kleur/colors'; import type { AddressInfo } from 'net'; +import fs from 'node:fs'; import { fileURLToPath } from 'node:url'; import type { InlineConfig, ViteDevServer } from 'vite'; import { diff --git a/packages/astro/src/vite-plugin-scanner/index.ts b/packages/astro/src/vite-plugin-scanner/index.ts index 1213652a1..35579f22f 100644 --- a/packages/astro/src/vite-plugin-scanner/index.ts +++ b/packages/astro/src/vite-plugin-scanner/index.ts @@ -1,12 +1,18 @@ import { Plugin as VitePlugin } from 'vite'; import { AstroSettings } from '../@types/astro.js'; -import { isPage, isEndpoint } from '../core/util.js'; import type { LogOptions } from '../core/logger/core.js'; +import { isEndpoint, isPage } from '../core/util.js'; import { normalizeFilename } from '../vite-plugin-utils/index.js'; import { scan } from './scan.js'; -export default function astroScannerPlugin({ settings, logging }: { settings: AstroSettings, logging: LogOptions }): VitePlugin { +export default function astroScannerPlugin({ + settings, + logging, +}: { + settings: AstroSettings; + logging: LogOptions; +}): VitePlugin { return { name: 'astro:scanner', enforce: 'post', @@ -26,7 +32,7 @@ export default function astroScannerPlugin({ settings, logging }: { settings: As const fileIsPage = isPage(fileURL, settings); const fileIsEndpoint = isEndpoint(fileURL, settings); if (!(fileIsPage || fileIsEndpoint)) return; - const pageOptions = await scan(code, id) + const pageOptions = await scan(code, id); const { meta = {} } = this.getModuleInfo(id) ?? {}; return { diff --git a/packages/astro/src/vite-plugin-scanner/scan.ts b/packages/astro/src/vite-plugin-scanner/scan.ts index 51529e904..28e453f43 100644 --- a/packages/astro/src/vite-plugin-scanner/scan.ts +++ b/packages/astro/src/vite-plugin-scanner/scan.ts @@ -1,6 +1,6 @@ import * as eslexer from 'es-module-lexer'; +import { AstroError, AstroErrorData } from '../core/errors/index.js'; import { PageOptions } from '../vite-plugin-astro/types.js'; -import { AstroError, AstroErrorCodes, AstroErrorData } from '../core/errors/index.js' const BOOLEAN_EXPORTS = new Set(['prerender']); @@ -29,15 +29,21 @@ export async function scan(code: string, id: string): Promise { if (BOOLEAN_EXPORTS.has(name)) { // For a given export, check the value of the local declaration // Basically extract the `const` from the statement `export const prerender = true` - const prefix = code.slice(0, endOfLocalName).split('export').pop()!.trim().replace('prerender', '').trim(); - // For a given export, check the value of the first non-whitespace token. + const prefix = code + .slice(0, endOfLocalName) + .split('export') + .pop()! + .trim() + .replace('prerender', '') + .trim(); + // 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')) { throw new AstroError({ ...AstroErrorData.InvalidPrerenderExport, message: AstroErrorData.InvalidPrerenderExport.message(prefix, suffix), - location: { file: id } + location: { file: id }, }); } else { pageOptions[name as keyof PageOptions] = suffix === 'true'; diff --git a/packages/astro/test/units/vite-plugin-astro/compile.test.js b/packages/astro/test/units/vite-plugin-astro/compile.test.js index db3f4d666..7f3eb20d7 100644 --- a/packages/astro/test/units/vite-plugin-astro/compile.test.js +++ b/packages/astro/test/units/vite-plugin-astro/compile.test.js @@ -70,7 +70,7 @@ const name = 'world const result = await compile(`

Hello World

`, '/src/components/index.astro'); await init; const [, exports] = parse(result.code); - const names = exports.map(e => e.n); + const names = exports.map((e) => e.n); expect(names).to.include('default'); expect(names).to.include('file'); expect(names).to.include('url'); diff --git a/packages/astro/test/units/vite-plugin-scanner/scan.test.js b/packages/astro/test/units/vite-plugin-scanner/scan.test.js index f27286da4..8bd1f0857 100644 --- a/packages/astro/test/units/vite-plugin-scanner/scan.test.js +++ b/packages/astro/test/units/vite-plugin-scanner/scan.test.js @@ -23,7 +23,9 @@ describe('astro scan', () => { expect(false).to.be.true; } catch (e) { expect(e.errorCode).to.equal(3019); - expect(e.message).to.contain(`A \`prerender\` export has been detected, but its value cannot be statically analyzed.`); + expect(e.message).to.contain( + `A \`prerender\` export has been detected, but its value cannot be statically analyzed.` + ); } }); @@ -33,7 +35,9 @@ describe('astro scan', () => { expect(false).to.be.true; } catch (e) { expect(e.errorCode).to.equal(3019); - expect(e.message).to.contain(`A \`prerender\` export has been detected, but its value cannot be statically analyzed.`); + expect(e.message).to.contain( + `A \`prerender\` export has been detected, but its value cannot be statically analyzed.` + ); } }); @@ -43,7 +47,9 @@ describe('astro scan', () => { expect(false).to.be.true; } catch (e) { expect(e.errorCode).to.equal(3019); - expect(e.message).to.contain(`A \`prerender\` export has been detected, but its value cannot be statically analyzed.`); + expect(e.message).to.contain( + `A \`prerender\` export has been detected, but its value cannot be statically analyzed.` + ); } }); @@ -53,27 +59,39 @@ describe('astro scan', () => { expect(false).to.be.true; } catch (e) { expect(e.errorCode).to.equal(3019); - expect(e.message).to.contain(`A \`prerender\` export has been detected, but its value cannot be statically analyzed.`); + expect(e.message).to.contain( + `A \`prerender\` export has been detected, but its value cannot be statically analyzed.` + ); } }); it('throws on unknown values III', async () => { try { - const result = await scan(`export let prerender = undefined; prerender = true;`, '/src/components/index.astro'); + const result = await scan( + `export let prerender = undefined; prerender = true;`, + '/src/components/index.astro' + ); expect(false).to.be.true; } catch (e) { expect(e.errorCode).to.equal(3019); - expect(e.message).to.contain(`A \`prerender\` export has been detected, but its value cannot be statically analyzed.`); + expect(e.message).to.contain( + `A \`prerender\` export has been detected, but its value cannot be statically analyzed.` + ); } }); it('throws on unknown values IV', async () => { try { - const result = await scan(`let prerender = true; export { prerender }`, '/src/components/index.astro'); + const result = await scan( + `let prerender = true; export { prerender }`, + '/src/components/index.astro' + ); expect(false).to.be.true; } catch (e) { expect(e.errorCode).to.equal(3019); - expect(e.message).to.contain(`A \`prerender\` export has been detected, but its value cannot be statically analyzed.`); + expect(e.message).to.contain( + `A \`prerender\` export has been detected, but its value cannot be statically analyzed.` + ); } }); });