fireproof-storage / fireproof

Realtime database, runs anywhere. Install Fireproof in your front-end app or edge function, and sync data via any backend.
https://fireproof.storage
Other
219 stars 16 forks source link

Node/Typescript cannot resolve types if importing app uses "type":"module" #68

Closed Jollarvia closed 4 months ago

Jollarvia commented 5 months ago

I have a test application that will not completely resolve the fireproof object.

test.zip

If I run the "run" script (without typescript) fireproof is located and the console statement is logged. (test.js)

If I run the "build-esm" script it (usually) fails with the error File 'C:/Users/Julio/repos/test/node_modules/typescript/lib/package.json' does not exist according to earlier cached lookups. File 'C:/Users/Julio/repos/test/node_modules/typescript/package.json' exists according to earlier cached lookups. test.js(1,10): error TS2305: Module '"@fireproof/core/node"' has no exported member 'fireproof'.

running "build-esm-traceres" to have typescript do resolution tracing, I notice this in the file.

Found 'package.json' at 'C:/Users/Julio/repos/test/package.json'.
======== Resolving module '@fireproof/core/node' from 'C:/Users/Julio/repos/test/test.js'. ========
Module resolution kind is not specified, using 'NodeNext'.
Resolving in ESM mode with conditions 'import', 'types', 'node'.
'baseUrl' option is set to 'C:/Users/Julio/repos/test', using this value to resolve non-relative module name '@fireproof/core/node'.
Resolving module name '@fireproof/core/node' relative to base url 'C:/Users/Julio/repos/test' - 'C:/Users/Julio/repos/test/@fireproof/core/node'.
Loading module as file / folder, candidate module location 'C:/Users/Julio/repos/test/@fireproof/core/node', target file types: TypeScript, JavaScript, Declaration.
File 'C:/Users/Julio/repos/test/package.json' exists according to earlier cached lookups.
Loading module '@fireproof/core/node' from 'node_modules' folder, target file types: TypeScript, JavaScript, Declaration.
Searching all ancestor node_modules directories for preferred extensions: TypeScript, Declaration.
Found 'package.json' at 'C:/Users/Julio/repos/test/node_modules/@fireproof/core/package.json'.
Entering conditional exports.
Matched 'exports' condition 'import'.
Using 'exports' subpath './node' with target './dist/node/fireproof.esm.js'.
File name 'C:/Users/Julio/repos/test/node_modules/@fireproof/core/dist/node/fireproof.esm.js' has a '.js' extension - stripping it.
File 'C:/Users/Julio/repos/test/node_modules/@fireproof/core/dist/node/fireproof.esm.ts' does not exist.
File 'C:/Users/Julio/repos/test/node_modules/@fireproof/core/dist/node/fireproof.esm.tsx' does not exist.
File 'C:/Users/Julio/repos/test/node_modules/@fireproof/core/dist/node/fireproof.esm.d.ts' does not exist.
Failed to resolve under condition 'import'.
Saw non-matching condition 'require'.
Matched 'exports' condition 'types'.
Using 'exports' subpath './node' with target './dist/types/fireproof.d.ts'.
File 'C:/Users/Julio/repos/test/node_modules/@fireproof/core/dist/types/fireproof.d.ts' exists - use it as a name resolution result.
Resolved under condition 'types'.
Exiting conditional exports.
Resolving real path for 'C:/Users/Julio/repos/test/node_modules/@fireproof/core/dist/types/fireproof.d.ts', result 'C:/Users/Julio/repos/test/node_modules/@fireproof/core/dist/types/fireproof.d.ts'.
======== Module name '@fireproof/core/node' was successfully resolved to 'C:/Users/Julio/repos/test/node_modules/@fireproof/core/dist/types/fireproof.d.ts' with Package ID '@fireproof/core/dist/types/fireproof.d.ts@0.16.1'. ========
File 'C:/Users/Julio/repos/test/node_modules/@fireproof/core/dist/types/package.json' does not exist.
File 'C:/Users/Julio/repos/test/node_modules/@fireproof/core/dist/package.json' does not exist.
File 'C:/Users/Julio/repos/test/node_modules/@fireproof/core/package.json' exists according to earlier cached lookups.
**======== Resolving module './database' from 'C:/Users/Julio/repos/test/node_modules/@fireproof/core/dist/types/fireproof.d.ts'. ========
Module resolution kind is not specified, using 'NodeNext'.
Resolving in ESM mode with conditions 'import', 'types', 'node'.
Loading module as file / folder, candidate module location 'C:/Users/Julio/repos/test/node_modules/@fireproof/core/dist/types/database', target file types: TypeScript, JavaScript, Declaration.
Directory 'C:/Users/Julio/repos/test/node_modules/@fireproof/core/dist/types/database' does not exist, skipping all lookups in it.
======== Module name './database' was not resolved. ========

If I were to change the import to: import * as fireproof from "@fireproof/core/node" it works and database and types are present but there is no typescript type information found. This is unacceptable for my repo.

I am new to fireproof and fairly new to typescript. But I could not find an acceptable permutation between how I import the fireproof module and configuring the tsconfig and package.json for success. I define success as being able to run at runtime and having access to all type information at compile time.

Assumptions: C:/Users/Julio/repos/test/node_modules/@fireproof/core/dist/types/database does, in fact, exist as C:/Users/Julio/repos/test/node_modules/@fireproof/core/dist/types/database.d.ts but typescript does not pick it up.

I believe it is related my use of type:module in the package.json. This cannot be changed by me either.

See attached repo to try it out. Thanks for any guidance on this.

jchris commented 5 months ago

Thank you so much for sharing. We are currently cleaning up exactly this stuff in the build, and the next upcoming release of 0.16.x should be worth trying. Expect a release early next week if not sooner.

Jollarvia commented 5 months ago

Awesome to hear. Thanks for the update. You guys are doing great work.