Skip to content

Commit 25baf2b

Browse files
committed
ci(build): Try to cache mdx bundler
1 parent 5ccf3a3 commit 25baf2b

File tree

2 files changed

+38
-7
lines changed

2 files changed

+38
-7
lines changed

scripts/generate-md-exports.mjs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,10 @@ async function genMDFromHTML(source, target, {cacheDir, noCache}) {
156156
.process(text)
157157
)
158158
);
159-
await copyFile(target, cacheFile, fsConstants.COPYFILE_FICLONE);
159+
copyFile(target, cacheFile, fsConstants.COPYFILE_FICLONE).catch(error => {
160+
// eslint-disable-next-line no-console
161+
console.error(`Failed to cache file ${cacheFile}:`, error);
162+
});
160163
}
161164

162165
async function processTaskList({id, tasks, cacheDir, noCache}) {

src/mdx.ts

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
import fs from 'fs';
2-
import path from 'path';
1+
import {createHash} from 'crypto';
32

43
import {cache} from 'react';
54
import matter from 'gray-matter';
65
import {s} from 'hastscript';
76
import yaml from 'js-yaml';
87
import {bundleMDX} from 'mdx-bundler';
8+
import fs from 'node:fs';
9+
import {readFile, writeFile} from 'node:fs/promises';
10+
import path from 'node:path';
911
import rehypeAutolinkHeadings from 'rehype-autolink-headings';
1012
import rehypePresetMinify from 'rehype-preset-minify';
1113
import rehypePrismDiff from 'rehype-prism-diff';
@@ -33,6 +35,10 @@ import {isNotNil} from './utils';
3335
import {isVersioned, VERSION_INDICATOR} from './versioning';
3436

3537
const root = process.cwd();
38+
const CACHE_DIR = path.join(root, '.next', 'cache', 'mdx-bundler');
39+
fs.mkdirSync(CACHE_DIR, {recursive: true});
40+
41+
const md5 = data => createHash('md5').update(data).digest('hex');
3642

3743
function formatSlug(slug: string) {
3844
return slug.replace(/\.(mdx|md)/, '');
@@ -304,8 +310,10 @@ export async function getFileBySlug(slug: string) {
304310
const configPath = path.join(root, slug.split(VERSION_INDICATOR)[0], 'config.yml');
305311

306312
let configFrontmatter: PlatformConfig | undefined;
307-
if (fs.existsSync(configPath)) {
308-
configFrontmatter = yaml.load(fs.readFileSync(configPath, 'utf8')) as PlatformConfig;
313+
try {
314+
configFrontmatter = yaml.load(await readFile(configPath, 'utf8')) as PlatformConfig;
315+
} catch (e) {
316+
// If the config file does not exist, we can ignore it.
309317
}
310318

311319
let mdxPath = path.join(root, `${slug}.mdx`);
@@ -352,7 +360,20 @@ export async function getFileBySlug(slug: string) {
352360
[mdxPath, mdxIndexPath, mdPath, versionedMdxIndexPath].find(fs.existsSync) ??
353361
mdIndexPath;
354362

355-
const source = fs.readFileSync(sourcePath, 'utf8');
363+
const source = await readFile(sourcePath, 'utf8');
364+
const cacheKey = md5(source);
365+
const cacheFile = path.join(CACHE_DIR, cacheKey);
366+
367+
try {
368+
const cached = JSON.parse(await readFile(cacheFile, 'utf8'));
369+
return cached;
370+
} catch (e) {
371+
if (e.code !== 'ENOENT') {
372+
// If cache is corrupted, ignore and proceed
373+
// eslint-disable-next-line no-console
374+
console.warn(`Failed to read MDX cache: ${cacheFile}`, e);
375+
}
376+
}
356377

357378
process.env.ESBUILD_BINARY_PATH = path.join(
358379
root,
@@ -469,7 +490,7 @@ export async function getFileBySlug(slug: string) {
469490
mergedFrontmatter = {...frontmatter, ...configFrontmatter};
470491
}
471492

472-
return {
493+
const resultObj = {
473494
matter: result.matter,
474495
mdxSource: code,
475496
toc,
@@ -478,6 +499,13 @@ export async function getFileBySlug(slug: string) {
478499
slug,
479500
},
480501
};
502+
503+
writeFile(cacheFile, JSON.stringify(resultObj), 'utf8').catch(e => {
504+
// eslint-disable-next-line no-console
505+
console.warn(`Failed to write MDX cache: ${cacheFile}`, e);
506+
});
507+
508+
return resultObj;
481509
}
482510

483511
/**

0 commit comments

Comments
 (0)