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

Demonstrate how to replace __DEV__ with false in production builds. #51

Merged
merged 2 commits into from
Jul 29, 2021

Conversation

benjamn
Copy link
Member

@benjamn benjamn commented Jun 4, 2021

When using a version of @apollo/client with apollographql/apollo-client#8347 applied, this configuration saves a whopping 3.73kB of minified+gzip code (due to removing development-only code) when running npm run build to generate an optimized production build, without requiring CRA ejection.

@hwillson @brainkim If we land apollographql/apollo-client#8347 for Apollo Client v3.4, this configuration will most likely be what we recommend to Create React App folks, so I'm open to any ways we might be able to simplify it further.

@DennieMello
Copy link

After installing version 3.4 in Next Js, when building and running the application, the following errors occur. How can you fix them?

Снимок экрана 2021-07-29 в 00 09 21

When using a version of @apollo/client with apollographql/apollo-client#8347 applied, this configuration saves a whopping 3.73kB of minified+gzip code (due to removing development-only code) when running npm run build to generate an optimized production build, without requiring CRA ejection.

@hwillson @brainkim If we land apollographql/apollo-client#8347 for Apollo Client v3.4, this configuration will most likely be what we recommend to Create React App folks, so I'm open to any ways we might be able to simplify it further.

@benjamn
Copy link
Member Author

benjamn commented Jul 29, 2021

@DennieMello Any chance you can reproduce this problem without minification? I think I recognize the admit stack frame on top, but it would be great to know the rest.

It looks like window.__DEV__ does end up getting defined, but perhaps __DEV__ is used for the first time before it's defined.

As a workaround, you should be able to set window.__DEV__ yourself before importing anything from @apollo/client.

@benjamn benjamn force-pushed the configure-__DEV__-minification branch from 17a7649 to 5c27627 Compare July 29, 2021 17:12
@benjamn benjamn merged commit 3a071a0 into main Jul 29, 2021
@benjamn benjamn deleted the configure-__DEV__-minification branch July 29, 2021 17:14
@benjamn
Copy link
Member Author

benjamn commented Jul 29, 2021

I think this problem may be fixed by apollographql/apollo-client#8558 (coming soon).

@benjamn
Copy link
Member Author

benjamn commented Jul 29, 2021

@DennieMello Alright, if this is indeed the same problem, it should be fixed in @apollo/client@3.4.1 (just published). Thanks for being the first to let us know about this problem!

@DennieMello
Copy link

@DennieMello Alright, if this is indeed the same problem, it should be fixed in @apollo/client@3.4.1 (just published). Thanks for being the first to let us know about this problem!

Thank you for such a quick response! Problem solved, everything works well

@tubbo
Copy link

tubbo commented Aug 3, 2021

I'm using Webpack 5 and Electron, but I still see this __DEV__ error happening when I apply the configuration from this PR.

Here's my plugins:

[
  new ForkTsCheckerWebpackPlugin(),
  new ProvidePlugin({
    process: 'process/browser',
  }),
  new DefinePlugin({
    'process.env': JSON.stringify({ ...DEFAULT_CONFIG, ...process.env }),
    __DEV__: JSON.stringify(process.env.NODE_ENV !== 'production'),
  }),
]

But I can't get past this error:

Screen Shot 2021-08-03 at 4 10 34 PM

@benjamn
Copy link
Member Author

benjamn commented Aug 3, 2021

@tubbo Any chance you can capture that in a runnable reproduction, in a new issue?

That error doesn't look quite the same as

ReferenceError: __DEV__ is not defined

from above, so I'd like to understand what's really happening. Thanks!

benjamn added a commit that referenced this pull request Aug 3, 2021
Although a string will be handled correctly (as a code fragment), using
JSON.stringify only seems to be necessary when the original value is a
string: https://webpack.js.org/plugins/define-plugin/#usage

I verified with `npm run build` that this change has no effect on the
total bundle size of the production build.

Follow-up to #51.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants