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

support minify() output as AST #1878

Merged
merged 1 commit into from
May 7, 2017
Merged

Conversation

alexlamsl
Copy link
Collaborator

@alexlamsl alexlamsl commented May 7, 2017

  • options.output.ast (default false)
  • options.output.code (default true)

@kzc this is from your #1877 (comment)

@alexlamsl
Copy link
Collaborator Author

@kzc
Copy link
Contributor

kzc commented May 7, 2017

output: "ast"

How would the following output scenarios be supported?

  1. output AST, output code string
  2. do not output AST, do not output code string
  3. output AST, do not output code string
  4. do not output AST, output code string

@kzc
Copy link
Contributor

kzc commented May 7, 2017

I'd prefer the output: { ast: true, code: false } syntax because the comparisons are faster at the output level - not relying on string comparisons or regexes, but it's up to you. As long as the 4 options above are supported. Keep in mind that outputting code strings is frequently called in compress.js to choose the shorter length code string.

@alexlamsl
Copy link
Collaborator Author

How would the following output scenarios be supported?

I thought about supporting those, but take options.output.comments for instance - if we output both code and AST, they won't reflect each other because the code would have (some) comments dropped whereas the AST would still have them.

@kzc
Copy link
Contributor

kzc commented May 7, 2017

but take options.output.comments for instance

I can live with the AST/code mismatch in that scenario. Comments are a whole different problem unto themselves.

@alexlamsl
Copy link
Collaborator Author

Keep in mind that outputting code strings is frequently called in compress.js to choose the shorter length code string.

The way this PR is done, output:"ast" completely bypasses OutputStream, so it won't affect AST_Node.print_to_string() which is used within lib/compress.js in way you described.

@kzc
Copy link
Contributor

kzc commented May 7, 2017

it won't affect AST_Node.print_to_string() which is used within lib/compress.js in way you described.

Okay, as long as it does not incur any output cost for AST_Node.print_to_string() I'm good with it. Just have to support the 4 scenarios in #1878 (comment)

@alexlamsl
Copy link
Collaborator Author

I can live with the AST/code mismatch in that scenario. Comments are a whole different problem unto themselves.

In that case, we can just output both ast and code all the time. There isn't any extra CPU memory usage anyway.

@kzc
Copy link
Contributor

kzc commented May 7, 2017

we can just output both ast and code all the time. There isn't any extra CPU memory usage anyway.

No, no, no. That would mean that the AST cannot be released in most common minify() scenarios causing more memory use. We only want to return result.ast from minify() when requested.

@alexlamsl
Copy link
Collaborator Author

Ah wait, what I meant to say is we don't need ast:true but we do need code:false, because if we only want to parse() then OutputStream can be bypassed completely without any runtime overhead.

@alexlamsl
Copy link
Collaborator Author

That would mean that the AST cannot be released in most common minify() scenarios causing more memory use.

True, VMs do optimise memory usage quite aggressively. I'll update the PR with your proposal then.

@kzc
Copy link
Contributor

kzc commented May 7, 2017

I think the minify() output options { ast : boolean, code : boolean } would cover all bases in the simplest way. Obviously ast would default to false, and code would default to true.

- `options.output.ast` (default `false`)
- `options.output.code` (default `true`)
@alexlamsl
Copy link
Collaborator Author

@kzc agreed - PTAL at 170db9b

@kzc
Copy link
Contributor

kzc commented May 7, 2017

As of your latest PR commit, we no longer need to export _parse and _Compressor, correct?

@kzc
Copy link
Contributor

kzc commented May 7, 2017

PTAL at 170db9b

looks good

@alexlamsl
Copy link
Collaborator Author

As of your latest PR commit, we no longer need to export _parse and _Compressor , correct?

After removing that test under test/mocha/minify.js, yes. I am planning to do so in #1877 after this PR merges.

@kzc
Copy link
Contributor

kzc commented May 7, 2017

If the CLI bin/uglifyjs happens to set -b ast=true, there's no harm done right?

@alexlamsl
Copy link
Collaborator Author

$ echo 'console.log("PASS")' | node bin/uglifyjs -cmb ast
console.log("PASS");
$ echo 'console.log("PASS")' | node bin/uglifyjs -cmb ast=0
console.log("PASS");
$ echo 'console.log("PASS")' | node bin/uglifyjs -cmb code
console.log("PASS");

However,

$ echo 'console.log("PASS")' | node bin/uglifyjs -cmb code=0
undefined

@kzc
Copy link
Contributor

kzc commented May 7, 2017

$ echo 'console.log("PASS")' | node bin/uglifyjs -cmb code=0
undefined

That makes sense - it was requested. As long the bash program return value is still 0 indicating success.

@kzc
Copy link
Contributor

kzc commented May 7, 2017

Eventually I'd like to see the dump uglify AST patch integrated into the CLI with the -b ast option. The only thing the prevented its inclusion was the inefficiency introduced by the overhead of adding _class to every AST node instance which we do not want in default use.

@alexlamsl
Copy link
Collaborator Author

The only thing the prevented its inclusion was the inefficiency introduced by the overhead of adding _class to every AST node instance which we do not want in default use.

I just had a brief visit at #769 - what does ._class do on top of .prototype.TYPE?

@kzc
Copy link
Contributor

kzc commented May 7, 2017

what does ._class do on top of .prototype.TYPE?

They're essentially the same except that ._class has an AST_ prefix. But the point was to output the class name as JSON for every AST node, which is not done for TYPE.

Edit: and to make _class the first entry for each object.

@kzc
Copy link
Contributor

kzc commented May 7, 2017

@alexlamsl If you do merge some variant of the dump AST patch into the CLI, please apply these changes: #769 (comment)

@alexlamsl
Copy link
Collaborator Author

Okay, in that case we can safely eliminate that since we have exports["AST_" + type] = ctor; so we know that prefix is constant.

I read the PR and my first reaction was "why isn't this done as JSON.stringify(ast, replacer)" - I agree with your suggestion of preserving order of properties, as they do convey semantics somewhat.

@kzc
Copy link
Contributor

kzc commented May 7, 2017

I read the PR and my first reaction was "why isn't this done as JSON.stringify(ast, replacer)"

By all means - whatever works to not introduce more AST object overhead.

@alexlamsl alexlamsl merged commit e547483 into mishoo:master May 7, 2017
@alexlamsl alexlamsl deleted the minify-ast branch May 7, 2017 18:12
@alexlamsl alexlamsl mentioned this pull request May 7, 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.

2 participants