Skip to content

Commit

Permalink
chore: migrate to rollup >= 1
Browse files Browse the repository at this point in the history
  • Loading branch information
mjeanroy committed Jan 14, 2019
1 parent a61e24a commit d8dc78d
Show file tree
Hide file tree
Showing 12 changed files with 884 additions and 483 deletions.
99 changes: 99 additions & 0 deletions src/index-rollup-legacy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2016-2018 Mickael Jeanroy
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

'use strict';

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

module.exports = (options = {}) => {
const plugin = new LicensePlugin(options);

return {
/**
* Name of the plugin, used automatically by rollup.
* @type {string}
*/
name: plugin.name,

/**
* Function called by rollup when a JS file is loaded: it is used to scan
* third-party dependencies.
*
* @param {string} id JS file path.
* @return {void}
*/
load(id) {
plugin.scanDependency(id);
},

/**
* Function called by rollup to read global options: if source map parameter
* is truthy, enable it on the plugin.
*
* @param {object} opts Rollup options.
* @return {void}
*/
options(opts) {
if (!opts) {
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) {
plugin.disableSourceMap();
}
},

/**
* Function called by rollup when the final bundle is generated: it is used
* to prepend the banner file on the generated bundle.
*
* @param {string} code Bundle content.
* @param {Object} outputOptions The options for this output.
* @return {void}
*/
transformBundle(code, outputOptions = {}) {
const sourcemap = outputOptions.sourcemap !== false || outputOptions.sourceMap !== false;
return plugin.prependBanner(code, sourcemap);
},

/**
* Function called by rollup when the final bundle will be written on disk: it
* is used to generate a file containing a summary of all third-party dependencies
* with license information.
*
* @return {void}
*/
ongenerate() {
plugin.exportThirdParties();
},
};
};
74 changes: 74 additions & 0 deletions src/index-rollup-stable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2016-2018 Mickael Jeanroy
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

'use strict';

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

module.exports = (options = {}) => {
const plugin = new LicensePlugin(options);

return {
/**
* Name of the plugin, used automatically by rollup.
* @type {string}
*/
name: plugin.name,

/**
* Function called by rollup when a JS file is loaded: it is used to scan
* third-party dependencies.
*
* @param {string} id JS file path.
* @return {void}
*/
load(id) {
plugin.scanDependency(id);
},

/**
* Function called by rollup when the final bundle is generated: it is used
* to prepend the banner file on the generated bundle.
*
* @param {string} code Bundle content.
* @param {Object} chunk The chunk being generated.
* @param {Object} outputOptions The options for the generated output.
* @return {void}
*/
renderChunk(code, chunk, outputOptions = {}) {
return plugin.prependBanner(code, outputOptions.sourcemap !== false);
},

/**
* Function called by rollup when the final bundle will be written on disk: it
* is used to generate a file containing a summary of all third-party dependencies
* with license information.
*
* @return {void}
*/
generateBundle() {
plugin.exportThirdParties();
},
};
};
75 changes: 5 additions & 70 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,74 +24,9 @@

'use strict';

const _ = require('lodash');
const LicensePlugin = require('./license-plugin.js');
const rollup = require('rollup');
const VERSION = rollup.VERSION;
const MAJOR_VERSION = VERSION ? Number(VERSION.split('.')[0]) : 0;
const IS_ROLLUP_LEGACY = MAJOR_VERSION === 0;

module.exports = (options = {}) => {
const plugin = new LicensePlugin(options);

return {
/**
* Name of the plugin, used automatically by rollup.
* @type {string}
*/
name: plugin.name,

/**
* Function called by rollup when a JS file is loaded: it is used to scan
* third-party dependencies.
*
* @param {string} id JS file path.
* @return {void}
*/
load(id) {
plugin.scanDependency(id);
},

/**
* Function called by rollup to read global options: if source map parameter
* is truthy, enable it on the plugin.
*
* @param {object} opts Rollup options.
* @return {void}
*/
options(opts) {
if (!opts) {
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) {
plugin.disableSourceMap();
}
},

/**
* Function called by rollup when the final bundle is generated: it is used
* to prepend the banner file on the generated bundle.
*
* @param {string} code Bundle content.
* @return {void}
*/
transformBundle(code) {
return plugin.prependBanner(code);
},

/**
* Function called by rollup when the final bundle will be written on disk: it
* is used to generate a file containing a summary of all third-party dependencies
* with license information.
*
* @return {void}
*/
ongenerate() {
plugin.exportThirdParties();
},
};
};
module.exports = IS_ROLLUP_LEGACY ? require('./index-rollup-legacy') : require('./index-rollup-stable');
9 changes: 5 additions & 4 deletions src/license-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class LicensePlugin {
this._debug = options.debug || false;

// SourceMap can now be disable/enable on the plugin.
this._sourceMap = options.sourceMap !== false && options.sourcemap !== false;
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
Expand All @@ -68,7 +68,7 @@ class LicensePlugin {
* @return {void}
*/
disableSourceMap() {
this._sourceMap = false;
this._sourcemap = false;
}

/**
Expand Down Expand Up @@ -137,10 +137,11 @@ class LicensePlugin {
* This hook is used here to prepend the license banner to the final bundle.
*
* @param {string} code The bundle content.
* @param {boolean} sourcemap If sourcemap must be generated.
* @return {Object} The result containing the code and, optionnally, the source map
* if it has been enabled (using `enableSourceMap` method).
*/
prependBanner(code) {
prependBanner(code, sourcemap) {
// Create a magicString: do not manipulate the string directly since it
// will be used to generate the sourcemap.
const magicString = new MagicString(code);
Expand Down Expand Up @@ -192,7 +193,7 @@ class LicensePlugin {
code: magicString.toString(),
};

if (this._sourceMap) {
if (this._sourcemap !== false && sourcemap !== false) {
result.map = magicString.generateMap({
hires: true,
});
Expand Down
Loading

0 comments on commit d8dc78d

Please sign in to comment.