Inline process.browser for better code elimination #2583
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
↪️ Pull Request
Working on a project with server-side rendering I want to enable/ disable some code that should only run in a specific environment (browser or server). As I am using the middleware in development mode it was hard to rely on
process.env.BROWSER
because this is shared between the NodeJS application and the bundler for the client code (as far as I found out there is no way to pass an private/ non global environment variable at runtime to the bundler).The process module inserts a
process.browser
already adds the same information I passed as an environment before, but the dead code elimination was not deleated, therefore the file size grow a lot.Tersers
compress.global_defs
option also didn't work for me (not 100% sure why), therefore I checked the implementation forprocess.env
and implemented more or less the same forprocess.browser
.💻 Examples
🚨 Test instructions
I added some integration tests for this feature you can check out, further the example should give a good idea what should happen. On a real-world project, the code archived the same results as using
process.env.BROWSER
for me.✔️ PR Todo