rspack icon indicating copy to clipboard operation
rspack copied to clipboard

Missing types for programmatic usage with TypeScript and Node

Open alshdavid opened this issue 2 years ago • 4 comments

System Info

System: OS: Linux 4.4 Debian GNU/Linux 12 (bookworm) 12 (bookworm) CPU: (24) x64 AMD Ryzen 9 5900X 12-Core Processor
Memory: 31.09 GB / 47.91 GB Container: Yes Shell: 5.9 - /usr/bin/zsh Binaries: Node: 18.12.1 - /usr/local/node-version-manager/bin/node Yarn: 1.22.19 - /usr/local/node-version-manager/bin/yarn npm: 9.6.0 - /usr/local/node-version-manager/bin/npm npmPackages: @rspack/core: ^0.1.7 => 0.1.7

Details

When trying to use rspack programmatically via node and @rspack/core - I have type errors due to missing internal dependencies.

// index.ts
import * as rspack from '@rspack/core'
console.log(rspack.rspack)
$ npx tsc
../node_modules/.pnpm/@[email protected]/node_modules/@rspack/core/dist/compilation.d.ts:11:24 - error TS7016: Could not find a declaration file for module 'webpack-sources'.
 
'/home/alshdavid/Development/alshdavid/davidalsh-com/node_modules/.pnpm/[email protected]/node_modules/webpack-sources/lib/index.js' implicitly has an 'any' type.

  Try `npm i --save-dev @types/webpack-sources` if it exists or add a new declaration (.d.ts) file containing `declare module 'webpack-sources';`
../node_modules/.pnpm/@[email protected]/node_modules/@rspack/core/dist/compilation.d.ts:16:8 - error TS1259:
 Module '"/home/alshdavid/Development/alshdavid/davidalsh-com/node_modules/.pnpm/@[email protected]/node_modules/@rspack/core/dist/ResolverFactory"' can only be default-imported using the 'esModuleInterop' flag

and so on

These type dependencies are marked as devDependencies in the package.json

image

However they are not marked as dependencies or peerDependencies of @rspack/core - so tsc fails

I can add these dependencies to my application manually

Reproduce link

No response

Reproduce Steps

mkdir foobar cd foobar

pnpm init pnpm install --save typescript @rspack/core

echo "import * as rspack from '@rspack/core'" > main.ts echo "console.log(rspack.rspack)" >> main.ts

npx tsc --outDir dist --module CommonJS --strict true --target ESNext main.ts

alshdavid avatar Apr 15 '23 02:04 alshdavid

Did you enable skipLibCheck feature of TypeScipr? You might try compiling with skipLibCheck:true to see if the error still exists.

hyf0 avatar Apr 15 '23 03:04 hyf0

Hey, thanks for the suggestion. That configuration flag does indeed resolve the compilation issue however (I could be wrong but) I believe using skipLibCheck is not considered a best practice as it reduces the accuracy of types obtained from libraries (beyond just rspack). My understanding is that it's normally enabled to resolve type collisions in edge cases rather than a setting expected to be used by default

alshdavid avatar Apr 15 '23 04:04 alshdavid

I believe using skipLibCheck is not considered a best practice as

I agree with you. So you might take skipLibCheck: true as a workaround. Currently, we are the focus on https://github.com/web-infra-dev/rspack/issues/2048 and lack manpower

But we have planned to pre-compile dependencies of Rspack, which should solve this problem.

Thanks for your feedback. It's very useful for us to decide which parts we should concentrate on in the future.

hyf0 avatar Apr 15 '23 10:04 hyf0

But we have planned to pre-compile dependencies of Rspack, which should solve this problem.

That's perfect, thanks for taking the time to respond!

alshdavid avatar Apr 15 '23 11:04 alshdavid

Tacked in https://github.com/web-infra-dev/rspack/issues/6481 and https://github.com/web-infra-dev/rspack/issues/5867

chenjiahan avatar Jun 26 '24 06:06 chenjiahan