-
-
Notifications
You must be signed in to change notification settings - Fork 10.1k
React: Improve import rewriting when tsconfig paths are used #33072
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
6f65dbc
React: Improve import rewriting when tsconfig paths are used
kasperpeulen 6a32c54
Update code/renderers/react/src/componentManifest/valid-package-name.ts
kasperpeulen 84e6b2d
Update code/renderers/react/src/componentManifest/valid-package-name.ts
kasperpeulen 61ea592
Initial plan
Copilot bea320c
Add test for unscoped package subpath (valid bare specifier)
Copilot bb4be98
Merge pull request #33075 from storybookjs/copilot/sub-pr-33072
kasperpeulen a8643bd
Fix regex
kasperpeulen bf50ed9
Merge remote-tracking branch 'origin/kasper/imports' into kasper/imports
kasperpeulen abdc858
Merge branch 'next' into kasper/imports
kasperpeulen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
code/renderers/react/src/componentManifest/valid-package-name.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| // inspired by https://github.com/npm/validate-npm-package-name/blob/main/lib/index.js | ||
| const scopedPackagePattern = new RegExp('^(?:@([^/]+?)[/])?([^/]+?)$'); | ||
|
|
||
|
kasperpeulen marked this conversation as resolved.
|
||
| export function stripSubpath(name: string): string { | ||
| const parts = name.split('/'); | ||
|
|
||
| if (name.startsWith('@')) { | ||
| // @scope/pkg/... | ||
| if (parts.length >= 3) { | ||
| return `${parts[0]}/${parts[1]}`; | ||
| } | ||
| return name; | ||
| } | ||
|
|
||
| // react/..., lodash/..., etc | ||
| return parts[0]; | ||
| } | ||
|
|
||
|
kasperpeulen marked this conversation as resolved.
|
||
| export function validPackageName(name: string) { | ||
| if (!name.length) { | ||
| return false; | ||
| } | ||
|
|
||
| if (name.startsWith('.')) { | ||
| return false; | ||
| } | ||
|
|
||
| if (name.match(/^_/)) { | ||
|
kasperpeulen marked this conversation as resolved.
|
||
| return false; | ||
| } | ||
|
|
||
| if (name.trim() !== name) { | ||
| return false; | ||
| } | ||
|
|
||
| if (name.length > 214) { | ||
| return false; | ||
| } | ||
|
|
||
| // mIxeD CaSe nAMEs | ||
| if (name.toLowerCase() !== name) { | ||
| return false; | ||
| } | ||
|
|
||
| if (/[~'!()*]/.test(name.split('/').slice(-1)[0])) { | ||
| return false; | ||
| } | ||
|
|
||
| if (encodeURIComponent(name) !== name) { | ||
| const nameMatch = name.match(scopedPackagePattern); | ||
| if (nameMatch) { | ||
| const org = nameMatch[1]; | ||
| const pkg = nameMatch[2]; | ||
|
|
||
| if (pkg.startsWith('.')) { | ||
| return false; | ||
| } | ||
|
|
||
| if (encodeURIComponent(org) === org && encodeURIComponent(pkg) === pkg) { | ||
| return true; | ||
|
kasperpeulen marked this conversation as resolved.
|
||
| } | ||
| } | ||
| return false; | ||
| } | ||
|
kasperpeulen marked this conversation as resolved.
|
||
|
|
||
| return true; | ||
| } | ||
|
kasperpeulen marked this conversation as resolved.
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.