From e5cfd05f49f0f5061497d2d38c6d76206ebf545c Mon Sep 17 00:00:00 2001 From: Yuriy Ostapenko Date: Thu, 8 Jun 2017 14:18:27 +0300 Subject: [PATCH] Cache assets similar to how the normal compilation does it Previously extract-text-webpack-plugin emitted css assets every time anything else changed. Injected assets were also not included in compilation hash calculation and caused some rebuilds to be hidden. --- index.js | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 75ef6216..45bd56a0 100644 --- a/index.js +++ b/index.js @@ -4,6 +4,7 @@ */ var fs = require('fs'); var ConcatSource = require("webpack-sources").ConcatSource; +var CachedSource = require("webpack-sources").CachedSource; var async = require("async"); var ExtractedModule = require("./ExtractedModule"); var Chunk = require("webpack/lib/Chunk"); @@ -337,8 +338,22 @@ ExtractTextPlugin.prototype.apply = function(compiler) { }); var file = (isFunction(filename)) ? filename(getPath) : getPath(filename); - + + var cacheKey = "extracted " + file; + 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)) + }; + } + } + compilation.assets[file] = source; + compilation.modifyHash(chunkHash); chunk.files.push(file); } }, this);