Skip to content

Commit

Permalink
refactor: deprecate thirdParty.encoding property
Browse files Browse the repository at this point in the history
  • Loading branch information
mjeanroy committed Aug 9, 2019
1 parent 59cf1e7 commit d2d9c96
Show file tree
Hide file tree
Showing 5 changed files with 193 additions and 50 deletions.
44 changes: 42 additions & 2 deletions src/license-plugin-option.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,13 @@ const SCHEMA = {

thirdParty: Joi.object().keys({
includePrivate: Joi.boolean(),
encoding: Joi.string(),
output: [
Joi.func(),
Joi.string(),
Joi.object().keys({
file: Joi.string(),
encoding: Joi.string(),
}),
],
}),
};
Expand Down Expand Up @@ -153,14 +156,51 @@ function fixBannerOptions(options) {
});
}

/**
* Fix option object, replace `thirdParty.encoding` with `thirdParty.output.encoding`.
*
* @param {Object} options Original option object.
* @return {Object} The new fixed option object.
*/
function fixThirdPartyOptions(options) {
if (!_.hasIn(options, 'thirdParty')) {
return options;
}

const thirdParty = options.thirdParty;
if (!_.hasIn(thirdParty, 'encoding')) {
return options;
}

warn(
'"thirdParty.encoding" has been deprecated and will be removed in a future version, ' +
'please use "thirdParty.output.encoding" instead.'
);

const newThirdParty = _.omitBy(thirdParty, (value, key) => (
key === 'encoding'
));

if (_.isString(thirdParty.output)) {
newThirdParty.output = {
file: thirdParty.output,
encoding: thirdParty.encoding,
};
}

return _.extend({}, options, {
thirdParty: newThirdParty,
});
}

/**
* Normalize option object by removing deprecated options and migrate these to the new version.
*
* @param {Object} options Option object.
* @return {Object} Normalized option object.
*/
function normalizeOptions(options) {
return _.reduce([fixSourceMapOptions, fixBannerOptions], (acc, fn) => fn(acc), options);
return _.reduce([fixSourceMapOptions, fixBannerOptions, fixThirdPartyOptions], (acc, fn) => fn(acc), options);
}

/**
Expand Down
10 changes: 6 additions & 4 deletions src/license-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,15 +282,17 @@ class LicensePlugin {
.trim()
.value();

const encoding = thirdParty.encoding || 'utf-8';
const isOutputFile = _.isString(output);
const file = isOutputFile ? output : output.file;
const encoding = isOutputFile ? 'utf-8' : (output.encoding || 'utf-8');

this.debug(`exporting third-party summary to ${output}`);
this.debug(`exporting third-party summary to ${file}`);
this.debug(`use encoding: ${encoding}`);

// Create directory if it does not already exist.
mkdirp.sync(path.parse(output).dir);
mkdirp.sync(path.parse(file).dir);

fs.writeFileSync(output, text || 'No third parties dependencies', {
fs.writeFileSync(file, text || 'No third parties dependencies', {
encoding,
});
}
Expand Down
40 changes: 40 additions & 0 deletions test/integration/it.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,46 @@ describe('Dependency', () => {
});
});

it('should generate bundle with dependency output as an object', (done) => {
const bundleOutput = path.join(tmpDir.name, 'bundle.js');
const thirdPartyOutput = path.join(tmpDir.name, 'dependencies.txt');

const rollupConfig = {
input: path.join(__dirname, 'bundle.js'),

output: {
file: bundleOutput,
format: 'es',
},

plugins: [
nodeResolve(),
commonjs(),

licensePlugin({
thirdParty: {
output: {
file: thirdPartyOutput,
},
},
}),
],
};

rollup.rollup(rollupConfig)
.then((bundle) => bundle.write(rollupConfig.output))
.then(() => {
fs.readFile(thirdPartyOutput, 'utf8', (err, data) => {
if (err) {
done.fail(err);
}

expect(data.toString()).toContain('lodash');
done();
});
});
});

it('should generate bundle and export dependencies to given function', (done) => {
const bundleOutput = path.join(tmpDir.name, 'bundle.js');
const thirdPartyOutput = jasmine.createSpy('thirdPartyOutput');
Expand Down
57 changes: 57 additions & 0 deletions test/license-plugin-option.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,40 @@ describe('licensePluginOptions', () => {
});
});

it('should normalize option object and fix deprecated "thirdParty.encoding" entry', () => {
const warn = spyOn(console, 'warn');
const options = {
thirdParty: {
output: 'ThirdParty.txt',
encoding: 'utf-16',
},
};

const result = licensePluginOptions(options);

expect(warn).toHaveBeenCalledWith(
'[rollup-plugin-license] -- "thirdParty.encoding" has been deprecated and will be removed in a future version, ' +
'please use "thirdParty.output.encoding" instead.'
);

expect(result).toEqual({
thirdParty: {
output: {
file: 'ThirdParty.txt',
encoding: 'utf-16',
},
},
});

// Ensure original option object has not changed
expect(options).toEqual({
thirdParty: {
output: 'ThirdParty.txt',
encoding: 'utf-16',
},
});
});

it('should normalize option object and fix deprecated "banner.encoding" entry', () => {
const warn = spyOn(console, 'warn');
const options = {
Expand Down Expand Up @@ -185,11 +219,17 @@ describe('licensePluginOptions', () => {
sourceMap: true,
cwd: '.',
debug: true,

banner: {
file: 'LICENSE.md',
encoding: 'utf-16',
commentStyle: 'regular',
},

thirdParty: {
output: 'ThirdParty.txt',
encoding: 'utf-8',
},
};

const result = licensePluginOptions(options);
Expand All @@ -209,17 +249,30 @@ describe('licensePluginOptions', () => {
'please use "banner.content.encoding" instead.'
);

expect(warn).toHaveBeenCalledWith(
'[rollup-plugin-license] -- "thirdParty.encoding" has been deprecated and will be removed in a future version, ' +
'please use "thirdParty.output.encoding" instead.'
);

expect(result).toEqual({
sourcemap: true,
cwd: '.',
debug: true,

banner: {
commentStyle: 'regular',
content: {
file: 'LICENSE.md',
encoding: 'utf-16',
},
},

thirdParty: {
output: {
file: 'ThirdParty.txt',
encoding: 'utf-8',
},
},
});

// Ensure original option object has not changed
Expand All @@ -232,6 +285,10 @@ describe('licensePluginOptions', () => {
encoding: 'utf-16',
commentStyle: 'regular',
},
thirdParty: {
output: 'ThirdParty.txt',
encoding: 'utf-8',
},
});
});
});
Loading

0 comments on commit d2d9c96

Please sign in to comment.