Skip to content

Commit

Permalink
feat: allow banner option to be used with inline string banner
Browse files Browse the repository at this point in the history
  • Loading branch information
mjeanroy committed Aug 7, 2019
1 parent 88e3632 commit 66e7256
Show file tree
Hide file tree
Showing 3 changed files with 384 additions and 45 deletions.
53 changes: 44 additions & 9 deletions src/license-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -359,35 +359,70 @@ module.exports = class LicensePlugin {
}
}

/**
* Log a warning to the console.
*
* @param {string} msg Message to log.
* @return {void}
*/
warn(msg) {
console.warn(`[${this.name}] -- ${msg}`);
}

/**
* Read banner from given options and returns it.
*
* @param {Object|string} banner Banner as a raw string, or banner options.
* @return {string} The banner template.
* @return {string|null} The banner template.
* @private
*/
_readBanner(banner) {
if (!banner) {
return '';
if (_.isNil(banner)) {
return null;
}

// Banner can be defined as a simple inline string.
if (_.isString(banner)) {
this.debug('prepend banner from template');
this.debug('prepend banner from raw string');
return banner;
}

const file = banner.file;
// Warn about deprecated option.
if (_.has(banner, 'file') || _.has(banner, 'encoding')) {
this.warn(
'option `"banner.file"` and `"banner.encoding"` are deprecated and will be removed in a future version, ' +
'please use `"banner.content": {file, encoding}` option instead'
);
}

// Extract banner content.
const content = _.has(banner, 'content') ? _.result(banner, 'content') : {file: banner.file, encoding: banner.encoding};

// Content can be an inline string.
if (_.isString(content)) {
this.debug('prepend banner from content raw string');
return content;
}

// Otherwise, file must be defined (if not, that's an error).
if (!_.has(content, 'file')) {
throw new Error(`[${this.name}] -- Cannot find banner content, please specify an inline content, or a path to a file`);
}

const file = content.file;
const encoding = content.encoding || 'utf-8';

this.debug(`prepend banner from file: ${file}`);
this.debug(`use encoding: ${encoding}`);

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

// Fail fast if file does not exist.
if (!exists) {
this.debug('template file does not exist, skip.');
return '';
throw new Error(`[${this.name}] -- Template file ${filePath} does not exist, or cannot be read`);
}

const encoding = banner.encoding || 'utf-8';
this.debug(`use encoding: ${encoding}`);
return fs.readFileSync(filePath, encoding);
}

Expand Down
215 changes: 206 additions & 9 deletions test/integration/it.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const fs = require('fs');
const rollup = require('rollup');
const nodeResolve = require('rollup-plugin-node-resolve');
const commonjs = require('rollup-plugin-commonjs');
const join = require('../utils/join.js');
const licensePlugin = require('../../dist/index.js');

describe('Dependency', () => {
Expand Down Expand Up @@ -77,8 +78,7 @@ describe('Dependency', () => {
done.fail(err);
}

const content = data.toString();
expect(content).toContain('lodash');
expect(data.toString()).toContain('lodash');
done();
});
});
Expand All @@ -87,7 +87,6 @@ describe('Dependency', () => {
it('should generate bundle with license header', (done) => {
const bundleOutput = path.join(tmpDir.name, 'bundle.js');
const banner = 'test banner';
const EOL = '\n';

const rollupConfig = {
input: path.join(__dirname, 'bundle.js'),
Expand All @@ -112,13 +111,211 @@ describe('Dependency', () => {
done.fail(err);
}

const content = data.toString();
const expectedBanner =
`/**${EOL}` +
` * ${banner}${EOL}` +
` */${EOL}`;
expect(data.toString()).toContain(join([
`/**`,
` * ${banner}`,
` */`,
]));

done();
});
});
});

it('should generate bundle with license header from given file', (done) => {
spyOn(console, 'warn');

const bundleOutput = path.join(tmpDir.name, 'bundle.js');
const banner = {
file: path.join(__dirname, '..', 'fixtures', 'banner.txt'),
encoding: 'utf-8',
};

const rollupConfig = {
input: path.join(__dirname, 'bundle.js'),

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

plugins: [
nodeResolve(),
commonjs(),
licensePlugin({
banner,
}),
],
};

rollup.rollup(rollupConfig)
.then((bundle) => bundle.write(rollupConfig.output))
.then(() => {
fs.readFile(bundleOutput, 'utf8', (err, data) => {
if (err) {
done.fail(err);
}

expect(data.toString()).toContain(join([
'/**',
' * Test banner.',
' *',
' * With a second line.',
' */',
]));

expect(console.warn).toHaveBeenCalledWith(
'[rollup-plugin-license] -- option `"banner.file"` and `"banner.encoding"` are deprecated and will be ' +
'removed in a future version, please use `"banner.content": {file, encoding}` option instead'
);

done();
});
});
});

it('should generate bundle with license header from content as a raw string', (done) => {
spyOn(console, 'warn');

const bundleOutput = path.join(tmpDir.name, 'bundle.js');
const content = 'Banner from inline content';
const banner = {
content,
};

const rollupConfig = {
input: path.join(__dirname, 'bundle.js'),

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

plugins: [
nodeResolve(),
commonjs(),
licensePlugin({
banner,
}),
],
};

rollup.rollup(rollupConfig)
.then((bundle) => bundle.write(rollupConfig.output))
.then(() => {
fs.readFile(bundleOutput, 'utf8', (err, data) => {
if (err) {
done.fail(err);
}

expect(data.toString()).toContain(join([
`/**`,
` * ${content}`,
` */`,
]));

expect(console.warn).not.toHaveBeenCalled();

done();
});
});
});

it('should generate bundle with license header from content as a string returned from function', (done) => {
spyOn(console, 'warn');

const bundleOutput = path.join(tmpDir.name, 'bundle.js');
const content = 'Banner from inline content';
const banner = {
content() {
return content;
},
};

const rollupConfig = {
input: path.join(__dirname, 'bundle.js'),

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

plugins: [
nodeResolve(),
commonjs(),
licensePlugin({
banner,
}),
],
};

rollup.rollup(rollupConfig)
.then((bundle) => bundle.write(rollupConfig.output))
.then(() => {
fs.readFile(bundleOutput, 'utf8', (err, data) => {
if (err) {
done.fail(err);
}

expect(data.toString()).toContain(join([
`/**`,
` * ${content}`,
` */`,
]));

expect(console.warn).not.toHaveBeenCalled();

done();
});
});
});

it('should generate bundle with license header from given content file', (done) => {
spyOn(console, 'warn');

const bundleOutput = path.join(tmpDir.name, 'bundle.js');
const banner = {
content: {
file: path.join(__dirname, '..', 'fixtures', 'banner.txt'),
encoding: 'utf-8',
},
};

const rollupConfig = {
input: path.join(__dirname, 'bundle.js'),

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

plugins: [
nodeResolve(),
commonjs(),
licensePlugin({
banner,
}),
],
};

rollup.rollup(rollupConfig)
.then((bundle) => bundle.write(rollupConfig.output))
.then(() => {
fs.readFile(bundleOutput, 'utf8', (err, data) => {
if (err) {
done.fail(err);
}

expect(data.toString()).toContain(join([
'/**',
' * Test banner.',
' *',
' * With a second line.',
' */',
]));

expect(console.warn).not.toHaveBeenCalled();

expect(content).toContain(expectedBanner);
done();
});
});
Expand Down
Loading

0 comments on commit 66e7256

Please sign in to comment.