diff --git a/src/index.js b/src/index.js index 4c904b79..ad6cbcd8 100644 --- a/src/index.js +++ b/src/index.js @@ -24,6 +24,7 @@ 'use strict'; +const _ = require('lodash'); const LicensePlugin = require('./license-plugin.js'); module.exports = (options = {}) => { @@ -59,6 +60,11 @@ module.exports = (options = {}) => { return; } + if (_.has(options, 'sourceMap') || _.has(options, 'sourcemap')) { + // SourceMap has been set on the plugin itself. + return; + } + // Rollup >= 0.48 replace `sourceMap` with `sourcemap`. // If `sourcemap` is disabled globally, disable it on the plugin. if (opts.sourceMap === false || opts.sourcemap === false) { diff --git a/src/license-plugin.js b/src/license-plugin.js index 0c20d8a6..5dc1f0f9 100644 --- a/src/license-plugin.js +++ b/src/license-plugin.js @@ -48,12 +48,14 @@ class LicensePlugin { this.name = 'rollup-plugin-license'; this._options = options; - this._sourceMap = true; this._cwd = process.cwd(); this._dependencies = {}; this._pkg = require(path.join(this._cwd, 'package.json')); this._debug = options.debug || false; + // SourceMap can now be disable/enable on the plugin. + this._sourceMap = options.sourceMap !== false && options.sourcemap !== false; + // This is a cache storing a directory path to associated package. // This is an improvement to avoid looking for package information for // already scanned directory. diff --git a/test/index.spec.js b/test/index.spec.js index 73a2a949..56ffeae4 100644 --- a/test/index.spec.js +++ b/test/index.spec.js @@ -92,6 +92,30 @@ describe('rollup-plugin-license', () => { expect(LicensePlugin.prototype.disableSourceMap).toHaveBeenCalledWith(); }); + it('should not try to disable sourceMap if sourceMap is set in plugin options', () => { + spyOn(LicensePlugin.prototype, 'disableSourceMap').and.callThrough(); + + const instance = plugin({sourceMap: true}); + const result = instance.options({ + sourceMap: false, + }); + + expect(result).not.toBeDefined(); + expect(LicensePlugin.prototype.disableSourceMap).not.toHaveBeenCalledWith(); + }); + + it('should not try to disable sourceMap if sourcemap (lowercase) is set in plugin options', () => { + spyOn(LicensePlugin.prototype, 'disableSourceMap').and.callThrough(); + + const instance = plugin({sourcemap: true}); + const result = instance.options({ + sourceMap: false, + }); + + expect(result).not.toBeDefined(); + expect(LicensePlugin.prototype.disableSourceMap).not.toHaveBeenCalledWith(); + }); + it('should enable sourceMap by default', () => { spyOn(LicensePlugin.prototype, 'disableSourceMap').and.callThrough(); diff --git a/test/license-plugin.spec.js b/test/license-plugin.spec.js index cb6e35e9..4ac1f0a0 100644 --- a/test/license-plugin.spec.js +++ b/test/license-plugin.spec.js @@ -46,6 +46,28 @@ describe('LicensePlugin', () => { expect(plugin._dependencies).toEqual({}); }); + it('should initialize instance with sourceMap = false', () => { + const plugin = new LicensePlugin({ + sourceMap: false, + }); + + expect(plugin._cwd).toBeDefined(); + expect(plugin._pkg).toBeDefined(); + expect(plugin._sourceMap).toBe(false); + expect(plugin._dependencies).toEqual({}); + }); + + it('should initialize instance with sourcemap = false (lowercase)', () => { + const plugin = new LicensePlugin({ + sourcemap: false, + }); + + expect(plugin._cwd).toBeDefined(); + expect(plugin._pkg).toBeDefined(); + expect(plugin._sourceMap).toBe(false); + expect(plugin._dependencies).toEqual({}); + }); + it('should disable source map', () => { const plugin = new LicensePlugin(); expect(plugin._sourceMap).toBe(true);