Skip to content

Commit

Permalink
Merge pull request #22873 from storybookjs/shilman/21414-exclude-node…
Browse files Browse the repository at this point in the history
…-modules

Core: Fix indexing errors by excluding node_modules stories
  • Loading branch information
shilman authored Jun 6, 2023
2 parents 87c4731 + 2a02bf5 commit 821e93a
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 6 deletions.
23 changes: 23 additions & 0 deletions MIGRATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
- [Deploying build artifacts](#deploying-build-artifacts)
- [Dropped support for file URLs](#dropped-support-for-file-urls)
- [Serving with nginx](#serving-with-nginx)
- [Ignore story files from node\_modules](#ignore-story-files-from-node_modules)
- [7.0 Core changes](#70-core-changes)
- [7.0 feature flags removed](#70-feature-flags-removed)
- [Story context is prepared before for supporting fine grained updates](#story-context-is-prepared-before-for-supporting-fine-grained-updates)
Expand Down Expand Up @@ -833,6 +834,28 @@ With [nginx](https://www.nginx.com/), you need to extend [the MIME type handling

It would otherwise default to serving the `.mjs` files as `application/octet-stream`.

##### Ignore story files from node_modules

In 6.x Storybook literally followed the glob patterns specified in your `.storybook/main.js` `stories` field. Storybook 7.0 ignores files from `node_modules` unless your glob pattern includes the string `"node_modules"`.

Given the following `main.js`:

```js
export default {
stories: ['../**/*.stories.*']
}
```

If you want to restore the previous behavior to include `node_modules`, you can update it to:

```js
export default {
stories: ['../**/*.stories.*', '../**/node_modules/**/*.stories.*']
}
```

The first glob would have node_modules automatically excluded by Storybook, and the second glob would include all stories that are under a nested `node_modules` directory.

### 7.0 Core changes

#### 7.0 feature flags removed
Expand Down
7 changes: 5 additions & 2 deletions code/builders/builder-vite/src/list-stories.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as path from 'path';
import slash from 'slash';
import { glob } from 'glob';
import { normalizeStories } from '@storybook/core-common';
import { normalizeStories, commonGlobOptions } from '@storybook/core-common';

import type { Options } from '@storybook/types';
import { normalizePath } from 'vite';
Expand All @@ -18,7 +18,10 @@ export async function listStories(options: Options) {
? pattern
: path.join(options.configDir, pattern);

return glob(slash(absolutePattern), { follow: true });
return glob(slash(absolutePattern), {
...commonGlobOptions(absolutePattern),
follow: true,
});
})
)
).reduce((carry, stories) => carry.concat(stories.map(normalizePath)), []);
Expand Down
3 changes: 2 additions & 1 deletion code/lib/cli/src/automigrate/fixes/mdx-gfm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import semver from 'semver';
import { join } from 'path';
import slash from 'slash';
import glob from 'globby';
import { commonGlobOptions } from '@storybook/core-common';
import { getStorybookData, updateMainConfig } from '../helpers/mainConfigFile';
import type { Fix } from '../types';
import { getStorybookVersionSpecifier } from '../../helpers';
Expand Down Expand Up @@ -37,7 +38,7 @@ export const mdxgfm: Fix<Options> = {
? slash(join(configDir, item))
: slash(join(configDir, item.directory, item.files));

const files = await glob(pattern);
const files = await glob(pattern, commonGlobOptions(pattern));

return files.some((f) => f.endsWith('.mdx'));
}, Promise.resolve(false));
Expand Down
1 change: 1 addition & 0 deletions code/lib/core-common/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export * from './utils/cache';
export * from './utils/check-addon-order';
export * from './utils/envs';
export * from './utils/findDistEsm';
export * from './utils/common-glob-options';
export * from './utils/get-builder-options';
export * from './utils/get-framework-name';
export * from './utils/get-renderer-name';
Expand Down
5 changes: 5 additions & 0 deletions code/lib/core-common/src/utils/common-glob-options.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const NODE_MODULES_RE = /node_modules/;

// Exclude node_modules stories everywhere we call `glob`
export const commonGlobOptions = (glob: string) =>
NODE_MODULES_RE.test(glob) ? {} : { ignore: ['**/node_modules/**'] };
4 changes: 2 additions & 2 deletions code/lib/core-server/src/utils/StoryIndexGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import type {
StoryName,
} from '@storybook/types';
import { userOrAutoTitleFromSpecifier, sortStoriesV7 } from '@storybook/preview-api';
import { normalizeStoryPath } from '@storybook/core-common';
import { commonGlobOptions, normalizeStoryPath } from '@storybook/core-common';
import { logger, once } from '@storybook/node-logger';
import { getStorySortParameter } from '@storybook/csf-tools';
import { toId } from '@storybook/csf';
Expand Down Expand Up @@ -122,7 +122,7 @@ export class StoryIndexGenerator {
const fullGlob = slash(
path.join(this.options.workingDir, specifier.directory, specifier.files)
);
const files = await glob(fullGlob);
const files = await glob(fullGlob, commonGlobOptions(fullGlob));

if (files.length === 0) {
once.warn(
Expand Down
3 changes: 2 additions & 1 deletion code/lib/core-server/src/utils/watch-story-specifiers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import glob from 'globby';
import uniq from 'lodash/uniq.js';

import type { NormalizedStoriesSpecifier, Path } from '@storybook/types';
import { commonGlobOptions } from '@storybook/core-common';

const isDirectory = (directory: Path) => {
try {
Expand Down Expand Up @@ -74,7 +75,7 @@ export function watchStorySpecifiers(
path.basename(specifier.files)
);
// glob only supports forward slashes
const files = await glob(slash(dirGlob));
const files = await glob(slash(dirGlob), commonGlobOptions(dirGlob));

files.forEach((filePath) => {
const fileImportPath = toImportPath(
Expand Down

0 comments on commit 821e93a

Please sign in to comment.