npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

webpack-prepend-append-wrapper

v1.0.0

Published

Wraps output files (chunks) with custom text or code.

Downloads

5

Readme

A webpack plugin that wraps output files (chunks) with custom text or code.

This plugin is a webpack 5 compatible version of the wrapper-webpack-plugin, that includes the Fix webpack 5 compatibility PR created by AlexKlimenkov.

Installation

Install locally using npm:
npm i webpack-prepend-append-wrapper

Webpack compatibility

This plugin only works with webpack >=4.

Usage

The WrapperPlugin class has a single parameter, an object with a header and/or footer properties. Header text will be prepended to the output file, footer text will be appended. These can be either a string or a function. A string will simply be a appended/prepended to the file output. A function is expected to return a string, and will receive the name of the output file as an argument.

An optional test property (a string or a RegExp object) can control which output files are affected; otherwise all output files will be wrapped.

New in 2.1:
The optional afterOptimization property can be used to avoid having the added text affected by the optimization stage, e.g. if you don't want it to be minified.

API

function WrapperPlugin({
    test: string | RegExp,
    header: string | function,
    footer: string | function,
    afterOptimization: bool // default: false
})

Example configuration #1

Wraps bundle files with '.js' extension in a self invoking function and enables strict mode:

const WrapperPlugin = require('webpack-prepend-append-wrapper');

module.exports = {
  // other webpack config here
  
  plugins: [
    // strict mode for the whole bundle
    new WrapperPlugin({
      test: /\.js$/, // only wrap output of bundle files with '.js' extension 
      header: '(function () { "use strict";\n',
      footer: '\n})();'
    })
  ]
};

Example configuration #2

Prepends bundles with a doc comment:

const WrapperPlugin = require('webpack-prepend-append-wrapper');

module.exports = {
  // other webpack config here
  
  plugins: [
    new WrapperPlugin({
      header: function (fileName) {
        return '/*! file: ' + fileName + ', created by dev123 */\n';
      }
    })
  ]
};

Example configuration #3

Accessing file name, build hash, and chunk hash at runtime.

const WrapperPlugin = require('webpack-prepend-append-wrapper');

module.exports = {
  // other webpack config here
	
  output: {
    filename: '[name].[chunkhash].js'
  },
  plugins: [
    new WrapperPlugin({
      header: `(function (FILE_NAME, BUILD_HASH, CHUNK_HASH) {`,
      footer(fileName, args) {
        return `})('${fileName}', '${args.hash}', '${args.chunkhash}');`;
        // note: args.hash and args.chunkhash correspond to the [hash] and [chunkhash] 
        // placeholders you can specify in the output.filename option.
      }
    })
  ]
};

Example configuration #4

Keeping header in a separate file:

file: header.js

/*!
 * my awesome app!
 */

file: webpack.config

const fs = require('fs');
 WrapperPlugin = require('webpack-prepend-append-wrapper');

const headerDoc = fs.readFileSync('./header.js', 'utf8');

module.exports = {
  // other webpack config here

  plugins: [
    new WrapperPlugin({
      header: headerDoc
    })
  ]
};

Example configuration #5

A slightly more complex example using lodash templates:

const WrapperPlugin = require('webpack-prepend-append-wrapper');
const template = require('lodash.template');
const pkg = require('./package.json');

const tpl = '/*! <%= name %> v<%= version %> | <%= author %> */\n';

module.exports = {
  // other webpack config here

  plugins: [
    new WrapperPlugin({
      header: template(tpl)(pkg)
    })
  ]
};

Compatibility with other plugins

This plugin should play nicely with most other plugins. E.g. adding the webpack.optimize.UglifyJsPlugin plugin to the plugins array after the WrapperPlugin will result in the wrapper text also being minified.

License

ISC