Redux-toolkit: Improve packaging: webpack uses UMD build, inlines immer, selectorator, etc.

Created on 10 Dec 2018  路  3Comments  路  Source: reduxjs/redux-toolkit

The goal: Ideally, if I only import createAction then I wouldn't expect immer or selectorator to be pulled into my bundle. This is the intention behind the ESM build pointed to by the module field in this library's current package.json.

Unfortunately, webpack in its default configuration (including how it's configured in frameworks like Next.js) prefers the browser field to module, so the entire 103KB UMD build is pulled in no matter what you import. Instead of requiring people to fiddle with a non-default webpack config, it would be better to behave nicely with the defaults.

If you check out the package.json for redux and react-redux, they don't specify the browser field at all 鈥撀爄nstead, the UMD build is linked in the unpkg field. That way, webpack will prefer the module build but there's still a UMD build included.

This comment also suggests a potential way that these fields could behave together, by providing browser alternatives to both the ESM and CJS entry points. I'm not sure how that works exactly, but might be worth exploring.

bug

Most helpful comment

All 3 comments

Hmm. I take that back - the fix in 0.3.2 resolved the UMD build issue, but I also see the rest of the libs getting pulled into the output.

Per the example from #68 , import {getDefaultMiddleware} from "redux-starter-kit" in a CRA project and building it results in folders like redux/es, redux-thunk/es, immer/dist, and redux-immutable-state-invariant showing up in the sourcemap, indicating they got pulled in.

I assume this is because we're pulling it from the RSK index file, and that's causing things to not get tree-shaken.

I don't even pretend to understand what contortions you have to go through these days to get tree shaking to work properly. Open to suggestions :)

Based on what I'm seeing in the 1.3 alphas, pretty sure this is fixed by now.

Was this page helpful?
0 / 5 - 0 ratings