Skip to content

Commit

Permalink
Created listStories() function and removed duplicated code. TODO: M…
Browse files Browse the repository at this point in the history
…erge with storybookjs#182
  • Loading branch information
mrauhu committed Jan 7, 2022
1 parent 5da4c62 commit 7e73d0b
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 48 deletions.
20 changes: 3 additions & 17 deletions packages/storybook-builder-vite/codegen-iframe-script.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import * as path from 'path';
import { promise as glob } from 'glob-promise';
import { normalizePath } from 'vite';
import { loadPreviewOrConfigFile } from '@storybook/core-common';
import { normalizePath } from 'vite';
import { listStories } from './list-stories';

import type { StoriesEntry } from '@storybook/core-common';
import type { ExtendedOptions } from './types';

// This is somewhat of a hack; the problem is that previewEntries resolves to
Expand All @@ -25,19 +23,7 @@ export async function generateIframeScriptCode(options: ExtendedOptions) {
const presetEntries = await presets.apply('config', [], options);
const configEntries = [...presetEntries, previewOrConfigFile].filter(Boolean);

const storyEntries = (
await Promise.all(
(
await presets.apply<Promise<StoriesEntry[]>>('stories')
).map((storiesEntry) => {
const files = typeof storiesEntry === 'string' ? storiesEntry : storiesEntry.files;
if (!files) {
return [] as string[];
}
return glob(path.isAbsolute(files) ? files : path.join(configDir, files));
})
)
).reduce((carry, stories) => carry.concat(stories), []);
const storyEntries = await listStories(options);

const absoluteFilesToImport = (files: string[], name: string) =>
files.map((el, i) => `import ${name ? `* as ${name}_${i} from ` : ''}'/@fs/${normalizePath(el)}'`).join('\n');
Expand Down
20 changes: 4 additions & 16 deletions packages/storybook-builder-vite/codegen-importfn-script.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { promise as glob } from 'glob-promise';
import * as path from 'path';
import { normalizePath } from 'vite';
import { listStories } from './list-stories';

import type { Options, StoriesEntry } from '@storybook/core-common';
import type { Options } from '@storybook/core-common';

/**
* This file is largely based on https://github.com/storybookjs/storybook/blob/d1195cbd0c61687f1720fefdb772e2f490a46584/lib/core-common/src/utils/to-importFn.ts
Expand Down Expand Up @@ -40,21 +40,9 @@ async function toImportFn(stories: string[]) {
`;
}

export async function generateImportFnScriptCode({ configDir, presets }: Options) {
export async function generateImportFnScriptCode(options: Options) {
// First we need to get an array of stories and their absolute paths.
const stories = (
await Promise.all(
(
await presets.apply<Promise<StoriesEntry[]>>('stories')
).map((storiesEntry) => {
const files = typeof storiesEntry === 'string' ? storiesEntry : storiesEntry.files;
if (!files) {
return [] as string[];
}
return glob(path.isAbsolute(files) ? files : path.join(configDir, files));
})
)
).reduce((carry, stories) => carry.concat(stories), []);
const stories = await listStories(options);

// We can then call toImportFn to create a function that can be used to load each story dynamically.
return (await toImportFn(stories)).trim();
Expand Down
21 changes: 21 additions & 0 deletions packages/storybook-builder-vite/list-stories.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import * as path from 'path';
import { promise as glob } from 'glob-promise';

import type { Options, StoriesEntry } from '@storybook/core-common';

// TODO: Merge with https://github.com/eirslett/storybook-builder-vite/pull/182
export async function listStories({ presets, configDir }: Options) {
return (
await Promise.all(
(
await presets.apply<Promise<StoriesEntry[]>>('stories')
).map((storiesEntry) => {
const files = typeof storiesEntry === 'string' ? storiesEntry : storiesEntry.files;
if (!files) {
return [] as string[];
}
return glob(path.isAbsolute(files) ? files : path.join(configDir, files));
})
)
).reduce((carry, stories) => carry.concat(stories), []);
}
20 changes: 5 additions & 15 deletions packages/storybook-builder-vite/optimizeDeps.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,12 @@
import * as path from 'path';
import { normalizePath } from 'vite';
import { listStories } from './list-stories';

import type { StoriesEntry } from '@storybook/core-common';
import type { ExtendedOptions } from './types';

export async function getOptimizeDeps(root: string, { configDir, presets }: ExtendedOptions) {
const stories = await Promise.all(
(
await presets.apply<Promise<StoriesEntry[]>>('stories')
)
.map((storiesEntry) => {
const files = typeof storiesEntry === 'string' ? storiesEntry : storiesEntry.files;
if (!files) {
return undefined;
}
return path.relative(root, path.isAbsolute(files) ? files : path.join(configDir, files));
})
.filter((path): path is string => typeof path !== 'undefined')
);
export async function getOptimizeDeps(root: string, options: ExtendedOptions) {
const absoluteStories = await listStories(options);
const stories = absoluteStories.map((storyPath) => normalizePath(path.relative(root, storyPath)));

return {
// We don't need to resolve the glob since vite supports globs for entries.
Expand Down

0 comments on commit 7e73d0b

Please sign in to comment.