mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-04 20:57:17 +02:00
refactor: use cache for blogDatas generation
This commit is contained in:
parent
f7f063c56e
commit
a80399631f
4 changed files with 25 additions and 9 deletions
|
@ -17,8 +17,8 @@ class App extends React.Component {
|
||||||
<div>
|
<div>
|
||||||
{blogDatas.map(({path}) => {
|
{blogDatas.map(({path}) => {
|
||||||
return (
|
return (
|
||||||
<div>
|
<div key={path}>
|
||||||
<Link key={path} to={path}>
|
<Link to={path}>
|
||||||
{path}
|
{path}
|
||||||
</Link>
|
</Link>
|
||||||
</div>
|
</div>
|
||||||
|
|
14
lib/helpers/generate.js
Normal file
14
lib/helpers/generate.js
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
const path = require('path');
|
||||||
|
const fs = require('fs-extra');
|
||||||
|
|
||||||
|
const genPath = path.resolve(__dirname, '../generated');
|
||||||
|
fs.ensureDirSync(genPath);
|
||||||
|
|
||||||
|
const genCache = new Map();
|
||||||
|
module.exports = async function(file, content) {
|
||||||
|
const cached = genCache.get(file);
|
||||||
|
if (cached !== content) {
|
||||||
|
await fs.writeFile(path.join(genPath, file), content);
|
||||||
|
genCache.set(file, content);
|
||||||
|
}
|
||||||
|
};
|
1
lib/helpers/index.js
Normal file
1
lib/helpers/index.js
Normal file
|
@ -0,0 +1 @@
|
||||||
|
exports.generate = require('./generate');
|
|
@ -2,6 +2,7 @@ const fs = require('fs-extra');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const loadConfig = require('./config');
|
const loadConfig = require('./config');
|
||||||
const loadBlog = require('./blog');
|
const loadBlog = require('./blog');
|
||||||
|
const {generate} = require('../helpers');
|
||||||
|
|
||||||
module.exports = async function load(sourceDir) {
|
module.exports = async function load(sourceDir) {
|
||||||
// load siteConfig
|
// load siteConfig
|
||||||
|
@ -10,13 +11,13 @@ module.exports = async function load(sourceDir) {
|
||||||
// extract data from all blog files
|
// extract data from all blog files
|
||||||
const blogDatas = await loadBlog(sourceDir);
|
const blogDatas = await loadBlog(sourceDir);
|
||||||
|
|
||||||
fs.writeFile(
|
await generate(
|
||||||
path.resolve(__dirname, '../generated/blogDatas.js'),
|
'blogDatas.js',
|
||||||
`${'/**\n' +
|
`${'/**\n * @generated\n */\n' + 'module.exports = '}${JSON.stringify(
|
||||||
' * @' +
|
blogDatas,
|
||||||
'generated\n' +
|
null,
|
||||||
' */\n' +
|
2
|
||||||
'module.exports = '}${JSON.stringify(blogDatas, null, 2)};\n`
|
)};\n`
|
||||||
);
|
);
|
||||||
|
|
||||||
// resolve outDir
|
// resolve outDir
|
||||||
|
|
Loading…
Add table
Reference in a new issue