mirror of
https://github.com/reactjs/react.dev.git
synced 2026-02-23 20:23:08 +00:00
[Beta] Add disk cache for MDX (#5005)
* [Beta] Add disk cache for MDX
* Test cache
* Revert "Test cache"
This reverts commit 7d64ec4cd1.
* Add cache miss log
This commit is contained in:
@@ -73,6 +73,7 @@
|
||||
"is-ci": "^3.0.1",
|
||||
"lint-staged": ">=10",
|
||||
"mdast-util-to-string": "^1.1.0",
|
||||
"metro-cache": "0.72.2",
|
||||
"npm-run-all": "^4.1.5",
|
||||
"patch-package": "^6.2.2",
|
||||
"postcss": "^8.4.5",
|
||||
|
||||
@@ -86,26 +86,60 @@ export async function getStaticProps(context) {
|
||||
} catch {
|
||||
mdxWithFrontmatter = fs.readFileSync(rootDir + path + '/index.md', 'utf8');
|
||||
}
|
||||
const {content: mdx, data: meta} = fm(mdxWithFrontmatter);
|
||||
|
||||
const DISK_CACHE_BREAKER = 1; // <-- Bump to reset the cache
|
||||
async function compileMdxToJs_FROM_DISK_CACHE(mdxContent, mdxComponentNames) {
|
||||
const {FileStore, stableHash} = require('metro-cache');
|
||||
const store = new FileStore({
|
||||
root: process.cwd() + '/node_modules/.cache/react-docs-mdx/',
|
||||
});
|
||||
const hash = Buffer.from(
|
||||
stableHash({
|
||||
mdxContent,
|
||||
mdxComponentNames,
|
||||
lockfile: fs.readFileSync(process.cwd() + '/yarn.lock', 'utf8'),
|
||||
DISK_CACHE_BREAKER,
|
||||
})
|
||||
);
|
||||
const cached = await store.get(hash);
|
||||
if (cached) {
|
||||
console.log(
|
||||
'Reading compiled MDX for /' + path + ' from ./node_modules/.cache/'
|
||||
);
|
||||
return cached;
|
||||
}
|
||||
if (process.ENV.NODE_ENV === 'production') {
|
||||
console.log(
|
||||
'Cache miss for MDX for /' + path + ' from ./node_modules/.cache/'
|
||||
);
|
||||
}
|
||||
const output = await compileMdxToJs(mdxContent, mdxComponentNames);
|
||||
await store.set(hash, output);
|
||||
return output;
|
||||
}
|
||||
|
||||
// IMPORTANT: Keep this a pure function. Its output gets cached on disk.
|
||||
async function compileMdxToJs(mdxContent, mdxComponentNames) {
|
||||
// If we don't add these fake imports, the MDX compiler
|
||||
// will insert a bunch of opaque components we can't introspect.
|
||||
// This will break the prepareMDX() call below.
|
||||
let mdxWithFakeImports = mdxComponentNames
|
||||
.map((key) => 'import ' + key + ' from "' + key + '";\n')
|
||||
.join('\n');
|
||||
mdxWithFakeImports += '\n' + mdxContent;
|
||||
const jsxCode = await compileMdx(mdxWithFakeImports, {
|
||||
remarkPlugins,
|
||||
});
|
||||
return transform(jsxCode, {
|
||||
plugins: ['@babel/plugin-transform-modules-commonjs'],
|
||||
presets: ['@babel/preset-react'],
|
||||
}).code;
|
||||
}
|
||||
|
||||
// Turn the MDX we just read into some JS we can execute.
|
||||
let mdxWithFakeImports = '';
|
||||
for (let key in MDXComponents) {
|
||||
if (MDXComponents.hasOwnProperty(key)) {
|
||||
// If we don't add these fake imports, the MDX compiler
|
||||
// will insert a bunch of opaque components we can't introspect.
|
||||
// This will break the prepareMDX() call below.
|
||||
mdxWithFakeImports += 'import ' + key + ' from "' + key + '";\n';
|
||||
}
|
||||
}
|
||||
mdxWithFakeImports += '\n' + mdx;
|
||||
const jsxCode = await compileMdx(mdxWithFakeImports, {
|
||||
remarkPlugins,
|
||||
});
|
||||
const jsCode = transform(jsxCode, {
|
||||
plugins: ['@babel/plugin-transform-modules-commonjs'],
|
||||
presets: ['@babel/preset-react'],
|
||||
}).code;
|
||||
const {content: mdx, data: meta} = fm(mdxWithFrontmatter);
|
||||
const mdxComponentNames = Object.keys(MDXComponents);
|
||||
const jsCode = await compileMdxToJs_FROM_DISK_CACHE(mdx, mdxComponentNames);
|
||||
|
||||
// Prepare environment for MDX.
|
||||
let fakeExports = {};
|
||||
|
||||
@@ -1331,6 +1331,11 @@
|
||||
resolved "https://registry.yarnpkg.com/@yarnpkg/lockfile/-/lockfile-1.1.0.tgz#e77a97fbd345b76d83245edcd17d393b1b41fb31"
|
||||
integrity sha512-GpSwvyXOcOOlV70vbnzjj4fW5xW/FdUF6nQEt1ENy7m4ZCczi1+/buVUPAqmGfqznsORNFzUMjctTIp8a9tuCQ==
|
||||
|
||||
absolute-path@^0.0.0:
|
||||
version "0.0.0"
|
||||
resolved "https://registry.yarnpkg.com/absolute-path/-/absolute-path-0.0.0.tgz#a78762fbdadfb5297be99b15d35a785b2f095bf7"
|
||||
integrity sha512-HQiug4c+/s3WOvEnDRxXVmNtSG5s2gJM9r19BTcqjp7BWcE48PB+Y2G6jE65kqI0LpsQeMZygt/b60Gi4KxGyA==
|
||||
|
||||
accepts@~1.3.8:
|
||||
version "1.3.8"
|
||||
resolved "https://registry.yarnpkg.com/accepts/-/accepts-1.3.8.tgz#0bf0be125b67014adcb0b0921e62db7bffe16b2e"
|
||||
@@ -3521,6 +3526,11 @@ lodash.merge@^4.6.2:
|
||||
resolved "https://registry.yarnpkg.com/lodash.merge/-/lodash.merge-4.6.2.tgz#558aa53b43b661e1925a0afdfa36a9a1085fe57a"
|
||||
integrity sha512-0KpjqXRVvrYyCsX1swR/XTK0va6VQkQM6MNo7PqW77ByjAhoARA8EfrP1N4+KlKj8YS0ZUCtRT/YUuhyYDujIQ==
|
||||
|
||||
lodash.throttle@^4.1.1:
|
||||
version "4.1.1"
|
||||
resolved "https://registry.yarnpkg.com/lodash.throttle/-/lodash.throttle-4.1.1.tgz#c23e91b710242ac70c37f1e1cda9274cc39bf2f4"
|
||||
integrity sha512-wIkUCfVKpVsWo3JSZlc+8MB5it+2AN5W8J7YVMST30UrvcQNZ1Okbj+rbVniijTWE6FGYy4XJq/rHkas8qJMLQ==
|
||||
|
||||
lodash.truncate@^4.4.2:
|
||||
version "4.4.2"
|
||||
resolved "https://registry.yarnpkg.com/lodash.truncate/-/lodash.truncate-4.4.2.tgz#5a350da0b1113b837ecfffd5812cbe58d6eae193"
|
||||
@@ -3683,6 +3693,29 @@ methods@~1.1.2:
|
||||
resolved "https://registry.yarnpkg.com/methods/-/methods-1.1.2.tgz#5529a4d67654134edcc5266656835b0f851afcee"
|
||||
integrity sha512-iclAHeNqNm68zFtnZ0e+1L2yUIdvzNoauKU4WBA3VvH/vPFieF7qfRlwUZU+DA9P9bPXIS90ulxoUoCH23sV2w==
|
||||
|
||||
metro-cache@0.72.2:
|
||||
version "0.72.2"
|
||||
resolved "https://registry.yarnpkg.com/metro-cache/-/metro-cache-0.72.2.tgz#114e62dad3539c41cf5625045b9a6e5181499f20"
|
||||
integrity sha512-0Yw3J32eYTp7x7bAAg+a9ScBG/mpib6Wq4WPSYvhoNilPFHzh7knLDMil3WGVCQlI1r+5xtpw/FDhNVKuypQqg==
|
||||
dependencies:
|
||||
metro-core "0.72.2"
|
||||
rimraf "^2.5.4"
|
||||
|
||||
metro-core@0.72.2:
|
||||
version "0.72.2"
|
||||
resolved "https://registry.yarnpkg.com/metro-core/-/metro-core-0.72.2.tgz#ca975cfebce5c89f51dca905777bc3877008ae69"
|
||||
integrity sha512-OXNH8UbKIhvpyHGJrdQYnPUmyPHSuVY4OO6pQxODdTW+uiO68PPPgIIVN67vlCAirZolxRFpma70N7m0sGCZyg==
|
||||
dependencies:
|
||||
lodash.throttle "^4.1.1"
|
||||
metro-resolver "0.72.2"
|
||||
|
||||
metro-resolver@0.72.2:
|
||||
version "0.72.2"
|
||||
resolved "https://registry.yarnpkg.com/metro-resolver/-/metro-resolver-0.72.2.tgz#332ecd646d683a47923fc403e3df37a7cf96da3b"
|
||||
integrity sha512-5KTWolUgA6ivLkg3DmFS2WltphBPQW7GT7An+6Izk/NU+y/6crmsoaLmNxjpZo4Fv+i/FxDSXqpbpQ6KrRWvlQ==
|
||||
dependencies:
|
||||
absolute-path "^0.0.0"
|
||||
|
||||
micromatch@^4.0.2, micromatch@^4.0.4:
|
||||
version "4.0.4"
|
||||
resolved "https://registry.yarnpkg.com/micromatch/-/micromatch-4.0.4.tgz#896d519dfe9db25fce94ceb7a500919bf881ebf9"
|
||||
@@ -4908,7 +4941,7 @@ rfdc@^1.3.0:
|
||||
resolved "https://registry.yarnpkg.com/rfdc/-/rfdc-1.3.0.tgz#d0b7c441ab2720d05dc4cf26e01c89631d9da08b"
|
||||
integrity sha512-V2hovdzFbOi77/WajaSMXk2OLm+xNIeQdMMuB7icj7bk6zi2F8GGAxigcnDFpJHbNyNcgyJDiP+8nOrY5cZGrA==
|
||||
|
||||
rimraf@^2.6.3:
|
||||
rimraf@^2.5.4, rimraf@^2.6.3:
|
||||
version "2.7.1"
|
||||
resolved "https://registry.yarnpkg.com/rimraf/-/rimraf-2.7.1.tgz#35797f13a7fdadc566142c29d4f07ccad483e3ec"
|
||||
integrity sha512-uWjbaKIK3T1OSVptzX7Nl6PvQ3qAGtKEtVRjRuazjfL3Bx5eI409VZSqgND+4UNnmzLVdPj9FqFJNPqBZFve4w==
|
||||
|
||||
Reference in New Issue
Block a user