[WIP] allow global_defs to pass in objects and arrays#1467
[WIP] allow global_defs to pass in objects and arrays#1467alexlamsl wants to merge 1 commit intomishoo:masterfrom
Conversation
| @@ -1,29 +0,0 @@ | |||
| do_not_update_lhs: { | |||
There was a problem hiding this comment.
This is the only file with "extensive" testing for global_defs before (there is one in test/compress/reduce_vars.js as well).
I was hoping git will pick this up as a file rename, but this is basically copied verbatim to test/compress/global_defs.js with added evaluate:true.
| this.walk(new TreeWalker(function(node){ | ||
| if (node instanceof AST_SymbolRef) { | ||
| var d = node.definition(); | ||
| if (d && d.init) { |
| this._evaluating = true; | ||
| try { | ||
| var d = this.definition(); | ||
| if (d && (d.constant || compressor.option("reduce_vars") && !d.modified) && d.init) { |
| // } | ||
| // })).transform(compressor); | ||
|
|
||
| if (val instanceof AST_Node) return val.transform(compressor); |
|
Could you please add the positive and negative test case scenarios I mentioned in #1198? |
|
What happens in a mixed LHS and RHS scenario? Will all DEBUG replacements be disallowed with an appropriate warning? Or at the very least a fatal error thrown upon first attempt to change a LHS expression. |
|
This PR breaks existing CLI usage: uglify-js@2.7.5: this PR: Could you please add a mocha test for the CLI Also, how to set compress Would prefer if existing test files are not renamed. That way it is easier to discern if the tests' behavior has been changed. |
Unfortunately that would replace the RHS cases (and leave the LHS ones alone) - this is the same behaviour as before this PR though. Investigating the command-line breakage at the moment, will add mocha and those other tests as you mentioned above. Then I'll ponder about the mixed case and see how to go about fixing it. (or may be open another PR?) |
The current behavior is not great in this regard. Would prefer it to throw a fatal exception at the first attempt to set a LHS variable so the user is aware that their code must be fixed. |
Understood - you want this to be in the same PR or separate? |
|
You can make the fixes in this PR I think. Can you please add a [WIP] prefix? |
On second thought, a warning for LHS variable use would be better. Changing to it a fatal exception would be a breaking change unfortunately. There's probably use of mixed RHS/LHS use in the wild. By the way it's possible to test for warnings in the test framework. See: |
test/mocha/cli.js
Outdated
| }); | ||
| }); | ||
| it("Should work with --define", function (done) { | ||
| var command = uglifyjscmd + ' test/input/issue-1467/sample.js --define D=5 -cm'; |
There was a problem hiding this comment.
I think -cm could be replaced with -c. I only put mangle in the post out of habit.
There was a problem hiding this comment.
Indeed. I type faster than I think. 😅
|
Incorporated those tests you've mentioned above except the first one, which is a mixed LHS/RHS case. I shall now work on that mixed case and emit warnings as requested. 😉 |
I'd like to see a mixed use LHS/RHS test case with Edit: I should better read your comments...
|
|
|
FWIW, |
test/compress/global_defs.js
Outdated
| function f(CONFIG) { | ||
| return CONFIG.VALUE; | ||
| } | ||
|
|
There was a problem hiding this comment.
Could you please remove the empty lines in the test so it's easier to view the test in its entirety on screen at once?
|
From a CLI usability point of view, this is awkward: The Users would expect to use it in this way: with it producing: Think of the original use case of replacing |
|
That is the use case I have in mind, though I use it via API. Your case about
|
I see. Could you document that in the README? It's not an obvious association. |
Will do 👍 With regards to global_defs: {
process: {
env: { NODE_ENV: "production" }
}
}to be more natural in the API form? |
|
I'm not sure. Webpack apparently supports both: new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV || 'development')
}),and new webpack.DefinePlugin({
"process.env": {
NODE_ENV: JSON.stringify("production")
}
});Can you do something similar - assume dots in keys imply parent objects? |
|
@kzc feel free to suggest anything to expand on |
|
Your README addition looks fine. |
f214d9b to
80a80be
Compare
|
With latest PR did not see any warnings for LHS defines: |
|
Should be all fixed up now. |
|
(Travis CI timed out.) |
|
Just checked 84d3d5a - |
|
Not directly relevant to this PR per se, but there are code out there which uses IIFEs to capture global variables: (function(window) {
// mian program code
})(window);Which aids mangling, but will be opaque to |
|
Warning is incorrect here in latest PR: |
|
Just curious how we might achieve replacing a symbol name with another symbol name... String replacement "works" in a sense: but how can we achieve the following output? |
|
Sorry - let's try that again... With this PR the following does not work: I recognize it does not work in |
84d3d5a to
4fbd38f
Compare
|
@kzc sorry for the botched fix - please try this second attempt after some sleep. Meanwhile I'll look at the case above. |
4fbd38f to
0f61e2c
Compare
|
Swapping {
global_defs: {
println: UglifyJS.parse("console.log").body[0].body
}
}Any better ideas? Also, /me already thinks |
|
Speaking of the devil, |
0d3b8db to
979b11d
Compare
- 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
979b11d to
263fb4c
Compare
|
@kzc so I think about it some more on this Would you mind elaborating on the general use case of this feature? I'm trying to gauge the scope of work to see if this is straightforward or I should branch it to another PR. |
|
Another thing I'm thinking about for future work is to allow functions to {
global_defs: {
require: function(path) {
if (path == "./config") return { env: "production", isNode: false }
throw this;
}
}
}Which will also work on command line as well. |
|
I thought substituting functions would be a nice to have. I appreciate it's non-trivial and involves creating nodes. It's not essential. |
|
Closing in favour of #1469. |
This is an alternative approach to #1198 which is based upon #1425 to fix #1416. We extend
global_defsto allow object as input values, which it would previously throw an error upon compression.With this PR
global_defsnow requiresevaluate(trueby default) for substitutions to take place. Also theevaluate()caching that was implemented in #1425 is now used in general.