Skip to content

Commit

Permalink
chore: fix sourceMap deprecation
Browse files Browse the repository at this point in the history
  • Loading branch information
mjeanroy committed Jan 14, 2019
1 parent 5b74f50 commit a695b3d
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 27 deletions.
16 changes: 1 addition & 15 deletions src/index-rollup-legacy.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,7 @@ const _ = require('lodash');
const LicensePlugin = require('./license-plugin.js');

module.exports = (options = {}) => {
// Rollup <= 0.48 used `sourceMap` in camelcase, so this plugin used
// this convention at the beginning.
// Now, the `sourcemap` key should be used, but legacy version should still
// be able to use the `sourceMap` key.
const newOptions = _.omitBy(options, (value, key) => (
key === 'sourceMap'
));

// If the old `sourceMap` key is used, set it to `sourcemap` key.
if (!_.hasIn(newOptions, 'sourcemap') && _.hasIn(options, 'sourceMap')) {
console.warn(`[${LicensePlugin.NAME}] sourceMap has been deprecated, please use sourcemap instead.`);
newOptions.sourcemap = options.sourceMap;
}

const plugin = new LicensePlugin(newOptions);
const plugin = new LicensePlugin(options);

return {
/**
Expand Down
17 changes: 16 additions & 1 deletion src/index-rollup-stable.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,25 @@

'use strict';

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

module.exports = (options = {}) => {
const plugin = new LicensePlugin(options);
// Rollup <= 0.48 used `sourceMap` in camelcase, so this plugin used
// this convention at the beginning.
// Now, the `sourcemap` key should be used, but legacy version should still
// be able to use the `sourceMap` key.
const newOptions = _.omitBy(options, (value, key) => (
key === 'sourceMap'
));

// If the old `sourceMap` key is used, set it to `sourcemap` key.
if (!_.hasIn(newOptions, 'sourcemap') && _.hasIn(options, 'sourceMap')) {
console.warn(`[${LicensePlugin.NAME}] sourceMap has been deprecated, please use sourcemap instead.`);
newOptions.sourcemap = options.sourceMap;
}

const plugin = new LicensePlugin(newOptions);

return {
/**
Expand Down
41 changes: 32 additions & 9 deletions src/license-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,30 @@ const OPTIONS = [
*/
const PLUGIN_NAME = 'rollup-plugin-license';

/**
* Fix option object, replace `sourceMap` with `sourcemap` if needed.
*
* @param {Object} options Original option object.
* @return {Object} The new fixed option object.
*/
function fixSourceMapOptions(options) {
// Rollup <= 0.48 used `sourceMap` in camelcase, so this plugin used
// this convention at the beginning.
// Now, the `sourcemap` key should be used, but legacy version should still
// be able to use the `sourceMap` key.
const newOptions = _.omitBy(options, (value, key) => (
key === 'sourceMap'
));

// If the old `sourceMap` key is used, set it to `sourcemap` key.
if (!_.hasIn(newOptions, 'sourcemap') && _.hasIn(options, 'sourceMap')) {
console.warn(`[${PLUGIN_NAME}] sourceMap has been deprecated, please use sourcemap instead.`);
newOptions.sourcemap = options.sourceMap;
}

return newOptions;
}

/**
* Rollup Plugin.
*/
Expand All @@ -62,20 +86,20 @@ class LicensePlugin {
* @param {Object} options Plugin options.
*/
constructor(options = {}) {
this._validateOptions(options);

// Plugin name, used by rollup.
this.name = PLUGIN_NAME;

// Initialize main options.
this._options = options;
this._cwd = options.cwd || process.cwd();
this._options = fixSourceMapOptions(options);
this._validateOptions();

this._cwd = this._options.cwd || process.cwd();
this._dependencies = {};
this._pkg = require(path.join(this._cwd, 'package.json'));
this._debug = options.debug || false;
this._debug = this._options.debug || false;

// SourceMap can now be disable/enable on the plugin.
this._sourcemap = options.sourcemap !== false;
this._sourcemap = this._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
Expand All @@ -87,11 +111,10 @@ class LicensePlugin {
* Validate option object before being used, and print for warnings if
* needed.
*
* @param {Object} options Option object.
* @return {void}
*/
_validateOptions(options) {
const keys = _.keys(options);
_validateOptions() {
const keys = _.keys(this._options);
const notSupported = _.filter(keys, (key) => (
_.indexOf(OPTIONS, key) < 0
));
Expand Down
22 changes: 21 additions & 1 deletion test/index-rollup-stable.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ describe('rollup-plugin-license [rollup stable]', () => {
expect(result.map).toBeDefined();
});

it('should disbable sourcemap', () => {
it('should disbable sourcemap (lowercase) in plugin options', () => {
const instance = plugin({
sourcemap: false,
});
Expand All @@ -166,6 +166,26 @@ describe('rollup-plugin-license [rollup stable]', () => {
expect(result.map).not.toBeDefined();
});

it('should disable sourceMap (camelcase) in plugin options', () => {
spyOn(console, 'warn');

const instance = plugin({
sourceMap: false,
});

const code = 'var foo = 0;';
const chunk = {};
const outputOptions = {};
const result = instance.renderChunk(code, chunk, outputOptions);

expect(result).toBeDefined();
expect(result.code).toBeDefined();
expect(result.map).not.toBeDefined();
expect(console.warn).toHaveBeenCalledWith(
'[rollup-plugin-license] sourceMap has been deprecated, please use sourcemap instead.'
);
});

it('should disable sourcemap using output options', () => {
const instance = plugin();

Expand Down
14 changes: 13 additions & 1 deletion test/license-plugin.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,19 @@ describe('LicensePlugin', () => {
});

expect(console.warn).toHaveBeenCalledWith(
'[rollup-plugin-license] Options sourceMap are not supported, use following options: cwd,debug,sourcemap,banner,thirdParty'
'[rollup-plugin-license] sourceMap has been deprecated, please use sourcemap instead.'
);
});

it('should print warning with unknown options', () => {
spyOn(console, 'warn');

new LicensePlugin({
foobar: false,
});

expect(console.warn).toHaveBeenCalledWith(
'[rollup-plugin-license] Options foobar are not supported, use following options: cwd,debug,sourcemap,banner,thirdParty'
);
});

Expand Down

0 comments on commit a695b3d

Please sign in to comment.