[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: {
'build:before': () => {
return {
vitePlugin: astroContentProdBundlePlugin({ internals })
vitePlugin: astroContentProdBundlePlugin({ internals }),
};
}
}
},
},
};
}

View file

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

View file

@ -1,14 +1,13 @@
import type { AstroBuildPluginContainer, AstroBuildPlugin } from '../plugin';
import type { PageBuildData, StaticBuildOptions } from '../types';
import { astroConfigBuildPlugin } from '../../../content/vite-plugin-content-assets.js';
import type { AstroBuildPluginContainer } from '../plugin';
import { pluginAliasResolve } from './plugin-alias-resolve.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 { pluginPages } from './plugin-pages.js';
import { pluginCSS } from './plugin-css.js';
import { pluginPrerender } from './plugin-prerender.js';
import { astroConfigBuildPlugin } from '../../../content/vite-plugin-content-assets.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) {
register(pluginAliasResolve(internals));

View file

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

View file

@ -1,7 +1,7 @@
import type { PluginContext } from 'rollup';
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 { BuildInternals } from '../internal.js';
import type { AstroBuildPlugin } from '../plugin.js';
import { prependForwardSlash } from '../../path.js';
@ -130,9 +130,9 @@ export function pluginAnalyzer(internals: BuildInternals): AstroBuildPlugin {
hooks: {
'build:before': () => {
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 { isCSSRequest } from '../../render/util.js';
import type { BuildInternals } from '../internal';
import type { PageBuildData, StaticBuildOptions } from '../types';
import type { AstroBuildPlugin } from '../plugin';
import type { PageBuildData, StaticBuildOptions } from '../types';
import { PROPAGATED_ASSET_FLAG } from '../../../content/consts.js';
import * as assetName from '../css-asset-name.js';
@ -78,7 +78,7 @@ export function rollupPluginAstroBuildCSS(options: PluginOptions): VitePlugin[]
}
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 {
build: 'both',
hooks: {
@ -273,13 +276,13 @@ export function pluginCSS(options: StaticBuildOptions, internals: BuildInternals
let plugins = rollupPluginAstroBuildCSS({
buildOptions: options,
internals,
target: build === 'ssr' ? 'server' : 'client'
target: build === 'ssr' ? 'server' : 'client',
});
return {
vitePlugin: plugins
vitePlugin: plugins,
};
}
}
}
},
},
};
}

View file

@ -1,10 +1,10 @@
import type { Plugin as VitePlugin } from 'vite';
import type { AstroSettings } from '../../../@types/astro';
import type { BuildInternals } from '../internal.js';
import { viteID } from '../../util.js';
import type { BuildInternals } from '../internal.js';
import { getPageDataByViteID } from '../internal.js';
import { StaticBuildOptions } from '../types';
import { AstroBuildPlugin } from '../plugin';
import { StaticBuildOptions } from '../types';
function virtualHoistedEntry(id: string) {
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 {
build: 'client',
hooks: {
'build:before': () => {
return {
vitePlugin: vitePluginHoistedScripts(options.settings, internals)
vitePlugin: vitePluginHoistedScripts(options.settings, internals),
};
}
}
},
},
};
}

View file

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

View file

@ -1,6 +1,6 @@
import type { Plugin as VitePlugin } from 'vite';
import type { StaticBuildOptions } from '../types';
import type { AstroBuildPlugin } from '../plugin';
import type { StaticBuildOptions } from '../types';
import { pagesVirtualModuleId, resolvedPagesVirtualModuleId } from '../../app/index.js';
import { addRollupInput } from '../add-rollup-input.js';
@ -62,9 +62,9 @@ export function pluginPages(opts: StaticBuildOptions, internals: BuildInternals)
hooks: {
'build:before': () => {
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
return `pages/all`;
}
}
},
});
},
};
}
export function pluginPrerender(opts: StaticBuildOptions, internals: BuildInternals): AstroBuildPlugin {
export function pluginPrerender(
opts: StaticBuildOptions,
internals: BuildInternals
): AstroBuildPlugin {
return {
build: 'ssr',
hooks: {
'build:before': () => {
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 glob from 'fast-glob';
import * as fs from 'fs';
import { fileURLToPath } from 'url';
import { getContentPaths } from '../../../content/index.js';
import { runHookBuildSsr } from '../../../integrations/index.js';
@ -220,7 +219,10 @@ function buildManifest(
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';
return {
build: 'ssr',
@ -230,15 +232,15 @@ export function pluginSSR(options: StaticBuildOptions, internals: BuildInternals
return {
enforce: 'after-user-plugins',
vitePlugin
}
vitePlugin,
};
},
'build:post': async ({ mutate }) => {
if(!ssr) {
if (!ssr) {
return;
}
if(!internals.ssrEntryChunk) {
if (!internals.ssrEntryChunk) {
throw new Error(`Did not generate an entry chunk for SSR`);
}
// Mutate the filename
@ -246,7 +248,7 @@ export function pluginSSR(options: StaticBuildOptions, internals: BuildInternals
const code = await injectManifest(options, internals);
mutate(internals.ssrEntryChunk, 'server', code);
}
}
},
},
};
}

View file

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

View file

@ -20,10 +20,10 @@ import { info } from '../logger/core.js';
import { getOutDirWithinCwd } from './common.js';
import { generatePages } from './generate.js';
import { trackPageData } from './internal.js';
import type { PageBuildData, StaticBuildOptions } from './types';
import { getTimeStat } from './util.js';
import { AstroBuildPluginContainer, createPluginContainer } from './plugin.js';
import { registerAllPlugins } from './plugins/index.js';
import type { PageBuildData, StaticBuildOptions } from './types';
import { getTimeStat } from './util.js';
export async function staticBuild(opts: StaticBuildOptions) {
const { allPages, settings } = opts;
@ -67,7 +67,6 @@ export async function staticBuild(opts: StaticBuildOptions) {
const container = createPluginContainer(opts, internals);
registerAllPlugins(container);
// Build your project (SSR application code, assets, client JS, etc.)
timer.ssr = performance.now();
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 ssr = settings.config.output === 'server';
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 },
reportCompressedSize: false,
},
plugins: [
...vitePlugins,
...(viteConfig.plugins || []),
...lastVitePlugins
],
plugins: [...vitePlugins, ...(viteConfig.plugins || []), ...lastVitePlugins],
envPrefix: viteConfig.envPrefix ?? 'PUBLIC_',
base: settings.config.base,
};
@ -199,7 +199,6 @@ async function clientBuild(
const { lastVitePlugins, vitePlugins } = container.runBeforeHook('client', input);
info(opts.logging, null, `\n${bgGreen(black(' building client '))}`);
const viteBuildConfig: vite.InlineConfig = {
...viteConfig,
mode: viteConfig.mode || 'production',
@ -222,11 +221,7 @@ async function clientBuild(
preserveEntrySignatures: 'exports-only',
},
},
plugins: [
...vitePlugins,
...(viteConfig.plugins || []),
...lastVitePlugins,
],
plugins: [...vitePlugins, ...(viteConfig.plugins || []), ...lastVitePlugins],
envPrefix: viteConfig.envPrefix ?? 'PUBLIC_',
base: settings.config.base,
};
@ -251,7 +246,7 @@ async function runPostBuildHooks(
) {
const mutations = await container.runPostHook(ssrReturn, clientReturn);
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 fileURL = new URL(fileName, root);
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 {
AstroConfig,
AstroSettings,
@ -11,7 +11,6 @@ import type {
} from '../../@types/astro';
import type { LogOptions } from '../logger/core';
import type { RouteCache } from '../render/route-cache';
import type { default as vite } from 'vite';
export type ComponentPath = string;
export type ViteID = string;
@ -47,5 +46,8 @@ export interface SingleFileBuiltModule {
}
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' }>;