panva / openid-client

OAuth 2 / OpenID Connect Client API for JavaScript Runtimes
MIT License
1.83k stars 392 forks source link

Tree-shake with Webpack to only bundle certain classes #314

Closed l1b3r closed 3 years ago

l1b3r commented 3 years ago

Hi, First off, thank you for such an amazing package!

I'm building a server-side solution with openid-client (with TypeScript), which is split into separate applications/bundles independent from each other "build-wise", so in the end I have multiple separate main.js files with whatever 3rd-party modules each particular app requires.

One of the apps only needs a very little part of openid-client: just to check whether the TokenSet is .expired:

import { TokenSet, TokenSetParameters } from 'openid-client';

export function checkTokenExpiration(tokenSetParams: TokenSetParameters) {
    const tokenSet = new TokenSet(tokenSetParameters);
    return tokenSet.expired();
}

This app doesn't do anything else auth-wise, so TokenSet is the only real class that is required. Yet, when I bundle the app using Webpack I notice that openid-client is included in the bundle entirely along with all of its own deps. Everything works fine, but my production environment has strict bundle size constrictions, so I'm trying to shrink the bundles as much as possible.

The question is: is it possible to somehow tree-shake openid-client with webpack so that is only includes only what I actually import from the package? For the case above that would presumably be openid-client/lib/token_set.js along with whatever that module requires.

Alternatively I could just copy the logic of .expired() into my own function and ditch openid-client for that app completely, but this seems like an anti-pattern to me.

Thanks

panva commented 3 years ago

If you just need to check if a timestamp is past now, isn't using openid-client an overkill?

Anyway, treeshaking is a matter of tooling, if the exports could be done differently i'm happy to do it unless it's breaking, but i will not be spending time figuring out the how.