Skip to content

Commit

Permalink
feat: output option can now be an array of output exports
Browse files Browse the repository at this point in the history
  • Loading branch information
mjeanroy committed Aug 13, 2019
1 parent be3ee04 commit cf5e0f6
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 20 deletions.
13 changes: 13 additions & 0 deletions src/license-plugin-option.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,19 @@ const SCHEMA = {
Joi.func(),
],
}),

Joi.array().items(
Joi.func(),
Joi.string(),
Joi.object().keys({
file: Joi.string(),
encoding: Joi.string(),
template: [
Joi.string(),
Joi.func(),
],
})
),
],
}),
],
Expand Down
42 changes: 22 additions & 20 deletions src/license-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ class LicensePlugin {

const output = thirdParty.output;
if (output) {
return this._exportThirdParties(outputDependencies, output);
this._exportThirdParties(outputDependencies, output);
}
}

Expand Down Expand Up @@ -442,31 +442,33 @@ class LicensePlugin {
* @return {void}
*/
_exportThirdParties(outputDependencies, output) {
if (_.isFunction(output)) {
return output(outputDependencies);
}
_.forEach(_.castArray(output), (output) => {
if (_.isFunction(output)) {
return output(outputDependencies);
}

// Default is to export to given file.
// Default is to export to given file.

// Allow custom formatting of output using given template option.
const template = _.isString(output.template) ? (dependencies) => _.template(output.template)({dependencies, _, moment}) : output.template;
const defaultTemplate = (dependencies) => (
_.isEmpty(dependencies) ? 'No third parties dependencies' : _.map(dependencies, (d) => d.text()).join(`${EOL}${EOL}---${EOL}${EOL}`)
);
// Allow custom formatting of output using given template option.
const template = _.isString(output.template) ? (dependencies) => _.template(output.template)({dependencies, _, moment}) : output.template;
const defaultTemplate = (dependencies) => (
_.isEmpty(dependencies) ? 'No third parties dependencies' : _.map(dependencies, (d) => d.text()).join(`${EOL}${EOL}---${EOL}${EOL}`)
);

const text = _.isFunction(template) ? template(outputDependencies) : defaultTemplate(outputDependencies);
const isOutputFile = _.isString(output);
const file = isOutputFile ? output : output.file;
const encoding = isOutputFile ? 'utf-8' : (output.encoding || 'utf-8');
const text = _.isFunction(template) ? template(outputDependencies) : defaultTemplate(outputDependencies);
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 ${file}`);
this.debug(`use encoding: ${encoding}`);
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(file).dir);
// Create directory if it does not already exist.
mkdirp.sync(path.parse(file).dir);

fs.writeFileSync(file, (text || '').trim(), {
encoding,
fs.writeFileSync(file, (text || '').trim(), {
encoding,
});
});
}
}
Expand Down
68 changes: 68 additions & 0 deletions test/integration/it.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
const path = require('path');
const tmp = require('tmp');
const fs = require('fs');
const _ = require('lodash');
const rollup = require('rollup');
const nodeResolve = require('rollup-plugin-node-resolve');
const commonjs = require('rollup-plugin-commonjs');
Expand Down Expand Up @@ -171,6 +172,73 @@ describe('Dependency', () => {
});
});

it('should generate bundle with dependency output as a JSON & a text file', (done) => {
const bundleOutput = path.join(tmpDir.name, 'bundle.js');
const jsonOutput = path.join(tmpDir.name, 'dependencies.json');
const txtOutput = path.join(tmpDir.name, 'dependencies.json');
const rollupConfig = {
input: path.join(__dirname, 'bundle.js'),

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

plugins: [
nodeResolve(),
commonjs(),

licensePlugin({
thirdParty: {
output: [
{
file: txtOutput,
},
{
file: jsonOutput,
template(dependencies) {
return JSON.stringify(dependencies);
},
},
],
},
}),
],
};

rollup.rollup(rollupConfig)
.then((bundle) => bundle.write(rollupConfig.output))
.then(() => {
const onDone = _.after(2, () => done());

fs.readFile(jsonOutput, 'utf8', (err, data) => {
if (err) {
done.fail(err);
}

const content = data.toString();
const json = JSON.parse(content);
expect(json).toBeDefined();
expect(json.length).toBe(1);
expect(json[0].name).toBe('lodash');

onDone();
});

fs.readFile(txtOutput, 'utf8', (err, data) => {
if (err) {
done.fail(err);
}

const content = data.toString();
expect(content).toBeDefined();
expect(content).toContain('lodash');

onDone();
});
});
});

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

0 comments on commit cf5e0f6

Please sign in to comment.