Skip to content
This repository was archived by the owner on Dec 5, 2019. It is now read-only.

Commit 430111c

Browse files
evilebottnawimichael-ciniawsky
authored andcommitted
fix(index): correct [name].js.LICENSE file path (options.extractComments) (#249)
1 parent 14d2c4c commit 430111c

File tree

3 files changed

+65
-1
lines changed

3 files changed

+65
-1
lines changed

Diff for: src/index.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
no-param-reassign
33
*/
44
import crypto from 'crypto';
5+
import path from 'path';
56
import { SourceMapConsumer } from 'source-map';
67
import { SourceMapSource, RawSource, ConcatSource } from 'webpack-sources';
78
import RequestShortener from 'webpack/lib/RequestShortener';
@@ -211,7 +212,8 @@ class UglifyJsPlugin {
211212
if (commentsFile && extractedComments.length > 0) {
212213
// Add a banner to the original file
213214
if (this.options.extractComments.banner !== false) {
214-
let banner = this.options.extractComments.banner || `For license information please see ${commentsFile}`;
215+
let banner = this.options.extractComments.banner
216+
|| `For license information please see ${path.posix.basename(commentsFile)}`;
215217

216218
if (typeof banner === 'function') {
217219
banner = banner(commentsFile);

Diff for: test/__snapshots__/extract-comments-options.test.js.snap

+24
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,28 @@ exports[`errors 3`] = `Array []`;
88

99
exports[`errors 4`] = `Array []`;
1010

11+
exports[`errors 5`] = `Array []`;
12+
13+
exports[`nested/nested/test1.js 1`] = `
14+
"/*! For license information please see test1.js.LICENSE */
15+
var foo=1;"
16+
`;
17+
18+
exports[`nested/nested/test1.js.LICENSE 1`] = `
19+
"/* Comment */
20+
"
21+
`;
22+
23+
exports[`nested/test.js 1`] = `
24+
"/*! For license information please see test.js.LICENSE */
25+
var foo=1;"
26+
`;
27+
28+
exports[`nested/test1.js.LICENSE 1`] = `
29+
"// Comment
30+
"
31+
`;
32+
1133
exports[`test.js 1`] = `"var foo=1;"`;
1234

1335
exports[`test.js 2`] = `"var foo=1;"`;
@@ -81,3 +103,5 @@ exports[`warnings 2`] = `Array []`;
81103
exports[`warnings 3`] = `Array []`;
82104

83105
exports[`warnings 4`] = `Array []`;
106+
107+
exports[`warnings 5`] = `Array []`;

Diff for: test/extract-comments-options.test.js

+38
Original file line numberDiff line numberDiff line change
@@ -258,4 +258,42 @@ describe('when options.extractComments', () => {
258258
expect(compilation2.warnings).toMatchSnapshot('warnings');
259259
});
260260
});
261+
262+
it('output respect nested directories', () => {
263+
const pluginEnvironment = new PluginEnvironment();
264+
const compilerEnv = pluginEnvironment.getEnvironmentStub();
265+
compilerEnv.context = '';
266+
267+
const plugin = new UglifyJsPlugin({
268+
extractComments: 'all',
269+
});
270+
plugin.apply(compilerEnv);
271+
const [eventBinding] = pluginEnvironment.getEventBindings();
272+
const chunkPluginEnvironment = new PluginEnvironment();
273+
const compilation2 = chunkPluginEnvironment.getEnvironmentStub();
274+
compilation2.assets = {
275+
'nested/test.js': {
276+
source: () => '// Comment\nvar foo = 1;',
277+
},
278+
'nested/nested/test1.js': {
279+
source: () => '/* Comment */\nvar foo = 1;',
280+
},
281+
};
282+
compilation2.warnings = [];
283+
compilation2.errors = [];
284+
285+
eventBinding.handler(compilation2);
286+
[compilationEventBinding] = chunkPluginEnvironment.getEventBindings();
287+
288+
compilationEventBinding.handler([{
289+
files: ['nested/test.js', 'nested/nested/test1.js'],
290+
}], () => {
291+
expect(compilation2.assets['nested/test.js'].source()).toMatchSnapshot('nested/test.js');
292+
expect(compilation2.assets['nested/test.js.LICENSE'].source()).toMatchSnapshot('nested/test1.js.LICENSE');
293+
expect(compilation2.assets['nested/nested/test1.js'].source()).toMatchSnapshot('nested/nested/test1.js');
294+
expect(compilation2.assets['nested/nested/test1.js.LICENSE'].source()).toMatchSnapshot('nested/nested/test1.js.LICENSE');
295+
expect(compilation2.errors).toMatchSnapshot('errors');
296+
expect(compilation2.warnings).toMatchSnapshot('warnings');
297+
});
298+
});
261299
});

0 commit comments

Comments
 (0)