lib/index.js
function browserifyBanner (browserify, options) {
  options = options || {};

  if (typeof browserify === "string") {
    // browserify-banner was loaded as a transform, not a plug-in.
    // So return a stream that does nothing.
    return through();
  }

  browserify.on("package", setPackageOption);
  browserify.on("file", setFileOption);
  browserify.on("bundle", wrapBundle);
  browserify.on("reset", wrapBundle);

  /**
   * If the `pkg` option isn't set, then it defaults to the first package that's read
   *
   * @param {object} pkg - The parsed package.json file
   */
  function setPackageOption (pkg) {
    if (!options.pkg) {
      options.pkg = pkg;
    }
  }

  /**
   * If the `file` option isn't set, then it defaults to "banner.txt" in the same directory
   * as the first entry file.  We'll crawl up the directory tree from there if necessary.
   *
   * @param {string} file - The full file path
   */
  function setFileOption (file) {
    if (!options.file) {
      options.file = path.join(path.dirname(file), "banner.txt");
    }
  }

  /**
   * Adds transforms to the Browserify "wrap" pipeline
   */
  function wrapBundle () {
    let wrap = browserify.pipeline.get("wrap");
    let bannerAlreadyAdded = false;
    let banner;

    wrap.push(through(addBannerToBundle));
    if (browserify._options.debug) {
      wrap.push(through(addBannerToSourcemap));
    }

    /**
     * Injects the banner comment block into the Browserify bundle
     */
    function addBannerToBundle (chunk, enc, next) {
      if (!bannerAlreadyAdded) {
        bannerAlreadyAdded = true;
        try {
          banner = getBanner(options);
          this.push(new Buffer(banner));
        }
        catch (e) {
          next(e);
        }
      }
      this.push(chunk);
      next();
    }

    /**
     * Adjusts the sourcemap to account for the banner comment block
     */
    function addBannerToSourcemap (chunk, enc, next) {
      let pushed = false;

      if (banner) {
        // Get the sourcemap, once it exists
        let conv = convertSourcemap.fromSource(chunk.toString("utf8"));
        if (conv) {
          // Offset the sourcemap by the number of lines in the banner
          let offsetMap = offsetSourcemap(conv.toObject(), countLines(banner));
          this.push(new Buffer("\n" + convertSourcemap.fromObject(offsetMap).toComment() + "\n"));
          pushed = true;
        }
      }

      if (!pushed) {
        // This chunk doesn't contain anything for us to modify,
        // so just pass it along as-is
        this.push(chunk);
      }
      next();
    }
  }
}
function browserifyBanner (browserify, options) {
  options = options || {};

  if (typeof browserify === "string") {
    // browserify-banner was loaded as a transform, not a plug-in.
    // So return a stream that does nothing.
    return through();
  }

  browserify.on("package", setPackageOption);
  browserify.on("file", setFileOption);
  browserify.on("bundle", wrapBundle);
  browserify.on("reset", wrapBundle);

  /**
   * If the `pkg` option isn't set, then it defaults to the first package that's read
   *
   * @param {object} pkg - The parsed package.json file
   */
  function setPackageOption (pkg) {
    if (!options.pkg) {
      options.pkg = pkg;
    }
  }

  /**
   * If the `file` option isn't set, then it defaults to "banner.txt" in the same directory
   * as the first entry file.  We'll crawl up the directory tree from there if necessary.
   *
   * @param {string} file - The full file path
   */
  function setFileOption (file) {
    if (!options.file) {
      options.file = path.join(path.dirname(file), "banner.txt");
    }
  }

  /**
   * Adds transforms to the Browserify "wrap" pipeline
   */
  function wrapBundle () {
    let wrap = browserify.pipeline.get("wrap");
    let bannerAlreadyAdded = false;
    let banner;

    wrap.push(through(addBannerToBundle));
    if (browserify._options.debug) {
      wrap.push(through(addBannerToSourcemap));
    }

    /**
     * Injects the banner comment block into the Browserify bundle
     */
    function addBannerToBundle (chunk, enc, next) {
      if (!bannerAlreadyAdded) {
        bannerAlreadyAdded = true;
        try {
          banner = getBanner(options);
          this.push(new Buffer(banner));
        }
        catch (e) {
          next(e);
        }
      }
      this.push(chunk);
      next();
    }

    /**
     * Adjusts the sourcemap to account for the banner comment block
     */
    function addBannerToSourcemap (chunk, enc, next) {
      let pushed = false;

      if (banner) {
        // Get the sourcemap, once it exists
        let conv = convertSourcemap.fromSource(chunk.toString("utf8"));
        if (conv) {
          // Offset the sourcemap by the number of lines in the banner
          let offsetMap = offsetSourcemap(conv.toObject(), countLines(banner));
          this.push(new Buffer("\n" + convertSourcemap.fromObject(offsetMap).toComment() + "\n"));
          pushed = true;
        }
      }

      if (!pushed) {
        // This chunk doesn't contain anything for us to modify,
        // so just pass it along as-is
        this.push(chunk);
      }
      next();
    }
  }
}
function browserifyBanner ( browserify , options ) { options = options || { } ; if ( typeof browserify === "string" ) { // browserify-banner was loaded as a transform, not a plug-in. // So return a stream that does nothing. return through ( ) ; } browserify . on ( "package" , setPackageOption ) ; browserify . on ( "file" , setFileOption ) ; browserify . on ( "bundle" , wrapBundle ) ; browserify . on ( "reset" , wrapBundle ) ; /**
   * If the `pkg` option isn't set, then it defaults to the first package that's read
   *
   * @param {object} pkg - The parsed package.json file
   */ function setPackageOption ( pkg ) { if ( ! options . pkg ) { options . pkg = pkg ; } } /**
   * If the `file` option isn't set, then it defaults to "banner.txt" in the same directory
   * as the first entry file.  We'll crawl up the directory tree from there if necessary.
   *
   * @param {string} file - The full file path
   */ function setFileOption ( file ) { if ( ! options . file ) { options . file = path . join ( path . dirname ( file ) , "banner.txt" ) ; } } /**
   * Adds transforms to the Browserify "wrap" pipeline
   */ function wrapBundle ( ) { let wrap = browserify . pipeline . get ( "wrap" ) ; let bannerAlreadyAdded = false ; let banner ; wrap . push ( through ( addBannerToBundle ) ) ; if ( browserify . _options . debug ) { wrap . push ( through ( addBannerToSourcemap ) ) ; } /**
     * Injects the banner comment block into the Browserify bundle
     */ function addBannerToBundle ( chunk , enc , next ) { if ( ! bannerAlreadyAdded ) { bannerAlreadyAdded = true ; try { banner = getBanner ( options ) ; this . push ( new Buffer ( banner ) ) ; } catch ( e ) { next ( e ) ; } } this . push ( chunk ) ; next ( ) ; } /**
     * Adjusts the sourcemap to account for the banner comment block
     */ function addBannerToSourcemap ( chunk , enc , next ) { let pushed = false ; if ( banner ) { // Get the sourcemap, once it exists let conv = convertSourcemap . fromSource ( chunk . toString ( "utf8" ) ) ; if ( conv ) { // Offset the sourcemap by the number of lines in the banner let offsetMap = offsetSourcemap ( conv . toObject ( ) , countLines ( banner ) ) ; this . push ( new Buffer ( "\n" + convertSourcemap . fromObject ( offsetMap ) . toComment ( ) + "\n" ) ) ; pushed = true ; } } if ( ! pushed ) { // This chunk doesn't contain anything for us to modify, // so just pass it along as-is this . push ( chunk ) ; } next ( ) ; } } }
Browserify plugin that adds a banner comment block to the top of the bundle.

@param {Browserify} browserify - The Browserify instance
@param {object} options - The plugin options
Browserify plugin that adds a banner comment block to the top of the bundle .