feat: inline types to reduce install size#194
Open
eduardoboucas wants to merge 1 commit intoneondatabase:mainfrom
Open
feat: inline types to reduce install size#194eduardoboucas wants to merge 1 commit intoneondatabase:mainfrom
eduardoboucas wants to merge 1 commit intoneondatabase:mainfrom
Conversation
Collaborator
|
@eduardoboucas Thanks — this looks like a great PR. Can I ask if you have an urgent need for this? (I need to spend a bit more time checking it out, and I'm away next week). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
@types/nodeand@types/pgfrom runtime dependencies by replacing them with local type shims that are inlined at build timeProblem
@types/node(~2.5 MB) and@types/pg(~44 KB) were listed as runtimedependencies, meaning they — along with their transitive dependencies (pg-protocol,pg-types,undici-types, and variouspostgres-*packages) — were installed by every consumer of this package. In total, 12 packages and 3.8 MB were pulled in, when only 456 KB was the actual package.Solution
Replace the runtime dependency on
@types/nodeand@types/pgwith local type shims that are inlined into the published declaration files at build time.New files
src/shims/pg/index.d.ts— Local declarations for the pg types this package re-exports (ClientConfig,PoolConfig,QueryResult, etc.). These areproper
.d.tsfiles, type-checked by the compiler.src/shims/events/index.d.ts— MinimalEventEmitterdeclaration, replacing the one from@types/node.tests/type-compatibility.ts— Compile-time test that verifies our shims' property keys match the real@types/pgin both directions.Modified files
tsconfig.json— Addedpathsto resolvepgandeventsimports to the local shims during type generation. esbuild ignores tsconfig paths, so the real modules are still used for the runtime bundle.api-extractor.json— AddedpgandeventstobundledPackagesso api-extractor inlines the shim types into the published.d.tsfiles.build.sh— Added asedstep to strip/// <reference types="node" />from thesubtlsdependency before api-extractor runs (subtls's upstream.d.tsreferences node types).package.json— Moved@types/nodeand@types/pgfromdependenciestodevDependencies. Addedtest:type-shimsscript.src/shims/net/index.ts— Changedwrite()andend()type signatures fromBuffer | stringtoUint8Array | string. This is type-only; the runtime implementation is unchanged.Buffer extends Uint8Array, so existing code that passesBuffervalues continues to work.Preventing drift
npm run test:type-shims(wired intonpm test) verifies that the property keys on our shims match the real@types/pgtypes in both directions. If@types/pgadds or removes a property, the build fails.Impact
node_modules).d.tspgandevents