Skip to content

Commit

Permalink
chore(v2): replace few Lodash methods with native counterparts (#2529)
Browse files Browse the repository at this point in the history
  • Loading branch information
Simek authored Apr 5, 2020
1 parent 3dd83be commit 84baab3
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 32 deletions.
12 changes: 6 additions & 6 deletions packages/docusaurus/src/commands/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import chalk = require('chalk');
import chokidar from 'chokidar';
import express from 'express';
import HtmlWebpackPlugin from 'html-webpack-plugin';
import _ from 'lodash';
import path from 'path';
import portfinder from 'portfinder';
import openBrowser from 'react-dev-utils/openBrowser';
Expand Down Expand Up @@ -64,11 +63,12 @@ export async function start(
return posixPath(filepath);
};

const pluginPaths: string[] = _.compact(
_.flatten<string | undefined>(
plugins.map(plugin => plugin.getPathsToWatch && plugin.getPathsToWatch()),
),
).map(normalizeToSiteDir);
const pluginPaths: string[] = ([] as string[]).concat(
...plugins
.map<any>(plugin => plugin.getPathsToWatch && plugin.getPathsToWatch())
.filter(Boolean)
.map(normalizeToSiteDir),
);
const fsWatcher = chokidar.watch([...pluginPaths, CONFIG_FILE_NAME], {
cwd: siteDir,
ignoreInitial: true,
Expand Down
15 changes: 4 additions & 11 deletions packages/docusaurus/src/server/client-modules/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,12 @@
* LICENSE file in the root directory of this source tree.
*/

import _ from 'lodash';
import {Plugin} from '@docusaurus/types';

export function loadClientModules(plugins: Plugin<any>[]): string[] {
return _.compact(
_.flatten<string | null>(
plugins.map(plugin => {
if (!plugin.getClientModules) {
return null;
}

return plugin.getClientModules();
}),
),
return ([] as string[]).concat(
...plugins
.map<any>(plugin => plugin.getClientModules && plugin.getClientModules())
.filter(Boolean),
);
}
3 changes: 1 addition & 2 deletions packages/docusaurus/src/server/html-tags/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
* LICENSE file in the root directory of this source tree.
*/

import _ from 'lodash';
import {htmlTagObjectToString} from './htmlTags';
import {
Plugin,
Expand All @@ -19,7 +18,7 @@ function toString(val: string | HtmlTagObject): string {
}

export function createHtmlTagsString(tags: HtmlTags): string {
return _.isArray(tags) ? tags.map(toString).join('\n') : toString(tags);
return Array.isArray(tags) ? tags.map(toString).join('\n') : toString(tags);
}

export function loadHtmlTags(plugins: Plugin<any>[]): InjectedHtmlTags {
Expand Down
7 changes: 4 additions & 3 deletions packages/docusaurus/src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
*/

import {generate} from '@docusaurus/utils';
import _ from 'lodash';
import path from 'path';
import {
BUILD_DIR_NAME,
Expand Down Expand Up @@ -85,8 +84,10 @@ export async function load(

// Themes.
const fallbackTheme = path.resolve(__dirname, '../client/theme-fallback');
const pluginThemes = _.compact(
plugins.map(plugin => plugin.getThemePath && plugin.getThemePath()),
const pluginThemes = ([] as string[]).concat(
...plugins
.map<any>(plugin => plugin.getThemePath && plugin.getThemePath())
.filter(Boolean),
);
const userTheme = path.resolve(siteDir, THEME_PATH);
const alias = loadThemeAlias([fallbackTheme, ...pluginThemes], [userTheme]);
Expand Down
9 changes: 4 additions & 5 deletions packages/docusaurus/src/server/plugins/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
* LICENSE file in the root directory of this source tree.
*/

import _ from 'lodash';
import importFresh from 'import-fresh';
import {LoadContext, Plugin, PluginConfig} from '@docusaurus/types';

Expand All @@ -16,8 +15,8 @@ export function initPlugins({
pluginConfigs: PluginConfig[];
context: LoadContext;
}): Plugin<any>[] {
const plugins: Plugin<any>[] = _.compact(
pluginConfigs.map(pluginItem => {
const plugins: Plugin<any>[] = pluginConfigs
.map(pluginItem => {
let pluginModuleImport;
let pluginOptions = {};

Expand All @@ -40,8 +39,8 @@ export function initPlugins({
// module identifier - npm package or locally-resolved path.
const pluginModule: any = importFresh(pluginModuleImport);
return (pluginModule.default || pluginModule)(context, pluginOptions);
}),
);
})
.filter(Boolean);

return plugins;
}
5 changes: 2 additions & 3 deletions packages/docusaurus/src/server/presets/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
*/

import importFresh from 'import-fresh';
import _ from 'lodash';
import {
LoadContext,
PluginConfig,
Expand Down Expand Up @@ -47,7 +46,7 @@ export function loadPresets(
});

return {
plugins: _.compact(_.flatten<PluginConfig>(unflatPlugins)),
themes: _.compact(_.flatten<PluginConfig>(unflatThemes)),
plugins: ([] as PluginConfig[]).concat(...unflatPlugins).filter(Boolean),
themes: ([] as PluginConfig[]).concat(...unflatThemes).filter(Boolean),
};
}
5 changes: 3 additions & 2 deletions packages/docusaurus/src/server/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export async function loadRoutes(
return null;
}

if (_.isArray(value)) {
if (Array.isArray(value)) {
return value.map((val, index) =>
genRouteChunkNames(val, `${index}`, name),
);
Expand Down Expand Up @@ -113,7 +113,8 @@ export async function loadRoutes(
return newValue;
}

routesChunkNames[routePath] = _.assign(
routesChunkNames[routePath] = Object.assign(
{},
routesChunkNames[routePath],
genRouteChunkNames({component}, 'component', component),
genRouteChunkNames(modules, 'module', routePath),
Expand Down

0 comments on commit 84baab3

Please sign in to comment.