Skip to content

ESBuild resolves modules by using the TypeScript moduleSuffixes per package instead of per bundle #2395

Closed
@sverrejoh

Description

The moduleSuffixes feature of TypeScript is now being used for module resolution in ESBuild, but for a bundle this should be set once per whole bundle, and not locally per package.

React Native has Platform Specific Extensions, which is used by its Metro Bundler to allow JavaScript modules to be forked per platform. So for example a Button module could have a Button.ios.jsx implementation for iOS and a Button.android.jsx for Android. The right fork (for example ios) is selected per bundle, so you choose the current platform for your bundle globally.

In TypeScript this is now supported via the moduleSuffixes configuration. You can then set the moduleSuffixes in a shared package's tsconfig.json, and TypeScript will then type-check that package using the platform extensions in the order of the moduleSuffixes list. This is though not shared outside that package. The order is only used as one arbitrary way to test a set of resolutions. If you want to type check for multiple platforms you would have to have multiple configuration files with its own moduleSuffixes list and run TypeScript once per the platform you want to test.

The problem is that ESBuild is using the per package moduleSuffixes when resolving module paths for the bundle. The correct way would be to ignore moduleSuffixes for bundling, and rather let the user set the resolution order with the per bundle ESBuild resolveExtensions setting. For example resolveExtensions: [ ".ios.tsx", ".tsx", ".ios", ".ts", ".js", ".jsx" ],.

I'm not sure I can see a use for ESBuild to support moduleSuffixes at all, but at least it should be configurable as the usual way to set the resolution is once for all the code in your bundle.

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions