Skip to content

Commit

Permalink
Updates paths for Node16 module resolution.
Browse files Browse the repository at this point in the history
Node16 module resolution [does not support "directory modules"](https://www.typescriptlang.org/docs/handbook/modules/reference.html#supported-features), yet the `paths` option was linking to a directory. This updates the `paths` to link directly to the associated file or default to `index.mjs`.

This isn't an ideal solution as it does not respect `exports` and effectively requires that any exported subpath link to an internal file at exact same path. [TS recommendation](https://www.typescriptlang.org/docs/handbook/modules/reference.html#paths-should-not-point-to-monorepo-packages-or-node_modules-packages) is to avoid `paths` for monorepo resolution for this reason and instead rely on `pnpm` linking. I made an attempt at this and found that it works for `@rules_prerender/declarative_shadow_dom` and `@rules_prerender/preact` pretty well, since they link against their deps via the `workspace:` protocol already. However it doesn't work for the rest of the workspace because they rely on the `npm_link_all_packages` target and if the `package.json` depends on `rules_prerender` or `@rules_prerender/*` packages, then it will duplicate the existing `//:node_modules/rules_prerender`-style targets. However we can't get rid of the existing ones since those build from real source.

For now we'll just a less-than-optimal `paths` option, however we should explore in the future how to drop it entirely and rely on `pnpm` linking to solve this problem.
  • Loading branch information
dgp1130 committed Aug 8, 2024
1 parent a797db9 commit e5bb9df
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
3 changes: 2 additions & 1 deletion packages/rules_prerender/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
"name": "rules_prerender",
"version": "0.0.0-PLACEHOLDER",
"type": "module",
"main": "./index.mjs"
"main": "./index.mjs",
"types": "./index.d.mts"
}
8 changes: 6 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,12 @@
"moduleResolution": "Node16", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
"baseUrl": "./", /* Base directory to resolve non-absolute module names. */
"paths": { /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
"rules_prerender": ["packages/rules_prerender"], // Map `rules_prerender` module to its implementation.
"@rules_prerender/*": ["packages/*"],
"rules_prerender": ["packages/rules_prerender/index.mts"],
"rules_prerender/*": ["packages/rules_prerender/*"],
"@rules_prerender/declarative_shadow_dom": ["packages/declarative_shadow_dom/index.mts"],
"@rules_prerender/declarative_shadow_dom/*": ["packages/declarative_shadow_dom/*"],
"@rules_prerender/preact": ["packages/preact/index.mts"],
"@rules_prerender/preact/*": ["packages/preact/*"],
},
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
// "typeRoots": [], /* List of folders to include type definitions from. */
Expand Down

0 comments on commit e5bb9df

Please sign in to comment.