[ci] format
This commit is contained in:
parent
d2960984c5
commit
d1abb63a64
18 changed files with 89 additions and 46 deletions
|
@ -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
|
||||
|
|
|
@ -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':
|
||||
|
|
|
@ -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 `))}`);
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -52,7 +52,7 @@ export function vitePluginInternals(input: Set<string>, 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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
|
|||
}
|
||||
};
|
||||
},
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -105,9 +105,7 @@ export function resolveFlags(flags: Partial<Flags>): CLIFlags {
|
|||
? flags.experimentalErrorOverlay
|
||||
: undefined,
|
||||
experimentalPrerender:
|
||||
typeof flags.experimentalPrerender === 'boolean'
|
||||
? flags.experimentalPrerender
|
||||
: undefined,
|
||||
typeof flags.experimentalPrerender === 'boolean' ? flags.experimentalPrerender : undefined,
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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`.',
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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<PageOptions> {
|
|||
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';
|
||||
|
|
|
@ -70,7 +70,7 @@ const name = 'world
|
|||
const result = await compile(`<h1>Hello World</h1>`, '/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');
|
||||
|
|
|
@ -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.`
|
||||
);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue