Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify __DEV__ polyfill to use imports instead of global scope #10521

Merged
merged 6 commits into from Feb 7, 2023

Conversation

benjamn
Copy link
Member

@benjamn benjamn commented Feb 6, 2023

This PR substantially undoes much of the complexity introduced by #8347, at the cost of regressing bundle sizes by approximately 3.75KB for any application where the __DEV__ imports get transpiled to something else before the minifier has a chance to prune unreachable code, such as globals.__DEV__ following a CJS import like var globals = require("../../utilities/globals").

If compilers/transpilers/bundlers would just leave the imported __DEV__ identifiers alone, this would all work very nicely, but the ESM transpilation behavior of third-party toolchains is not something we can control.

Relevant past work:

@changeset-bot

This comment was marked as resolved.

@benjamn
Copy link
Member Author

benjamn commented Feb 6, 2023

I expected this npm run bundlesize regression to be greater (>1KB min/gzip), based on past experience, so it's encouraging that it's only 0.08KB:

Screen Shot 2023-02-06 at 8 51 41 AM

For comparison, the bundlesize is 32.41KB right now on main, so it may be worth reviewing where the increases have happened on the release-3.8 branch, just in case any of them was both large and unintentional.

@benjamn
Copy link
Member Author

benjamn commented Feb 6, 2023

Okay, the/one reason the bundle size regression isn't so bad is that we use Rollup to generate our all-in-one dist/apollo-client.cjs bundle, which we then minify and gzip before computing the bundle size, and Rollup manages to declare __DEV__ as a plain identifier in the CommonJS bundle:

function getDEV() {
    return "__DEV__" in global$1
        ? Boolean(global$1.__DEV__)
        : maybe(function () { return process.env.NODE_ENV; }) !== "production";
}
var DEV = getDEV();
var __DEV__ = DEV;

If we try using these changes in a webpack/CRA app like https://github.com/apollographql/react-apollo-error-template, we see a much worse regression when running npm run build:

File sizes after gzip:

  122.68 kB  build/static/js/main.0b2ae3e8.js
  208 B      build/static/css/main.94a33c21.css

It appears Webpack now applies DefinePlugin transforms like this one successfully, as long as the target is ESM rather than CJS. Removing that DefinePlugin raises the bundle size to 126.43KB, which is more in line with what I was expecting.

It's encouraging that ESM tooling seems to be getting this right by default now, whereas previously the ESM-to-CJS compilation step threw a 🔧 into the works. I'm still a little worried about regressing bundle sizes for existing apps that compile all the way to CommonJS, but perhaps that's (more) tolerable since compiling to ESM is getting easier and is more often the default behavior.

See #10521 for
discussion of concerns about this change.
Comment on lines +99 to +100
export * from './common/responseIterator';
export * from './common/incrementalResult';
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Explaining the changes from 4207078:

When adding new exports to @apollo/client/utilities, the exported stuff needs to be accessible from the top-level @apollo/client/utilities package if it's going to be used elsewhere within @apollo/client (outside /utilities). This problem is flagged during npm run build by warnings like

Risky cross-entry-point nested import of ../version in core/ApolloClient.js
...
Risky cross-entry-point nested import of ../../react/cache in testing/react/MockedProvider.js
Risky cross-entry-point nested import of ../../react/cache/index.js in testing/react/MockedProvider.js

These are just warnings because they are not always a problem: cross-entry-point code may be unintentionally duplicated across CJS bundles, which can be a problem if the code defines a class that's only supposed to have one definition (for example), but in other cases the duplication is convenient and harmless, such as when the CJS bundle is used only for testing, or the duplicated code is safe to duplicate (like the import of ../version from within core/ApolloClient.js).

@benjamn benjamn marked this pull request as ready for review February 6, 2023 14:48
@benjamn benjamn self-assigned this Feb 6, 2023
src/utilities/globals/DEV.ts Outdated Show resolved Hide resolved
src/utilities/globals/index.ts Show resolved Hide resolved
@jerelmiller
Copy link
Member

re:

it may be worth reviewing where the increases have happened on the release-3.8 branch

Much if this from the new useSuspenseQuery hook introduced in this branch. We've had some other minor changes, but this is the biggest contributing factor.

Copy link
Member

@jerelmiller jerelmiller left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚢 This looks good to me! Really appreciate you diving into this right away!

@jerelmiller
Copy link
Member

Oh ya, and don't forget to add a changeset (npx changeset) so we can make sure this change bumps a patch release.

@alessbell
Copy link
Member

/release:pr

@github-actions
Copy link
Contributor

github-actions bot commented Feb 6, 2023

A new release has been made for this PR. You can install it with npm i @apollo/client@0.0.0-pr-10521-20230206180228.

@benjamn
Copy link
Member Author

benjamn commented Feb 6, 2023

Nice, thanks to that release, I'm confident this version of the library still works without bundling, as you can see by running this code in your browser console:

await import("https://cdn.jsdelivr.net/npm/@apollo/client@0.0.0-pr-10521-20230206180228/+esm")

@bignimbus bignimbus added this to the Release 3.8 milestone Feb 6, 2023
Copy link
Member

@alessbell alessbell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the background! Excited to have people try out the PR/alpha build with these changes 😄

@benjamn benjamn changed the title Attempt to simplify __DEV__ polyfill to use imports instead of global scope Simplify __DEV__ polyfill to use imports instead of global scope Feb 7, 2023
@benjamn benjamn merged commit 3a1369c into release-3.8 Feb 7, 2023
@benjamn benjamn deleted the attempt-to-simplify-__DEV__ branch February 7, 2023 16:02
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 10, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants