-
-
Notifications
You must be signed in to change notification settings - Fork 10.1k
Maintenance: Extract shared ReactDocgenResolveError and RESOLVE_EXTENSIONS to internal/common #34356
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
Closed
mixelburg
wants to merge
1
commit into
storybookjs:next
from
mixelburg:refactor/extract-shared-docgen-resolver
Closed
Maintenance: Extract shared ReactDocgenResolveError and RESOLVE_EXTENSIONS to internal/common #34356
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| import { extname } from 'node:path'; | ||
|
|
||
| import resolve from 'resolve'; | ||
|
|
||
| export class ReactDocgenResolveError extends Error { | ||
| // the magic string that react-docgen uses to check if a module is ignored | ||
| readonly code = 'MODULE_NOT_FOUND'; | ||
|
|
||
| constructor(filename: string) { | ||
| super(`'${filename}' was ignored by react-docgen.`); | ||
| } | ||
| } | ||
|
|
||
| /* The below code was copied from: | ||
| * https://github.com/reactjs/react-docgen/blob/df2daa8b6f0af693ecc3c4dc49f2246f60552bcb/packages/react-docgen/src/importer/makeFsImporter.ts#L14-L63 | ||
| * because it wasn't exported from the react-docgen package. | ||
| */ | ||
|
|
||
| // These extensions are sorted by priority | ||
| // resolve() will check for files in the order these extensions are sorted | ||
| export const RESOLVE_EXTENSIONS = [ | ||
| '.js', | ||
| '.cts', // These were originally not in the code, I added them | ||
| '.mts', // These were originally not in the code, I added them | ||
| '.ctsx', // These were originally not in the code, I added them | ||
| '.mtsx', // These were originally not in the code, I added them | ||
| '.ts', | ||
| '.tsx', | ||
| '.mjs', | ||
| '.cjs', | ||
| '.mts', | ||
| '.cts', | ||
| '.jsx', | ||
| ]; | ||
|
|
||
| export function defaultLookupModule(filename: string, basedir: string): string { | ||
| const resolveOptions = { | ||
| basedir, | ||
| extensions: RESOLVE_EXTENSIONS, | ||
| // we do not need to check core modules as we cannot import them anyway | ||
| includeCoreModules: false, | ||
| }; | ||
|
|
||
| try { | ||
| return resolve.sync(filename, resolveOptions); | ||
| } catch (error) { | ||
| const ext = extname(filename); | ||
| let newFilename: string; | ||
|
|
||
| // if we try to import a JavaScript file it might be that we are actually pointing to | ||
| // a TypeScript file. This can happen in ES modules as TypeScript requires to import other | ||
| // TypeScript files with .js extensions | ||
| // https://www.typescriptlang.org/docs/handbook/esm-node.html#type-in-packagejson-and-new-extensions | ||
| switch (ext) { | ||
| case '.js': | ||
| case '.mjs': | ||
| case '.cjs': | ||
| newFilename = `${filename.slice(0, -2)}ts`; | ||
| break; | ||
|
|
||
| case '.jsx': | ||
| newFilename = `${filename.slice(0, -3)}tsx`; | ||
| break; | ||
| default: | ||
| throw error; | ||
| } | ||
|
|
||
| return resolve.sync(newFilename, { | ||
| ...resolveOptions, | ||
| // we already know that there is an extension at this point, so no need to check other extensions | ||
| extensions: [], | ||
| }); | ||
| } | ||
| } | ||
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 |
|---|---|---|
| @@ -1,75 +1,5 @@ | ||
| import { extname } from 'node:path'; | ||
|
|
||
| import resolve from 'resolve'; | ||
|
|
||
| export class ReactDocgenResolveError extends Error { | ||
| // the magic string that react-docgen uses to check if a module is ignored | ||
| readonly code = 'MODULE_NOT_FOUND'; | ||
|
|
||
| constructor(filename: string) { | ||
| super(`'${filename}' was ignored by react-docgen.`); | ||
| } | ||
| } | ||
|
|
||
| /* The below code was copied from: | ||
| * https://github.com/reactjs/react-docgen/blob/df2daa8b6f0af693ecc3c4dc49f2246f60552bcb/packages/react-docgen/src/importer/makeFsImporter.ts#L14-L63 | ||
| * because it wasn't exported from the react-docgen package. | ||
| * watch out: when updating this code, also update the code in code/presets/react-webpack/src/loaders/docgen-resolver.ts | ||
| */ | ||
|
|
||
| // These extensions are sorted by priority | ||
| // resolve() will check for files in the order these extensions are sorted | ||
| export const RESOLVE_EXTENSIONS = [ | ||
| '.js', | ||
| '.cts', // These were originally not in the code, I added them | ||
| '.mts', // These were originally not in the code, I added them | ||
| '.ctsx', // These were originally not in the code, I added them | ||
| '.mtsx', // These were originally not in the code, I added them | ||
| '.ts', | ||
| '.tsx', | ||
| '.mjs', | ||
| '.cjs', | ||
| '.mts', | ||
| '.cts', | ||
| '.jsx', | ||
| ]; | ||
|
|
||
| export function defaultLookupModule(filename: string, basedir: string): string { | ||
| const resolveOptions = { | ||
| basedir, | ||
| extensions: RESOLVE_EXTENSIONS, | ||
| // we do not need to check core modules as we cannot import them anyway | ||
| includeCoreModules: false, | ||
| }; | ||
|
|
||
| try { | ||
| return resolve.sync(filename, resolveOptions); | ||
| } catch (error) { | ||
| const ext = extname(filename); | ||
| let newFilename: string; | ||
|
|
||
| // if we try to import a JavaScript file it might be that we are actually pointing to | ||
| // a TypeScript file. This can happen in ES modules as TypeScript requires to import other | ||
| // TypeScript files with .js extensions | ||
| // https://www.typescriptlang.org/docs/handbook/esm-node.html#type-in-packagejson-and-new-extensions | ||
| switch (ext) { | ||
| case '.js': | ||
| case '.mjs': | ||
| case '.cjs': | ||
| newFilename = `${filename.slice(0, -2)}ts`; | ||
| break; | ||
|
|
||
| case '.jsx': | ||
| newFilename = `${filename.slice(0, -3)}tsx`; | ||
| break; | ||
| default: | ||
| throw error; | ||
| } | ||
|
|
||
| return resolve.sync(newFilename, { | ||
| ...resolveOptions, | ||
| // we already know that there is an extension at this point, so no need to check other extensions | ||
| extensions: [], | ||
| }); | ||
| } | ||
| } | ||
| export { | ||
| ReactDocgenResolveError, | ||
| RESOLVE_EXTENSIONS, | ||
| defaultLookupModule, | ||
| } from 'storybook/internal/common'; |
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 |
|---|---|---|
| @@ -1,75 +1,5 @@ | ||
| import { extname } from 'node:path'; | ||
|
|
||
| import resolve from 'resolve'; | ||
|
|
||
| export class ReactDocgenResolveError extends Error { | ||
| // the magic string that react-docgen uses to check if a module is ignored | ||
| readonly code = 'MODULE_NOT_FOUND'; | ||
|
|
||
| constructor(filename: string) { | ||
| super(`'${filename}' was ignored by react-docgen.`); | ||
| } | ||
| } | ||
|
|
||
| /* The below code was copied from: | ||
| * https://github.com/reactjs/react-docgen/blob/df2daa8b6f0af693ecc3c4dc49f2246f60552bcb/packages/react-docgen/src/importer/makeFsImporter.ts#L14-L63 | ||
| * because it wasn't exported from the react-docgen package. | ||
| * watch out: when updating this code, also update the code in code/frameworks/react-vite/src/plugins/docgen-resolver.ts | ||
| */ | ||
|
|
||
| // These extensions are sorted by priority | ||
| // resolve() will check for files in the order these extensions are sorted | ||
| export const RESOLVE_EXTENSIONS = [ | ||
| '.js', | ||
| '.cts', // These were originally not in the code, I added them | ||
| '.mts', // These were originally not in the code, I added them | ||
| '.ctsx', // These were originally not in the code, I added them | ||
| '.mtsx', // These were originally not in the code, I added them | ||
| '.ts', | ||
| '.tsx', | ||
| '.mjs', | ||
| '.cjs', | ||
| '.mts', | ||
| '.cts', | ||
| '.jsx', | ||
| ]; | ||
|
|
||
| export function defaultLookupModule(filename: string, basedir: string): string { | ||
| const resolveOptions = { | ||
| basedir, | ||
| extensions: RESOLVE_EXTENSIONS, | ||
| // we do not need to check core modules as we cannot import them anyway | ||
| includeCoreModules: false, | ||
| }; | ||
|
|
||
| try { | ||
| return resolve.sync(filename, resolveOptions); | ||
| } catch (error) { | ||
| const ext = extname(filename); | ||
| let newFilename: string; | ||
|
|
||
| // if we try to import a JavaScript file it might be that we are actually pointing to | ||
| // a TypeScript file. This can happen in ES modules as TypeScript requires to import other | ||
| // TypeScript files with .js extensions | ||
| // https://www.typescriptlang.org/docs/handbook/esm-node.html#type-in-packagejson-and-new-extensions | ||
| switch (ext) { | ||
| case '.js': | ||
| case '.mjs': | ||
| case '.cjs': | ||
| newFilename = `${filename.slice(0, -2)}ts`; | ||
| break; | ||
|
|
||
| case '.jsx': | ||
| newFilename = `${filename.slice(0, -3)}tsx`; | ||
| break; | ||
| default: | ||
| throw error; | ||
| } | ||
|
|
||
| return resolve.sync(newFilename, { | ||
| ...resolveOptions, | ||
| // we already know that there is an extension at this point, so no need to check other extensions | ||
| extensions: [], | ||
| }); | ||
| } | ||
| } | ||
| export { | ||
| ReactDocgenResolveError, | ||
| RESOLVE_EXTENSIONS, | ||
| defaultLookupModule, | ||
| } from 'storybook/internal/common'; |
11 changes: 2 additions & 9 deletions
11
code/renderers/react/src/componentManifest/reactDocgen/docgenResolver.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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Duplicate extensions in
RESOLVE_EXTENSIONS..mtsand.ctsappear twice in the array: once at lines 23-24 (newly added) and again at lines 31-32 (from the original code). This redundancy doesn't break resolution but indicates a merge oversight and adds unnecessary iteration.🔧 Proposed fix to remove duplicates
📝 Committable suggestion
🤖 Prompt for AI Agents
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mixelburg ☝️