Repository navigation
Conversation
Bob writes a package.json with a `type` field to output folders such as lib/module and lib/typescript. That file starts a new package scope. Node.js, TypeScript and Metro resolve the `imports` field from the closest package.json, so subpath imports used in the source fail to resolve in the built files. When the root package.json already has the same `type`, the generated file isn't needed, so skip it. Also warn when the file is generated and the root package.json has an `imports` field.
This branch has not been deployed
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
With
esm: true, Bob writes{"type": "module"}tolib/module/package.jsonandlib/typescript/package.json, and{"type": "commonjs"}for CommonJS output. Node.js, TypeScript and Metro read theimportsfield from the closest package.json, so a library that uses subpath imports breaks after it's built:ERR_PACKAGE_IMPORT_NOT_DEFINED: Package import specifier "#internal/helper" is not defined in package .../lib/module/package.jsonTS2307: Cannot find module '#internal/helper' or its corresponding type declarations.The path for #internal/helper could not be resolved.Setting
"type": "module"in the root package.json doesn't help today because Bob writes the file anyway. With this change, Bob skips it when the roottypehas the same value and the output folder is inside the project, since the built files already get that module type from the root. If Bob still has to write the file and the root package.json has a non-emptyimportsfield, it prints a warning.Copying
importsinto the generated file isn't a general fix: targets outside the output folder would need../, which Node.js, TypeScript and Metro all reject.Libraries without a root
type(the create-react-native-library default) get the same output as before. For libraries that set it, other root fields such assideEffectsnow apply to the built files too, as they already do withesm: false. In a dual package setup the CommonJS build still gets its package.json, so subpath imports only work in the ESM build. The ESM docs have a new section about this.Test plan
yarn lint,yarn typecheckandyarn testpass. There are new tests for the helper (including an emptyimportsfield and an output folder outside the project), plus 2 tests intypescript.test.tsthat fail without this change.mainand from this branch: ESM-only, dual, CommonJS-only,esm: falseand legacy targets, with the roottypeunset,moduleorcommonjs, with and withoutimports. The only output difference is the missing package.json where the roottypematches.importsbehave the same as onmain. With"type": "module"andimports, every check passes, including a.cjstarget outsidelib.