Code Monkey home page Code Monkey logo

wrapper-webpack-plugin's Introduction

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

Installation

Install locally using npm:
npm i -D wrapper-webpack-plugin

Webpack compatibility

Version 2 of this plugin only works with webpack >=4.
For webpack <4 use version 1 of the plugin with npm i -D wrapper-webpack-plugin@1

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 afterOptimizations 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,
    afterOptimizations: bool // default: false
})

Example configuration #1

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

const WrapperPlugin = require('wrapper-webpack-plugin');

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('wrapper-webpack-plugin');

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('wrapper-webpack-plugin');

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('wrapper-webpack-plugin');

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('wrapper-webpack-plugin');
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

wrapper-webpack-plugin's People

Contributors

alexklimenkov avatar crimx avatar influvio avatar laserdev avatar levp avatar shulandmimi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

wrapper-webpack-plugin's Issues

Uses deprecated hooks in Webpack 5

With Webpack 5 this plugin results in a deprecation warning:

(node:96995) [DEP_WEBPACK_COMPILATION_OPTIMIZE_CHUNK_ASSETS] DeprecationWarning: optimizeChunkAssets is deprecated (use Compilation.hook.processAssets instead and use one of Compilation.PROCESS_ASSETS_STAGE_* as stage option)
    at /Users/<REDACTED>/node_modules/wrapper-webpack-plugin/wrapper-webpack-plugin.js:40:43

I've fixed this in a bunch of our local plugins in a way that's compatible with Webpack 4 and Webpack 5 (by detecting the presence of the processAssets hook), I'll try and create a PR for that here, just raising the issue as a placeholder in case it takes me longer to get to it!

webpack should in the dependencies

In our project, we flatten all dependencies to the top node_modules folder.

image

as webpack is not in the dependencies, when excute, it cannot resolve the webpack module

image

Avoid minification

Currently with webpack 4 if you use minification as it is intended, the wrapper output gets all minified, and that's really bad for what we're trying to do. As the platform that requires the wrapped content cannot understand minified IIFE returns.

Would there be a solution to that?

    minimizer: [
      new UglifyJSPlugin({
        parallel: true,
        uglifyOptions: {
          parse: {
            bare_returns: true,
          },

          compress: {
            warnings: false,
            dead_code: true,
            drop_console: false,
          },

          output: {
            comments: false,
          },

          mangle: true,
        },
      }),
    ],

WrapperPlugin is not compatible with webpack 5

wrapper-webpack-plugin causes a build error when used with webpack 5

Demo

https://github.com/AlexKlimenkov/webpack-sources-issue

Steps

git clone [email protected]:AlexKlimenkov/webpack-sources-issue.git
cd webpack-sources-issue
# npm install -g yarn
yarn
yarn start

Result

[webpack-cli] HookWebpackError: item.node is not a function
    at makeWebpackError (D:\Work\webpack-sources-issue\node_modules\webpack\lib\HookWebpackError.js:49:9)
    at D:\Work\webpack-sources-issue\node_modules\webpack\lib\Compilation.js:1995:11
    at eval (eval at create (D:\Work\webpack-sources-issue\node_modules\tapable\lib\HookCodeFactory.js:33:10), <anonymous>:17:1)
    at processTicksAndRejections (node:internal/process/task_queues:94:5)
-- inner error --
TypeError: item.node is not a function
    at D:\work\webpack-sources-issue\node_modules\webpack-sources\lib\ConcatSource.js:59:50
    at Array.map (<anonymous>)
    at ConcatSource.node (D:\work\webpack-sources-issue\node_modules\webpack-sources\lib\ConcatSource.js:58:63)
    at ConcatSource.proto.sourceAndMap (D:\work\webpack-sources-issue\node_modules\webpack-sources\lib\SourceAndMapMixin.js:29:18)
    at D:\work\webpack-sources-issue\node_modules\terser-webpack-plugin\dist\index.js:159:25
    at D:\work\webpack-sources-issue\node_modules\p-try\index.js:4:10
    at new Promise (<anonymous>)
    at pTry (D:\work\webpack-sources-issue\node_modules\p-try\index.js:3:37)
    at run (D:\work\webpack-sources-issue\node_modules\terser-webpack-plugin\node_modules\p-limit\index.js:24:18)
    at D:\work\webpack-sources-issue\node_modules\terser-webpack-plugin\node_modules\p-limit\index.js:46:18
error Command failed with exit code 1.

original discussion here: https://ask.csdn.net/questions/4560546

Fix

The issue seems to be fixed by accessing webpack-sources from the compiler.webpack object instead of requiring it directly using require("webpack-sources")

	apply(compiler) {
		const WebpackSources = compiler.webpack.sources;
		const ConcatSource = WebpackSources.ConcatSource;

All output files register changes with any file change while watching

When we use this plugin while running a webpack watch, with many output/entry files, all of these files are registering changes if any file has a change. Since most of the file source is cached for rebuilding the output files, output file construction does not seem to take long, but with sourcemaps enabled, all sourcemaps are also rebuilding, taking a lot of time.

This amounts to a watcher change taking close to the same amount of time as a whole webpack build for us.

Do you think there would be any way to add watcher support for this plugin such that it does not trigger changes to everything?

I've been poking around to see what's possible using the source of this project, but it seems if I check the chunk hash for changes and only run your logic on these, all the wrappers for other output files are removed.

Perhaps there is a different event hook that could be tapped into?

Webpack 5 update breaks wrapper-webpack-plugin

Hello, guys!

I have a Preact project that uses Webpack 4 as it's bundling utility. I want to update to Webpack 5. I encountered lots of errors and changes that had to be done and right now I am almost at the end of it. But I am currently facing a huge blocker.

The project I work on has a webpack-server.config that uses wrapper-webpack-plugin at one point and it looks like this:

        new WrapperPlugin({
            test: /\.js$/,
            header: `
                exports['setupGlobals'] = function (globals) {
                    var navigator = globals.navigator;
                    var location = globals.location;
                            
                    var render = `,
            footer: `
                return { render: render };
            }`
        })

Later it uses setupGlobals().render() as:

const { html, chunks, http2Links } = serverApp.setupGlobals(getGlobals(ctx)).render(clone(state));

After I upgraded Webpack 4 -> Webpack 5, this Wrapper doesn't work anymore. It returns render as undefined so it can't be called.

The build is working webpack 5.84.0 compiled -> Build done! but when I try to access the app via browser, I get error 500 and this error:

500 TypeError: serverApp.setupGlobals(...).render is not a function

Does anyhow have any idea why this wrapper wouldn't work in Webpack 5? Thanks a bunch!

Code gets removed even with `afterOptimizations` to `true`

I'm using the plugin like this:

new WrapperPlugin({
 header: (fileName, { hash }) => {
    return fileName === 'serviceWorker.bundle.js' ? `/* hash: ${hash} */` : ''
  },
  afterOptimizations: true
})

When running in development mode, the comment is there, but when running in production mode, the comment is not there.

I think this might be because of #11.

WrapperPlugin doesn't change [contenthash]

For production we use namings like [name].[contenthash:8].min.js. We add WrapperPlugin to add a header and a footer, but js-file on CDN didn't change because the contenthash is old.

Doesn't play nice with ExtractTextPlugin

When I use WrapperPlugin with ExtractTextPlugin, I get the following exception

Module build failed: TypeError: text.forEach is not a function
    at /Users/jknight/Workspace/xxx/node_modules/extract-text-webpack-plugin/dist/loader.js:131:14
    at compile (/Users/jknight/Workspace/xxx/node_modules/webpack/lib/Compiler.js:304:11)
    at applyPluginsAsync.err (/Users/jknight/Workspace/xxx/node_modules/webpack/lib/Compiler.js:520:14)
    at next (/Users/jknight/Workspace/xxx/node_modules/tapable/lib/Tapable.js:184:11)
    at Compiler.<anonymous> (/Users/jknight/Workspace/xxx/node_modules/extract-text-webpack-plugin/dist/loader.js:108:7)
    at next (/Users/jknight/Workspace/xxx/node_modules/tapable/lib/Tapable.js:186:14)
    at Compiler.<anonymous> (/Users/jknight/Workspace/xxx/node_modules/webpack/lib/CachePlugin.js:62:5)
    at Compiler.applyPluginsAsyncSeries (/Users/jknight/Workspace/xxx/node_modules/tapable/lib/Tapable.js:188:13)
    at compilation.seal.err (/Users/jknight/Workspace/xxx/node_modules/webpack/lib/Compiler.js:517:11)
    at Compilation.applyPluginsAsyncSeries (/Users/jknight/Workspace/xxx/node_modules/tapable/lib/Tapable.js:177:46)
    at self.applyPluginsAsync.err (/Users/jknight/Workspace/xxx/node_modules/webpack/lib/Compilation.js:654:19)
    at Compilation.applyPluginsAsyncSeries (/Users/jknight/Workspace/xxx/node_modules/tapable/lib/Tapable.js:177:46)
    at self.applyPluginsAsync.err (/Users/jknight/Workspace/xxx/node_modules/webpack/lib/Compilation.js:645:11)
    at next (/Users/jknight/Workspace/xxx/node_modules/tapable/lib/Tapable.js:184:11)
    at Compilation.<anonymous> (/Users/jknight/Workspace/xxx/node_modules/wrapper-webpack-plugin/wrapper-webpack-plugin.js:31:4)
    at Compilation.applyPluginsAsyncSeries (/Users/jknight/Workspace/xxx/node_modules/tapable/lib/Tapable.js:188:13)
    at self.applyPluginsAsync.err (/Users/jknight/Workspace/xxx/node_modules/webpack/lib/Compilation.js:640:10)
    at Compilation.applyPluginsAsyncSeries (/Users/jknight/Workspace/xxx/node_modules/tapable/lib/Tapable.js:177:46)
    at sealPart2 (/Users/jknight/Workspace/xxx/node_modules/webpack/lib/Compilation.js:636:9)
    at Compilation.applyPluginsAsyncSeries (/Users/jknight/Workspace/xxx/node_modules/tapable/lib/Tapable.js:177:46)
    at Compilation.seal (/Users/jknight/Workspace/xxx/node_modules/webpack/lib/Compilation.js:579:8)
    at applyPluginsParallel.err (/Users/jknight/Workspace/xxx/node_modules/webpack/lib/Compiler.js:514:17)
    at /Users/jknight/Workspace/xxx/node_modules/tapable/lib/Tapable.js:271:11
    at _addModuleChain (/Users/jknight/Workspace/xxx/node_modules/webpack/lib/Compilation.js:481:11)
    at processModuleDependencies.err (/Users/jknight/Workspace/xxx/node_modules/webpack/lib/Compilation.js:452:13)
    at _combinedTickCallback (internal/process/next_tick.js:95:7)
    at process._tickCallback (internal/process/next_tick.js:161:9)
Child extract-text-webpack-plugin node_modules/extract-text-webpack-plugin/dist node_modules/css-loader/index.js!src/ot-common/components/Main/styles.css:
       2 modules
Child extract-text-webpack-plugin node_modules/extract-text-webpack-plugin/dist node_modules/css-loader/index.js!src/ot-common/components/Main/styles.css:
       2 modules

Removing WrapperPlugin causes the issue to go away

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.