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

How to simulate 'if debug' without changing source code (using gulp&webpack) #16

Closed
ron23 opened this issue Mar 10, 2016 · 6 comments
Closed
Labels

Comments

@ron23
Copy link

ron23 commented Mar 10, 2016

Hi,
So i'm trying to use this GREAT tool but I don't want to change the source code, and I want it to work only in debug mode. I'm using gulp and webpack to build.
Any ideas, recommendations, examples?
Thanks!

@colbyr
Copy link
Contributor

colbyr commented Mar 11, 2016

@ron23 you could use webpack.DefinePlugin to create a condition that will be allowed to install immutable-devtools in the debug build but unreachable in the production build.

// webpack.config.js
var webpack = require('webpack');
module.exports = {
  // ...
  plugins: [
    new webpack.DefinePlugin({
      __DEV__: JSON.stringify(process.env.NODE_ENV !== 'production')
    })
  ],
};

In your source you'd have something like this...

// index.js
var immutable = require('immutable');
if (__DEV__) {
  var installDevTools = require('immutable-devtools');
  installDevTools(immutable);
}

When you set NODE_ENV=production, __DEV__ will be replaced with false.

NODE_ENV=production webpack index.js dist/index.min.js

generates

var immutable = require('immutable');
if (false) {
  var installDevTools = require('immutable-devtools');
  installDevTools(immutable);
}
// ...

And then by adding the -p shortcut to webpack for production mode which enables dead code elimination, the condition that requires immutable-devtools will be removed.

NODE_ENV=production webpack -p index.js
var immutable = require('immutable');
// ...

@ron23
Copy link
Author

ron23 commented Mar 11, 2016

Nice! I thought about everything you said but I didn't know the magic -p !!! Thanks a lot!

@colbyr
Copy link
Contributor

colbyr commented Mar 11, 2016

@ron23 you're welcome! You can also get dead code elimination by adding the uglifyjs plugin new webpack.optimize.UglifyJsPlugin() to the plugins array in your webpack.config.js if you want to tune your production build.

@ron23
Copy link
Author

ron23 commented Mar 11, 2016

Already using it, just didn't know about the dead code removal feature. Thanks a bunch!

@langpavel
Copy link
Collaborator

Hi everybody!
This is something that Wiki is interested in :-)
Can anybody write shot paragraph about this in project wiki?

@langpavel
Copy link
Collaborator

@ron23 Thanks! I've added section Using with webpack in Readme based on your comment

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

No branches or pull requests

3 participants