diff --git a/README.md b/README.md index 0064046e..fc55731e 100644 --- a/README.md +++ b/README.md @@ -51,6 +51,13 @@ module.exports = { plugins: [ license({ banner: `Copyright <%= moment().format('YYYY') %>`, + + // May be an object or a function returning an object. + data() { + return { + foo: 'foo', + }; + }, }), ], } @@ -65,6 +72,7 @@ Note that the content will be translated to a lodash template with the following - `dependencies`: An array of all the dependencies included in the bundle. - `moment`: The `moment` object. - `_`: The lodash object. +- `data` A custom data object, defined in banner options. Here is a valid banner: @@ -95,6 +103,8 @@ license({ - 0.6.0 - Add `cwd` option to specify custom working directory (optional option). + - Add a way to specify custom data object when rendering banner. + - Upgrade dependencies. - 0.5.0 - Feat: Sourcemap is now enable by default to ensure compatibility with other rollup plugins. - Fix: Add compatibility with rollup >= 0.48.0 (the new `sourcemap` option). diff --git a/src/license-plugin.js b/src/license-plugin.js index 8fa5d6ba..3651a69b 100644 --- a/src/license-plugin.js +++ b/src/license-plugin.js @@ -173,7 +173,9 @@ class LicensePlugin { // Generate the banner. const pkg = this._pkg; const dependencies = _.values(this._dependencies); - let text = tmpl({_, moment, pkg, dependencies}); + const data = banner.data ? _.result(banner, 'data') : {}; + + let text = tmpl({_, moment, pkg, dependencies, data}); // Make a block comment if needed const trimmedBanner = text.trim(); diff --git a/test/fixtures/banner-with-data.js b/test/fixtures/banner-with-data.js new file mode 100644 index 00000000..559da052 --- /dev/null +++ b/test/fixtures/banner-with-data.js @@ -0,0 +1,4 @@ +/** + * Foo: <%= data.foo %> + * Bar: <%= data.bar %> + */ diff --git a/test/license-plugin.spec.js b/test/license-plugin.spec.js index f928d0c6..f52c04c9 100644 --- a/test/license-plugin.spec.js +++ b/test/license-plugin.spec.js @@ -550,6 +550,60 @@ describe('LicensePlugin', () => { ); }); + it('should prepend banner and replace custom data object', () => { + const instance = new LicensePlugin({ + banner: { + file: path.join(__dirname, 'fixtures', 'banner-with-data.js'), + data: { + foo: 'bar', + bar: 'baz', + }, + }, + }); + + const code = 'var foo = 0;'; + + const result = instance.prependBanner(code); + + expect(result).toBeDefined(); + expect(result.code).toEqual( + `/**\n` + + ` * Foo: bar\n` + + ` * Bar: baz\n` + + ` */\n` + + `\n` + + `var foo = 0;` + ); + }); + + it('should prepend banner and replace custom data function', () => { + const instance = new LicensePlugin({ + banner: { + file: path.join(__dirname, 'fixtures', 'banner-with-data.js'), + data() { + return { + foo: 'bar', + bar: 'baz', + }; + }, + }, + }); + + const code = 'var foo = 0;'; + + const result = instance.prependBanner(code); + + expect(result).toBeDefined(); + expect(result.code).toEqual( + `/**\n` + + ` * Foo: bar\n` + + ` * Bar: baz\n` + + ` */\n` + + `\n` + + `var foo = 0;` + ); + }); + it('should prepend banner and replace package variables', () => { const instance = new LicensePlugin({ banner: {