Coder Social home page Coder Social logo

nmfr / last-call-webpack-plugin Goto Github PK

View Code? Open in Web Editor NEW
31.0 4.0 6.0 152 KB

A Webpack plugin to change assets just before they are written to files.

License: MIT License

JavaScript 100.00%
webpack-plugin assets-processors assets-manipulation webpack-assets assets manipulation

last-call-webpack-plugin's Introduction

Last Call Webpack Plugin

A Webpack plugin that allows you to transform \ modify assets just before Webpack emits them.

What does the plugin do?

It allows you to transform \ modify Webpack assets just before Webpack emits them (writes them to files or memory in case you are using something like Webpack dev server).

It can be used for example to:

  • Prefix a /* Author: John Doe */ comment on all the .js files Webpack generates.
  • Run some final optimization on all .css files Webpack generates.

Installation:

Using npm:

$ npm install --save-dev last-call-webpack-plugin

โš ๏ธ For webpack v3 or below please use [email protected]. The [email protected] version and above supports webpack v4.

Configuration:

The plugin can receive the following options:

  • assetProcessors: An Array of objects that describe asset processors:
    • regExp: A regular expression to match the asset name that the processor handles.
    • processor: A function with the signature of function(assetName, webpackAssetObject, assets) that returns a Promise. If the Promise returns a result this result will replace the assets content.
    • phase: The webpack compilation phase that at which the processor should be called. Default value is compilation.optimize-assets. Can be one of the following values:
      • compilation.optimize-chunk-assets
      • compilation.optimize-assets
      • emit
  • canPrint: A boolean indicating if the plugin can print messages to the console, defaults to true.

Note: An environment supporting Promises or a Promise polyfill is needed for this plugin to be used.

Example:

var cssnano = require('cssnano');
var LastCallWebpackPlugin = require('last-call-webpack-plugin');
module.exports = {
  module: {
    loaders: [
      { test: /\.css$/, loader: ExtractTextPlugin.extract("style-loader", "css-loader") }
    ]
  },
  plugins: [
    new ExtractTextPlugin("styles.css"),
    new LastCallWebpackPlugin({
      assetProcessors: [
        {
          regExp:  /\.js$/,
          processor: (assetName, asset) => Promise.resolve('// Author: John Doe \n' + asset.source())
        }, {
          regExp:  /\.css$/,
          processor: (assetName, asset) => cssnano.process(asset.source())
            .then(r => r.css)
        }
      ],
      canPrint: true
    })
	]
}

Assets manipulation

The processor method is supplied an assets object that allows asset manipulation via the setAsset(assetName, assetValue) method. If assetValue is null the asset will be deleted. This object can be used to generate aditional assets (like source maps) or rename the an asset (create a new asset and delete the current one).

Example:

var cssnano = require('cssnano');
var LastCallWebpackPlugin = require('last-call-webpack-plugin');
module.exports = {
  module: {
    loaders: [
      { test: /\.css$/, loader: ExtractTextPlugin.extract("style-loader", "css-loader") }
    ]
  },
  plugins: [
    new ExtractTextPlugin("styles.css"),
    new LastCallWebpackPlugin({
      assetProcessors: [{
        regExp:  /\.css$/,
        processor: (assetName, asset, assets) => {
          assets.setAsset(assetName + '.map', null); // Delete the <assetName>.map asset.
          assets.setAsset(assetName + '.log', 'All OK'); // Add the <assetName>.log asset with the content 'All OK'.
          return cssnano
            .process(asset.source())
            .then(r => r.css)
        }
      }],
      canPrint: true
    })
	]
}

The assets object also has a getAsset(assetName) method to get the content of an asset (returns undefined in case the asset does not exist).

License

MIT (http://www.opensource.org/licenses/mit-license.php)

last-call-webpack-plugin's People

Contributors

nmfr 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

Watchers

 avatar  avatar  avatar  avatar

last-call-webpack-plugin's Issues

Support [hash], [contenthash] and etc from loader-utils

Using filename: "[name]-[contenthash].css" in extract-text-webpack-plugin, i have file like app-6f8bbde7c2.css, but after using cssnano content changed and [contexthash] now is invalid.

Can we add filename option as in extract-text-webpack-plugin for changing hash or passassetName in onEnd callback (also support async callback or promise)?

Upgrade lodash to version >=4.17.12

Overview
Versions of lodash before 4.17.12 are vulnerable to Prototype Pollution. The function defaultsDeep allows a malicious user to modify the prototype of Object via {constructor: {prototype: {...}}} causing the addition or modification of an existing property that will exist on all objects.

Remediation
Update to version 4.17.12 or later.

Resources
Snyk Advisory

Add webpack 5 support

As webpack 5 has multiple deprecations, it seems this plugin is facing some of them as well. It seems the plugin still works but the APIs it depends on will be gone in the future.

I'm using the plugin through optimize-css-assets-webpack-plugin and when you use last-call-webpack-plugin, it emits the following deprecation warning:

DeprecationWarning: optimizeChunkAssets is deprecated (use Compilation.hook.processAssets instead and use one of Compilation.PROCESS_ASSETS_STAGE_* as stage option)

I believe you may need to add a check against webpack 4/5 (see https://github.com/webpack-contrib/mini-css-extract-plugin/blob/master/src/index.js for reference) and then adapt to the new API.

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.