Skip to content

Commit

Permalink
fix: ensure plugin sourcemap option is used instead of global sourcem…
Browse files Browse the repository at this point in the history
…ap option
  • Loading branch information
mjeanroy committed Aug 21, 2017
1 parent f2fd89d commit 5b8ef6e
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

'use strict';

const _ = require('lodash');
const LicensePlugin = require('./license-plugin.js');

module.exports = (options = {}) => {
Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 3 additions & 1 deletion src/license-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
24 changes: 24 additions & 0 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
22 changes: 22 additions & 0 deletions test/license-plugin.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 5b8ef6e

Please sign in to comment.