Title: Resolving multiple package.json "main" fields · Issue #21423 · microsoft/TypeScript · GitHub
Open Graph Title: Resolving multiple package.json "main" fields · Issue #21423 · microsoft/TypeScript
X Title: Resolving multiple package.json "main" fields · Issue #21423 · microsoft/TypeScript
Description: TL;DR: A new compiler option mainFields for selecting multiple fields in package.json instead of just package.json#main. There are lots of related issues to this one (which I link to below), but I want to focus on just this specific prop...
Open Graph Description: TL;DR: A new compiler option mainFields for selecting multiple fields in package.json instead of just package.json#main. There are lots of related issues to this one (which I link to below), but I ...
X Description: TL;DR: A new compiler option mainFields for selecting multiple fields in package.json instead of just package.json#main. There are lots of related issues to this one (which I link to below), but I ...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/21423
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Resolving multiple package.json \"main\" fields","articleBody":"**TL;DR:** A new compiler option `mainFields` for selecting multiple fields in `package.json` instead of just `package.json#main`.\r\n\r\n---\r\n\r\nThere are lots of related issues to this one (which I link to below), but I want to focus on just this specific proposal.\r\n\r\n---\r\n\r\nPackages often look like this:\r\n\r\n```js\r\n{\r\n \"name\": \"my-package\",\r\n \"main\": \"dist/cjs/index.js\",\r\n \"module\": \"dist/esm/index.js\"\r\n \"source\": \"src/index.ts\"\r\n}\r\n```\r\n\r\nNotice how we have multiple fields which specify multiple entry points. These entry points all refer to the same code, just in different compile states and configurations.\r\n\r\nMany tools use these fields in order to find the entry point that they care about. For example, tools like Webpack and Rollup will use `package.json#module` in order to find ES modules. Other tools will use fields like `package.json#source` (or `src`) for local package development.\r\n\r\nWhile these fields aren't part of the official Node module resolution algorithm. They are a community convention which has proven to be useful in lots of scenarios.\r\n\r\nFor TypeScript, one such scenario that this would be useful for is with multi-package repos or \"monorepos\". These are repositories where the code for multiple npm packages exist and are symlinked together locally.\r\n\r\n```\r\n/project/\r\n package.json\r\n /packages/\r\n /package-one/\r\n package.json\r\n /node_modules/\r\n /package-two/ -> ../../package-two (symlink)\r\n /package-two/\r\n package.json\r\n```\r\n\r\nInside each package, you'll generally have a `src/` directory that gets compiled to `dist/`\r\n\r\n```\r\n/package-two/\r\n package.json\r\n /src/\r\n index.ts\r\n /dist/\r\n index.js\r\n index.d.ts\r\n```\r\n\r\nRight now it is really painful to use TypeScript with one of these repos. This is because TypeScript will use the `package.json#main` to resolve to the packages `dist` folders. The problem with this is that the `dist` folders might not exist and if they do exist they might not be compiled from the most recent version of `src`. \r\n\r\nTo work around this today you can add a `index.ts` file in the root of each of your packages to point to the right location and make sure that the root `index.ts` file does not get shipped to npm.\r\n\r\n```\r\n/package-two/\r\n index.ts\r\n /src/index.ts\r\n```\r\n\r\n```ts\r\n// package-two/index.ts\r\nexport * from './src/index'\r\n```\r\n\r\nIt sucks that you need this file, and if you ever forget to create it in a new package, you'll revert back to really crap behavior.\r\n\r\nIf, instead of all that, TypeScript supported a new compiler option `mainFields` which looked like:\r\n\r\n```js\r\n{\r\n \"compilerOptions\": {\r\n \"mainFields\": [\"source\", \"main\"]\r\n }\r\n}\r\n```\r\n\r\n> **Note:** [Webpack has this same configuration option](https://webpack.js.org/configuration/resolve/#resolve-mainfields)\r\n\r\nYou could add `package.json#source` (in addition to `package.json#main`) and resolve it to the right location locally.\r\n\r\nThe algorithm would look like this:\r\n\r\nFor each `mainField`:\r\n\r\n1. Check if the `package.json` has a field with that name\r\n2. If the package.json does not have the field, continue to next `mainField`\r\n3. If it field exists, check for a file at that location.\r\n4. If no file at that location exists, continue to the next `mainField`\r\n5. If the file exists, use that file as the resolved module and stop looking\r\n\r\nI think this is the relevant code:\r\n\r\nhttps://github.com/Microsoft/TypeScript/blob/b363f4f9cd6ef98f9451ccdcc7321d151195200b/src/compiler/moduleNameResolver.ts#L987-L1014\r\n\r\n**Related Issues:**\r\n\r\n- https://github.com/Microsoft/TypeScript/issues/21137 \"Path mapping in yarn workspaces\"\r\n- https://github.com/Microsoft/TypeScript/issues/20248 \"Module resolution for sub-packages picks d.ts file when .ts file is available\"\r\n- https://github.com/Microsoft/TypeScript/issues/18442 \"Support `.mjs` output\"\r\n","author":{"url":"https://github.com/jamiebuilds","@type":"Person","name":"jamiebuilds"},"datePublished":"2018-01-26T09:47:50Z","url":"https://github.com/microsoft/TypeScript/issues/21423"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:c3fbc144-e653-234a-56cf-d81cf0f48865 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B328:21AC3F:2D9BD32:39B94BC:6B019CA7 |
| html-safe-nonce | 06259c4039698a632ff76100a2f1b441330b5ea5c41bf61efcd2a28168742cd3 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCMzI4OjIxQUMzRjoyRDlCRDMyOjM5Qjk0QkM6NkIwMTlDQTciLCJ2aXNpdG9yX2lkIjoiNzMxODc3MTc5NzcxNTk1MDc1OSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | e7639f613c20636e29190d7c7d49252a6ca57f72e649a1a37fc7f42121d97218 |
| hovercard-subject-tag | issue:291847743 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/microsoft/TypeScript/21423/issue_layout |
| twitter:image | https://opengraph.githubassets.com/3fdd3a5d4da17e0f7fea09466165549b942ef9d1157968392feb430056af20c5/microsoft/TypeScript/issues/21423 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/3fdd3a5d4da17e0f7fea09466165549b942ef9d1157968392feb430056af20c5/microsoft/TypeScript/issues/21423 |
| og:image:alt | TL;DR: A new compiler option mainFields for selecting multiple fields in package.json instead of just package.json#main. There are lots of related issues to this one (which I link to below), but I ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | jamiebuilds |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| turbo-cache-control | no-preview |
| go-import | github.com/microsoft/TypeScript git https://github.com/microsoft/TypeScript.git |
| octolytics-dimension-user_id | 6154722 |
| octolytics-dimension-user_login | microsoft |
| octolytics-dimension-repository_id | 20929025 |
| octolytics-dimension-repository_nwo | microsoft/TypeScript |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 20929025 |
| octolytics-dimension-repository_network_root_nwo | microsoft/TypeScript |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 8c77c9e8439031ad54c75a250694842892f693e1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width