Create actual browser bundles and transpile everything else#789
Create actual browser bundles and transpile everything else#789BinaryMuse merged 30 commits intorelease-19.0.0from
Conversation
|
This pull request is being automatically deployed with Vercel (learn more). 🔍 Inspect: https://vercel.com/primer/primer-components/ni2ajrvgh |
|
Since this PR will make every component importable directly from its transpiled source file, each component's exports shape will be part of the public API of the package. As such, I'm proposing making that contract explicit in our tests: checkExports('Position', {
default: null,
Position,
Absolute,
Fixed,
Relative,
Sticky
})
checkExports('Box', {
default: Box
})
Before I go through the entire codebase adding this, I wanted to get some feedback from the team to see how people felt about such a thing. |
054397d to
2a22b79
Compare
As we begin to implement things like deprecation warnings that we only want to appear in non-production environments, it's useful to have an easy check that completely compiles out in production. This commit adds `babel-plugin-transform-replace-expressions`, which replaces expressions in code. The primary expression is `__DEV__`, which compiles to `process.env.NODE_ENV !== "production"`. When building the browser bundles, we compile it instead to `false`, so that terser automatcally removes the entire `if(__DEV__)...` block, resulting in zero runtime overhead. This commit also adds the define to Gatsby's webpack config, as Gatsby doesn't seem to respect the Babel configuration for the main Primer Components project. In addition, to get Gatsby to run Primer Components through the new webpack plugin we add, we need to alter the webpack rules to include files loaded from `@primer/components`. Finally, when building a production build (like in our browser bundles), we explicitly set `process.env.NODE_ENV` to `"production"`.
2a22b79 to
66cbd50
Compare
|
As I was looking through the exports, I noticed two modules where importing directly from the
Open to other thoughts on organization, and also curious the best way to go about documenting individual exports when the component isn't directly available on the default export. |
Deprecation testing and zero runtime cost in prod
|
I like the checkExports idea! |
emplums
left a comment
There was a problem hiding this comment.
This looks great! might be helpful to have at least another person review this too since it's lots of changes!
dmarcey
left a comment
There was a problem hiding this comment.
I'm definitely not a rollup / babel expert, but I looked through the changes to try to give another set of 👀 and things looked good to me! Nice work! ✨
|
Potentially a dumb question: Is there a real benefit to shipping bundles in the first place? How many consumers want a UMD module bundle? If the answer is "not really" could we just get rid of rollup completely and force consumers to use a bundler (because let's be honest, if you're writing frontend JS you're using a bundler). |
I don't think so, and in fact it's not documented. I think it's potentially useful for us internally for seeing how our package would behave if it was bundled; a good example is cutting off ~800KB off the minified library size based off the work in this PR. |

Required reading: #779
This PR significantly changes up our bundling situation:
dist/browser.esm.jsanddist/browser.umd.js. These are actual browser bundles — all dependencies except for our peer dependencies (reactandstyled-components) are included in the bundle. These are really only useful for dropping onto a page with the appropriate ESM/UMD setup (e.g. the user is also including the peer deps). We may decide we don't even need these, but they may be useful for tracking deltas in size over time. Note: these files are quite large (500KB, even after minification), but include all the dependencies.src/intodist/. This will allow users to use bothimport {Box, BorderBox} from '@primer/components'and alsoimport Box from '@primer/components/dist/Box'. This also enablesimport ShinyNewThing from '@primer/components/dist/experimental/ShinyNewThing'Todo:
Release Notes
import {Box} from '@primer/components'can now also be written asimport Box from '@primer/components/dist/Box'Closes #779
Closes #456
Closes #797
Closes #801