From c562eebd27bb4f270688d6b07fe293510ddacc8c Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 17 Jun 2022 14:29:56 -0400 Subject: [PATCH] Inject post-build --- packages/astro/src/core/build/static-build.ts | 7 ++-- .../astro/src/core/build/vite-plugin-ssr.ts | 40 ++++++++----------- 2 files changed, 20 insertions(+), 27 deletions(-) diff --git a/packages/astro/src/core/build/static-build.ts b/packages/astro/src/core/build/static-build.ts index 01cbe2d5d..2300c790a 100644 --- a/packages/astro/src/core/build/static-build.ts +++ b/packages/astro/src/core/build/static-build.ts @@ -22,7 +22,7 @@ import { getTimeStat } from './util.js'; import { vitePluginHoistedScripts } from './vite-plugin-hoisted-scripts.js'; import { vitePluginInternals } from './vite-plugin-internals.js'; import { vitePluginPages } from './vite-plugin-pages.js'; -import { vitePluginSSR, vitePluginSSRInject } from './vite-plugin-ssr.js'; +import { vitePluginSSR, injectManifest } from './vite-plugin-ssr.js'; import { vitePluginAnalyzer } from './vite-plugin-analyzer.js'; export async function staticBuild(opts: StaticBuildOptions) { @@ -89,6 +89,9 @@ export async function staticBuild(opts: StaticBuildOptions) { await cleanSsrOutput(opts); } } else { + // Inject the manifest + await injectManifest(opts, internals) + info(opts.logging, null, `\n${bgMagenta(black(' finalizing server assets '))}\n`); await ssrMoveAssets(opts); } @@ -219,8 +222,6 @@ async function clientBuild( target: 'client', }), ...(viteConfig.plugins || []), - // SSR needs to be last - isBuildingToSSR(opts.astroConfig) && vitePluginSSRInject(opts, internals), ], publicDir: viteConfig.publicDir, root: viteConfig.root, diff --git a/packages/astro/src/core/build/vite-plugin-ssr.ts b/packages/astro/src/core/build/vite-plugin-ssr.ts index d42f738c3..4fc1274c7 100644 --- a/packages/astro/src/core/build/vite-plugin-ssr.ts +++ b/packages/astro/src/core/build/vite-plugin-ssr.ts @@ -96,31 +96,23 @@ if(_start in adapter) { }; } -export function vitePluginSSRInject( - buildOpts: StaticBuildOptions, - internals: BuildInternals, -): VitePlugin { - return { - name: '@astrojs/vite-plugin-astro-ssr-inject', - async writeBundle(opts, bundle) { - if(!internals.ssrEntryChunk) { - throw new Error(`Did not generate an entry chunk for SSR`); - } - - const staticFiles = internals.staticFiles; - const manifest = buildManifest(buildOpts, internals, Array.from(staticFiles)); - await runHookBuildSsr({ config: buildOpts.astroConfig, manifest }); - - const chunk = internals.ssrEntryChunk; - const code = chunk.code; - chunk.code = code.replace(replaceExp, () => { - 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'); - }, +export async function injectManifest(buildOpts: StaticBuildOptions, internals: BuildInternals) { + if(!internals.ssrEntryChunk) { + throw new Error(`Did not generate an entry chunk for SSR`); } + + const staticFiles = internals.staticFiles; + const manifest = buildManifest(buildOpts, internals, Array.from(staticFiles)); + await runHookBuildSsr({ config: buildOpts.astroConfig, manifest }); + + const chunk = internals.ssrEntryChunk; + const code = chunk.code; + chunk.code = code.replace(replaceExp, () => { + 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(