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

Enable the --define and (global_defs) option to define contents of random variables. #1198

Closed
wants to merge 1 commit into from

Conversation

fabiosantoscode
Copy link
Contributor

Sometimes your "debug" flag may not be a global variable, it could be a property of a global variable. So I'm sending this PR to allow --define 'globalVariable.foo.bar=false' as well as just --define globalVariable=false.

The reasoning behind building this was to support React and common React code which uses if (process.env.NODE_ENV !== "production") guards all the time for development-only code. I found some inspiration in webpack's DefinePlugin for the interface, and started implementing.

@avdg
Copy link
Contributor

avdg commented Jul 9, 2016

So this pr has also support for the squared bracket notation as well. Maybe the documentation should say that javascript style array reference is allowed instead of just saying "oh, you can do this too (but silently don't say what is fine and what is not)".

I like the idea for this pr in general though.

@kzc
Copy link
Contributor

kzc commented Jul 9, 2016

What if the define expression is on the left hand side?

$ echo 'D = 27; console.log(D);' | bin/uglifyjs --define D=5 -cm
D=27,console.log(5);
$ echo 'D.E = 27; console.log(D.E);' | bin/uglifyjs --define D.E=5 -cm
5=27,console.log(5);

Or if the define expression is referenced indirectly?

$ echo 'var d = D; console.log(D.E, d.E);' | bin/uglifyjs --define D.E=5 -cm
var d=D;console.log(5,d.E);

@avdg
Copy link
Contributor

avdg commented Jul 9, 2016

The compressor has some part of code that test if a node evaluates to a const. Maybe that code can be used to evaluate the expression. Although one can think "Why stop at constants and inject javascript while at it..."

@kzc
Copy link
Contributor

kzc commented Jul 10, 2016

The compressor has some part of code that test if a node evaluates to a const. Maybe that code can be used to evaluate the expression.

globalVariable.foo.bar would not be considered to be a constant expression, so evaluate() wouldn't be applicable here.

@kzc
Copy link
Contributor

kzc commented Jul 10, 2016

@fabiosantoscode It's good that this PR distinguishes between a local foo.bar and a global foo.bar for the code:

function f(foo) { return foo.bar; }
console.log(f({bar:3}), foo.bar);
$ echo 'function f(foo){return foo.bar;} console.log(f({bar: 3}), foo.bar);' | bin/uglifyjs --define foo.bar=5 -c | node
3 5

Could you add such a test to this PR to ensure no future regressions?

@kzc
Copy link
Contributor

kzc commented Jul 10, 2016

Here's another variation that also works correctly:

function f() {
    var foo = { bar: 2 };
    return foo.bar;
}

function g() {
    return foo.bar;
}

console.log(f(), g(), foo.bar);
$ echo 'function f(){var foo={bar:2};return foo.bar}function g(){return foo.bar}console.log(f(),g(),foo.bar);' | bin/uglifyjs --define foo.bar=5 -c | node
2 5 5

That could be added as a test as well.

alexlamsl added a commit to alexlamsl/UglifyJS that referenced this pull request Feb 6, 2017
- no longer throw errors
- guarded by `evaluate` and `unsafe`

fixes mishoo#1416
closes mishoo#1198
alexlamsl added a commit to alexlamsl/UglifyJS that referenced this pull request Feb 6, 2017
- no longer throw errors against objects
- now depends on `evaluate`
- `unsafe` when using objects
- support `"a.b":1` on both cli & API
- emit warning if variable is modified

fixes mishoo#1416
closes mishoo#1198
alexlamsl added a commit to alexlamsl/UglifyJS that referenced this pull request Feb 7, 2017
- no longer throw errors against objects
- now depends on `evaluate`
- `unsafe` when using objects
- support `"a.b":1` on both cli & API
- emit warning if variable is modified

fixes mishoo#1416
closes mishoo#1198
alexlamsl added a commit to alexlamsl/UglifyJS that referenced this pull request Feb 7, 2017
- no longer throw errors against objects
- now depends on `evaluate`
- `unsafe` when using objects
- support `"a.b":1` on both cli & API
- emit warning if variable is modified

fixes mishoo#1416
closes mishoo#1198
alexlamsl added a commit to alexlamsl/UglifyJS that referenced this pull request Feb 7, 2017
- support arrays, objects & AST_Node
- support `"a.b":1` on both cli & API
- emit warning if variable is modified

fixes mishoo#1416
closes mishoo#1198
alexlamsl added a commit to alexlamsl/UglifyJS that referenced this pull request Feb 9, 2017
- support arrays, objects & AST_Node
- support `"a.b":1` on both cli & API
- emit warning if variable is modified
- nested definitions override top-level variables

fixes mishoo#1416
closes mishoo#1198
alexlamsl added a commit to alexlamsl/UglifyJS that referenced this pull request Feb 11, 2017
- support arrays, objects & AST_Node
- support `"a.b":1` on both cli & API
- emit warning if variable is modified
- override top-level variables

fixes mishoo#1416
closes mishoo#1198
alexlamsl added a commit to alexlamsl/UglifyJS that referenced this pull request Feb 11, 2017
- support arrays, objects & AST_Node
- support `"a.b":1` on both cli & API
- emit warning if variable is modified
- override top-level variables

fixes mishoo#1416
closes mishoo#1198
alexlamsl added a commit to alexlamsl/UglifyJS that referenced this pull request Feb 18, 2017
- support arrays, objects & AST_Node
- support `"a.b":1` on both cli & API
- emit warning if variable is modified
- override top-level variables

fixes mishoo#1416
closes mishoo#1198
alexlamsl added a commit to alexlamsl/UglifyJS that referenced this pull request Feb 18, 2017
- support arrays, objects & AST_Node
- support `"a.b":1` on both cli & API
- emit warning if variable is modified
- override top-level variables

fixes mishoo#1416
closes mishoo#1198
closes mishoo#1469
@alexlamsl alexlamsl closed this in e275148 Feb 23, 2017
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