diff --git a/src/index.js b/src/index.js index ec26cff4..9e0ea247 100644 --- a/src/index.js +++ b/src/index.js @@ -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'; @@ -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); diff --git a/test/__snapshots__/extract-comments-options.test.js.snap b/test/__snapshots__/extract-comments-options.test.js.snap index f76d8294..aa83de00 100644 --- a/test/__snapshots__/extract-comments-options.test.js.snap +++ b/test/__snapshots__/extract-comments-options.test.js.snap @@ -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;"`; @@ -81,3 +103,5 @@ exports[`warnings 2`] = `Array []`; exports[`warnings 3`] = `Array []`; exports[`warnings 4`] = `Array []`; + +exports[`warnings 5`] = `Array []`; diff --git a/test/extract-comments-options.test.js b/test/extract-comments-options.test.js index e963bab2..2dfa09d4 100644 --- a/test/extract-comments-options.test.js +++ b/test/extract-comments-options.test.js @@ -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'); + }); + }); });