build: add types export conditions #108
Merged
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.
To be fair, I'm not entirely sure what the best practice here is😅
Under the status quo, when you try to compile a file (which will be compiled to a CJS module) containing:
And with tsconfig.json:
TS errors that it can't find a declaration file for
webpackbar
, because the respectiveexports
entry does not have atypes
field, and TS won't fall back to the roottypes
field (for some reason).If you simply add a
types
field to each exports field, TS now errors that "Module 'webpackbar' cannot be imported using this construct. The specifier only resolves to an ES module, which cannot be imported synchronously. Use dynamic import instead." This is because the root package.json has a"type": "module"
field, and therequire
override doesn't seem to be respected (microsoft/TypeScript#49299).I figured out in the end that the best way may be to simply remove the
"type": "module"
field so that the declaration can at least be interpreted as a CJS declaration, which will work in both CJS and ESM. Since the two dist files both have explicit extensions, this shouldn't cause any issues.There's some reference here: https://devblogs.microsoft.com/typescript/announcing-typescript-4-7/#package-json-exports-imports-and-self-referencing But I failed to find a comprehensive guide in any docs.