diff --git a/src/license-plugin-option.js b/src/license-plugin-option.js index 656b25c5..44a89a7a 100644 --- a/src/license-plugin-option.js +++ b/src/license-plugin-option.js @@ -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(), + ], + }) + ), ], }), ], diff --git a/src/license-plugin.js b/src/license-plugin.js index 7d637a0d..2f5e300c 100644 --- a/src/license-plugin.js +++ b/src/license-plugin.js @@ -292,7 +292,7 @@ class LicensePlugin { const output = thirdParty.output; if (output) { - return this._exportThirdParties(outputDependencies, output); + this._exportThirdParties(outputDependencies, output); } } @@ -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, + }); }); } } diff --git a/test/integration/it.spec.js b/test/integration/it.spec.js index 28477fc9..eaec0c2d 100644 --- a/test/integration/it.spec.js +++ b/test/integration/it.spec.js @@ -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'); @@ -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');