Skip to content

Commit

Permalink
Added check for a case if the StoriesEntry is object in functions:
Browse files Browse the repository at this point in the history
* `generateImportFnScriptCode()`;
* `generateIframeScriptCode()`;
* `getOptimizeDeps()`.
  • Loading branch information
mrauhu committed Jan 7, 2022
1 parent bda9c63 commit bbd0c9a
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
11 changes: 10 additions & 1 deletion packages/storybook-builder-vite/codegen-iframe-script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { promise as glob } from 'glob-promise';
import { normalizePath } from 'vite';
import { loadPreviewOrConfigFile } from '@storybook/core-common';

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 @@ -26,7 +27,15 @@ export async function generateIframeScriptCode(options: ExtendedOptions) {

const storyEntries = (
await Promise.all(
(await presets.apply('stories')).map((g) => glob(path.isAbsolute(g) ? g : path.join(configDir, g)))
(
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), []);

Expand Down
14 changes: 10 additions & 4 deletions packages/storybook-builder-vite/codegen-importfn-script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { promise as glob } from 'glob-promise';
import * as path from 'path';
import { normalizePath } from 'vite';

import type { Options } from '@storybook/core-common';
import type { Options, StoriesEntry } 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,13 +40,19 @@ async function toImportFn(stories: string[]) {
`;
}

export async function generateImportFnScriptCode(options: Options) {
export async function generateImportFnScriptCode({ configDir, presets }: Options) {
// First we need to get an array of stories and their absolute paths.
const stories = (
await Promise.all(
(
await options.presets.apply('stories', [], options)
).map((storyEntry) => glob(path.isAbsolute(storyEntry) ? storyEntry : path.join(options.configDir, storyEntry)))
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), []);

Expand Down
17 changes: 13 additions & 4 deletions packages/storybook-builder-vite/optimizeDeps.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
import * as path from 'path';

export async function getOptimizeDeps(root, options) {
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 options.presets.apply('stories', [], options)
).map((storyEntry) =>
path.relative(root, path.isAbsolute(storyEntry) ? storyEntry : path.join(options.configDir, storyEntry))
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')
);

return {
Expand Down

0 comments on commit bbd0c9a

Please sign in to comment.