-
Notifications
You must be signed in to change notification settings - Fork 680
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 for license comments /*! license */ #332
Conversation
/*! license */ These will be new elements in the AST, and will always be generated into the minified code. I also pass the comments and line numbers out of the parser, by adding extra properties to the AST arrays. This is useful to those using the parser without the minifier.
Due to an extra
|
Hmm. Yes, good point. Wondered whether that next() would affect anything. |
This might break the bin implementation which already extracts comments and then readds them. https://github.com/mishoo/UglifyJS/blob/master/bin/uglifyjs#L265 |
I think it's compatible with show_copyight. Doesn't cause me errors with a few simple test cases. (Sorry for weird last commit message. Forgot !s didn't work in commit msgs.) |
@@ -258,6 +258,12 @@ function JS_Parse_Error(message, line, col, pos) { | |||
this.col = col + 1; | |||
this.pos = pos + 1; | |||
this.stack = new Error().stack; | |||
try { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What does this have to do with the pull request?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nothing. It just reports errors with context, which makes it far far easier to debug this sort of thing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't you then remove line 260?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've removed this, for a cleaner pull request.
A huge +1 for this functionality being added in! |
Another +1... I'm using @kennethkufluk's fork in the meantime (thanks!). |
Another +1 -- this makes UglifyJS more suitable for people wanting to use the "official" (1) way to embed their license. (1) jQuery does it, so it must be "the right way". ;-) |
Unfortunately adding the license to the AST means that it breaks contextual minifications that look around using in.js /*! License 1 */
/*! Multiline
License 2
*/
var a=1;
/*! License 3 */
var b=1;
var a=1,b=1; mishoo/master $ uglifyjs -nc in.js var a=1,b=1; kennethkufluk/master $ bin/uglifyjs -nc -nl in.js var a=1;var b=1; The new |
@mal that makes sense, and is the behavior I'd expect. The most common use-case for preserving inline license comments assumes they are at the top of individual files concatenated together into a single script, in which case each sub-script will most likely be inside an IIFE anyways. |
@mal Hmm, good catch. |
@cowboy I agree with the common use-case, however there's still some loss with IIFEs; consider: /*! License 1 */
(function(){var a=1;})();
/*! License 2 */
(function(){var b=1;})(); mishoo/master (function(){var a=1})(),function(){var a=1}(); kennethkufluk/master (function(){var a=1})();(function(){var a=1})(); |
@mal I still think the license should be in the AST, to prevent loss & confusion during mangling. I guess I could add exceptions to the next, prev and peek methods? Do you think that would work? |
@kennethkufluk In theory ignoring non-code AST nodes unless processing the node itself should resolve that particular problem, but there's other perils associated with having non-code items as first class nodes in the AST: Function Hoisting /*! License goes walk about with no IIFE */
function a () {}
var b; function a(){}/*! License goes walk about with no IIFE */var b; Sequences var a=1,
/*! Very suspiciously placed license comment kills the parser */
b=1; DEBUG: Error
at new JS_Parse_Error (/home/mal/code/js/UglifyJS/lib/parse-js.js:260:22)
... |
@mal Yes, I see the problem. I always knew that badly placed licenses would cause havoc. You can put a comment pretty much anywhere. Maybe we need to exclude badly-placed licenses. But, hey, licenses are not hard to place. Maybe the solution is to simply not AST them if --no-licenses is enabled, solving the issue for those that don't care. |
FWIW, keeping |
In an ideal world I think we'd just keep the For now though @kennethkufluk's suggestion of ignoring weird license placements sounds like the best short term solution. Potential rules being something like: they must be in the |
Alternatively; another, much simpler, solution that I imagine solves most use-cases (command line only) is to add documentation for, and make use of, the |
Adding complex rules that dictate where to-be-preserved comments need to be located might be overly complex. What if you just documented that comment nodes between statements that would otherwise be combined will prevent them from being combined? |
RT @cowboy
|
@cowboy Agreed, documentation > complex rules, but at a minimum weirdly placed comments should not be allowed to crash the parser. |
@mal fair enough! |
@mal In "compiled" version of var Sizzle =
/*!
* Sizzle CSS Selector Engine v2.2.0-pre
* http://sizzlejs.com/
*
* Copyright 2008, 2014 jQuery Foundation, Inc. and other contributors
* Released under the MIT license
* http://jquery.org/license
*
* Date: 2014-12-16
*/
(function( window ) {
// ...
}); At the same time, Well, to be honest I don't think preserving comments can be done in a reasonable way without some insight into It's been three years. Do you guys plan to merge this PR someday? |
@mal The examples you provided are not going to happen in real life scenarios. Really. |
This entire project has been superceeded by https://github.com/mishoo/UglifyJS2 ... |
@mal Ah, ok ... thank you for the information! |
Yeah, I got stuck here too. Simply switching to |
Adding support for license statements marked like this:
/*!
license
*/
These will be new elements in the AST, and will always be generated into the minified code.
This fixes #85, #306, some of #275, maybe some others.
I also pass the comments and line numbers out of the parser, by adding extra properties to the AST arrays.
This is useful to those using the parser without the minifier.