Skip to content

Instantly share code, notes, and snippets.

@boopathi
Created March 17, 2016 22:00
Show Gist options
  • Save boopathi/33a98faef4fef00d22a3 to your computer and use it in GitHub Desktop.
Save boopathi/33a98faef4fef00d22a3 to your computer and use it in GitHub Desktop.

Revisions

  1. boopathi created this gist Mar 17, 2016.
    60 changes: 60 additions & 0 deletions combine-chunks-plugin.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,60 @@
    var ConcatSource = require('webpack/lib/ConcatSource');
    var loaderUtils = require('loader-utils');

    module.exports = CombineChunksPlugin;

    // opts.filename = 'vendor.[contenthash].js'

    function CombineChunksPlugin(opts) {
    if (opts) {
    this.filename = opts.filename ? opts.filename : 'vendor.bundle.js';
    this.prelude = opts.prelude;
    }
    }

    CombineChunksPlugin.prototype.apply = function(compiler) {
    var filename = this.filename;
    var prelude = this.prelude;

    compiler.plugin('compilation', function(compilation) {

    // put chunks in correct order
    // prelude first - which will contain common chunk and webpackJsonp defn
    compilation.plugin('optimize-chunk-order', function(chunks) {
    chunks.sort(function(a, b) {
    if (a.name === prelude) return -1;
    if (b.name === prelude) return 1;
    return 0;
    });
    });

    // add our asset into compilation
    compilation.plugin('optimize-chunk-assets', function(chunks, callback) {
    var vendorSource = new ConcatSource('');
    chunks.forEach(function(chunk) {
    chunk.files.forEach(function(file) {
    vendorSource.add(compilation.assets[file]);
    vendorSource.add('\n');
    });
    });
    filename = filename.replace(/\[(?:(\w+):)?contenthash(?::([a-z]+\d*))?(?::(\d+))?\]/ig, function() {
    return loaderUtils.getHashDigest(vendorSource.source(), arguments[1], arguments[2], parseInt(arguments[3], 10));
    });
    compilation.assets[filename] = vendorSource;
    callback();
    });
    });

    // now we can clean up other chunks that we don't require after the compilation is over
    // note that we don't iterate over compilation.assets
    // we do this just before emit giving chance for other plugins
    // to add its last file as the compilation results to preserve state
    compiler.plugin('after-compile', function(compilation, callback) {
    compilation.chunks.forEach(function(chunk) {
    chunk.files.forEach(function(file) {
    delete compilation.assets[file];
    });
    });
    callback();
    });
    };