Skip to content

Commit

Permalink
refactor(v2): minor code refactoring on component creator (#2080)
Browse files Browse the repository at this point in the history
  • Loading branch information
endiliey authored and yangshun committed Dec 3, 2019
1 parent af059e6 commit b26948d
Showing 1 changed file with 11 additions and 27 deletions.
38 changes: 11 additions & 27 deletions packages/docusaurus/src/client/exports/ComponentCreator.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import Loadable from 'react-loadable';
import Loading from '@theme/Loading';
import routesChunkNames from '@generated/routesChunkNames';
import registry from '@generated/registry';
import flat from '../flat';

function ComponentCreator(path) {
// 404 page
Expand All @@ -36,34 +37,17 @@ function ComponentCreator(path) {
- optsModules: ['./Pages.js', './doc1.md']
- optsWebpack: [require.resolveWeak('./Pages.js'), require.resolveWeak('./doc1.md')]
*/
function traverseChunk(target, keys) {
if (Array.isArray(target)) {
target.forEach((value, index) => {
traverseChunk(value, [...keys, index]);
});
return;
}

if (target == null) {
return;
}

if (typeof target === 'object') {
Object.keys(target).forEach(key => {
traverseChunk(target[key], [...keys, key]);
});
return;
const flatChunkNames = flat(chunkNames);
Object.keys(flatChunkNames).forEach(key => {
const chunkRegistry = registry[flatChunkNames[key]];
if (chunkRegistry) {
/* eslint-disable prefer-destructuring */
optsLoader[key] = chunkRegistry[0];
optsModules.push(chunkRegistry[1]);
optsWebpack.push(chunkRegistry[2]);
/* eslint-enable prefer-destructuring */
}

const chunkRegistry = registry[target] || {};
/* eslint-disable prefer-destructuring */
optsLoader[keys.join('.')] = chunkRegistry[0];
optsModules.push(chunkRegistry[1]);
optsWebpack.push(chunkRegistry[2]);
/* eslint-enable prefer-destructuring */
}

traverseChunk(chunkNames, []);
});

return Loadable.Map({
loading: Loading,
Expand Down

0 comments on commit b26948d

Please sign in to comment.