dnt icon indicating copy to clipboard operation
dnt copied to clipboard

Cannot resolve package subpaths in type checking phase

Open sgwilym opened this issue 2 years ago • 3 comments

In the mappings config you can do stuff like this:

"https://deno.land/x/my_module/extras": {
  name: "my-module",
  version: "1.0.0",
  subPath: "extras",
}

I tried this today and dnt will even check that my-module even has extras in its exports field. Cool.

But it can never get past the type checking phase:

[dnt] Type checking...
some/path - error TS2307: Cannot find module 'my-module/extras' or its corresponding type declarations.

I know from painful experience that TS does not yet have support for the package.json exports field unless the tsconfig is changed to have module: 'nodenext', moduleResolution: 'nodenext'. Could that have something to do with it?

sgwilym avatar Mar 02 '22 14:03 sgwilym

Related: https://github.com/denoland/dnt/issues/118

This is something that hasn't been implemented in TypeScript ☹️

dsherret avatar Mar 02 '22 14:03 dsherret

I have come up with a truly filthy hack to get around this, after finding the solutions in #118 did not work for me.

Typescript does not know how to resolve using the exports field, but it can resolve paths. So I added this at the end of my build script:

Deno.writeTextFileSync(
    'npm/browser.js',
    `export * from "./esm/src/entries/browser.js";`,
);

Deno.writeTextFileSync(
    'npm/browser.d.ts',
    `export * from './types/src/entries/browser';`,
);

Deno.writeTextFileSync(
    'npm/node.js',
    `export * from "./esm/src/entries/node.js";`,
);

Deno.writeTextFileSync(
    'npm/node.d.ts',
    `export * from './types/src/entries/node';`,
);

Now exports-unaware TS is able to pick up on the now very real subpaths, while stuff that supports it (webpack, rollup, etc) can use it! Going to go take a shower.

sgwilym avatar Mar 02 '22 15:03 sgwilym

Related: #118

This is something that hasn't been implemented in TypeScript ☹️

@dsherret hasn't this been implemented now? why does the issue persist?

jajaperson avatar Aug 28 '22 12:08 jajaperson