Skip to content

Commit

Permalink
refactor(v2): shift generated files out of core into website dir
Browse files Browse the repository at this point in the history
  • Loading branch information
yangshun committed Feb 24, 2019
1 parent d5722f0 commit 211e04f
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ node_modules

yarn-error.log
build
__generated__
.docusaurus
2 changes: 1 addition & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
__generated__
__fixtures__
dist
node_modules
build
.docusaurus
1 change: 0 additions & 1 deletion v2/.eslintignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
__generated__
__fixtures__
dist
node_modules
Expand Down
10 changes: 10 additions & 0 deletions v2/lib/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* Copyright (c) 2017-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

module.exports = {
GENERATED_FILES_DIR_NAME: '.docusaurus',
};
22 changes: 20 additions & 2 deletions v2/lib/load/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/

const fs = require('fs-extra');
const path = require('path');
const loadBlog = require('./blog');
const loadConfig = require('./config');
Expand All @@ -14,18 +15,30 @@ const loadPages = require('./pages');
const loadTheme = require('./theme');
const {generate} = require('./utils');
const genRoutesConfig = require('./routes');
const constants = require('../constants');

module.exports = async function load(siteDir) {
const generatedFilesDir = path.resolve(
siteDir,
constants.GENERATED_FILES_DIR_NAME,
);
fs.ensureDirSync(generatedFilesDir);

// Site Config - @tested
const siteConfig = loadConfig.loadConfig(siteDir);
await generate(
generatedFilesDir,
loadConfig.configFileName,
`export default ${JSON.stringify(siteConfig, null, 2)};`,
);

// Env - @tested
const env = loadEnv({siteDir, siteConfig});
await generate('env.js', `export default ${JSON.stringify(env, null, 2)};`);
await generate(
generatedFilesDir,
'env.js',
`export default ${JSON.stringify(env, null, 2)};`,
);

// Docs
const docsDir = path.resolve(siteDir, '..', siteConfig.customDocsPath);
Expand All @@ -36,10 +49,12 @@ module.exports = async function load(siteDir) {
siteConfig,
});
await generate(
generatedFilesDir,
'docsMetadatas.js',
`export default ${JSON.stringify(docsMetadatas, null, 2)};`,
);
await generate(
generatedFilesDir,
'docsSidebars.js',
`export default ${JSON.stringify(docsSidebars, null, 2)};`,
);
Expand All @@ -60,6 +75,7 @@ module.exports = async function load(siteDir) {
const pagesDir = path.resolve(siteDir, 'pages');
const pagesMetadatas = await loadPages({pagesDir, env, siteConfig});
await generate(
generatedFilesDir,
'pagesMetadatas.js',
`export default ${JSON.stringify(pagesMetadatas, null, 2)};`,
);
Expand All @@ -68,6 +84,7 @@ module.exports = async function load(siteDir) {
const blogDir = path.resolve(siteDir, 'blog');
const blogMetadatas = await loadBlog({blogDir, env, siteConfig});
await generate(
generatedFilesDir,
'blogMetadatas.js',
`export default ${JSON.stringify(blogMetadatas, null, 2)};`,
);
Expand Down Expand Up @@ -99,11 +116,12 @@ module.exports = async function load(siteDir) {
sourceToMetadata,
versionedDir,
translatedDir,
generatedFilesDir,
};

// Generate React Router Config.
const routesConfig = await genRoutesConfig(props);
await generate('routes.js', routesConfig);
await generate(generatedFilesDir, 'routes.js', routesConfig);

return props;
};
7 changes: 2 additions & 5 deletions v2/lib/load/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,11 @@ const fm = require('front-matter');
const escapeStringRegexp = require('escape-string-regexp');
const fs = require('fs-extra');

const genPath = path.resolve(__dirname, '../core/__generated__');
fs.ensureDirSync(genPath);

const genCache = new Map();
async function generate(file, content) {
async function generate(generatedFilesDir, file, content) {
const cached = genCache.get(file);
if (cached !== content) {
await fs.writeFile(path.join(genPath, file), content);
await fs.writeFile(path.join(generatedFilesDir, file), content);
genCache.set(file, content);
}
}
Expand Down
3 changes: 2 additions & 1 deletion v2/lib/webpack/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ module.exports = function createBaseConfig(props, isServer) {
versionedDir,
translatedDir,
baseUrl,
generatedFilesDir,
} = props;

const config = new Config();
Expand All @@ -69,7 +70,7 @@ module.exports = function createBaseConfig(props, isServer) {
.set('@docs', docsDir)
.set('@pages', pagesDir)
.set('@build', outDir)
.set('@generated', path.resolve(__dirname, '../core/__generated__'))
.set('@generated', generatedFilesDir)
.set('@core', path.resolve(__dirname, '../core'))
.set('@docusaurus', path.resolve(__dirname, '../docusaurus'))
.end()
Expand Down
1 change: 1 addition & 0 deletions v2/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
},
"scripts": {
"docusaurus": "node bin/docusaurus",
"start": "yarn docusaurus start ../v2-website",
"prettier": "prettier --config ../.prettierrc --write \"**/*.js\"",
"lint": "eslint --cache \"{lib,bin,test}/**/*.js\"",
"test": "jest --config test/jest.config.js"
Expand Down

0 comments on commit 211e04f

Please sign in to comment.