Inject post-build
This commit is contained in:
parent
599447c905
commit
c562eebd27
2 changed files with 20 additions and 27 deletions
|
@ -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,
|
||||
|
|
|
@ -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(
|
||||
|
|
Loading…
Reference in a new issue