Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webpack: reduce bundle size using googleapis

I am writing NodeJS code in typescript that I bundle with Webpack. I am using "googleapis" which is a huge NPM package, but I only need the gmail and the OAuth part:

import { google } from "googleapis";
const oAuth2Client = await new google.auth.OAuth2(client_id, client_secret, redirect_uri);
google.gmail({ version: "v1", auth: oAuth2Client });

The webpack chunks are huge:

Bundling with Webpack...
Time: 23015ms
Asset        Size          
wait.js      10 MB         
wait.js.map  11.9 MB

Now how can I shrink the bundle size? I tried to use externals in my webpack.config:

externals: ["googleapis"]

but of course this excludes the whole googleapis-package so the bundle will not work anymore.

Any ideas?

like image 434
josh Avatar asked Jul 05 '26 00:07

josh


1 Answers

Instead importing entire googleapis, just import needed functions and classes.

import { OAuth2Client } from 'google-auth-library';
import { gmail } from 'googleapis/build/src/apis/gmail';

const oAuth2Client = new OAuth2Client(client_id, client_secret, redirect_uri);
gmail({ version: 'v1', auth: oAuth2Client });

chunk size as below.

.vite/build/main.js  1,295.55 kB │ gzip: 309.68 kB │ map: 2,535.85 kB
like image 137
N.F. Avatar answered Jul 06 '26 16:07

N.F.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!