[ci] format

This commit is contained in:
matthewp 2023-02-03 13:06:16 +00:00 committed by fredkbot
parent 40bf96f05d
commit a0efdc8f76
14 changed files with 111 additions and 100 deletions

View file

@ -106,9 +106,9 @@ export function astroConfigBuildPlugin(internals: BuildInternals): AstroBuildPlu
hooks: { hooks: {
'build:before': () => { 'build:before': () => {
return { return {
vitePlugin: astroContentProdBundlePlugin({ internals }) vitePlugin: astroContentProdBundlePlugin({ internals }),
}; };
} },
} },
}; };
} }

View file

@ -3,7 +3,7 @@ import type { BuildInternals } from './internal';
import type { StaticBuildOptions, ViteBuildReturn } from './types'; import type { StaticBuildOptions, ViteBuildReturn } from './types';
type RollupOutputArray = Extract<ViteBuildReturn, Array<any>>; type RollupOutputArray = Extract<ViteBuildReturn, Array<any>>;
type OutputChunkorAsset = RollupOutputArray[number]['output'][number] type OutputChunkorAsset = RollupOutputArray[number]['output'][number];
type OutputChunk = Extract<OutputChunkorAsset, { type: 'chunk' }>; type OutputChunk = Extract<OutputChunkorAsset, { type: 'chunk' }>;
type MutateChunk = (chunk: OutputChunk, build: 'server' | 'client', newCode: string) => void; type MutateChunk = (chunk: OutputChunk, build: 'server' | 'client', newCode: string) => void;
@ -11,15 +11,16 @@ type MutateChunk = (chunk: OutputChunk, build: 'server' | 'client', newCode: str
export type AstroBuildPlugin = { export type AstroBuildPlugin = {
build: 'ssr' | 'client' | 'both'; build: 'ssr' | 'client' | 'both';
hooks?: { hooks?: {
'build:before'?: (opts: { 'build:before'?: (opts: { build: 'ssr' | 'client'; input: Set<string> }) => {
build: 'ssr' | 'client';
input: Set<string>;
}) => {
enforce?: 'after-user-plugins'; enforce?: 'after-user-plugins';
vitePlugin: VitePlugin | VitePlugin[] | undefined vitePlugin: VitePlugin | VitePlugin[] | undefined;
}; };
'build:post'?: (opts: {ssrOutputs: RollupOutputArray; clientOutputs: RollupOutputArray; mutate: MutateChunk}) => void | Promise<void>; 'build:post'?: (opts: {
} ssrOutputs: RollupOutputArray;
clientOutputs: RollupOutputArray;
mutate: MutateChunk;
}) => void | Promise<void>;
};
}; };
export function createPluginContainer(options: StaticBuildOptions, internals: BuildInternals) { export function createPluginContainer(options: StaticBuildOptions, internals: BuildInternals) {
@ -32,7 +33,7 @@ export function createPluginContainer(options: StaticBuildOptions, internals: Bu
internals, internals,
register(plugin: AstroBuildPlugin) { register(plugin: AstroBuildPlugin) {
allPlugins.add(plugin); allPlugins.add(plugin);
switch(plugin.build) { switch (plugin.build) {
case 'client': { case 'client': {
clientPlugins.push(plugin); clientPlugins.push(plugin);
break; break;
@ -54,10 +55,10 @@ export function createPluginContainer(options: StaticBuildOptions, internals: Bu
let plugins = build === 'ssr' ? ssrPlugins : clientPlugins; let plugins = build === 'ssr' ? ssrPlugins : clientPlugins;
let vitePlugins: Array<VitePlugin | VitePlugin[]> = []; let vitePlugins: Array<VitePlugin | VitePlugin[]> = [];
let lastVitePlugins: Array<VitePlugin | VitePlugin[]> = []; let lastVitePlugins: Array<VitePlugin | VitePlugin[]> = [];
for(const plugin of plugins) { for (const plugin of plugins) {
if(plugin.hooks?.['build:before']) { if (plugin.hooks?.['build:before']) {
let result = plugin.hooks['build:before']({ build, input }); let result = plugin.hooks['build:before']({ build, input });
if(result.vitePlugin) { if (result.vitePlugin) {
vitePlugins.push(result.vitePlugin); vitePlugins.push(result.vitePlugin);
} }
} }
@ -65,27 +66,30 @@ export function createPluginContainer(options: StaticBuildOptions, internals: Bu
return { return {
vitePlugins, vitePlugins,
lastVitePlugins lastVitePlugins,
}; };
}, },
async runPostHook(ssrReturn: ViteBuildReturn, clientReturn: ViteBuildReturn | null) { async runPostHook(ssrReturn: ViteBuildReturn, clientReturn: ViteBuildReturn | null) {
const mutations = new Map<string, { const mutations = new Map<
build: 'server' | 'client'; string,
code: string; {
}>(); build: 'server' | 'client';
code: string;
}
>();
const ssrOutputs: RollupOutputArray = []; const ssrOutputs: RollupOutputArray = [];
const clientOutputs: RollupOutputArray = []; const clientOutputs: RollupOutputArray = [];
if(Array.isArray(ssrReturn)) { if (Array.isArray(ssrReturn)) {
ssrOutputs.push(...ssrReturn); ssrOutputs.push(...ssrReturn);
} else if('output' in ssrReturn) { } else if ('output' in ssrReturn) {
ssrOutputs.push(ssrReturn); ssrOutputs.push(ssrReturn);
} }
if(Array.isArray(clientReturn)) { if (Array.isArray(clientReturn)) {
clientOutputs.push(...clientReturn); clientOutputs.push(...clientReturn);
} else if(clientReturn && 'output' in clientReturn) { } else if (clientReturn && 'output' in clientReturn) {
clientOutputs.push(clientReturn); clientOutputs.push(clientReturn);
} }
@ -97,19 +101,19 @@ export function createPluginContainer(options: StaticBuildOptions, internals: Bu
}); });
}; };
for(const plugin of allPlugins) { for (const plugin of allPlugins) {
const postHook = plugin.hooks?.['build:post']; const postHook = plugin.hooks?.['build:post'];
if(postHook) { if (postHook) {
await postHook({ await postHook({
ssrOutputs, ssrOutputs,
clientOutputs, clientOutputs,
mutate mutate,
}); });
} }
} }
return mutations; return mutations;
} },
}; };
} }

View file

@ -1,14 +1,13 @@
import type { AstroBuildPluginContainer, AstroBuildPlugin } from '../plugin'; import { astroConfigBuildPlugin } from '../../../content/vite-plugin-content-assets.js';
import type { PageBuildData, StaticBuildOptions } from '../types'; import type { AstroBuildPluginContainer } from '../plugin';
import { pluginAliasResolve } from './plugin-alias-resolve.js';
import { pluginAnalyzer } from './plugin-analyzer.js'; import { pluginAnalyzer } from './plugin-analyzer.js';
import { pluginCSS } from './plugin-css.js';
import { pluginHoistedScripts } from './plugin-hoisted-scripts.js';
import { pluginInternals } from './plugin-internals.js'; import { pluginInternals } from './plugin-internals.js';
import { pluginPages } from './plugin-pages.js'; import { pluginPages } from './plugin-pages.js';
import { pluginCSS } from './plugin-css.js';
import { pluginPrerender } from './plugin-prerender.js'; import { pluginPrerender } from './plugin-prerender.js';
import { astroConfigBuildPlugin } from '../../../content/vite-plugin-content-assets.js';
import { pluginSSR } from './plugin-ssr.js'; import { pluginSSR } from './plugin-ssr.js';
import { pluginAliasResolve } from './plugin-alias-resolve.js';
import { pluginHoistedScripts } from './plugin-hoisted-scripts.js';
export function registerAllPlugins({ internals, options, register }: AstroBuildPluginContainer) { export function registerAllPlugins({ internals, options, register }: AstroBuildPluginContainer) {
register(pluginAliasResolve(internals)); register(pluginAliasResolve(internals));

View file

@ -56,9 +56,9 @@ export function pluginAliasResolve(internals: BuildInternals): AstroBuildPlugin
hooks: { hooks: {
'build:before': () => { 'build:before': () => {
return { return {
vitePlugin: vitePluginAliasResolve(internals) vitePlugin: vitePluginAliasResolve(internals),
}; };
} },
} },
}; };
} }

View file

@ -1,7 +1,7 @@
import type { PluginContext } from 'rollup'; import type { PluginContext } from 'rollup';
import type { Plugin as VitePlugin } from 'vite'; import type { Plugin as VitePlugin } from 'vite';
import type { BuildInternals } from '../internal.js';
import type { PluginMetadata as AstroPluginMetadata } from '../../../vite-plugin-astro/types'; import type { PluginMetadata as AstroPluginMetadata } from '../../../vite-plugin-astro/types';
import type { BuildInternals } from '../internal.js';
import type { AstroBuildPlugin } from '../plugin.js'; import type { AstroBuildPlugin } from '../plugin.js';
import { prependForwardSlash } from '../../path.js'; import { prependForwardSlash } from '../../path.js';
@ -130,9 +130,9 @@ export function pluginAnalyzer(internals: BuildInternals): AstroBuildPlugin {
hooks: { hooks: {
'build:before': () => { 'build:before': () => {
return { return {
vitePlugin: vitePluginAnalyzer(internals) vitePlugin: vitePluginAnalyzer(internals),
}; };
} },
} },
}; };
} }

View file

@ -4,8 +4,8 @@ import type { GetModuleInfo } from 'rollup';
import { Plugin as VitePlugin, ResolvedConfig, transformWithEsbuild } from 'vite'; import { Plugin as VitePlugin, ResolvedConfig, transformWithEsbuild } from 'vite';
import { isCSSRequest } from '../../render/util.js'; import { isCSSRequest } from '../../render/util.js';
import type { BuildInternals } from '../internal'; import type { BuildInternals } from '../internal';
import type { PageBuildData, StaticBuildOptions } from '../types';
import type { AstroBuildPlugin } from '../plugin'; import type { AstroBuildPlugin } from '../plugin';
import type { PageBuildData, StaticBuildOptions } from '../types';
import { PROPAGATED_ASSET_FLAG } from '../../../content/consts.js'; import { PROPAGATED_ASSET_FLAG } from '../../../content/consts.js';
import * as assetName from '../css-asset-name.js'; import * as assetName from '../css-asset-name.js';
@ -78,7 +78,7 @@ export function rollupPluginAstroBuildCSS(options: PluginOptions): VitePlugin[]
} }
return createNameForParentPages(id, meta); return createNameForParentPages(id, meta);
} }
} },
}); });
}, },
@ -265,7 +265,10 @@ export function rollupPluginAstroBuildCSS(options: PluginOptions): VitePlugin[]
]; ];
} }
export function pluginCSS(options: StaticBuildOptions, internals: BuildInternals): AstroBuildPlugin { export function pluginCSS(
options: StaticBuildOptions,
internals: BuildInternals
): AstroBuildPlugin {
return { return {
build: 'both', build: 'both',
hooks: { hooks: {
@ -273,13 +276,13 @@ export function pluginCSS(options: StaticBuildOptions, internals: BuildInternals
let plugins = rollupPluginAstroBuildCSS({ let plugins = rollupPluginAstroBuildCSS({
buildOptions: options, buildOptions: options,
internals, internals,
target: build === 'ssr' ? 'server' : 'client' target: build === 'ssr' ? 'server' : 'client',
}); });
return { return {
vitePlugin: plugins vitePlugin: plugins,
}; };
} },
} },
} };
} }

View file

@ -1,10 +1,10 @@
import type { Plugin as VitePlugin } from 'vite'; import type { Plugin as VitePlugin } from 'vite';
import type { AstroSettings } from '../../../@types/astro'; import type { AstroSettings } from '../../../@types/astro';
import type { BuildInternals } from '../internal.js';
import { viteID } from '../../util.js'; import { viteID } from '../../util.js';
import type { BuildInternals } from '../internal.js';
import { getPageDataByViteID } from '../internal.js'; import { getPageDataByViteID } from '../internal.js';
import { StaticBuildOptions } from '../types';
import { AstroBuildPlugin } from '../plugin'; import { AstroBuildPlugin } from '../plugin';
import { StaticBuildOptions } from '../types';
function virtualHoistedEntry(id: string) { function virtualHoistedEntry(id: string) {
return id.startsWith('/astro/hoisted.js?q='); return id.startsWith('/astro/hoisted.js?q=');
@ -94,15 +94,18 @@ export function vitePluginHoistedScripts(
}; };
} }
export function pluginHoistedScripts(options: StaticBuildOptions, internals: BuildInternals): AstroBuildPlugin { export function pluginHoistedScripts(
options: StaticBuildOptions,
internals: BuildInternals
): AstroBuildPlugin {
return { return {
build: 'client', build: 'client',
hooks: { hooks: {
'build:before': () => { 'build:before': () => {
return { return {
vitePlugin: vitePluginHoistedScripts(options.settings, internals) vitePlugin: vitePluginHoistedScripts(options.settings, internals),
}; };
} },
} },
}; };
} }

View file

@ -68,9 +68,9 @@ export function pluginInternals(internals: BuildInternals): AstroBuildPlugin {
hooks: { hooks: {
'build:before': ({ input }) => { 'build:before': ({ input }) => {
return { return {
vitePlugin: vitePluginInternals(input, internals) vitePlugin: vitePluginInternals(input, internals),
}; };
} },
} },
}; };
} }

View file

@ -1,6 +1,6 @@
import type { Plugin as VitePlugin } from 'vite'; import type { Plugin as VitePlugin } from 'vite';
import type { StaticBuildOptions } from '../types';
import type { AstroBuildPlugin } from '../plugin'; import type { AstroBuildPlugin } from '../plugin';
import type { StaticBuildOptions } from '../types';
import { pagesVirtualModuleId, resolvedPagesVirtualModuleId } from '../../app/index.js'; import { pagesVirtualModuleId, resolvedPagesVirtualModuleId } from '../../app/index.js';
import { addRollupInput } from '../add-rollup-input.js'; import { addRollupInput } from '../add-rollup-input.js';
@ -62,9 +62,9 @@ export function pluginPages(opts: StaticBuildOptions, internals: BuildInternals)
hooks: { hooks: {
'build:before': () => { 'build:before': () => {
return { return {
vitePlugin: vitePluginPages(opts, internals) vitePlugin: vitePluginPages(opts, internals),
}; };
} },
} },
}; };
} }

View file

@ -28,21 +28,24 @@ export function vitePluginPrerender(
// dynamic pages should all go in their own chunk in the pages/* directory // dynamic pages should all go in their own chunk in the pages/* directory
return `pages/all`; return `pages/all`;
} }
} },
}); });
}, },
}; };
} }
export function pluginPrerender(opts: StaticBuildOptions, internals: BuildInternals): AstroBuildPlugin { export function pluginPrerender(
opts: StaticBuildOptions,
internals: BuildInternals
): AstroBuildPlugin {
return { return {
build: 'ssr', build: 'ssr',
hooks: { hooks: {
'build:before': () => { 'build:before': () => {
return { return {
vitePlugin: vitePluginPrerender(opts, internals) vitePlugin: vitePluginPrerender(opts, internals),
}; };
} },
} },
}; };
} }

View file

@ -5,7 +5,6 @@ import type { BuildInternals } from '../internal.js';
import type { StaticBuildOptions } from '../types'; import type { StaticBuildOptions } from '../types';
import glob from 'fast-glob'; import glob from 'fast-glob';
import * as fs from 'fs';
import { fileURLToPath } from 'url'; import { fileURLToPath } from 'url';
import { getContentPaths } from '../../../content/index.js'; import { getContentPaths } from '../../../content/index.js';
import { runHookBuildSsr } from '../../../integrations/index.js'; import { runHookBuildSsr } from '../../../integrations/index.js';
@ -220,7 +219,10 @@ function buildManifest(
return ssrManifest; return ssrManifest;
} }
export function pluginSSR(options: StaticBuildOptions, internals: BuildInternals): AstroBuildPlugin { export function pluginSSR(
options: StaticBuildOptions,
internals: BuildInternals
): AstroBuildPlugin {
const ssr = options.settings.config.output === 'server'; const ssr = options.settings.config.output === 'server';
return { return {
build: 'ssr', build: 'ssr',
@ -230,15 +232,15 @@ export function pluginSSR(options: StaticBuildOptions, internals: BuildInternals
return { return {
enforce: 'after-user-plugins', enforce: 'after-user-plugins',
vitePlugin vitePlugin,
} };
}, },
'build:post': async ({ mutate }) => { 'build:post': async ({ mutate }) => {
if(!ssr) { if (!ssr) {
return; return;
} }
if(!internals.ssrEntryChunk) { if (!internals.ssrEntryChunk) {
throw new Error(`Did not generate an entry chunk for SSR`); throw new Error(`Did not generate an entry chunk for SSR`);
} }
// Mutate the filename // Mutate the filename
@ -246,7 +248,7 @@ export function pluginSSR(options: StaticBuildOptions, internals: BuildInternals
const code = await injectManifest(options, internals); const code = await injectManifest(options, internals);
mutate(internals.ssrEntryChunk, 'server', code); mutate(internals.ssrEntryChunk, 'server', code);
} },
} },
}; };
} }

View file

@ -7,14 +7,14 @@ type OutputOptions = Parameters<OutputOptionsHook>[0];
type ExtendManualChunksHooks = { type ExtendManualChunksHooks = {
before?: (id: string, meta: any) => string | undefined; before?: (id: string, meta: any) => string | undefined;
after?: (id: string, meta: any) => string | undefined; after?: (id: string, meta: any) => string | undefined;
} };
export function extendManualChunks(outputOptions: OutputOptions, hooks: ExtendManualChunksHooks) { export function extendManualChunks(outputOptions: OutputOptions, hooks: ExtendManualChunksHooks) {
const manualChunks = outputOptions.manualChunks; const manualChunks = outputOptions.manualChunks;
outputOptions.manualChunks = function(id, meta) { outputOptions.manualChunks = function (id, meta) {
if(hooks.before) { if (hooks.before) {
let value = hooks.before(id, meta); let value = hooks.before(id, meta);
if(value) { if (value) {
return value; return value;
} }
} }
@ -31,8 +31,8 @@ export function extendManualChunks(outputOptions: OutputOptions, hooks: ExtendMa
return outid; return outid;
} }
} }
if(hooks.after) { if (hooks.after) {
return hooks.after(id, meta) || null; return hooks.after(id, meta) || null;
} }
return null; return null;

View file

@ -20,10 +20,10 @@ import { info } from '../logger/core.js';
import { getOutDirWithinCwd } from './common.js'; import { getOutDirWithinCwd } from './common.js';
import { generatePages } from './generate.js'; import { generatePages } from './generate.js';
import { trackPageData } from './internal.js'; import { trackPageData } from './internal.js';
import type { PageBuildData, StaticBuildOptions } from './types';
import { getTimeStat } from './util.js';
import { AstroBuildPluginContainer, createPluginContainer } from './plugin.js'; import { AstroBuildPluginContainer, createPluginContainer } from './plugin.js';
import { registerAllPlugins } from './plugins/index.js'; import { registerAllPlugins } from './plugins/index.js';
import type { PageBuildData, StaticBuildOptions } from './types';
import { getTimeStat } from './util.js';
export async function staticBuild(opts: StaticBuildOptions) { export async function staticBuild(opts: StaticBuildOptions) {
const { allPages, settings } = opts; const { allPages, settings } = opts;
@ -67,7 +67,6 @@ export async function staticBuild(opts: StaticBuildOptions) {
const container = createPluginContainer(opts, internals); const container = createPluginContainer(opts, internals);
registerAllPlugins(container); registerAllPlugins(container);
// Build your project (SSR application code, assets, client JS, etc.) // Build your project (SSR application code, assets, client JS, etc.)
timer.ssr = performance.now(); timer.ssr = performance.now();
info(opts.logging, 'build', `Building ${settings.config.output} entrypoints...`); info(opts.logging, 'build', `Building ${settings.config.output} entrypoints...`);
@ -112,7 +111,12 @@ export async function staticBuild(opts: StaticBuildOptions) {
} }
} }
async function ssrBuild(opts: StaticBuildOptions, internals: BuildInternals, input: Set<string>, container: AstroBuildPluginContainer) { async function ssrBuild(
opts: StaticBuildOptions,
internals: BuildInternals,
input: Set<string>,
container: AstroBuildPluginContainer
) {
const { settings, viteConfig } = opts; const { settings, viteConfig } = opts;
const ssr = settings.config.output === 'server'; const ssr = settings.config.output === 'server';
const out = ssr ? opts.buildConfig.server : getOutDirWithinCwd(settings.config.outDir); const out = ssr ? opts.buildConfig.server : getOutDirWithinCwd(settings.config.outDir);
@ -155,11 +159,7 @@ async function ssrBuild(opts: StaticBuildOptions, internals: BuildInternals, inp
modulePreload: { polyfill: false }, modulePreload: { polyfill: false },
reportCompressedSize: false, reportCompressedSize: false,
}, },
plugins: [ plugins: [...vitePlugins, ...(viteConfig.plugins || []), ...lastVitePlugins],
...vitePlugins,
...(viteConfig.plugins || []),
...lastVitePlugins
],
envPrefix: viteConfig.envPrefix ?? 'PUBLIC_', envPrefix: viteConfig.envPrefix ?? 'PUBLIC_',
base: settings.config.base, base: settings.config.base,
}; };
@ -199,7 +199,6 @@ async function clientBuild(
const { lastVitePlugins, vitePlugins } = container.runBeforeHook('client', input); const { lastVitePlugins, vitePlugins } = container.runBeforeHook('client', input);
info(opts.logging, null, `\n${bgGreen(black(' building client '))}`); info(opts.logging, null, `\n${bgGreen(black(' building client '))}`);
const viteBuildConfig: vite.InlineConfig = { const viteBuildConfig: vite.InlineConfig = {
...viteConfig, ...viteConfig,
mode: viteConfig.mode || 'production', mode: viteConfig.mode || 'production',
@ -222,11 +221,7 @@ async function clientBuild(
preserveEntrySignatures: 'exports-only', preserveEntrySignatures: 'exports-only',
}, },
}, },
plugins: [ plugins: [...vitePlugins, ...(viteConfig.plugins || []), ...lastVitePlugins],
...vitePlugins,
...(viteConfig.plugins || []),
...lastVitePlugins,
],
envPrefix: viteConfig.envPrefix ?? 'PUBLIC_', envPrefix: viteConfig.envPrefix ?? 'PUBLIC_',
base: settings.config.base, base: settings.config.base,
}; };
@ -251,7 +246,7 @@ async function runPostBuildHooks(
) { ) {
const mutations = await container.runPostHook(ssrReturn, clientReturn); const mutations = await container.runPostHook(ssrReturn, clientReturn);
const buildConfig = container.options.settings.config.build; const buildConfig = container.options.settings.config.build;
for(const [fileName, mutation] of mutations) { for (const [fileName, mutation] of mutations) {
const root = mutation.build === 'server' ? buildConfig.server : buildConfig.client; const root = mutation.build === 'server' ? buildConfig.server : buildConfig.client;
const fileURL = new URL(fileName, root); const fileURL = new URL(fileName, root);
await fs.promises.mkdir(new URL('./', fileURL), { recursive: true }); await fs.promises.mkdir(new URL('./', fileURL), { recursive: true });

View file

@ -1,4 +1,4 @@
import type { InlineConfig } from 'vite'; import type { default as vite, InlineConfig } from 'vite';
import type { import type {
AstroConfig, AstroConfig,
AstroSettings, AstroSettings,
@ -11,7 +11,6 @@ import type {
} from '../../@types/astro'; } from '../../@types/astro';
import type { LogOptions } from '../logger/core'; import type { LogOptions } from '../logger/core';
import type { RouteCache } from '../render/route-cache'; import type { RouteCache } from '../render/route-cache';
import type { default as vite } from 'vite';
export type ComponentPath = string; export type ComponentPath = string;
export type ViteID = string; export type ViteID = string;
@ -47,5 +46,8 @@ export interface SingleFileBuiltModule {
} }
export type ViteBuildReturn = Awaited<ReturnType<typeof vite.build>>; export type ViteBuildReturn = Awaited<ReturnType<typeof vite.build>>;
export type RollupOutput = Extract<Extract<ViteBuildReturn, Exclude<ViteBuildReturn, Array<any>>>, { output: any }>; export type RollupOutput = Extract<
Extract<ViteBuildReturn, Exclude<ViteBuildReturn, Array<any>>>,
{ output: any }
>;
export type OutputChunk = Extract<RollupOutput['output'][number], { type: 'chunk' }>; export type OutputChunk = Extract<RollupOutput['output'][number], { type: 'chunk' }>;