From 84baab33b4df4213c077c152d9d170064ea67a54 Mon Sep 17 00:00:00 2001 From: Bartosz Kaszubowski Date: Sun, 5 Apr 2020 06:18:01 +0200 Subject: [PATCH] chore(v2): replace few Lodash methods with native counterparts (#2529) --- packages/docusaurus/src/commands/start.ts | 12 ++++++------ .../docusaurus/src/server/client-modules/index.ts | 15 ++++----------- packages/docusaurus/src/server/html-tags/index.ts | 3 +-- packages/docusaurus/src/server/index.ts | 7 ++++--- packages/docusaurus/src/server/plugins/init.ts | 9 ++++----- packages/docusaurus/src/server/presets/index.ts | 5 ++--- packages/docusaurus/src/server/routes.ts | 5 +++-- 7 files changed, 24 insertions(+), 32 deletions(-) diff --git a/packages/docusaurus/src/commands/start.ts b/packages/docusaurus/src/commands/start.ts index 96c20efa1a9c..c05be5c9bcd1 100644 --- a/packages/docusaurus/src/commands/start.ts +++ b/packages/docusaurus/src/commands/start.ts @@ -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'; @@ -64,11 +63,12 @@ export async function start( return posixPath(filepath); }; - const pluginPaths: string[] = _.compact( - _.flatten( - plugins.map(plugin => plugin.getPathsToWatch && plugin.getPathsToWatch()), - ), - ).map(normalizeToSiteDir); + const pluginPaths: string[] = ([] as string[]).concat( + ...plugins + .map(plugin => plugin.getPathsToWatch && plugin.getPathsToWatch()) + .filter(Boolean) + .map(normalizeToSiteDir), + ); const fsWatcher = chokidar.watch([...pluginPaths, CONFIG_FILE_NAME], { cwd: siteDir, ignoreInitial: true, diff --git a/packages/docusaurus/src/server/client-modules/index.ts b/packages/docusaurus/src/server/client-modules/index.ts index 2826802d5b8e..1c7dba2ca94b 100644 --- a/packages/docusaurus/src/server/client-modules/index.ts +++ b/packages/docusaurus/src/server/client-modules/index.ts @@ -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[]): string[] { - return _.compact( - _.flatten( - plugins.map(plugin => { - if (!plugin.getClientModules) { - return null; - } - - return plugin.getClientModules(); - }), - ), + return ([] as string[]).concat( + ...plugins + .map(plugin => plugin.getClientModules && plugin.getClientModules()) + .filter(Boolean), ); } diff --git a/packages/docusaurus/src/server/html-tags/index.ts b/packages/docusaurus/src/server/html-tags/index.ts index 6c4b58760b77..03c3f72ea3ec 100644 --- a/packages/docusaurus/src/server/html-tags/index.ts +++ b/packages/docusaurus/src/server/html-tags/index.ts @@ -5,7 +5,6 @@ * LICENSE file in the root directory of this source tree. */ -import _ from 'lodash'; import {htmlTagObjectToString} from './htmlTags'; import { Plugin, @@ -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[]): InjectedHtmlTags { diff --git a/packages/docusaurus/src/server/index.ts b/packages/docusaurus/src/server/index.ts index acb97cf58180..266737a3e691 100644 --- a/packages/docusaurus/src/server/index.ts +++ b/packages/docusaurus/src/server/index.ts @@ -6,7 +6,6 @@ */ import {generate} from '@docusaurus/utils'; -import _ from 'lodash'; import path from 'path'; import { BUILD_DIR_NAME, @@ -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(plugin => plugin.getThemePath && plugin.getThemePath()) + .filter(Boolean), ); const userTheme = path.resolve(siteDir, THEME_PATH); const alias = loadThemeAlias([fallbackTheme, ...pluginThemes], [userTheme]); diff --git a/packages/docusaurus/src/server/plugins/init.ts b/packages/docusaurus/src/server/plugins/init.ts index 5e49831d68be..5898d2cdebf6 100644 --- a/packages/docusaurus/src/server/plugins/init.ts +++ b/packages/docusaurus/src/server/plugins/init.ts @@ -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'; @@ -16,8 +15,8 @@ export function initPlugins({ pluginConfigs: PluginConfig[]; context: LoadContext; }): Plugin[] { - const plugins: Plugin[] = _.compact( - pluginConfigs.map(pluginItem => { + const plugins: Plugin[] = pluginConfigs + .map(pluginItem => { let pluginModuleImport; let pluginOptions = {}; @@ -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; } diff --git a/packages/docusaurus/src/server/presets/index.ts b/packages/docusaurus/src/server/presets/index.ts index 75f1e3f5f009..cd09f1fede34 100644 --- a/packages/docusaurus/src/server/presets/index.ts +++ b/packages/docusaurus/src/server/presets/index.ts @@ -6,7 +6,6 @@ */ import importFresh from 'import-fresh'; -import _ from 'lodash'; import { LoadContext, PluginConfig, @@ -47,7 +46,7 @@ export function loadPresets( }); return { - plugins: _.compact(_.flatten(unflatPlugins)), - themes: _.compact(_.flatten(unflatThemes)), + plugins: ([] as PluginConfig[]).concat(...unflatPlugins).filter(Boolean), + themes: ([] as PluginConfig[]).concat(...unflatThemes).filter(Boolean), }; } diff --git a/packages/docusaurus/src/server/routes.ts b/packages/docusaurus/src/server/routes.ts index 67e00e579789..1e3586c6d1fb 100644 --- a/packages/docusaurus/src/server/routes.ts +++ b/packages/docusaurus/src/server/routes.ts @@ -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), ); @@ -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),