Repository navigation
UglifyJsPlugin: extract comments to separate file - #4134
Conversation
|
@sokra I added more tests and the coverage checks now succeed. |
|
@sokra I've just rebased this PR onto |
License comments use up a lot of space, especially when using many small
libraries with large license blocks. With this addition, you can extract
all license comments to a separate file and remove them from the bundle
files. A small banner points to the file containing all license
information such that the user can find it if needed.
We add a new option extractComments to the UglifyJsPlugin.
It can be omitted, then the behavior does not change, or it can be:
- true: All comments that normally would be preserved by the comments
option will be moved to a separate file. If the original file is
named foo.js, then the comments will be stored to foo.js.LICENSE
- regular expression (given as RegExp or string) or a function
(astNode, comment) -> boolean: All comments that match the given
expression (resp. are evaluated to true by the function) will be
extracted to the separate file. The comments option specifies
whether the comment will be preserved, i.e. it is possible to
preserve some comments (e.g. annotations) while extracting others or
even preserving comments that have been extracted.
- an object consisting of the following keys, all optional:
- condition: regular expression or function (see previous point)
- file: The file where the extracted comments will be stored. Can be
either a string (filename) or function (string) -> string which
will be given the original filename. Default is to append the
suffix .LICENSE to the original filename.
- banner: The banner text that points to the extracted file and will
be added on top of the original file. will be added to the
original file. Can be false (no banner), a string, or a function
(string) -> string that will be called with the filename where
extracted comments have been stored. Will be wrapped into comment.
Default: /*! For license information please see foo.js.LICENSE */
| case "boolean": | ||
| var b = condition[key]; | ||
| condition[key] = () => b; | ||
| case "function": // eslint-disable-line no-fallthrough |
There was a problem hiding this comment.
There should be a break before this line and the eslint comment removed.
| break; | ||
| } | ||
| condition[key] = new RegExp(condition[key]); | ||
| default: // eslint-disable-line no-fallthrough |
There was a problem hiding this comment.
Maybe solve this without fallthrough. It requires an additional line but is more readable..
| new SourceMapSource(stringifiedStream, file, JSON.parse(map), input, inputSourceMap) : | ||
| new RawSource(stringifiedStream)); | ||
| if(extractedComments.length > 0) { | ||
| let commentsFile = options.extractComments.file || file + ".LICENSE"; |
There was a problem hiding this comment.
extractComments.file -> extractComments.filename to be consistent with other webpack options.
| banner = banner(commentsFile); | ||
| } | ||
| if(banner) { | ||
| asset.__UglifyJsPlugin = compilation.assets[file] = new ConcatSource( |
There was a problem hiding this comment.
only write to asset.__UglifyJsPlugin = compilation.assets[file] = once in the UglifyJsPlugin. Use a local variable to keep the uglify result source and reassign this. Add asset.__UglifyJsPlugin = compilation.assets[file] = source to the end of the plugin.
|
@sokra thanks for your review, I added a commit to resolve all comments. |
|
Thanks. Also make sure to add a PR to the documentation |
What kind of change does this PR introduce?
feature in
UglifyJsPluginDid you add tests for your changes?
yes
If relevant, link to documentation update:
Not yet, I will create a documentation PR if you agree with this feature
Summary
License comments use up a lot of space, especially when using many small libraries with large license blocks. With this addition, you can extract all license comments to a separate file and remove them from the bundle files. A small banner points to the file containing all license information such that the user can find it if needed.
Does this PR introduce a breaking change?
No
Other information
This PR adds a new option
extractCommentsto the UglifyJsPlugin.It can be omitted, then the behavior does not change, or it can be:
true: All comments that normally would be preserved by thecommentsoption will be moved to a separate file. If the original file is namedfoo.js, then the comments will be stored tofoo.js.LICENSERegExporstring) or afunction (astNode, comment) -> boolean:All comments that match the given expression (resp. are evaluated to
trueby the function) will be extracted to the separate file. Thecommentsoption specifies whether the comment will be preserved, i.e. it is possible to preserve some comments (e.g. annotations) while extracting others or even preserving comments that have been extracted.objectconsisting of the following keys, all optional:condition: regular expression or function (see previous point)filename: The file where the extracted comments will be stored. Can be either astringorfunction (string) -> stringwhich will be given the original filename. Default is to append the suffix.LICENSEto the original filename.banner: The banner text that points to the extracted file and will be added on top of the original file. will be added to the original file. Can befalse(no banner), astring, or afunction (string) -> stringthat will be called with the filename where extracted comments have been stored. Will be wrapped into comment.Default:
/*! For license information please see foo.js.LICENSE */