Skip to content

Commit

Permalink
feat: add package variable in template
Browse files Browse the repository at this point in the history
  • Loading branch information
mjeanroy committed Nov 17, 2016
1 parent e522c6e commit 937188f
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,16 @@ module.exports = (options) => {
return deferred.reject(error);
}

// Import the `package.json` of the project.
// The `resolve` function
const pkgPath = path.resolve(process.cwd(), 'package.json');
const pkg = require(pkgPath);

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

// Generate the banner.
let banner = tmpl({_, moment});
let banner = tmpl({_, moment, pkg});

// Make a block comment if needed
const trimmedBanner = banner.trim();
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/banner-with-pkg.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/**
* Name: <%= pkg.name %>
*/
23 changes: 23 additions & 0 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,27 @@ describe('rollup-plugin-license', () => {
done();
});
});

it('should prepend banner and replace package variables', (done) => {
const instance = plugin({
file: path.join(__dirname, 'fixtures', 'banner-with-pkg.js'),
});

const code = 'var foo = 0;';

const promise = instance.transformBundle(code);

promise.then((result) => {
expect(result).toBeDefined();
expect(result.code).toEqual(
`/**\n` +
` * Name: rollup-plugin-license\n` +
` */\n` +
`\n` +
`var foo = 0;`
);

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

0 comments on commit 937188f

Please sign in to comment.