ezolenko / rollup-plugin-typescript2

Rollup plugin for typescript with compiler errors.
MIT License
819 stars 71 forks source link

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

Open zemse opened 1 year ago

zemse commented 1 year ago

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

:
```js ```

tsconfig.json

:
```json5 { // 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

:
```json ```

plugin output with verbosity 3

:
```text $ 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 ```
ezolenko commented 1 year ago

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

ezolenko commented 1 year ago

@chronoDave could you check if https://github.com/ezolenko/rollup-plugin-typescript2/pull/453 works for your usecase?

chronoDave commented 1 year ago

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