diff --git a/packages/astro/src/core/build/internal.ts b/packages/astro/src/core/build/internal.ts index 55cff37f3..090c69940 100644 --- a/packages/astro/src/core/build/internal.ts +++ b/packages/astro/src/core/build/internal.ts @@ -12,6 +12,8 @@ import { ASTRO_PAGE_EXTENSION_POST_PATTERN } from './plugins/util.js'; import type { PageBuildData, StylesheetAsset, ViteID } from './types.js'; export interface BuildInternals { + cache: any; + /** * Each CSS module is named with a chunk id derived from the Astro pages they * are used in by default. It's easy to crawl this relation in the SSR build as @@ -106,6 +108,8 @@ export function createBuildInternals(): BuildInternals { const hoistedScriptIdToPagesMap = new Map>(); return { + cache: [], + cssModuleToChunkIdMap: new Map(), hoistedScriptIdToHoistedMap, hoistedScriptIdToPagesMap, diff --git a/packages/astro/src/core/build/static-build.ts b/packages/astro/src/core/build/static-build.ts index 149b6760d..ee657f2df 100644 --- a/packages/astro/src/core/build/static-build.ts +++ b/packages/astro/src/core/build/static-build.ts @@ -77,12 +77,41 @@ export async function viteBuild(opts: StaticBuildOptions) { const container = createPluginContainer(opts, internals); registerAllPlugins(container); + // HACK(nate): Begin cache restoration + const cacheDir = new URL('./node_modules/.cache/astro/', settings.config.root); + const cacheFile = new URL('./build.json', cacheDir); + let restore = false; + if (fs.existsSync(cacheDir)) { + internals.cache = JSON.parse(fs.readFileSync(cacheFile, { encoding: 'utf8' })); + restore = true; + } + // Build your project (SSR application code, assets, client JS, etc.) const ssrTime = performance.now(); opts.logger.info('build', `Building ${settings.config.output} entrypoints...`); const ssrOutput = await ssrBuild(opts, internals, pageInput, container); opts.logger.info('build', dim(`Completed in ${getTimeStat(ssrTime, performance.now())}.`)); + // HACK(nate): write to cache + internals.cache = []; + for (const output of ssrOutput.output) { + const md = output.moduleIds.filter(id => id.endsWith('.md')) + if (!!md) { + console.log(output.moduleIds); + // const { fileName: id, code: content } = output.moduleIds + // internals.cache.push({ input, output: { id, content } }) + } + } + fs.mkdirSync(cacheDir, { recursive: true }); + fs.writeFileSync(cacheFile, JSON.stringify(internals.cache), { encoding: 'utf8' }); + + if (restore) { + console.log('RESTORING CACHE'); + for (const cached of internals.cache) { + fs.writeFileSync(new URL(`./${cached.output.id}`, settings.config.outDir), cached.output.content, { encoding: 'utf8' }); + } + } + settings.timer.end('SSR build'); settings.timer.start('Client build'); @@ -123,7 +152,7 @@ export async function staticBuild(opts: StaticBuildOptions, internals: BuildInte case settings.config.output === 'static': { settings.timer.start('Static generate'); await generatePages(opts, internals); - await cleanServerOutput(opts); + // await cleanServerOutput(opts); settings.timer.end('Static generate'); return; }