Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support moduleResolution: 'node16'+ for package.json exports #437

Open
zemse opened this issue Feb 15, 2023 · 3 comments · Fixed by #453
Open

Support moduleResolution: 'node16'+ for package.json exports #437

zemse opened this issue Feb 15, 2023 · 3 comments · Fixed by #453
Labels
kind: feature New feature or request priority: in progress solution: needs test This issue requires creating a test to assuredly close out topic: TS version Related to a change in a TS version

Comments

@zemse
Copy link

zemse commented Feb 15, 2023

Troubleshooting

  1. Does tsc have the same output? If so, please explain why this is incorrect behavior

    tsc uses actual moduleResolution from my tsconfig so it works.

  2. Does your Rollup plugin order match this plugin's compatibility? If not, please elaborate

    I'm using tsdx, which uses this plugin internally, so not really sure about this.

  3. Can you create a minimal example that reproduces this behavior? Preferably, use this environment for your reproduction

    I can do that if needed. But this issue is very easily reproducable.

What happens and why it is incorrect

I want to import something from a package that uses package.json exports. Here, ethers is a package, and address is defined as an export in it's package.json.

import type { AddressLike } from 'ethers/address';

The above compiles successfully with vanilla typescript compiler, when moduleResolution is set to node16. With rollup-plugin-typescript2, the moduleResolution is overridden and hence typescript compiler gives the exact error as if moduleResolution is set to node:

(typescript) Error: /my/project/src/file/path.ts(5,34): semantic error TS2307: Cannot 
find module 'ethers/address' or its corresponding type declarations.

The moduleResolution from user's tsconfig is overridden is set to legacy node resolution (relevant code). I think this is incorrect because typescript wants moduleResolution to be node16 or nodenext if a dev wants to use package json exports.

Environment

Versions

  System:
    OS: macOS 12.0.1
    CPU: (8) arm64 Apple M1
    Memory: 111.16 MB / 16.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 16.17.1 - ~/.nvm/versions/node/v16.17.1/bin/node
    Yarn: 1.22.19 - ~/.nvm/versions/node/v16.17.1/bin/yarn
    npm: 8.15.0 - ~/.nvm/versions/node/v16.17.1/bin/npm
  npmPackages:
    typescript: ^4.6.3 => 4.8.3 
  npmGlobalPackages:
    typescript: 4.8.4

rollup.config.js

:

tsconfig.json

:
{
  // see https://www.typescriptlang.org/tsconfig to better understand tsconfigs
  "include": ["src", "types"],
  "compilerOptions": {
    "module": "esnext",
    "lib": ["dom", "esnext"],
    "target": "ES2020",
    "importHelpers": true,
    // output .d.ts declaration files for consumers
    "declaration": true,
    // output .js.map sourcemap files for consumers
    "sourceMap": true,
    // match output dir to input dir. e.g. dist/index instead of dist/src/index
    "rootDir": "./src",
    // stricter type-checking for stronger correctness. Recommended by TS
    "strict": true,
    // linter checks for common issues
    "noImplicitReturns": true,
    "noFallthroughCasesInSwitch": true,
    // noUnused* overlap with @typescript-eslint/no-unused-vars, can disable if duplicative
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    // use Node's module resolution algorithm, instead of the legacy TS one
    "moduleResolution": "node16",
    // transpile JSX to React.createElement
    "jsx": "react",
    // interop between ESM and CJS modules. Recommended by TS
    "esModuleInterop": true,
    // significant perf increase by skipping checking .d.ts files, particularly those in node_modules. Recommended by TS
    "skipLibCheck": true,
    // error out if import and file system have a casing mismatch. Recommended by TS
    "forceConsistentCasingInFileNames": true,
    // `tsdx build` ignores this option, but it is commonly used when type-checking separately with `tsc`
    "noEmit": true,
    "resolveJsonModule": true
  }
}

package.json

:

plugin output with verbosity 3

:

$  tsdx build --tsconfig tsconfig.build.json && cp src/*.json dist && cp -r src/deployments dist
@rollup/plugin-replace: 'preventAssignment' currently defaults to false. It is recommended to set this option to `true`, as the next major version will default this option to `true`.
@rollup/plugin-replace: 'preventAssignment' currently defaults to false. It is recommended to set this option to `true`, as the next major version will default this option to `true`.
✓ Creating entry file 2.2 secs
(typescript) Error: //path/to/my/project/src/typechain/uniswap-core/factories/UniswapV3Factory__factory.ts(5,34): semantic error TS2307: Cannot find module 'ethers/address' or its corresponding type declarations.
Error: //path/to/my/project/src/typechain/uniswap-core/factories/UniswapV3Factory__factory.ts(5,34): semantic error TS2307: Cannot find module 'ethers/address' or its corresponding type declarations.
    at error (//path/to/my/project/node_modules/rollup/dist/shared/node-entry.js:5400:30)
    at throwPluginError (//path/to/my/project/node_modules/rollup/dist/shared/node-entry.js:11878:12)
    at Object.error (//path/to/my/project/node_modules/rollup/dist/shared/node-entry.js:12912:24)
    at Object.error (//path/to/my/project/node_modules/rollup/dist/shared/node-entry.js:12081:38)
    at RollupContext.error (//path/to/my/project/node_modules/rollup-plugin-typescript2/dist/rollup-plugin-typescript2.cjs.js:17237:30)
    at //path/to/my/project/node_modules/rollup-plugin-typescript2/dist/rollup-plugin-typescript2.cjs.js:25033:23
    at arrayEach (//path/to/my/project/node_modules/rollup-plugin-typescript2/dist/rollup-plugin-typescript2.cjs.js:545:11)
    at Function.forEach (//path/to/my/project/node_modules/rollup-plugin-typescript2/dist/rollup-plugin-typescript2.cjs.js:9397:14)
    at printDiagnostics (//path/to/my/project/node_modules/rollup-plugin-typescript2/dist/rollup-plugin-typescript2.cjs.js:25006:12)
    at //path/to/my/project/node_modules/rollup-plugin-typescript2/dist/rollup-plugin-typescript2.cjs.js:29264:21

@agilgur5 agilgur5 changed the title moduleResolution is overridden Support moduleResolution: 'node16' / 'nodenext' Feb 20, 2023
@agilgur5 agilgur5 changed the title Support moduleResolution: 'node16' / 'nodenext' Support moduleResolution: 'node16' / 'nodenext' Feb 20, 2023
@agilgur5 agilgur5 changed the title Support moduleResolution: 'node16' / 'nodenext' Support moduleResolution: 'node16'+ for package.json exports Feb 20, 2023
@ezolenko
Copy link
Owner

@zemse could you check if #453 works for your usecase?

@ezolenko
Copy link
Owner

@chronoDave could you check if #453 works for your usecase?

@chronoDave
Copy link

@ezolenko My usecase is no longer valid so cannot confirm whether or not this solution works.

@agilgur5 agilgur5 added kind: feature New feature or request priority: in progress topic: TS version Related to a change in a TS version solution: needs test This issue requires creating a test to assuredly close out labels Jul 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: feature New feature or request priority: in progress solution: needs test This issue requires creating a test to assuredly close out topic: TS version Related to a change in a TS version
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants