From 6e122495d8389363cdd8d9cd3e9a0f2b4c75a92c Mon Sep 17 00:00:00 2001 From: Yadong Zhang Date: Mon, 13 Feb 2023 07:02:37 +0000 Subject: [PATCH] fix: clean code. --- code/lib/core-server/src/build-static.ts | 15 ++++----------- code/lib/core-server/src/presets/common-preset.ts | 10 ++-------- code/lib/core-server/src/utils/constants.ts | 9 +++++++++ code/lib/core-server/src/utils/server-statics.ts | 10 ++-------- 4 files changed, 17 insertions(+), 27 deletions(-) diff --git a/code/lib/core-server/src/build-static.ts b/code/lib/core-server/src/build-static.ts index b417a0b99b0..f8f651a05c2 100644 --- a/code/lib/core-server/src/build-static.ts +++ b/code/lib/core-server/src/build-static.ts @@ -23,8 +23,6 @@ import { resolveAddonName, } from '@storybook/core-common'; -import isEqual from 'lodash/isEqual.js'; - import { outputStats } from './utils/output-stats'; import { copyAllStaticFiles, @@ -35,7 +33,6 @@ import { extractStoriesJson, convertToIndexV3 } from './utils/stories-json'; import { extractStorybookMetadata } from './utils/metadata'; import { StoryIndexGenerator } from './utils/StoryIndexGenerator'; import { summarizeIndex } from './utils/summarizeIndex'; -import { defaultStaticDirs } from './presets/common-preset'; export type BuildStaticStandaloneOptions = CLIOptions & LoadOptions & @@ -117,7 +114,7 @@ export async function buildStaticStandalone(options: BuildStaticStandaloneOption features, }; - if (staticDirs && options.staticDir && !isEqual(staticDirs, defaultStaticDirs)) { + if (staticDirs && options.staticDir) { throw new Error(dedent` Conflict when trying to read staticDirs: * Storybook's configuration option: 'staticDirs' @@ -133,17 +130,13 @@ export async function buildStaticStandalone(options: BuildStaticStandaloneOption await managerBuilder.build({ startTime: process.hrtime(), options: fullOptions }); - if (staticDirs && !isEqual(staticDirs, defaultStaticDirs)) { + if (staticDirs) { effects.push( copyAllStaticFilesRelativeToMain(staticDirs, options.outputDir, options.configDir) ); - } else if (options.staticDir) { + } + if (options.staticDir) { effects.push(copyAllStaticFiles(options.staticDir, options.outputDir)); - } else { - throw new Error(dedent` - Error when trying to read staticDirs, recommend to use - * Storybook's configuration option: 'staticDirs' in the main configuration file. - `); } const coreServerPublicDir = join( diff --git a/code/lib/core-server/src/presets/common-preset.ts b/code/lib/core-server/src/presets/common-preset.ts index f80152f0cf8..5b7e787e863 100644 --- a/code/lib/core-server/src/presets/common-preset.ts +++ b/code/lib/core-server/src/presets/common-preset.ts @@ -16,19 +16,13 @@ import type { PresetPropertyFn, } from '@storybook/types'; import { loadCsf } from '@storybook/csf-tools'; -import { dirname, join } from 'path'; +import { join } from 'path'; import { dedent } from 'ts-dedent'; import { parseStaticDir } from '../utils/server-statics'; +import { defaultStaticDirs } from '../utils/constants'; const defaultFavicon = require.resolve('@storybook/core-server/public/favicon.svg'); -export const defaultStaticDirs = [ - { - from: join(dirname(require.resolve('@storybook/manager/package.json')), 'static'), - to: '/sb-common-assets', - }, -]; - export const staticDirs: PresetPropertyFn<'staticDirs'> = async (values = []) => [ ...defaultStaticDirs, ...values, diff --git a/code/lib/core-server/src/utils/constants.ts b/code/lib/core-server/src/utils/constants.ts index f8644b21ef1..4309b6b900e 100644 --- a/code/lib/core-server/src/utils/constants.ts +++ b/code/lib/core-server/src/utils/constants.ts @@ -1 +1,10 @@ +import { dirname, join } from 'path'; + export const DEBOUNCE = 100; + +export const defaultStaticDirs = [ + { + from: join(dirname(require.resolve('@storybook/manager/package.json')), 'static'), + to: '/sb-common-assets', + }, +]; diff --git a/code/lib/core-server/src/utils/server-statics.ts b/code/lib/core-server/src/utils/server-statics.ts index 5af31c21a1a..552956906f6 100644 --- a/code/lib/core-server/src/utils/server-statics.ts +++ b/code/lib/core-server/src/utils/server-statics.ts @@ -4,18 +4,12 @@ import { getDirectoryFromWorkingDir } from '@storybook/core-common'; import chalk from 'chalk'; import express from 'express'; import { pathExists } from 'fs-extra'; -import path, { dirname, join } from 'path'; +import path from 'path'; import favicon from 'serve-favicon'; import isEqual from 'lodash/isEqual.js'; import { dedent } from 'ts-dedent'; - -const defaultStaticDirs = [ - { - from: join(dirname(require.resolve('@storybook/manager/package.json')), 'static'), - to: '/sb-common-assets', - }, -]; +import { defaultStaticDirs } from './constants'; export async function useStatics(router: any, options: Options) { const staticDirs = await options.presets.apply('staticDirs');