Skip to content

Commit

Permalink
[ci] format
Browse files Browse the repository at this point in the history
  • Loading branch information
natemoo-re authored and astrobot-houston committed Dec 16, 2022
1 parent d296098 commit d1abb63
Show file tree
Hide file tree
Showing 18 changed files with 89 additions and 46 deletions.
8 changes: 4 additions & 4 deletions packages/astro/src/@types/astro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -896,7 +896,7 @@ export interface AstroUserConfig {
astroFlavoredMarkdown?: boolean;
};

/**
/**
* @docs
* @kind heading
* @name Experimental Flags
Expand All @@ -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
Expand Down
3 changes: 1 addition & 2 deletions packages/astro/src/core/build/common.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -46,7 +45,7 @@ export function getOutFile(
astroConfig: AstroConfig,
outFolder: URL,
pathname: string,
routeType: RouteType,
routeType: RouteType
): URL {
switch (routeType) {
case 'endpoint':
Expand Down
9 changes: 7 additions & 2 deletions packages/astro/src/core/build/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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 `))}`);
Expand Down
8 changes: 4 additions & 4 deletions packages/astro/src/core/build/internal.ts
Original file line number Diff line number Diff line change
@@ -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 {
/**
Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions packages/astro/src/core/build/static-build.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion packages/astro/src/core/build/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion packages/astro/src/core/build/vite-plugin-internals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions packages/astro/src/core/build/vite-plugin-pages.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
7 changes: 5 additions & 2 deletions packages/astro/src/core/build/vite-plugin-prerender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',

Expand Down Expand Up @@ -39,5 +42,5 @@ export function vitePluginPrerender(opts: StaticBuildOptions, internals: BuildIn
}
};
},
}
};
}
15 changes: 12 additions & 3 deletions packages/astro/src/core/build/vite-plugin-ssr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 1 addition & 3 deletions packages/astro/src/core/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
}

Expand Down
2 changes: 1 addition & 1 deletion packages/astro/src/core/create-vite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions packages/astro/src/core/errors/errors-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`.',
Expand Down
2 changes: 1 addition & 1 deletion packages/astro/src/integrations/index.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
12 changes: 9 additions & 3 deletions packages/astro/src/vite-plugin-scanner/index.ts
Original file line number Diff line number Diff line change
@@ -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',
Expand All @@ -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 {
Expand Down
14 changes: 10 additions & 4 deletions packages/astro/src/vite-plugin-scanner/scan.ts
Original file line number Diff line number Diff line change
@@ -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']);

Expand Down Expand Up @@ -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';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
34 changes: 26 additions & 8 deletions packages/astro/test/units/vite-plugin-scanner/scan.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.`
);
}
});

Expand All @@ -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.`
);
}
});

Expand All @@ -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.`
);
}
});

Expand All @@ -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.`
);
}
});
});

0 comments on commit d1abb63

Please sign in to comment.