Files
30-seconds-of-code/node_modules/@comandeer/babel-plugin-banner/index.js
2019-08-20 15:52:05 +02:00

32 lines
753 B
JavaScript

'use strict';
const utils = require( './utils' );
const isComment = utils.isComment;
const getCommentContent = utils.getCommentContent;
module.exports = function( babel ) {
const t = babel.types;
return {
visitor: {
Program: function Program( path, $ ) {
const options = $.opts;
const banner = options.banner;
const newLine = typeof options.newLine !== 'undefined' ? Boolean( options.newLine ) : true;
if ( typeof banner !== 'string' || !isComment( banner ) ) {
throw new TypeError( 'Banner must be a valid comment.' );
}
if ( newLine ) {
path.unshiftContainer( 'body', t.noop() );
}
if ( isComment( banner ) ) {
path.addComment( 'leading', getCommentContent( banner ) );
}
}
}
};
}