refactor: remove a lot of implicit anys (#7468)

This commit is contained in:
Joshua Chen 2022-05-23 15:40:53 +08:00 committed by GitHub
parent 0c8e57de67
commit 3666a2ede5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
23 changed files with 148 additions and 163 deletions

View file

@ -12,6 +12,7 @@ import registry from '@generated/registry';
import Loading from '@theme/Loading';
import flat from '../flat';
import {RouteContextProvider} from '../routeContext';
import type {RouteContext} from '@docusaurus/types';
declare global {
interface NodeRequire {
@ -71,14 +72,21 @@ export default function ComponentCreator(
loader,
modules,
webpack: () => optsWebpack,
render(loaded, props) {
render(
loaded: {[keyPath: string]: {[exportedName: string]: unknown}},
props,
) {
// `loaded` will be a map from key path (as returned from the flattened
// chunk names) to the modules loaded from the loaders. We now have to
// restore the chunk names' previous shape from this flat record.
// We do so by taking advantage of the existing `chunkNames` and replacing
// each chunk name with its loaded module, so we don't create another
// object from scratch.
const loadedModules = JSON.parse(JSON.stringify(chunkNames));
const loadedModules = JSON.parse(JSON.stringify(chunkNames)) as {
__comp?: React.ComponentType<object>;
__context?: RouteContext;
[propName: string]: unknown;
};
Object.entries(loaded).forEach(([keyPath, loadedModule]) => {
// JSON modules are also loaded as `{ default: ... }` (`import()`
// semantics) but we just want to pass the actual value to props.
@ -100,7 +108,8 @@ export default function ComponentCreator(
Object.keys(loadedModule)
.filter((k) => k !== 'default')
.forEach((nonDefaultKey) => {
chunk[nonDefaultKey] = loadedModule[nonDefaultKey];
(chunk as {[key: string]: unknown})[nonDefaultKey] =
loadedModule[nonDefaultKey];
});
}
// We now have this chunk prepared. Go down the key path and replace the
@ -108,15 +117,15 @@ export default function ComponentCreator(
let val = loadedModules;
const keyPaths = keyPath.split('.');
keyPaths.slice(0, -1).forEach((k) => {
val = val[k];
val = val[k] as {[propName: string]: unknown};
});
val[keyPaths[keyPaths.length - 1]!] = chunk;
});
/* eslint-disable no-underscore-dangle */
const Component = loadedModules.__comp;
const Component = loadedModules.__comp!;
delete loadedModules.__comp;
const routeContext = loadedModules.__context;
const routeContext = loadedModules.__context!;
delete loadedModules.__context;
/* eslint-enable no-underscore-dangle */