Skip to content

Commit

Permalink
feat: add a way to specify custom data when rendering banner
Browse files Browse the repository at this point in the history
Close #225
  • Loading branch information
mjeanroy committed Jul 4, 2018
1 parent 25be5f5 commit 8828170
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 1 deletion.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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',
};
},
}),
],
}
Expand All @@ -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:

Expand Down Expand Up @@ -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).
Expand Down
4 changes: 3 additions & 1 deletion src/license-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/banner-with-data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* Foo: <%= data.foo %>
* Bar: <%= data.bar %>
*/
54 changes: 54 additions & 0 deletions test/license-plugin.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down

0 comments on commit 8828170

Please sign in to comment.