Created
June 8, 2017 11:24
-
-
Save yuriyostapenko/106c3cb407bf5dd38ebd6be0710afd4a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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