简体   繁体   中英

javascript minification that removes licenses?

Javascript minification will generally remove all comments from source. This means that license information is also removed.

If I run a big site, and I want to abide by licenses, does that mean that I cannot use automatic minification? There is no option to "preserve the first comment, but not the others " right?

I ask because a rather big company has used some code I wrote and not included my (MIT) license, but before I get all peeved, I want to put myself in their shoes.

edit :
It seems that as the author the burden is on me to comment the license in such a way (see answers) that it does not get removed during the minification. I am OK with doing that first, and then raising a stink if my license is still removed

This is a classic case of "it depends".

It depends on the minifier. For example UglifyJS will leave in the initial comment block in a file unless you specifically tell it not to with the -nc flag. Others (like Google's Closure Compiler) look for a special annotation (eg, @license or @preserve ). Some respect "loud" comments (eg, /*! ... */ ) while others don't.

It depends how you minify. Some organization are aggressively minifying and concatenating everything to squeeze out every byte. Others are adding the licenses in source control and "leaving them in" during minifying. Others are applying license files after everything has been concatenated together.

It can be easy for some place to drop a license in their production builds, especially if they're aggressively concatenating every library they use into a single JS file. They may not even know that they're doing it. Your best bet here is probably just to reach out and ask them to make sure that they're respecting the terms of the license and leave the license text on there -- they may not even realize that they're doing it.

No! if you used following comment style with YUIcompressor:

/*!
 *
 */

The exclamation tells the compressor to retain the comment.

Here is Documented Notes for these comments

The Google Closure Compiler supports @preserve and @license annotations that tell it to keep the comment:

/**
 * @preserve Copyright 2009 SomeThirdParty.
 * Here is the full license text and copyright
 * notice for this file. Note that the notice can span several
 * lines and is only terminated by the closing star and slash:
 */

UglifyJS supports a command-line option, --comments , that accepts a regex. Any comments matching the regex will be preserved in the output.

Other answers have shown that other minifiers also support this feature. You will have to read the documentation for your chosen minifier to determine the exact syntax/technique to use in your own case.

You could use node's r.js .

Here it is described how to use it

Sample output

Or wro4j that also preserves licence comments.

License information can be preserved. For example, YUI compressor can do it:

C-style comments starting with /*! are preserved. This is useful with comments containing copyright/license information.

( Documentation )

It depends of your minifying tool

/** Notice 
  *
  *
**/

or

/*!
 *
 *
 */

Look at your tool documentation

It depends on the minifier tool/API used. Though important comments can be left in the minifier output if the tool supports it. Following is the format for important comments

/*!
 * Your comment...1
 * ......
 * Your comment...n
 */

Following simple online tool which can be used to minify javascript code, which I found preserves the important comments :

http://utilninja.com/Computing/JavascriptMinifier

A few years later but...

Except for minifiers that eliminate dead code you could encode a reference to the license in something that isn't a comment with a namespace prefix.

For JS something like;

var NS_LICENSE_URL = "Cool Inc. - Apache 2.0 - http://github.com/nfisher/cooljs/LICENSE.txt";

For CSS something like;

.ns_license_url {
  content: "Cool Inc. - Apache 2.0 - http://github.com/nfisher/cooljs/LICENSE.txt";
}

Alternatively you could copy the whole license in. Could also add a console.out for JS to print it to the console but that might be overstepping.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM