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

Commit

Permalink
fix(index): correct [name].js.LICENSE file path (`options.extractCo…
Browse files Browse the repository at this point in the history
…mments`) (#249)
  • Loading branch information
evilebottnawi authored and michael-ciniawsky committed Mar 7, 2018
1 parent 14d2c4c commit 430111c
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
no-param-reassign
*/
import crypto from 'crypto';
import path from 'path';
import { SourceMapConsumer } from 'source-map';
import { SourceMapSource, RawSource, ConcatSource } from 'webpack-sources';
import RequestShortener from 'webpack/lib/RequestShortener';
Expand Down Expand Up @@ -211,7 +212,8 @@ class UglifyJsPlugin {
if (commentsFile && extractedComments.length > 0) {
// Add a banner to the original file
if (this.options.extractComments.banner !== false) {
let banner = this.options.extractComments.banner || `For license information please see ${commentsFile}`;
let banner = this.options.extractComments.banner
|| `For license information please see ${path.posix.basename(commentsFile)}`;

if (typeof banner === 'function') {
banner = banner(commentsFile);
Expand Down
24 changes: 24 additions & 0 deletions test/__snapshots__/extract-comments-options.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,28 @@ exports[`errors 3`] = `Array []`;

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

exports[`errors 5`] = `Array []`;

exports[`nested/nested/test1.js 1`] = `
"/*! For license information please see test1.js.LICENSE */
var foo=1;"
`;

exports[`nested/nested/test1.js.LICENSE 1`] = `
"/* Comment */
"
`;

exports[`nested/test.js 1`] = `
"/*! For license information please see test.js.LICENSE */
var foo=1;"
`;

exports[`nested/test1.js.LICENSE 1`] = `
"// Comment
"
`;

exports[`test.js 1`] = `"var foo=1;"`;

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

exports[`warnings 4`] = `Array []`;

exports[`warnings 5`] = `Array []`;
38 changes: 38 additions & 0 deletions test/extract-comments-options.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,4 +258,42 @@ describe('when options.extractComments', () => {
expect(compilation2.warnings).toMatchSnapshot('warnings');
});
});

it('output respect nested directories', () => {
const pluginEnvironment = new PluginEnvironment();
const compilerEnv = pluginEnvironment.getEnvironmentStub();
compilerEnv.context = '';

const plugin = new UglifyJsPlugin({
extractComments: 'all',
});
plugin.apply(compilerEnv);
const [eventBinding] = pluginEnvironment.getEventBindings();
const chunkPluginEnvironment = new PluginEnvironment();
const compilation2 = chunkPluginEnvironment.getEnvironmentStub();
compilation2.assets = {
'nested/test.js': {
source: () => '// Comment\nvar foo = 1;',
},
'nested/nested/test1.js': {
source: () => '/* Comment */\nvar foo = 1;',
},
};
compilation2.warnings = [];
compilation2.errors = [];

eventBinding.handler(compilation2);
[compilationEventBinding] = chunkPluginEnvironment.getEventBindings();

compilationEventBinding.handler([{
files: ['nested/test.js', 'nested/nested/test1.js'],
}], () => {
expect(compilation2.assets['nested/test.js'].source()).toMatchSnapshot('nested/test.js');
expect(compilation2.assets['nested/test.js.LICENSE'].source()).toMatchSnapshot('nested/test1.js.LICENSE');
expect(compilation2.assets['nested/nested/test1.js'].source()).toMatchSnapshot('nested/nested/test1.js');
expect(compilation2.assets['nested/nested/test1.js.LICENSE'].source()).toMatchSnapshot('nested/nested/test1.js.LICENSE');
expect(compilation2.errors).toMatchSnapshot('errors');
expect(compilation2.warnings).toMatchSnapshot('warnings');
});
});
});

0 comments on commit 430111c

Please sign in to comment.