From adada1ba0169516495ca19107f974aa0d4cf1f49 Mon Sep 17 00:00:00 2001 From: Pascal Schilp Date: Tue, 3 May 2022 16:55:25 +0200 Subject: [PATCH] Feat/ssr manifest (#3262) * feat: expose pagesVirtualModuleId * feat: add astro:build:ssr hook * fix: missing import for pagesVirtualModuleId * test: fix tests * refactor: move exports to index.ts * chore: changeset --- .changeset/twelve-onions-warn.md | 5 +++++ packages/astro/src/@types/astro.ts | 2 ++ packages/astro/src/core/app/index.ts | 3 +++ .../astro/src/core/build/vite-plugin-pages.ts | 12 +++++------- packages/astro/src/core/build/vite-plugin-ssr.ts | 4 +++- packages/astro/src/integrations/index.ts | 15 +++++++++++++++ packages/astro/src/vite-plugin-markdown/index.ts | 2 +- 7 files changed, 34 insertions(+), 9 deletions(-) create mode 100644 .changeset/twelve-onions-warn.md diff --git a/.changeset/twelve-onions-warn.md b/.changeset/twelve-onions-warn.md new file mode 100644 index 000000000..0136dd0d2 --- /dev/null +++ b/.changeset/twelve-onions-warn.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Adds astro:build:ssr integration hook diff --git a/packages/astro/src/@types/astro.ts b/packages/astro/src/@types/astro.ts index 9e127c62a..0ecc7e414 100644 --- a/packages/astro/src/@types/astro.ts +++ b/packages/astro/src/@types/astro.ts @@ -13,6 +13,7 @@ import type { import type { AstroConfigSchema } from '../core/config'; import type { AstroComponentFactory, Metadata } from '../runtime/server'; import type { ViteConfigWithSSR } from '../core/create-vite'; +import type { SerializedSSRManifest } from '../core/app/types'; export type { SSRManifest } from '../core/app/types'; export interface AstroBuiltinProps { @@ -907,6 +908,7 @@ export interface AstroIntegration { 'astro:server:setup'?: (options: { server: vite.ViteDevServer }) => void | Promise; 'astro:server:start'?: (options: { address: AddressInfo }) => void | Promise; 'astro:server:done'?: () => void | Promise; + 'astro:build:ssr'?: (options: { manifest: SerializedSSRManifest }) => void | Promise; 'astro:build:start'?: (options: { buildConfig: BuildConfig }) => void | Promise; 'astro:build:setup'?: (options: { vite: ViteConfigWithSSR; diff --git a/packages/astro/src/core/app/index.ts b/packages/astro/src/core/app/index.ts index 6fb2d61be..307508fc7 100644 --- a/packages/astro/src/core/app/index.ts +++ b/packages/astro/src/core/app/index.ts @@ -20,6 +20,9 @@ import { } from '../render/ssr-element.js'; import { prependForwardSlash } from '../path.js'; +export const pagesVirtualModuleId = '@astrojs-pages-virtual-entry'; +export const resolvedPagesVirtualModuleId = '\0' + pagesVirtualModuleId; + export class App { #manifest: Manifest; #manifestData: ManifestData; diff --git a/packages/astro/src/core/build/vite-plugin-pages.ts b/packages/astro/src/core/build/vite-plugin-pages.ts index 788994b78..25adbcae8 100644 --- a/packages/astro/src/core/build/vite-plugin-pages.ts +++ b/packages/astro/src/core/build/vite-plugin-pages.ts @@ -4,9 +4,7 @@ import type { StaticBuildOptions } from './types'; import { addRollupInput } from './add-rollup-input.js'; import { eachPageData } from './internal.js'; import { isBuildingToSSR } from '../util.js'; - -export const virtualModuleId = '@astrojs-pages-virtual-entry'; -export const resolvedVirtualModuleId = '\0' + virtualModuleId; +import { resolvedPagesVirtualModuleId, pagesVirtualModuleId } from '../app/index.js'; export function vitePluginPages(opts: StaticBuildOptions, internals: BuildInternals): VitePlugin { return { @@ -14,18 +12,18 @@ export function vitePluginPages(opts: StaticBuildOptions, internals: BuildIntern options(options) { if (!isBuildingToSSR(opts.astroConfig)) { - return addRollupInput(options, [virtualModuleId]); + return addRollupInput(options, [pagesVirtualModuleId]); } }, resolveId(id) { - if (id === virtualModuleId) { - return resolvedVirtualModuleId; + if (id === pagesVirtualModuleId) { + return resolvedPagesVirtualModuleId; } }, load(id) { - if (id === resolvedVirtualModuleId) { + if (id === resolvedPagesVirtualModuleId) { let importMap = ''; let imports = []; let i = 0; diff --git a/packages/astro/src/core/build/vite-plugin-ssr.ts b/packages/astro/src/core/build/vite-plugin-ssr.ts index 0bf1682f4..9f9acd57e 100644 --- a/packages/astro/src/core/build/vite-plugin-ssr.ts +++ b/packages/astro/src/core/build/vite-plugin-ssr.ts @@ -9,8 +9,9 @@ import { eachPageData } from './internal.js'; import { addRollupInput } from './add-rollup-input.js'; import { fileURLToPath } from 'url'; import glob from 'fast-glob'; -import { virtualModuleId as pagesVirtualModuleId } from './vite-plugin-pages.js'; +import { pagesVirtualModuleId } from '../app/index.js'; import { BEFORE_HYDRATION_SCRIPT_ID } from '../../vite-plugin-scripts/index.js'; +import { runHookBuildSsr } from '../../integrations/index.js'; export const virtualModuleId = '@astrojs-ssr-virtual-entry'; const resolvedVirtualModuleId = '\0' + virtualModuleId; @@ -73,6 +74,7 @@ if(_start in adapter) { }); const manifest = buildManifest(buildOpts, internals, staticFiles); + await runHookBuildSsr({config: buildOpts.astroConfig, manifest}); for (const [_chunkName, chunk] of Object.entries(bundle)) { if (chunk.type === 'asset') { diff --git a/packages/astro/src/integrations/index.ts b/packages/astro/src/integrations/index.ts index 452081e48..611106ca5 100644 --- a/packages/astro/src/integrations/index.ts +++ b/packages/astro/src/integrations/index.ts @@ -1,5 +1,6 @@ import type { AddressInfo } from 'net'; import type { ViteDevServer } from 'vite'; +import type { SerializedSSRManifest } from '../core/app/types'; import { AstroConfig, AstroRenderer, BuildConfig, RouteData } from '../@types/astro.js'; import { mergeConfig } from '../core/config.js'; import ssgAdapter from '../adapter-ssg/index.js'; @@ -135,6 +136,20 @@ export async function runHookBuildSetup({ } } +export async function runHookBuildSsr({ + config, + manifest, +}: { + config: AstroConfig; + manifest: SerializedSSRManifest; +}) { + for (const integration of config.integrations) { + if (integration.hooks['astro:build:ssr']) { + await integration.hooks['astro:build:ssr']({ manifest }); + } + } +} + export async function runHookBuildDone({ config, buildConfig, diff --git a/packages/astro/src/vite-plugin-markdown/index.ts b/packages/astro/src/vite-plugin-markdown/index.ts index 405fac742..2bbd72ffa 100644 --- a/packages/astro/src/vite-plugin-markdown/index.ts +++ b/packages/astro/src/vite-plugin-markdown/index.ts @@ -8,7 +8,7 @@ import { fileURLToPath } from 'url'; import type { Plugin } from 'vite'; import type { AstroConfig } from '../@types/astro'; import { PAGE_SSR_SCRIPT_ID } from '../vite-plugin-scripts/index.js'; -import { virtualModuleId as pagesVirtualModuleId } from '../core/build/vite-plugin-pages.js'; +import { pagesVirtualModuleId } from '../core/app/index.js'; import { appendForwardSlash } from '../core/path.js'; import { resolvePages } from '../core/util.js';