Inject post-build

This commit is contained in:
unknown 2022-06-17 14:29:56 -04:00
parent 599447c905
commit c562eebd27
2 changed files with 20 additions and 27 deletions

View file

@ -22,7 +22,7 @@ import { getTimeStat } from './util.js';
import { vitePluginHoistedScripts } from './vite-plugin-hoisted-scripts.js'; import { vitePluginHoistedScripts } from './vite-plugin-hoisted-scripts.js';
import { vitePluginInternals } from './vite-plugin-internals.js'; import { vitePluginInternals } from './vite-plugin-internals.js';
import { vitePluginPages } from './vite-plugin-pages.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'; import { vitePluginAnalyzer } from './vite-plugin-analyzer.js';
export async function staticBuild(opts: StaticBuildOptions) { export async function staticBuild(opts: StaticBuildOptions) {
@ -89,6 +89,9 @@ export async function staticBuild(opts: StaticBuildOptions) {
await cleanSsrOutput(opts); await cleanSsrOutput(opts);
} }
} else { } else {
// Inject the manifest
await injectManifest(opts, internals)
info(opts.logging, null, `\n${bgMagenta(black(' finalizing server assets '))}\n`); info(opts.logging, null, `\n${bgMagenta(black(' finalizing server assets '))}\n`);
await ssrMoveAssets(opts); await ssrMoveAssets(opts);
} }
@ -219,8 +222,6 @@ async function clientBuild(
target: 'client', target: 'client',
}), }),
...(viteConfig.plugins || []), ...(viteConfig.plugins || []),
// SSR needs to be last
isBuildingToSSR(opts.astroConfig) && vitePluginSSRInject(opts, internals),
], ],
publicDir: viteConfig.publicDir, publicDir: viteConfig.publicDir,
root: viteConfig.root, root: viteConfig.root,

View file

@ -96,31 +96,23 @@ if(_start in adapter) {
}; };
} }
export function vitePluginSSRInject( export async function injectManifest(buildOpts: StaticBuildOptions, internals: BuildInternals) {
buildOpts: StaticBuildOptions, if(!internals.ssrEntryChunk) {
internals: BuildInternals, throw new Error(`Did not generate an entry chunk for SSR`);
): 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');
},
} }
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( function buildManifest(