You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Dynamically import the fetch ponyfill in contexts where it isn't available
We're currently always statically importing `cross-fetch` to use as a fetch polyfill. The only environment we actually need to polyfill fetch in is node 16 which is used by old gadget apps on fv1 -- we already only support browsers that have `fetch` defined globally, and node 18 in fv2 has it as well. So, most folks are paying a bundle size penalty for no great reason.
Instead, let's move this chunk out into a separate import that is only dynamically imported if needed. This will happen only on node16 which will be nice and snappy, and shouldn't cause a request waterfall in browsers. You'll notice that vite picks up on this dynamic import and creates a new output chunk for it which is what we want for the browser as well.
Before:
```
vite v4.4.7 building for production...
✓ 173 modules transformed.
dist/api-read-stats.html 210.79 kB │ gzip: 46.88 kB
dist/test-bundle-api-read.js 211.80 kB │ gzip: 37.36 kB
dist/api-read-stats.html 211.39 kB │ gzip: 46.93 kB
dist/test-bundle-api-read.cjs 138.54 kB │ gzip: 30.51 kB
✓ built in 769ms
vite v4.4.7 building for production...
✓ 449 modules transformed.
dist/react-read-stats.html 302.29 kB │ gzip: 56.83 kB
dist/test-bundle-react-read.js 316.93 kB │ gzip: 66.53 kB
dist/react-read-stats.html 306.22 kB │ gzip: 56.92 kB
dist/test-bundle-react-read.cjs 213.27 kB │ gzip: 55.67 kB
✓ built in 1.13s
vite v4.4.7 building for production...
✓ 983 modules transformed.
dist/shopify-read-stats.html 515.87 kB │ gzip: 77.42 kB
dist/test-bundle-shopify-read.js 636.23 kB │ gzip: 125.20 kB
dist/shopify-read-stats.html 516.37 kB │ gzip: 77.42 kB
dist/test-bundle-shopify-read.cjs 447.28 kB │ gzip: 106.73 kB
✓ built in 2.29s
```
After:
```
vite v4.4.7 building for production...
✓ 173 modules transformed.
dist/api-read-stats.html 210.92 kB │ gzip: 46.91 kB
dist/browser-ponyfill-a950d05c.js 12.53 kB │ gzip: 3.50 kB
dist/test-bundle-api-read.js 199.78 kB │ gzip: 34.45 kB
dist/api-read-stats.html 211.51 kB │ gzip: 46.98 kB
dist/browser-ponyfill-0b1c63f2.cjs 8.88 kB │ gzip: 3.07 kB
dist/test-bundle-api-read.cjs 130.06 kB │ gzip: 28.07 kB
✓ built in 774ms
vite v4.4.7 building for production...
✓ 449 modules transformed.
dist/react-read-stats.html 302.31 kB │ gzip: 56.84 kB
dist/test-bundle-react-read.js 0.09 kB │ gzip: 0.10 kB
dist/browser-ponyfill-aae43e35.js 12.44 kB │ gzip: 3.50 kB
dist/react-read-b8f94e10.js 305.18 kB │ gzip: 63.69 kB
dist/react-read-stats.html 306.31 kB │ gzip: 56.96 kB
dist/test-bundle-react-read.cjs 0.17 kB │ gzip: 0.16 kB
dist/browser-ponyfill-1992ee4c.cjs 8.83 kB │ gzip: 3.08 kB
dist/react-read-e3b5cb04.cjs 204.97 kB │ gzip: 53.20 kB
✓ built in 1.16s
vite v4.4.7 building for production...
✓ 983 modules transformed.
dist/shopify-read-stats.html 514.75 kB │ gzip: 77.59 kB
dist/test-bundle-shopify-read.js 0.12 kB │ gzip: 0.12 kB
dist/browser-ponyfill-d3f0b39a.js 12.45 kB │ gzip: 3.50 kB
dist/shopify-read-62230a97.js 624.51 kB │ gzip: 122.38 kB
dist/shopify-read-stats.html 515.26 kB │ gzip: 77.60 kB
dist/test-bundle-shopify-read.cjs 0.19 kB │ gzip: 0.17 kB
dist/browser-ponyfill-8417ec9c.cjs 8.83 kB │ gzip: 3.08 kB
dist/shopify-read-25c98907.cjs 439.00 kB │ gzip: 104.22 kB
```
Another 6% bundle size reduction
0 commit comments