Skip to content
This repository has been archived by the owner on Dec 16, 2021. It is now read-only.

Remove process/browser.js #403

Closed
lukeed opened this issue Jul 14, 2017 · 6 comments
Closed

Remove process/browser.js #403

lukeed opened this issue Jul 14, 2017 · 6 comments

Comments

@lukeed
Copy link
Member

lukeed commented Jul 14, 2017

In continuation of #285, I found that preact-compat was forcing Webpack to include a process shim.

I was able to remove it locally by changing:

var DEV = typeof process==='undefined' || !process.env || process.env.NODE_ENV!=='production';

to this:

var DEV = process.env.NODE_ENV!=='production';

I can open a PR for this if you don't see a problem with it? Everyone & their mother uses some kind of bundler now, and all bundlers have a replaceText or DefinePlugin capability.

For example, with webpack, I did this, the oldest trick in the book:

new webpack.DefinePlugin({
  'process.env.NODE_ENV': JSON.stringify(isProd ? 'production' : 'development')
}),

Before:

screen shot 2017-07-13 at 6 49 59 pm

After:

screen shot 2017-07-13 at 6 53 34 pm


Worth noting that the DefinePlugin trick does not work unless this change is made. This is because instances of 'process' are found, thus loading the polyfill.

lukeed added a commit to lukeed/preact-compat that referenced this issue Jul 17, 2017
lukeed added a commit to lukeed/preact-compat that referenced this issue Aug 17, 2017
@kaisermann
Copy link
Contributor

I'm currently facing the same issue

@lukeed
Copy link
Member Author

lukeed commented Apr 9, 2018

@developit Thoughts? Seems like a simple fix. Also forgot this was still here

I can take care of it but I don't have collab on this repo

@kaisermann
Copy link
Contributor

Just to document it here: preact-cli doesn't mock process, making any app built with it to execute the prop-types check even in production.

@developit
Copy link
Member

developit commented Apr 18, 2018

This also affects people using the browser version of preact-compat, where process is undefined.
Problem is that a try/catch block doesn't get dead-code-eliminated by Uglify :(

@kaisermann
Copy link
Contributor

kaisermann commented Apr 18, 2018

🤔🤔🤔🤔🤔

What if we inverted the DEV conditional to:

const DEV = typeof process !== 'undefined' && process.env && process.env.NODE_ENV!=='production'

In browsers, it will fail at typeof process !== 'undefined'.

In bundlers, after replacing the process.env.NODE_ENV with the actual environment name, uglify should be able to catch the dead code.

development

const DEV = typeof process !== 'undefined' && process.env && 'development'!=='production'
// true

production

const DEV = typeof process !== 'undefined' && process.env && 'production'!=='production'
// false

@kaisermann
Copy link
Contributor

Or, maybe, even shorter:

const DEV = typeof process !== 'undefined' && process.env.NODE_ENV!=='production'

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants