lezer-parser / highlight

Syntax highlighting from Lezer trees
MIT License
31 stars 9 forks source link

Update TypeScript exports to support moduleResolution bundler #8

Closed kwangure closed 1 year ago

kwangure commented 1 year ago

moduleResolution: "bundler" is a recent setting added to TypeScript to support bundler setups. It's the setting you should probably be using when bundling code. With this setting TypeScript only searches for types in the exports field of the package.json.

It requires the following change:

 "exports": {
   "import": "./dist/index.js",
   "require": "./dist/index.cjs",
+  "types": "./dist/index.d.ts"
 },

This is issue is relevent to multiple @lezer packages I'm using.

marijnh commented 1 year ago

Can you provide an example setup that demonstrates this problem? When I set up a simple project that uses "moduleResolution": "bundler" (+ strict mode) and has a typescript file using @lezer/common, it compiles without error.

kwangure commented 1 year ago

I couldn't initially create a minimial repro with any Lezer package that caused TypeScript to throw an error, but I think I've figured it out now.

If the types and files are "siblings", e.g. index.js and index.d.ts, then TypeScript can resolve those without looking up the exports field. It seems all I needed to do was upgrade my Lezer packages which have now been updated to use "sibling" types. In older versions, for example @lezer/highlight, the module was at ./dist/index.js while the types were ./dist/highlight.d.ts such that TypeScript@^5.0 required the exports field to be defined.

The error is usually something along the lines of:

There are types at '/path/to/package/dist/file.d.ts', but this result could not be resolved when respecting package.json "exports". The 'package' library may need to update its package.json or typings.

Apologies for the spam PRs across several Lezer packages. Populating exports field is not needed for those particular packages.