Skip to content

Instantly share code, notes, and snippets.

@yuriyostapenko
Created June 8, 2017 11:24
Show Gist options
  • Save yuriyostapenko/106c3cb407bf5dd38ebd6be0710afd4a to your computer and use it in GitHub Desktop.
Save yuriyostapenko/106c3cb407bf5dd38ebd6be0710afd4a to your computer and use it in GitHub Desktop.
/* this plugin fixes missing asset caching support in extract-text-webpack-plugin, preventing unnecessary rebuilds */
const { CachedSource, ConcatSource } = require('webpack-sources');
const loaderUtils = require('loader-utils');
module.exports = class {
apply(compiler) {
compiler.plugin("compilation", function (compilation) {
compilation.plugin("optimize-assets", function (assets, callback) {
if (!assets) {
return;
}
Object.keys(assets).forEach((asset) => {
let source = assets[asset]
if (source instanceof CachedSource) {
return;
}
if (!(source instanceof ConcatSource || /\.map$/.test(asset))) {
// only process ConcatSource, as this plugin's main purpose is to fix extract-text-webpack-plugin output caching
// would probably make sense to generalize and filter out RawSource instead, but source maps are also RawSource in extract-text-webpack-plugin
return;
}
var cacheKey = 'extraCache ' + asset;
var chunkHash = loaderUtils.getHashDigest(source.source());
if (compilation.cache && compilation.cache[cacheKey] && compilation.cache[cacheKey].hash === chunkHash) {
source = compilation.cache[cacheKey].source;
} else if (compilation.cache) {
compilation.cache[cacheKey] = {
hash: chunkHash,
source: source = (source instanceof CachedSource ? source : new CachedSource(source))
};
}
assets[asset] = source;
compilation.modifyHash(chunkHash);
})
callback();
});
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment