Skip to content

Commit

Permalink
Merge pull request #25214 from storybookjs/valentin/fix-monorepo-stor…
Browse files Browse the repository at this point in the history
…ies-lookup

Core: Prevent stories lookup in node_modules
  • Loading branch information
valentinpalkovic authored Jan 11, 2024
2 parents 1086e75 + 569175b commit 91f1bb8
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ export default async (
rules: [
{
test: /\.stories\.([tj])sx?$|(stories|story)\.mdx$/,
exclude: /node_modules/,
enforce: 'post',
use: [
{
Expand Down
18 changes: 17 additions & 1 deletion code/lib/core-webpack/src/to-importFn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,20 @@ import { globToRegexp } from '@storybook/core-common';

import { importPipeline } from './importPipeline';

function adjustRegexToExcludeNodeModules(originalRegex: RegExp) {
const originalRegexString = originalRegex.source;
const startsWithCaret = originalRegexString.startsWith('^');
const excludeNodeModulesPattern = startsWithCaret ? '(?!.*node_modules)' : '^(?!.*node_modules)';

// Combine the new exclusion pattern with the original regex
const adjustedRegexString = startsWithCaret
? `^${excludeNodeModulesPattern}${originalRegexString.substring(1)}`
: excludeNodeModulesPattern + originalRegexString;

// Create and return the new regex
return new RegExp(adjustedRegexString);
}

export function webpackIncludeRegexp(specifier: NormalizedStoriesSpecifier) {
const { directory, files } = specifier;

Expand All @@ -17,7 +31,9 @@ export function webpackIncludeRegexp(specifier: NormalizedStoriesSpecifier) {
const webpackIncludeGlob = ['.', '..'].includes(directory)
? files
: `${directoryWithoutLeadingDots}/${files}`;
const webpackIncludeRegexpWithCaret = globToRegexp(webpackIncludeGlob);
const webpackIncludeRegexpWithCaret = webpackIncludeGlob.includes('node_modules')
? globToRegexp(webpackIncludeGlob)
: adjustRegexToExcludeNodeModules(globToRegexp(webpackIncludeGlob));
// picomatch is creating an exact match, but we are only matching the end of the filename
return new RegExp(webpackIncludeRegexpWithCaret.source.replace(/^\^/, ''));
}
Expand Down
3 changes: 2 additions & 1 deletion code/lib/csf-plugin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import type { EnrichCsfOptions } from '@storybook/csf-tools';

export type CsfPluginOptions = EnrichCsfOptions;

const STORIES_REGEX = /\.(story|stories)\.[tj]sx?$/;
// Ignore node_modules
const STORIES_REGEX = /(?<!node_modules.*)\.(story|stories)\.[tj]sx?$/;

const logger = console;

Expand Down

0 comments on commit 91f1bb8

Please sign in to comment.