Created
February 13, 2017 16:46
-
-
Save a-ignatov-parc/64d6fe5dbd08b9ddb931544d8c1c72e7 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
const defaults = { | |
filename: 'versions.json', | |
}; | |
class HashDictionaryPlugin { | |
constructor(options) { | |
this.options = Object.assign({}, defaults, options); | |
} | |
apply(compiler) { | |
compiler.plugin('emit', (compilation, done) => { | |
const { | |
context, | |
outputPath, | |
} = compilation.compiler; | |
const stats = compilation | |
.getStats() | |
.toJson({ | |
source: false, | |
modules: false, | |
publicPath: true, | |
}); | |
const files = stats.chunks | |
.reduce((result, chunk) => { | |
const {hash} = chunk; | |
const files = chunk.files.map(basename => { | |
const filepath = path.join(outputPath, basename); | |
return { | |
hash, | |
basename, | |
cwd: context, | |
path: filepath, | |
dirname: outputPath, | |
relative: path.relative(context, filepath), | |
}; | |
}); | |
return result.concat(files); | |
}, []) | |
.reduce((result, record) => { | |
result[record.relative] = record; | |
return result; | |
}, {}); | |
const contents = JSON.stringify(files); | |
compilation.assets[this.options.filename] = { | |
source() { | |
return contents; | |
}, | |
size() { | |
return contents.length; | |
}, | |
}; | |
done(); | |
}); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment