From a0efdc8f763b35e287ce1f1a371866a1c12731a8 Mon Sep 17 00:00:00 2001 From: matthewp Date: Fri, 3 Feb 2023 13:06:16 +0000 Subject: [PATCH] [ci] format --- .../src/content/vite-plugin-content-assets.ts | 6 +-- packages/astro/src/core/build/plugin.ts | 54 ++++++++++--------- .../astro/src/core/build/plugins/index.ts | 11 ++-- .../build/plugins/plugin-alias-resolve.ts | 6 +-- .../src/core/build/plugins/plugin-analyzer.ts | 8 +-- .../src/core/build/plugins/plugin-css.ts | 19 ++++--- .../build/plugins/plugin-hoisted-scripts.ts | 15 +++--- .../core/build/plugins/plugin-internals.ts | 6 +-- .../src/core/build/plugins/plugin-pages.ts | 8 +-- .../core/build/plugins/plugin-prerender.ts | 13 +++-- .../src/core/build/plugins/plugin-ssr.ts | 18 ++++--- packages/astro/src/core/build/plugins/util.ts | 12 ++--- packages/astro/src/core/build/static-build.ts | 27 ++++------ packages/astro/src/core/build/types.ts | 8 +-- 14 files changed, 111 insertions(+), 100 deletions(-) diff --git a/packages/astro/src/content/vite-plugin-content-assets.ts b/packages/astro/src/content/vite-plugin-content-assets.ts index a5b205151..89e417a3c 100644 --- a/packages/astro/src/content/vite-plugin-content-assets.ts +++ b/packages/astro/src/content/vite-plugin-content-assets.ts @@ -106,9 +106,9 @@ export function astroConfigBuildPlugin(internals: BuildInternals): AstroBuildPlu hooks: { 'build:before': () => { return { - vitePlugin: astroContentProdBundlePlugin({ internals }) + vitePlugin: astroContentProdBundlePlugin({ internals }), }; - } - } + }, + }, }; } diff --git a/packages/astro/src/core/build/plugin.ts b/packages/astro/src/core/build/plugin.ts index 47c87e334..e7b1fbc4a 100644 --- a/packages/astro/src/core/build/plugin.ts +++ b/packages/astro/src/core/build/plugin.ts @@ -3,7 +3,7 @@ import type { BuildInternals } from './internal'; import type { StaticBuildOptions, ViteBuildReturn } from './types'; type RollupOutputArray = Extract>; -type OutputChunkorAsset = RollupOutputArray[number]['output'][number] +type OutputChunkorAsset = RollupOutputArray[number]['output'][number]; type OutputChunk = Extract; 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; - }) => { + 'build:before'?: (opts: { build: 'ssr' | 'client'; input: Set }) => { enforce?: 'after-user-plugins'; - vitePlugin: VitePlugin | VitePlugin[] | undefined + vitePlugin: VitePlugin | VitePlugin[] | undefined; }; - 'build:post'?: (opts: {ssrOutputs: RollupOutputArray; clientOutputs: RollupOutputArray; mutate: MutateChunk}) => void | Promise; - } + 'build:post'?: (opts: { + ssrOutputs: RollupOutputArray; + clientOutputs: RollupOutputArray; + mutate: MutateChunk; + }) => void | Promise; + }; }; 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 = []; let lastVitePlugins: Array = []; - 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(); + 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; - } + }, }; } diff --git a/packages/astro/src/core/build/plugins/index.ts b/packages/astro/src/core/build/plugins/index.ts index f253fe289..29591185d 100644 --- a/packages/astro/src/core/build/plugins/index.ts +++ b/packages/astro/src/core/build/plugins/index.ts @@ -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)); diff --git a/packages/astro/src/core/build/plugins/plugin-alias-resolve.ts b/packages/astro/src/core/build/plugins/plugin-alias-resolve.ts index 1487ca2b9..7b0aba7e8 100644 --- a/packages/astro/src/core/build/plugins/plugin-alias-resolve.ts +++ b/packages/astro/src/core/build/plugins/plugin-alias-resolve.ts @@ -56,9 +56,9 @@ export function pluginAliasResolve(internals: BuildInternals): AstroBuildPlugin hooks: { 'build:before': () => { return { - vitePlugin: vitePluginAliasResolve(internals) + vitePlugin: vitePluginAliasResolve(internals), }; - } - } + }, + }, }; } diff --git a/packages/astro/src/core/build/plugins/plugin-analyzer.ts b/packages/astro/src/core/build/plugins/plugin-analyzer.ts index 4214cde8e..6fd2ef7b2 100644 --- a/packages/astro/src/core/build/plugins/plugin-analyzer.ts +++ b/packages/astro/src/core/build/plugins/plugin-analyzer.ts @@ -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), }; - } - } + }, + }, }; } diff --git a/packages/astro/src/core/build/plugins/plugin-css.ts b/packages/astro/src/core/build/plugins/plugin-css.ts index b52b5c0fc..acd59746a 100644 --- a/packages/astro/src/core/build/plugins/plugin-css.ts +++ b/packages/astro/src/core/build/plugins/plugin-css.ts @@ -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, }; - } - } - } + }, + }, + }; } diff --git a/packages/astro/src/core/build/plugins/plugin-hoisted-scripts.ts b/packages/astro/src/core/build/plugins/plugin-hoisted-scripts.ts index 9e21177bd..da40ede65 100644 --- a/packages/astro/src/core/build/plugins/plugin-hoisted-scripts.ts +++ b/packages/astro/src/core/build/plugins/plugin-hoisted-scripts.ts @@ -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), }; - } - } + }, + }, }; } diff --git a/packages/astro/src/core/build/plugins/plugin-internals.ts b/packages/astro/src/core/build/plugins/plugin-internals.ts index 7177d8bef..982d958c3 100644 --- a/packages/astro/src/core/build/plugins/plugin-internals.ts +++ b/packages/astro/src/core/build/plugins/plugin-internals.ts @@ -68,9 +68,9 @@ export function pluginInternals(internals: BuildInternals): AstroBuildPlugin { hooks: { 'build:before': ({ input }) => { return { - vitePlugin: vitePluginInternals(input, internals) + vitePlugin: vitePluginInternals(input, internals), }; - } - } + }, + }, }; } diff --git a/packages/astro/src/core/build/plugins/plugin-pages.ts b/packages/astro/src/core/build/plugins/plugin-pages.ts index ac436a364..f66494b28 100644 --- a/packages/astro/src/core/build/plugins/plugin-pages.ts +++ b/packages/astro/src/core/build/plugins/plugin-pages.ts @@ -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), }; - } - } + }, + }, }; } diff --git a/packages/astro/src/core/build/plugins/plugin-prerender.ts b/packages/astro/src/core/build/plugins/plugin-prerender.ts index 340e84ea6..911f153ba 100644 --- a/packages/astro/src/core/build/plugins/plugin-prerender.ts +++ b/packages/astro/src/core/build/plugins/plugin-prerender.ts @@ -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), }; - } - } + }, + }, }; } diff --git a/packages/astro/src/core/build/plugins/plugin-ssr.ts b/packages/astro/src/core/build/plugins/plugin-ssr.ts index 289703858..cfc58d71b 100644 --- a/packages/astro/src/core/build/plugins/plugin-ssr.ts +++ b/packages/astro/src/core/build/plugins/plugin-ssr.ts @@ -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); - } - } + }, + }, }; } diff --git a/packages/astro/src/core/build/plugins/util.ts b/packages/astro/src/core/build/plugins/util.ts index 6129aa1e1..50f5e0705 100644 --- a/packages/astro/src/core/build/plugins/util.ts +++ b/packages/astro/src/core/build/plugins/util.ts @@ -7,14 +7,14 @@ type OutputOptions = Parameters[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; diff --git a/packages/astro/src/core/build/static-build.ts b/packages/astro/src/core/build/static-build.ts index 726c1db88..4e3238978 100644 --- a/packages/astro/src/core/build/static-build.ts +++ b/packages/astro/src/core/build/static-build.ts @@ -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, container: AstroBuildPluginContainer) { +async function ssrBuild( + opts: StaticBuildOptions, + internals: BuildInternals, + input: Set, + 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 }); diff --git a/packages/astro/src/core/build/types.ts b/packages/astro/src/core/build/types.ts index a9ca08a0e..151398c54 100644 --- a/packages/astro/src/core/build/types.ts +++ b/packages/astro/src/core/build/types.ts @@ -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>; -export type RollupOutput = Extract>>, { output: any }>; +export type RollupOutput = Extract< + Extract>>, + { output: any } +>; export type OutputChunk = Extract;