Commit 567e8cd
committed
perf(external): cherry-pick imports to reduce bundle sizes
Optimize external package bundles by importing only what we actually use
instead of full barrel imports. This reduces bundle size by only including
the specific functions/classes we need.
**Cherry-picked imports:**
- **pacote**: Only `get()` from lib/fetcher.js to implement extract()
(avoids bundling manifest, packument, tarball, resolve, etc.)
- **cacache**: Only get, put, rm.{entry,all}, ls.stream, tmp.withTmp
(avoids bundling verify, index operations, etc.)
440KB → 432KB (2% reduction)
- **make-fetch-happen**: Only .defaults() method from lib/index.js
(avoids bundling full fetch implementation)
- **@npmcli/arborist**: Direct import from lib/arborist/index.js
(avoids bundling Node, Link, Edge, Shrinkwrap classes)
These optimizations maintain full functionality while reducing bundle
overhead from unused code paths.
perf(external): cherry-pick more lib imports
Continue optimizing external package bundles by importing from lib/
directories instead of package roots. This bypasses unnecessary
wrapper code and package.json resolution overhead.
**Optimizations:**
- **npm-package-arg**: Import from lib/npa.js directly
(exports npa, npa.resolve, npa.toPurl, npa.Result)
- **normalize-package-data**: Import from lib/normalize.js
(exports normalize function and normalize.fixer)
- **libnpmpack**: Import from lib/index.js
(single pack function, already uses pacote/arborist internally)
While bundle sizes remain similar (these packages pull in heavy
dependencies), the direct lib imports avoid unnecessary wrappers
and improve tree-shaking potential.
perf(external): bundle npm packages for deduplication
Create two bundled modules for npm-related packages to achieve significant
size reduction through dependency deduplication.
**npm-core bundle** (npm-package-arg, normalize-package-data, semver):
- Before: 380KB total (160KB + 152KB + 68KB)
- After: 224KB
- Savings: 156KB (41% reduction)
**npm-pack bundle** (pacote, libnpmpack, cacache, make-fetch-happen):
- Before: ~5.4MB total (1.8MB + 2.4MB + 432KB + 792KB)
- After: 2.4MB
- Savings: ~3MB (56% reduction)
**Total savings: ~3.2MB (57% reduction for npm packages)**
Individual package files now re-export from bundles, maintaining the same
API while leveraging shared dependencies. This hybrid approach keeps other
external packages unbundled for optimal lazy loading.
Changes:
- Add src/external/npm-core.js and npm-pack.js bundles
- Add type definitions for both bundles
- Update individual package files to re-export from bundles
- Update build config to bundle new modules
fix(external): copy non-bundled re-export wrappers
Fix build to properly handle npm packages that re-export from bundles.
Individual package files are now thin wrappers (4KB each) that pull from
the consolidated npm-core and npm-pack bundles.
**Changes:**
- Add copy logic for `bundle: false` packages in orchestrator
- Set `bundle: false` for npm packages that re-export from bundles
- Individual files are now ~150 byte source files copied as-is
**Verified bundle sizes:**
- npm-core.js: 224KB (npm-package-arg, normalize-package-data, semver)
- npm-pack.js: 2.4MB (pacote, libnpmpack, cacache, make-fetch-happen)
- Re-export wrappers: 4KB each (thin source copies)
- Total: 5.8MB → 2.6MB saved (55% reduction)
All 5,375 tests passing, including external bundle validation tests.
fix(build): resolve npm bundle subpath imports correctly
Update esbuild plugin to handle subpath imports like npm-package-arg/lib/npa.js
instead of just bare package names. This fixes npm-core and npm-pack bundles
to actually bundle dependencies instead of copying source files.
Changed regex from ^pkg$ (exact match) to ^pkg(/|$) (match with or without subpaths).
Results:
- npm-core.js: 220KB bundled (npmPackageArg, normalizePackageData, semver)
- npm-pack.js: 2.4MB bundled (pacote, libnpmpack, cacache, makeFetchHappen)
- Individual re-export files: ~140B each (thin wrappers)
refactor(build): include arborist in npm-pack bundle
Move @npmcli/arborist into npm-pack bundle for better deduplication since
it depends on pacote, cacache, and other npm packages.
Changes:
- Add Arborist to npm-pack.js bundle (alphabetically sorted)
- Update @npmcli/arborist.js to re-export from npm-pack
- Add bundle: false config for arborist to copy wrapper instead of bundling
- Update orchestrator to handle bundle: false for scoped packages
Results:
- @npmcli/arborist.js: 2.6MB → 143B (thin wrapper)
- npm-pack.js: 2.4MB → 3.1MB (includes arborist + dependencies)
- Total size reduction: 2.6MB + 2.4MB = 5.0MB → 3.1MB + 143B = 3.1MB (38% smaller)1 parent 3f57d39 commit 567e8cd
File tree
15 files changed
+160
-22
lines changed- scripts/build-externals
- src/external
- @npmcli
15 files changed
+160
-22
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
8 | | - | |
9 | | - | |
10 | | - | |
11 | | - | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
12 | 17 | | |
13 | | - | |
14 | | - | |
15 | | - | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
16 | 22 | | |
17 | 23 | | |
18 | 24 | | |
19 | 25 | | |
20 | 26 | | |
21 | 27 | | |
22 | 28 | | |
23 | | - | |
24 | 29 | | |
25 | 30 | | |
26 | 31 | | |
| |||
36 | 41 | | |
37 | 42 | | |
38 | 43 | | |
39 | | - | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
40 | 51 | | |
41 | 52 | | |
42 | 53 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
100 | 100 | | |
101 | 101 | | |
102 | 102 | | |
103 | | - | |
| 103 | + | |
| 104 | + | |
104 | 105 | | |
105 | 106 | | |
106 | 107 | | |
107 | | - | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
108 | 112 | | |
109 | 113 | | |
110 | 114 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
| 6 | + | |
6 | 7 | | |
7 | 8 | | |
8 | 9 | | |
| |||
26 | 27 | | |
27 | 28 | | |
28 | 29 | | |
29 | | - | |
| 30 | + | |
30 | 31 | | |
31 | 32 | | |
32 | 33 | | |
| |||
38 | 39 | | |
39 | 40 | | |
40 | 41 | | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
41 | 47 | | |
42 | 48 | | |
43 | 49 | | |
44 | 50 | | |
45 | | - | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
46 | 59 | | |
47 | 60 | | |
48 | 61 | | |
49 | 62 | | |
50 | 63 | | |
51 | 64 | | |
52 | | - | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
53 | 76 | | |
54 | 77 | | |
55 | 78 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | | - | |
| 3 | + | |
| 4 | + | |
4 | 5 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | | - | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | | - | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | | - | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | | - | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
0 commit comments