FormidableLabs / babel-plugin-transform-define

Compile time code replacement for babel similar to Webpack's DefinePlugin
MIT License
245 stars 31 forks source link

support falsy replacement values (includes tests) #33

Closed jedwards1211 closed 7 years ago

jedwards1211 commented 7 years ago

the current version doesn't perform replacements with falsy values because getFirstReplacementValueForNode returns get(obj, replacementKey) || null, and processNode does if (replacement).

This change uses lodash.has to check if the replacementKey is present in replacements, and if so, performs the replacement -- even if it's null, 0, '', false, NaN, or undefined.

joeyfigaro commented 7 years ago

Any chance of getting this merged?

jedwards1211 commented 7 years ago

Looks like @baer has a life 😎

baer commented 7 years ago

OMG! I am so sorry - thank you for pinging me. Every once in a while I miss a GH notification and this happens. I'll have a look tonight. Thank you so much for your contribution to the project.

jedwards1211 commented 7 years ago

@baer no worries, thanks!

baer commented 7 years ago

Ah, CI is failing on a lint error:

/home/travis/build/FormidableLabs/babel-plugin-transform-define/src/index.js
  4:7   error  A space is required after '{'   object-curly-spacing
  4:22  error  A space is required before '}'  object-curly-spacing
✖ 2 problems (2 errors, 0 warnings)
jedwards1211 commented 7 years ago

@baer whoops! I run lint in a precommit script (via husky) to avoid making those mistakes in my own projects. It's fixed now

baer commented 7 years ago

Will cut a release shortly.

jedwards1211 commented 7 years ago

@baer great, thanks!

minheq commented 7 years ago

Hi, I don't really understand how to actually use the false value if I needed to, because webpack's DefinePlugin actually allows this. Using this plugin though:

config.js file

module.exports = {
  PRODUCTION: false
};

with this the application throws an error of undefined because it does not get replace with false. any idea?

jedwards1211 commented 7 years ago

@minheq this is useful in situations where you don't want to use Webpack, for instance when you're running code on the server side via babel-node. It looks like @baer hasn't actually released this, so if you just installed from npm it's no surprise you would see that error. Replacing with false should only work if you use the code from this PR, which has not been released to npm.

minheq commented 7 years ago

@jedwards1211 Thanks for explanation, very much needed PR :) Pinging @baer to release it to npm ^^

remyrylan commented 7 years ago

@baer - any chance of getting 1.2.1 (with this merged included) pushed to npm soon?

baer commented 7 years ago

@jedwards1211 - After a bit of back and forth with the NPM (the package was broken) we got everything straightened out and published. Thanks again for your contribution. 1.3.x is live.

//cc @jrylan