For example, I have a
index.js file (copyrighted), I minify it using UglifyJS and store in
index.min.js file, but my copyright notice is removed, should I add it again in
If yes, Can I automate it?
If yes, How can I automate it?
For example, I have a
If the file is entirely your own work, without dependencies that aren't public domain, it's technically not required to preserve anything in the minified version. However, keeping a copyright notice in won't hurt anything, and is probably a good idea.
If the file contains work from others, you are bound by whatever license you received that work under. This will almost certainly include a copyright notice or other form of attribution. amon's answer explains how this can be done with UglifyJS specifically.
You should note that some licenses such as the GNU GPL family distinguish between distributing the source code for a work and other forms of a work. For example, the GPLv3 defines "source code" for a work as "the preferred form of the work for making modifications to it."
Yes, you should keep copyright and license notices in minified files as well. In case you haven't written the code entirely by yourself but have also included code under open source licenses (like MIT or BSD), you are legally required to preserve such notices. As an alternative to keeping the notices within the files, you could also have a separate page that contains open source license notices and attributions – but it would still be good to have a link to the attribution page within the JS file itself.
Per the UglifyJS documentation, it will preserve comments that contain a
@preserve annotation. Alternatively, you can pass a regex to the
--comments command line option to identify comments that should be retained in the output.
To obfuscate code? Stripping out comments could be sensible, but aside from that minification doesn't prevent anyone from un-minifying the code and reading it. If the code is open source anyway, this doesn't matter.
To reduce bandwidth? Minification is good as a first pass for compression. However, it doesn't replace actual compression algorithms. So keeping a license header is unlikely to produce noticeable overhead, especially if you bundle imports into a single minified file.