wip: incremental

This commit is contained in:
Nate Moore 2023-09-19 08:43:20 -05:00
parent e8495c853b
commit c7e3c8d0e2
2 changed files with 34 additions and 1 deletions

View file

@ -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<string, Set<string>>();
return {
cache: [],
cssModuleToChunkIdMap: new Map(),
hoistedScriptIdToHoistedMap,
hoistedScriptIdToPagesMap,

View file

@ -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;
}