Skip to content

feat: Add codemod to migrate to subpaths#9839

Merged
devongovett merged 1 commit intomainfrom
subpath-codemod
Mar 25, 2026
Merged

feat: Add codemod to migrate to subpaths#9839
devongovett merged 1 commit intomainfrom
subpath-codemod

Conversation

@devongovett
Copy link
Member

Adds a new codemod to migrate from monopackage imports to subpaths.

- import {Button} from 'react-aria-components';
+ import {Button} from 'react-aria-components/Button';

Tries to group related imports together. For example, even though Item is available via many different subpaths, it will be imported from the one that is being used elsewhere in the file.

If you want to migrate from individual packages (e.g. @react-aria/button) to subpaths, run the use-monopackages codemod first before running use-subpaths.

);
let indexPath: string;
try {
let pkgPath = path.dirname(Module.findPackageJSON(packages[pkg].monopackage, url.pathToFileURL(file.path || `${process.cwd()}/index`))!);
Copy link
Member Author

Choose a reason for hiding this comment

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

This makes the codemod work with pnpm, which doesn't put things directly in the root node_modules folder. Instead, we resolve it from the file we're transforming.

parser: 'tsx',
ignorePattern: '**/node_modules/**',
path: '.',
extensions: 'js,jsx,mjs,cjs,ts,tsx',
Copy link
Member Author

Choose a reason for hiding this comment

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

Noticed that running the codemod seemed to produce a lot of errors even though it worked. This was due to it trying to parse every file, e.g. mdx, source maps, etc. I think this is the list of extensions we actually care about.

@rspbot
Copy link

rspbot commented Mar 25, 2026

Copy link
Member

@reidbarber reidbarber left a comment

Choose a reason for hiding this comment

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

should we have a test for dynamic imports?

@devongovett
Copy link
Member Author

ah that's kinda harder to handle because you don't know which exports they used from the dynamic import, and rewriting a single dynamic import into multiple changes the runtime behavior. probably won't handle that for now.

Copy link
Member

@snowystinger snowystinger left a comment

Choose a reason for hiding this comment

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

Should we have something on our getting started docs page which says when you should use the subpaths vs the monorepo?

@devongovett devongovett added this pull request to the merge queue Mar 25, 2026
@devongovett
Copy link
Member Author

yes we need to decide what our guidance is. for now the codemod is an "if you want to" thing.

Merged via the queue into main with commit c8718f9 Mar 25, 2026
29 checks passed
@devongovett devongovett deleted the subpath-codemod branch March 25, 2026 21:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants