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

fix(index): correct [name].js.LICENSE file path (options.extractComments) #249

Merged
merged 1 commit into from
Mar 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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');
});
});
});