Skip to content

Commit

Permalink
core: banner can be a simple string
Browse files Browse the repository at this point in the history
  • Loading branch information
mjeanroy committed Feb 20, 2017
1 parent 90c5c1e commit 0f4dde0
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 26 deletions.
29 changes: 26 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,38 @@ Then add it to your rollup configuration:
const path = require('path');
const license = require('rollup-plugin-license');

module.exports ={
module.exports = {
plugins: [
license({
sourceMap: true,

banner: {
file: path.join(__dirname, 'LICENSE'),
encoding: 'utf-8', // Default is utf-8
},

thirdParty: {
output: path.join(__dirname, 'dist', 'dependencies.txt'),
includePrivate: true, // Default is false.
encoding: 'utf-8', // Default is utf-8.
},
})
]
}),
],
}
```

Since version 0.3.0, `banner` can be a simple string that will be used directly:

```javascript
const path = require('path');
const license = require('rollup-plugin-license');

module.exports = {
plugins: [
license({
banner: `Copyright <%= moment().format('YYYY') %>`,
}),
],
}
```

Expand Down Expand Up @@ -69,6 +86,12 @@ license({
})
```

## Changelogs

- 0.3.0
- Add encoding option for banner and third-party output file.
- Banner can be a simple string.

## License

MIT License (MIT)
Expand Down
50 changes: 28 additions & 22 deletions src/license-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,43 +137,49 @@ class LicensePlugin {
* if it has been enabled (using `enableSourceMap` method).
*/
prependBanner(code) {
const banner = this._options.banner;
const file = banner ? banner.file : banner;

// Create a magicString: do not manipulate the string directly since it
// will be used to generate the sourcemap.
const magicString = new MagicString(code);

if (file) {
const banner = this._options.banner;

let content;
if (_.isString(banner)) {
this.debug('prepend banner from template');
content = banner;
} else if (banner) {
const file = banner.file;
this.debug(`prepend banner from file: ${file}`);

const filePath = path.resolve(file);
const exists = fs.existsSync(filePath);

if (exists) {
const encoding = banner.encoding || 'utf-8';
this.debug(`use encoding: ${encoding}`);
content = fs.readFileSync(filePath, encoding);
} else {
this.debug('template file does not exist, skip.');
}
}

const content = fs.readFileSync(filePath, encoding);

// Create the template function with lodash.
const tmpl = _.template(content);
if (content) {
// Create the template function with lodash.
const tmpl = _.template(content);

// Generate the banner.
const pkg = this._pkg;
const dependencies = _.values(this._dependencies);
let text = tmpl({_, moment, pkg, dependencies});
// Generate the banner.
const pkg = this._pkg;
const dependencies = _.values(this._dependencies);
let text = tmpl({_, moment, pkg, dependencies});

// Make a block comment if needed
const trimmedBanner = text.trim();
const start = trimmedBanner.slice(0, 3);
if (start !== '/**' && start !== '/*!') {
text = generateBlockComment(text);
}

// Prepend the banner.
magicString.prepend(`${text}${EOL}`);
// Make a block comment if needed
const trimmedBanner = text.trim();
const start = trimmedBanner.slice(0, 3);
if (start !== '/**' && start !== '/*!') {
text = generateBlockComment(text);
}

// Prepend the banner.
magicString.prepend(`${text}${EOL}`);
}

const result = {
Expand Down
26 changes: 25 additions & 1 deletion test/license-plugin.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ describe('LicensePlugin', () => {
});
});

it('should prepend banner to bundle', () => {
it('should prepend banner to bundle from a file', () => {
const instance = new LicensePlugin({
banner: {
file: path.join(__dirname, 'fixtures', 'banner.js'),
Expand All @@ -353,6 +353,30 @@ describe('LicensePlugin', () => {
);
});

it('should prepend banner to bundle with template', () => {
const file = path.join(__dirname, 'fixtures', 'banner.js');
const tmpl = fs.readFileSync(file, 'utf-8');
const instance = new LicensePlugin({
banner: tmpl,
});

const code = 'var foo = 0;';

const result = instance.prependBanner(code);

expect(result).toBeDefined();
expect(result.map).not.toBeDefined();
expect(result.code).toEqual(
`/**\n` +
` * Test banner.\n` +
` *\n` +
` * With a second line.\n` +
` */\n` +
`\n` +
`${code}`
);
});

it('should prepend banner to bundle with custom encoding', () => {
spyOn(fs, 'readFileSync').and.callThrough();

Expand Down

0 comments on commit 0f4dde0

Please sign in to comment.