Merge pull request #11531 from daniel-ac-martin/expose-globbing-failure

Core: Improve translation of globs for main.js stories
This commit is contained in:
Michael Shilman 2020-07-21 18:04:57 +08:00 committed by GitHub
commit c17294b027
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 7 deletions

View File

@ -120,6 +120,32 @@ const testCases = [
'../src/stories/components/Icon/Icon.tsx',
],
},
{
glob: '../components/*/*.stories.js',
validPaths: ['../components/icon/Icon.stories.js'],
invalidPaths: [
'../components/icon/node_modules/icon/Icon.stories.js',
'./stories.js',
'./src/stories/Icon.stories.js',
'./Icon.stories.js',
'../src/Icon.stories.mdx',
'../src/stories/components/Icon/Icon.stories.ts',
'../src/stories/components/Icon/Icon.mdx',
],
},
{
glob: '../components/*/stories/*.js',
validPaths: ['../components/icon/stories/Icon.js'],
invalidPaths: [
'../components/icon/node_modules/icon/stories/Icon.js',
'./stories.js',
'./src/stories/Icon.stories.js',
'./Icon.stories.js',
'../src/Icon.stories.mdx',
'../src/stories/components/Icon/Icon.stories.ts',
'../src/stories/components/Icon/Icon.mdx',
],
},
];
describe('toRequireContext', () => {
@ -138,10 +164,10 @@ describe('toRequireContext', () => {
return baseIncluded && matched;
}
const isMatchedForValidPaths = validPaths.filter((filePath) => !isMatched(filePath));
const isNotMatchedForValidPaths = validPaths.filter((filePath) => !isMatched(filePath));
const isMatchedForInvalidPaths = invalidPaths.filter((filePath) => !!isMatched(filePath));
expect(isMatchedForValidPaths).toEqual([]);
expect(isNotMatchedForValidPaths).toEqual([]);
expect(isMatchedForInvalidPaths).toEqual([]);
});
});

View File

@ -31,14 +31,15 @@ export const toRequireContext = (input: any) => {
case typeof input === 'string': {
const { base, glob } = globBase(fixedInput);
const recursive = glob.startsWith('**');
const indicator = glob.replace(/^(\*\*\/)*/, '');
const regex = makeRe(indicator, { fastpaths: false, noglobstar: false, bash: true });
const recursive = glob.includes('**') || glob.split('/').length > 2;
const regex = makeRe(glob, { fastpaths: false, noglobstar: false, bash: false });
const { source } = regex;
if (source.startsWith('^')) {
// prepended '^' char causes webpack require.context to fail
const match = source.substring(1);
// webpack's require.context matches against paths starting `./`
// Globs starting `**` require special treatment due to the regex they
// produce, specifically a negative look-ahead
const match = ['^\\.', glob.startsWith('**') ? '' : '\\/', source.substring(1)].join('');
return { path: base, recursive, match };
}