Created
November 27, 2017 16:08
-
-
Save adamkiss/ce468efddbd0343088bc02c884678cf1 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
module.exports = class Transform { | |
constructor (opts) { | |
this.opts = opts | |
this.apply = this.apply.bind(this) | |
} | |
apply (compiler) { | |
const transforms = (Array.isArray(this.opts)) ? this.opts : [this.opts] | |
compiler.plugin('emit', function(compilation, done) { | |
transforms.forEach(transform => { | |
const doesRename = transform.hasOwnProperty('rename') | |
const doesReplace = transform.hasOwnProperty('replace') | |
Object.keys(compilation.assets) | |
.filter(transform.match) | |
.forEach(name => { | |
let obj = compilation.assets[name] | |
if (doesReplace) { | |
let src = compilation.assets[name].source() | |
src = transform.replace(Buffer.isBuffer(src) ? src.toString() : src) | |
obj = { | |
source: () => src, | |
size: () => src.length | |
} | |
} | |
if (doesRename) { | |
compilation.assets[transform.rename(name)] = obj | |
delete compilation.assets[name] | |
} else { | |
compilation.assets[name] = obj | |
} | |
}) | |
}) | |
done() | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment