Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove glob-base dependency #15399

Merged
merged 4 commits into from Jul 23, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 0 additions & 2 deletions lib/core-common/package.json
Expand Up @@ -63,7 +63,6 @@
"@babel/register": "^7.12.1",
"@storybook/node-logger": "6.4.0-alpha.18",
"@storybook/semver": "^7.3.2",
"@types/glob-base": "^0.3.0",
"@types/micromatch": "^4.0.1",
"@types/node": "^14.0.10",
"@types/pretty-hrtime": "^1.0.0",
Expand All @@ -77,7 +76,6 @@
"find-up": "^5.0.0",
"fork-ts-checker-webpack-plugin": "^6.0.4",
"glob": "^7.1.6",
"glob-base": "^0.3.0",
"interpret": "^2.2.0",
"json5": "^2.1.3",
"lazy-universal-dotenv": "^3.0.1",
Expand Down
22 changes: 17 additions & 5 deletions lib/core-common/src/utils/to-require-context.ts
@@ -1,7 +1,7 @@
import globBase from 'glob-base';
import { makeRe } from 'micromatch';
import { makeRe, scan } from 'micromatch';
import deprecate from 'util-deprecate';
import dedent from 'ts-dedent';
import path from 'path';

// LEGACY support for bad glob patterns we had in SB 5 - remove in SB7
const fixBadGlob = deprecate(
Expand All @@ -25,21 +25,33 @@ const detectBadGlob = (val: string) => {
const isObject = (val: Record<string, any>) =>
val != null && typeof val === 'object' && Array.isArray(val) === false;

const dirname = (pattern: string) => {
if (pattern.slice(-1) === '/') return pattern;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what if the user passes a directory but doesn't include a trailing slash? any chance you can provide a test case for this and for any other cases you considered in this logic?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was behaviour was something that I saw in the glob-base dependency that was behaving a bit strange. I have now checked with the implementation in storybook on how it currently behaves and I believe that this extra logic did not make any difference. I have removed this now.

I think the current test are already very good but I added two more that focus on the folder pattern.

return path.dirname(pattern);
};

export const toRequireContext = (input: any) => {
const fixedInput = detectBadGlob(input);
switch (true) {
case typeof input === 'string': {
const { base, glob } = globBase(fixedInput);
const globResult = scan(fixedInput);
const base = globResult.isGlob ? globResult.prefix + globResult.base : dirname(fixedInput);
const globFallback = base !== '.' ? fixedInput.substr(base.length) : fixedInput;
const glob = globResult.isGlob ? globResult.glob : globFallback;

const recursive = glob.includes('**') || glob.split('/').length > 1;
const regex = makeRe(glob, { fastpaths: false, noglobstar: false, bash: false });
const regex = makeRe(glob, {
fastpaths: false,
noglobstar: false,
bash: false,
});
const { source } = regex;

if (source.startsWith('^')) {
// 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('');
const recursive = glob.includes('**') || glob.split('/').length > 1;

return { path: base, recursive, match };
}
Expand Down
44 changes: 0 additions & 44 deletions yarn.lock
Expand Up @@ -6652,7 +6652,6 @@ __metadata:
"@storybook/node-logger": 6.4.0-alpha.18
"@storybook/react-docgen-typescript-plugin": 1.0.2-canary.253f8c1.0
"@storybook/semver": ^7.3.2
"@types/glob-base": ^0.3.0
"@types/interpret": ^1.1.1
"@types/micromatch": ^4.0.1
"@types/mock-fs": ^4.13.0
Expand All @@ -6668,7 +6667,6 @@ __metadata:
find-up: ^5.0.0
fork-ts-checker-webpack-plugin: ^6.0.4
glob: ^7.1.6
glob-base: ^0.3.0
interpret: ^2.2.0
json5: ^2.1.3
lazy-universal-dotenv: ^3.0.1
Expand Down Expand Up @@ -8480,13 +8478,6 @@ __metadata:
languageName: node
linkType: hard

"@types/glob-base@npm:^0.3.0":
version: 0.3.0
resolution: "@types/glob-base@npm:0.3.0"
checksum: 2c0cb3b7bb7c8661b9421194c0fd90a36e1c786a4124375749df9dc1dd8ade536c8eb2ac93b217db24ed3a427755def9a54bc86c2b6bf64a81fb82e7e6f44cc7
languageName: node
linkType: hard

"@types/glob@npm:*, @types/glob@npm:^7.1.1, @types/glob@npm:^7.1.3":
version: 7.1.3
resolution: "@types/glob@npm:7.1.3"
Expand Down Expand Up @@ -22554,25 +22545,6 @@ fsevents@^1.2.7:
languageName: node
linkType: hard

"glob-base@npm:^0.3.0":
version: 0.3.0
resolution: "glob-base@npm:0.3.0"
dependencies:
glob-parent: ^2.0.0
is-glob: ^2.0.0
checksum: 4ce785c1dac2ff1e4660c010fa43ed2f1b38993dfd004023a3e7080b20bc61f29fbfe5d265b7e64cc84096ecf44e8ca876c7c1aad8f1f995d4c0f33034f3ae8c
languageName: node
linkType: hard

"glob-parent@npm:^2.0.0":
version: 2.0.0
resolution: "glob-parent@npm:2.0.0"
dependencies:
is-glob: ^2.0.0
checksum: b9d59dc532d47aaaa4841046ff631b325a707f738445300b83b7a1ee603dd060c041a378e8a195c887d479bb703685cee4725c8f54b8dacef65355375f57d32a
languageName: node
linkType: hard

"glob-parent@npm:^3.1.0":
version: 3.1.0
resolution: "glob-parent@npm:3.1.0"
Expand Down Expand Up @@ -24975,13 +24947,6 @@ fsevents@^1.2.7:
languageName: node
linkType: hard

"is-extglob@npm:^1.0.0":
version: 1.0.0
resolution: "is-extglob@npm:1.0.0"
checksum: 1ce5366d19958f36069a45ca996c1e51ab607f42a01eb0505f0ccffe8f9c91f5bcba6e971605efd8b4d4dfd0111afa3c8df3e1746db5b85b9a8f933f5e7286b7
languageName: node
linkType: hard

"is-extglob@npm:^2.1.0, is-extglob@npm:^2.1.1":
version: 2.1.1
resolution: "is-extglob@npm:2.1.1"
Expand Down Expand Up @@ -25040,15 +25005,6 @@ fsevents@^1.2.7:
languageName: node
linkType: hard

"is-glob@npm:^2.0.0":
version: 2.0.1
resolution: "is-glob@npm:2.0.1"
dependencies:
is-extglob: ^1.0.0
checksum: ef156806af0924983325c9218a8b8a838fa50e1a104ed2a11fe94829a5b27c1b05a4c8cf98d96cb3a7fea539c21f14ae2081e1a248f3d5a9eea62f2d4e9f8b0c
languageName: node
linkType: hard

"is-glob@npm:^3.0.0, is-glob@npm:^3.1.0":
version: 3.1.0
resolution: "is-glob@npm:3.1.0"
Expand Down