0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-01-06 22:10:10 -05:00

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
This commit is contained in:
Pascal Schilp 2022-05-03 16:55:25 +02:00 committed by GitHub
parent dd9ffc317f
commit adada1ba01
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 34 additions and 9 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Adds astro:build:ssr integration hook

View file

@ -13,6 +13,7 @@ import type {
import type { AstroConfigSchema } from '../core/config'; import type { AstroConfigSchema } from '../core/config';
import type { AstroComponentFactory, Metadata } from '../runtime/server'; import type { AstroComponentFactory, Metadata } from '../runtime/server';
import type { ViteConfigWithSSR } from '../core/create-vite'; import type { ViteConfigWithSSR } from '../core/create-vite';
import type { SerializedSSRManifest } from '../core/app/types';
export type { SSRManifest } from '../core/app/types'; export type { SSRManifest } from '../core/app/types';
export interface AstroBuiltinProps { export interface AstroBuiltinProps {
@ -907,6 +908,7 @@ export interface AstroIntegration {
'astro:server:setup'?: (options: { server: vite.ViteDevServer }) => void | Promise<void>; 'astro:server:setup'?: (options: { server: vite.ViteDevServer }) => void | Promise<void>;
'astro:server:start'?: (options: { address: AddressInfo }) => void | Promise<void>; 'astro:server:start'?: (options: { address: AddressInfo }) => void | Promise<void>;
'astro:server:done'?: () => void | Promise<void>; 'astro:server:done'?: () => void | Promise<void>;
'astro:build:ssr'?: (options: { manifest: SerializedSSRManifest }) => void | Promise<void>;
'astro:build:start'?: (options: { buildConfig: BuildConfig }) => void | Promise<void>; 'astro:build:start'?: (options: { buildConfig: BuildConfig }) => void | Promise<void>;
'astro:build:setup'?: (options: { 'astro:build:setup'?: (options: {
vite: ViteConfigWithSSR; vite: ViteConfigWithSSR;

View file

@ -20,6 +20,9 @@ import {
} from '../render/ssr-element.js'; } from '../render/ssr-element.js';
import { prependForwardSlash } from '../path.js'; import { prependForwardSlash } from '../path.js';
export const pagesVirtualModuleId = '@astrojs-pages-virtual-entry';
export const resolvedPagesVirtualModuleId = '\0' + pagesVirtualModuleId;
export class App { export class App {
#manifest: Manifest; #manifest: Manifest;
#manifestData: ManifestData; #manifestData: ManifestData;

View file

@ -4,9 +4,7 @@ import type { StaticBuildOptions } from './types';
import { addRollupInput } from './add-rollup-input.js'; import { addRollupInput } from './add-rollup-input.js';
import { eachPageData } from './internal.js'; import { eachPageData } from './internal.js';
import { isBuildingToSSR } from '../util.js'; import { isBuildingToSSR } from '../util.js';
import { resolvedPagesVirtualModuleId, pagesVirtualModuleId } from '../app/index.js';
export const virtualModuleId = '@astrojs-pages-virtual-entry';
export const resolvedVirtualModuleId = '\0' + virtualModuleId;
export function vitePluginPages(opts: StaticBuildOptions, internals: BuildInternals): VitePlugin { export function vitePluginPages(opts: StaticBuildOptions, internals: BuildInternals): VitePlugin {
return { return {
@ -14,18 +12,18 @@ export function vitePluginPages(opts: StaticBuildOptions, internals: BuildIntern
options(options) { options(options) {
if (!isBuildingToSSR(opts.astroConfig)) { if (!isBuildingToSSR(opts.astroConfig)) {
return addRollupInput(options, [virtualModuleId]); return addRollupInput(options, [pagesVirtualModuleId]);
} }
}, },
resolveId(id) { resolveId(id) {
if (id === virtualModuleId) { if (id === pagesVirtualModuleId) {
return resolvedVirtualModuleId; return resolvedPagesVirtualModuleId;
} }
}, },
load(id) { load(id) {
if (id === resolvedVirtualModuleId) { if (id === resolvedPagesVirtualModuleId) {
let importMap = ''; let importMap = '';
let imports = []; let imports = [];
let i = 0; let i = 0;

View file

@ -9,8 +9,9 @@ import { eachPageData } from './internal.js';
import { addRollupInput } from './add-rollup-input.js'; import { addRollupInput } from './add-rollup-input.js';
import { fileURLToPath } from 'url'; import { fileURLToPath } from 'url';
import glob from 'fast-glob'; 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 { BEFORE_HYDRATION_SCRIPT_ID } from '../../vite-plugin-scripts/index.js';
import { runHookBuildSsr } from '../../integrations/index.js';
export const virtualModuleId = '@astrojs-ssr-virtual-entry'; export const virtualModuleId = '@astrojs-ssr-virtual-entry';
const resolvedVirtualModuleId = '\0' + virtualModuleId; const resolvedVirtualModuleId = '\0' + virtualModuleId;
@ -73,6 +74,7 @@ if(_start in adapter) {
}); });
const manifest = buildManifest(buildOpts, internals, staticFiles); const manifest = buildManifest(buildOpts, internals, staticFiles);
await runHookBuildSsr({config: buildOpts.astroConfig, manifest});
for (const [_chunkName, chunk] of Object.entries(bundle)) { for (const [_chunkName, chunk] of Object.entries(bundle)) {
if (chunk.type === 'asset') { if (chunk.type === 'asset') {

View file

@ -1,5 +1,6 @@
import type { AddressInfo } from 'net'; import type { AddressInfo } from 'net';
import type { ViteDevServer } from 'vite'; import type { ViteDevServer } from 'vite';
import type { SerializedSSRManifest } from '../core/app/types';
import { AstroConfig, AstroRenderer, BuildConfig, RouteData } from '../@types/astro.js'; import { AstroConfig, AstroRenderer, BuildConfig, RouteData } from '../@types/astro.js';
import { mergeConfig } from '../core/config.js'; import { mergeConfig } from '../core/config.js';
import ssgAdapter from '../adapter-ssg/index.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({ export async function runHookBuildDone({
config, config,
buildConfig, buildConfig,

View file

@ -8,7 +8,7 @@ import { fileURLToPath } from 'url';
import type { Plugin } from 'vite'; import type { Plugin } from 'vite';
import type { AstroConfig } from '../@types/astro'; import type { AstroConfig } from '../@types/astro';
import { PAGE_SSR_SCRIPT_ID } from '../vite-plugin-scripts/index.js'; 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 { appendForwardSlash } from '../core/path.js';
import { resolvePages } from '../core/util.js'; import { resolvePages } from '../core/util.js';