Refactor build into plugins (#6077)

* Refactor build into plugins

* maybe fix internals

* Await post-build hooks

* Use extendManualChunks

* Remove commented out code
This commit is contained in:
Matthew Phillips 2023-02-03 08:04:26 -05:00
parent 91b3307b98
commit ffd09a91aa
15 changed files with 462 additions and 143 deletions

View file

@ -2,6 +2,7 @@ import { pathToFileURL } from 'url';
import type { Plugin } from 'vite'; import type { Plugin } from 'vite';
import { moduleIsTopLevelPage, walkParentInfos } from '../core/build/graph.js'; import { moduleIsTopLevelPage, walkParentInfos } from '../core/build/graph.js';
import { BuildInternals, getPageDataByViteID } from '../core/build/internal.js'; import { BuildInternals, getPageDataByViteID } from '../core/build/internal.js';
import { AstroBuildPlugin } from '../core/build/plugin.js';
import type { ModuleLoader } from '../core/module-loader/loader.js'; import type { ModuleLoader } from '../core/module-loader/loader.js';
import { createViteLoader } from '../core/module-loader/vite.js'; import { createViteLoader } from '../core/module-loader/vite.js';
import { getStylesForURL } from '../core/render/dev/css.js'; import { getStylesForURL } from '../core/render/dev/css.js';
@ -115,3 +116,16 @@ export function astroContentProdBundlePlugin({ internals }: { internals: BuildIn
}, },
}; };
} }
export function astroConfigBuildPlugin(internals: BuildInternals): AstroBuildPlugin {
return {
build: 'ssr',
hooks: {
'build:before': () => {
return {
vitePlugin: astroContentProdBundlePlugin({ internals })
};
}
}
};
}

View file

@ -0,0 +1,116 @@
import type { Plugin as VitePlugin } from 'vite';
import type { BuildInternals } from './internal';
import type { StaticBuildOptions, ViteBuildReturn } from './types';
type RollupOutputArray = Extract<ViteBuildReturn, Array<any>>;
type OutputChunkorAsset = RollupOutputArray[number]['output'][number]
type OutputChunk = Extract<OutputChunkorAsset, { type: 'chunk' }>;
type MutateChunk = (chunk: OutputChunk, build: 'server' | 'client', newCode: string) => void;
export type AstroBuildPlugin = {
build: 'ssr' | 'client' | 'both';
hooks?: {
'build:before'?: (opts: {
build: 'ssr' | 'client';
input: Set<string>;
}) => {
enforce?: 'after-user-plugins';
vitePlugin: VitePlugin | VitePlugin[] | undefined
};
'build:post'?: (opts: {ssrOutputs: RollupOutputArray; clientOutputs: RollupOutputArray; mutate: MutateChunk}) => void | Promise<void>;
}
};
export function createPluginContainer(options: StaticBuildOptions, internals: BuildInternals) {
const clientPlugins: AstroBuildPlugin[] = [];
const ssrPlugins: AstroBuildPlugin[] = [];
const allPlugins = new Set<AstroBuildPlugin>();
return {
options,
internals,
register(plugin: AstroBuildPlugin) {
allPlugins.add(plugin);
switch(plugin.build) {
case 'client': {
clientPlugins.push(plugin);
break;
}
case 'ssr': {
ssrPlugins.push(plugin);
break;
}
case 'both': {
clientPlugins.push(plugin);
ssrPlugins.push(plugin);
break;
}
}
},
// Hooks
runBeforeHook(build: 'ssr' | 'client', input: Set<string>) {
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']) {
let result = plugin.hooks['build:before']({ build, input });
if(result.vitePlugin) {
vitePlugins.push(result.vitePlugin);
}
}
}
return {
vitePlugins,
lastVitePlugins
};
},
async runPostHook(ssrReturn: ViteBuildReturn, clientReturn: ViteBuildReturn | null) {
const mutations = new Map<string, {
build: 'server' | 'client';
code: string;
}>();
const ssrOutputs: RollupOutputArray = [];
const clientOutputs: RollupOutputArray = [];
if(Array.isArray(ssrReturn)) {
ssrOutputs.push(...ssrReturn);
} else if('output' in ssrReturn) {
ssrOutputs.push(ssrReturn);
}
if(Array.isArray(clientReturn)) {
clientOutputs.push(...clientReturn);
} else if(clientReturn && 'output' in clientReturn) {
clientOutputs.push(clientReturn);
}
const mutate: MutateChunk = (chunk, build, newCode) => {
chunk.code = newCode;
mutations.set(chunk.fileName, {
build,
code: newCode,
});
};
for(const plugin of allPlugins) {
const postHook = plugin.hooks?.['build:post'];
if(postHook) {
await postHook({
ssrOutputs,
clientOutputs,
mutate
});
}
}
return mutations;
}
};
}
export type AstroBuildPluginContainer = ReturnType<typeof createPluginContainer>;

View file

@ -0,0 +1,23 @@
import type { AstroBuildPluginContainer, AstroBuildPlugin } from '../plugin';
import type { PageBuildData, StaticBuildOptions } from '../types';
import { pluginAnalyzer } from './plugin-analyzer.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));
register(pluginAnalyzer(internals));
register(pluginInternals(internals));
register(pluginPages(options, internals));
register(pluginCSS(options, internals));
register(pluginPrerender(options, internals));
register(astroConfigBuildPlugin(internals));
register(pluginHoistedScripts(options, internals));
register(pluginSSR(options, internals));
}

View file

@ -1,5 +1,6 @@
import type { Alias, Plugin as VitePlugin } from 'vite'; import type { Alias, Plugin as VitePlugin } from 'vite';
import type { BuildInternals } from '../../core/build/internal.js'; import type { BuildInternals } from '../internal.js';
import { AstroBuildPlugin } from '../plugin.js';
/** /**
* `@rollup/plugin-alias` doesn't resolve aliases in Rollup input by default. This plugin fixes it * `@rollup/plugin-alias` doesn't resolve aliases in Rollup input by default. This plugin fixes it
@ -48,3 +49,16 @@ function matches(pattern: string | RegExp, importee: string) {
} }
return importee.startsWith(pattern + '/'); return importee.startsWith(pattern + '/');
} }
export function pluginAliasResolve(internals: BuildInternals): AstroBuildPlugin {
return {
build: 'client',
hooks: {
'build:before': () => {
return {
vitePlugin: vitePluginAliasResolve(internals)
};
}
}
};
}

View file

@ -1,12 +1,13 @@
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 '../../core/build/internal.js'; 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 { AstroBuildPlugin } from '../plugin.js';
import { prependForwardSlash } from '../../core/path.js'; import { prependForwardSlash } from '../../../core/path.js';
import { getTopLevelPages, moduleIsTopLevelPage, walkParentInfos } from './graph.js'; import { getTopLevelPages, moduleIsTopLevelPage, walkParentInfos } from '../graph.js';
import { getPageDataByViteID, trackClientOnlyPageDatas } from './internal.js'; import { getPageDataByViteID, trackClientOnlyPageDatas } from '../internal.js';
import { PROPAGATED_ASSET_FLAG } from '../../content/consts.js'; import { PROPAGATED_ASSET_FLAG } from '../../../content/consts.js';
function isPropagatedAsset(id: string) { function isPropagatedAsset(id: string) {
return new URL('file://' + id).searchParams.has(PROPAGATED_ASSET_FLAG); return new URL('file://' + id).searchParams.has(PROPAGATED_ASSET_FLAG);
@ -169,3 +170,16 @@ export function vitePluginAnalyzer(internals: BuildInternals): VitePlugin {
}, },
}; };
} }
export function pluginAnalyzer(internals: BuildInternals): AstroBuildPlugin {
return {
build: 'ssr',
hooks: {
'build:before': () => {
return {
vitePlugin: vitePluginAnalyzer(internals)
};
}
}
};
}

View file

@ -2,20 +2,22 @@ import * as crypto from 'node:crypto';
import * as npath from 'node:path'; import * as npath from 'node:path';
import type { GetModuleInfo } from 'rollup'; 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 { PageBuildData, StaticBuildOptions } from '../types';
import type { AstroBuildPlugin } from '../plugin';
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';
import { moduleIsTopLevelPage, walkParentInfos } from './graph.js'; import { moduleIsTopLevelPage, walkParentInfos } from '../graph.js';
import { import {
eachPageData, eachPageData,
getPageDataByViteID, getPageDataByViteID,
getPageDatasByClientOnlyID, getPageDatasByClientOnlyID,
getPageDatasByHoistedScriptId, getPageDatasByHoistedScriptId,
isHoistedScript, isHoistedScript,
} from './internal.js'; } from '../internal.js';
import { extendManualChunks } from './util.js';
interface PluginOptions { interface PluginOptions {
internals: BuildInternals; internals: BuildInternals;
@ -54,30 +56,19 @@ export function rollupPluginAstroBuildCSS(options: PluginOptions): VitePlugin[]
name: 'astro:rollup-plugin-build-css', name: 'astro:rollup-plugin-build-css',
outputOptions(outputOptions) { outputOptions(outputOptions) {
const manualChunks = outputOptions.manualChunks || Function.prototype;
const assetFileNames = outputOptions.assetFileNames; const assetFileNames = outputOptions.assetFileNames;
const namingIncludesHash = assetFileNames?.toString().includes('[hash]'); const namingIncludesHash = assetFileNames?.toString().includes('[hash]');
const createNameForParentPages = namingIncludesHash const createNameForParentPages = namingIncludesHash
? assetName.shortHashedName ? assetName.shortHashedName
: assetName.createSlugger(settings); : assetName.createSlugger(settings);
outputOptions.manualChunks = function (id, ...args) {
// Defer to user-provided `manualChunks`, if it was provided.
if (typeof manualChunks == 'object') {
if (id in manualChunks) {
return manualChunks[id];
}
} else if (typeof manualChunks === 'function') {
const outid = manualChunks.call(this, id, ...args);
if (outid) {
return outid;
}
}
extendManualChunks(outputOptions, {
after(id, meta) {
// For CSS, create a hash of all of the pages that use it. // For CSS, create a hash of all of the pages that use it.
// This causes CSS to be built into shared chunks when used by multiple pages. // This causes CSS to be built into shared chunks when used by multiple pages.
if (isCSSRequest(id)) { if (isCSSRequest(id)) {
for (const [pageInfo] of walkParentInfos(id, { for (const [pageInfo] of walkParentInfos(id, {
getModuleInfo: args[0].getModuleInfo, getModuleInfo: meta.getModuleInfo,
})) { })) {
if (new URL(pageInfo.id, 'file://').searchParams.has(PROPAGATED_ASSET_FLAG)) { if (new URL(pageInfo.id, 'file://').searchParams.has(PROPAGATED_ASSET_FLAG)) {
// Split delayed assets to separate modules // Split delayed assets to separate modules
@ -85,9 +76,10 @@ export function rollupPluginAstroBuildCSS(options: PluginOptions): VitePlugin[]
return createNameHash(id, [id]); return createNameHash(id, [id]);
} }
} }
return createNameForParentPages(id, args[0]); return createNameForParentPages(id, meta);
} }
}; }
});
}, },
async generateBundle(_outputOptions, bundle) { async generateBundle(_outputOptions, bundle) {
@ -272,3 +264,22 @@ export function rollupPluginAstroBuildCSS(options: PluginOptions): VitePlugin[]
}, },
]; ];
} }
export function pluginCSS(options: StaticBuildOptions, internals: BuildInternals): AstroBuildPlugin {
return {
build: 'both',
hooks: {
'build:before': ({ build }) => {
let plugins = rollupPluginAstroBuildCSS({
buildOptions: options,
internals,
target: build === 'ssr' ? 'server' : 'client'
});
return {
vitePlugin: plugins
};
}
}
}
}

View file

@ -1,8 +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 '../../core/build/internal.js'; import type { BuildInternals } from '../internal.js';
import { viteID } from '../util.js'; import { viteID } from '../../util.js';
import { getPageDataByViteID } from './internal.js'; import { getPageDataByViteID } from '../internal.js';
import { StaticBuildOptions } from '../types';
import { AstroBuildPlugin } from '../plugin';
function virtualHoistedEntry(id: string) { function virtualHoistedEntry(id: string) {
return id.startsWith('/astro/hoisted.js?q='); return id.startsWith('/astro/hoisted.js?q=');
@ -91,3 +93,16 @@ export function vitePluginHoistedScripts(
}, },
}; };
} }
export function pluginHoistedScripts(options: StaticBuildOptions, internals: BuildInternals): AstroBuildPlugin {
return {
build: 'client',
hooks: {
'build:before': () => {
return {
vitePlugin: vitePluginHoistedScripts(options.settings, internals)
};
}
}
};
}

View file

@ -1,5 +1,6 @@
import type { Plugin as VitePlugin, UserConfig } from 'vite'; import type { Plugin as VitePlugin, UserConfig } from 'vite';
import type { BuildInternals } from './internal.js'; import type { BuildInternals } from '../internal.js';
import type { AstroBuildPlugin } from '../plugin';
export function vitePluginInternals(input: Set<string>, internals: BuildInternals): VitePlugin { export function vitePluginInternals(input: Set<string>, internals: BuildInternals): VitePlugin {
return { return {
@ -60,3 +61,16 @@ export function vitePluginInternals(input: Set<string>, internals: BuildInternal
}, },
}; };
} }
export function pluginInternals(internals: BuildInternals): AstroBuildPlugin {
return {
build: 'both',
hooks: {
'build:before': ({ input }) => {
return {
vitePlugin: vitePluginInternals(input, internals)
};
}
}
};
}

View file

@ -1,8 +1,10 @@
import type { Plugin as VitePlugin } from 'vite'; import type { Plugin as VitePlugin } from 'vite';
import { pagesVirtualModuleId, resolvedPagesVirtualModuleId } from '../app/index.js'; import type { StaticBuildOptions } from '../types';
import { addRollupInput } from './add-rollup-input.js'; import type { AstroBuildPlugin } from '../plugin';
import { BuildInternals, eachPageData, hasPrerenderedPages } from './internal.js';
import type { StaticBuildOptions } from './types'; import { pagesVirtualModuleId, resolvedPagesVirtualModuleId } from '../../app/index.js';
import { addRollupInput } from '../add-rollup-input.js';
import { BuildInternals, eachPageData, hasPrerenderedPages } from '../internal.js';
export function vitePluginPages(opts: StaticBuildOptions, internals: BuildInternals): VitePlugin { export function vitePluginPages(opts: StaticBuildOptions, internals: BuildInternals): VitePlugin {
return { return {
@ -53,3 +55,16 @@ export const renderers = [${rendererItems}];`;
}, },
}; };
} }
export function pluginPages(opts: StaticBuildOptions, internals: BuildInternals): AstroBuildPlugin {
return {
build: 'ssr',
hooks: {
'build:before': () => {
return {
vitePlugin: vitePluginPages(opts, internals)
};
}
}
};
}

View file

@ -0,0 +1,48 @@
import type { Plugin as VitePlugin } from 'vite';
import type { BuildInternals } from '../internal.js';
import type { AstroBuildPlugin } from '../plugin.js';
import type { StaticBuildOptions } from '../types';
import { extendManualChunks } from './util.js';
export function vitePluginPrerender(
opts: StaticBuildOptions,
internals: BuildInternals
): VitePlugin {
return {
name: 'astro:rollup-plugin-prerender',
outputOptions(outputOptions) {
extendManualChunks(outputOptions, {
after(id, meta) {
// Split the Astro runtime into a separate chunk for readability
if (id.includes('astro/dist')) {
return 'astro';
}
const pageInfo = internals.pagesByViteID.get(id);
if (pageInfo) {
// prerendered pages should be split into their own chunk
// Important: this can't be in the `pages/` directory!
if (meta.getModuleInfo(id)?.meta.astro?.pageOptions?.prerender) {
return 'prerender';
}
// dynamic pages should all go in their own chunk in the pages/* directory
return `pages/all`;
}
}
});
},
};
}
export function pluginPrerender(opts: StaticBuildOptions, internals: BuildInternals): AstroBuildPlugin {
return {
build: 'ssr',
hooks: {
'build:before': () => {
return {
vitePlugin: vitePluginPrerender(opts, internals)
};
}
}
};
}

View file

@ -1,21 +1,22 @@
import type { Plugin as VitePlugin } from 'vite'; import type { Plugin as VitePlugin } from 'vite';
import type { AstroAdapter } from '../../@types/astro'; import type { AstroAdapter } from '../../../@types/astro';
import type { SerializedRouteInfo, SerializedSSRManifest } from '../app/types'; import type { SerializedRouteInfo, SerializedSSRManifest } from '../../app/types';
import type { BuildInternals } from './internal.js'; 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 * 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';
import { BEFORE_HYDRATION_SCRIPT_ID, PAGE_SCRIPT_ID } from '../../vite-plugin-scripts/index.js'; import { BEFORE_HYDRATION_SCRIPT_ID, PAGE_SCRIPT_ID } from '../../../vite-plugin-scripts/index.js';
import { pagesVirtualModuleId } from '../app/index.js'; import { pagesVirtualModuleId } from '../../app/index.js';
import { removeLeadingForwardSlash, removeTrailingForwardSlash } from '../path.js'; import { removeLeadingForwardSlash, removeTrailingForwardSlash } from '../../path.js';
import { serializeRouteData } from '../routing/index.js'; import { serializeRouteData } from '../../routing/index.js';
import { addRollupInput } from './add-rollup-input.js'; import { addRollupInput } from '../add-rollup-input.js';
import { getOutFile, getOutFolder } from './common.js'; import { getOutFile, getOutFolder } from '../common.js';
import { eachPrerenderedPageData, eachServerPageData, sortedCSS } from './internal.js'; import { eachPrerenderedPageData, eachServerPageData, sortedCSS } from '../internal.js';
import { AstroBuildPlugin } from '../plugin';
export const virtualModuleId = '@astrojs-ssr-virtual-entry'; export const virtualModuleId = '@astrojs-ssr-virtual-entry';
const resolvedVirtualModuleId = '\0' + virtualModuleId; const resolvedVirtualModuleId = '\0' + virtualModuleId;
@ -29,7 +30,7 @@ export function vitePluginSSR(internals: BuildInternals, adapter: AstroAdapter):
options(opts) { options(opts) {
return addRollupInput(opts, [virtualModuleId]); return addRollupInput(opts, [virtualModuleId]);
}, },
resolveId(id) { resolveId(id, parent) {
if (id === virtualModuleId) { if (id === virtualModuleId) {
return resolvedVirtualModuleId; return resolvedVirtualModuleId;
} }
@ -114,12 +115,10 @@ export async function injectManifest(buildOpts: StaticBuildOptions, internals: B
const chunk = internals.ssrEntryChunk; const chunk = internals.ssrEntryChunk;
const code = chunk.code; const code = chunk.code;
chunk.code = code.replace(replaceExp, () => {
return code.replace(replaceExp, () => {
return JSON.stringify(manifest); return JSON.stringify(manifest);
}); });
const serverEntryURL = new URL(buildOpts.buildConfig.serverEntry, buildOpts.buildConfig.server);
await fs.promises.mkdir(new URL('./', serverEntryURL), { recursive: true });
await fs.promises.writeFile(serverEntryURL, chunk.code, 'utf-8');
} }
function buildManifest( function buildManifest(
@ -220,3 +219,34 @@ function buildManifest(
return ssrManifest; return ssrManifest;
} }
export function pluginSSR(options: StaticBuildOptions, internals: BuildInternals): AstroBuildPlugin {
const ssr = options.settings.config.output === 'server';
return {
build: 'ssr',
hooks: {
'build:before': () => {
let vitePlugin = ssr ? vitePluginSSR(internals, options.settings.adapter!) : undefined;
return {
enforce: 'after-user-plugins',
vitePlugin
}
},
'build:post': async ({ mutate }) => {
if(!ssr) {
return;
}
if(!internals.ssrEntryChunk) {
throw new Error(`Did not generate an entry chunk for SSR`);
}
// Mutate the filename
internals.ssrEntryChunk.fileName = options.settings.config.build.serverEntry;
const code = await injectManifest(options, internals);
mutate(internals.ssrEntryChunk, 'server', code);
}
}
};
}

View file

@ -0,0 +1,40 @@
import type { Plugin as VitePlugin } from 'vite';
// eslint-disable-next-line @typescript-eslint/ban-types
type OutputOptionsHook = Extract<VitePlugin['outputOptions'], Function>;
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) {
let value = hooks.before(id, meta);
if(value) {
return value;
}
}
// Defer to user-provided `manualChunks`, if it was provided.
if (typeof manualChunks == 'object') {
if (id in manualChunks) {
let value = manualChunks[id];
return value[0];
}
} else if (typeof manualChunks === 'function') {
const outid = manualChunks.call(this, id, meta);
if (outid) {
return outid;
}
}
if(hooks.after) {
return hooks.after(id, meta) || null;
}
return null;
};
}

View file

@ -4,7 +4,6 @@ import fs from 'fs';
import { bgGreen, bgMagenta, black, dim } from 'kleur/colors'; import { bgGreen, bgMagenta, black, dim } from 'kleur/colors';
import { fileURLToPath } from 'url'; import { fileURLToPath } from 'url';
import * as vite from 'vite'; import * as vite from 'vite';
import { astroContentProdBundlePlugin } from '../../content/index.js';
import { import {
BuildInternals, BuildInternals,
createBuildInternals, createBuildInternals,
@ -23,14 +22,8 @@ import { generatePages } from './generate.js';
import { trackPageData } from './internal.js'; import { trackPageData } from './internal.js';
import type { PageBuildData, StaticBuildOptions } from './types'; import type { PageBuildData, StaticBuildOptions } from './types';
import { getTimeStat } from './util.js'; import { getTimeStat } from './util.js';
import { vitePluginAliasResolve } from './vite-plugin-alias-resolve.js'; import { AstroBuildPluginContainer, createPluginContainer } from './plugin.js';
import { vitePluginAnalyzer } from './vite-plugin-analyzer.js'; import { registerAllPlugins } from './plugins/index.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';
export async function staticBuild(opts: StaticBuildOptions) { export async function staticBuild(opts: StaticBuildOptions) {
const { allPages, settings } = opts; const { allPages, settings } = opts;
@ -70,10 +63,15 @@ export async function staticBuild(opts: StaticBuildOptions) {
// condition, so we are doing it ourselves // condition, so we are doing it ourselves
emptyDir(settings.config.outDir, new Set('.git')); emptyDir(settings.config.outDir, new Set('.git'));
// Register plugins
const container = createPluginContainer(opts, internals);
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...`);
await ssrBuild(opts, internals, pageInput); const ssrOutput = await ssrBuild(opts, internals, pageInput, container);
info(opts.logging, 'build', dim(`Completed in ${getTimeStat(timer.ssr, performance.now())}.`)); info(opts.logging, 'build', dim(`Completed in ${getTimeStat(timer.ssr, performance.now())}.`));
const rendererClientEntrypoints = settings.renderers const rendererClientEntrypoints = settings.renderers
@ -93,9 +91,11 @@ export async function staticBuild(opts: StaticBuildOptions) {
// Run client build first, so the assets can be fed into the SSR rendered version. // Run client build first, so the assets can be fed into the SSR rendered version.
timer.clientBuild = performance.now(); timer.clientBuild = performance.now();
await clientBuild(opts, internals, clientInput); const clientOutput = await clientBuild(opts, internals, clientInput, container);
timer.generate = performance.now(); timer.generate = performance.now();
await runPostBuildHooks(container, ssrOutput, clientOutput);
switch (settings.config.output) { switch (settings.config.output) {
case 'static': { case 'static': {
await generatePages(opts, internals); await generatePages(opts, internals);
@ -103,7 +103,6 @@ export async function staticBuild(opts: StaticBuildOptions) {
return; return;
} }
case 'server': { case 'server': {
await injectManifest(opts, internals);
await generatePages(opts, internals); await generatePages(opts, internals);
await cleanStaticOutput(opts, internals); await cleanStaticOutput(opts, internals);
info(opts.logging, null, `\n${bgMagenta(black(' finalizing server assets '))}\n`); info(opts.logging, null, `\n${bgMagenta(black(' finalizing server assets '))}\n`);
@ -113,11 +112,13 @@ export async function staticBuild(opts: StaticBuildOptions) {
} }
} }
async function ssrBuild(opts: StaticBuildOptions, internals: BuildInternals, input: Set<string>) { 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);
const { lastVitePlugins, vitePlugins } = container.runBeforeHook('ssr', input);
const viteBuildConfig: vite.InlineConfig = { const viteBuildConfig: vite.InlineConfig = {
...viteConfig, ...viteConfig,
mode: viteConfig.mode || 'production', mode: viteConfig.mode || 'production',
@ -155,19 +156,9 @@ async function ssrBuild(opts: StaticBuildOptions, internals: BuildInternals, inp
reportCompressedSize: false, reportCompressedSize: false,
}, },
plugins: [ plugins: [
vitePluginAnalyzer(internals), ...vitePlugins,
vitePluginInternals(input, internals),
vitePluginPages(opts, internals),
rollupPluginAstroBuildCSS({
buildOptions: opts,
internals,
target: 'server',
}),
vitePluginPrerender(opts, internals),
...(viteConfig.plugins || []), ...(viteConfig.plugins || []),
astroContentProdBundlePlugin({ internals }), ...lastVitePlugins
// SSR needs to be last
ssr && vitePluginSSR(internals, settings.adapter!),
], ],
envPrefix: viteConfig.envPrefix ?? 'PUBLIC_', envPrefix: viteConfig.envPrefix ?? 'PUBLIC_',
base: settings.config.base, base: settings.config.base,
@ -187,7 +178,8 @@ async function ssrBuild(opts: StaticBuildOptions, internals: BuildInternals, inp
async function clientBuild( async function clientBuild(
opts: StaticBuildOptions, opts: StaticBuildOptions,
internals: BuildInternals, internals: BuildInternals,
input: Set<string> input: Set<string>,
container: AstroBuildPluginContainer
) { ) {
const { settings, viteConfig } = opts; const { settings, viteConfig } = opts;
const timer = performance.now(); const timer = performance.now();
@ -204,8 +196,10 @@ async function clientBuild(
return null; return null;
} }
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',
@ -229,15 +223,9 @@ async function clientBuild(
}, },
}, },
plugins: [ plugins: [
vitePluginAliasResolve(internals), ...vitePlugins,
vitePluginInternals(input, internals),
vitePluginHoistedScripts(settings, internals),
rollupPluginAstroBuildCSS({
buildOptions: opts,
internals,
target: 'client',
}),
...(viteConfig.plugins || []), ...(viteConfig.plugins || []),
...lastVitePlugins,
], ],
envPrefix: viteConfig.envPrefix ?? 'PUBLIC_', envPrefix: viteConfig.envPrefix ?? 'PUBLIC_',
base: settings.config.base, base: settings.config.base,
@ -256,6 +244,21 @@ async function clientBuild(
return buildResult; return buildResult;
} }
async function runPostBuildHooks(
container: AstroBuildPluginContainer,
ssrReturn: Awaited<ReturnType<typeof ssrBuild>>,
clientReturn: Awaited<ReturnType<typeof clientBuild>>
) {
const mutations = await container.runPostHook(ssrReturn, clientReturn);
const buildConfig = container.options.settings.config.build;
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 });
await fs.promises.writeFile(fileURL, mutation.code, 'utf-8');
}
}
/** /**
* For each statically prerendered page, replace their SSR file with a noop. * For each statically prerendered page, replace their SSR file with a noop.
* This allows us to run the SSR build only once, but still remove dependencies for statically rendered routes. * This allows us to run the SSR build only once, but still remove dependencies for statically rendered routes.

View file

@ -11,6 +11,7 @@ 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;
@ -45,3 +46,7 @@ export interface SingleFileBuiltModule {
pageMap: Map<ComponentPath, ComponentInstance>; pageMap: Map<ComponentPath, ComponentInstance>;
renderers: SSRLoadedRenderer[]; renderers: SSRLoadedRenderer[];
} }
export type ViteBuildReturn = Awaited<ReturnType<typeof vite.build>>;
export type RollupOutput = Extract<Extract<ViteBuildReturn, Exclude<ViteBuildReturn, Array<any>>>, { output: any }>;
export type OutputChunk = Extract<RollupOutput['output'][number], { type: 'chunk' }>;

View file

@ -1,43 +0,0 @@
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 {
return {
name: 'astro:rollup-plugin-prerender',
outputOptions(outputOptions) {
const manualChunks = outputOptions.manualChunks || Function.prototype;
outputOptions.manualChunks = function (id, api, ...args) {
// Defer to user-provided `manualChunks`, if it was provided.
if (typeof manualChunks == 'object') {
if (id in manualChunks) {
return manualChunks[id];
}
} else if (typeof manualChunks === 'function') {
const outid = manualChunks.call(this, id, api, ...args);
if (outid) {
return outid;
}
}
// Split the Astro runtime into a separate chunk for readability
if (id.includes('astro/dist')) {
return 'astro';
}
const pageInfo = internals.pagesByViteID.get(id);
if (pageInfo) {
// prerendered pages should be split into their own chunk
// Important: this can't be in the `pages/` directory!
if (api.getModuleInfo(id)?.meta.astro?.pageOptions?.prerender) {
return `prerender`;
}
// dynamic pages should all go in their own chunk in the pages/* directory
return `pages/all`;
}
};
},
};
}