parent
879fe1df39
commit
7b837b90f1
@ -0,0 +1,79 @@
|
||||
const fs = require('fs')
|
||||
const crypto = require('crypto')
|
||||
|
||||
/**
|
||||
* Manifest
|
||||
*
|
||||
* creates manifest-file
|
||||
*
|
||||
* @author Björn Hase <me@herr-hase.wtf>
|
||||
* @license http://opensource.org/licenses/MIT The MIT License
|
||||
* @link https://git.node001.net/tiny-components/webpack.git
|
||||
*
|
||||
*/
|
||||
|
||||
class Manifest {
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @param {string} filename
|
||||
* @param {string} context
|
||||
*
|
||||
*/
|
||||
constructor(filename, context) {
|
||||
|
||||
// filename for manifest-file
|
||||
this._filename = filename
|
||||
|
||||
// context
|
||||
this._context = context
|
||||
|
||||
// results for write to file
|
||||
this._results = {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* run through all assets from assetsByChunkName
|
||||
* for each file create a hash
|
||||
*
|
||||
* @param {object} stats
|
||||
*
|
||||
*/
|
||||
run(stats) {
|
||||
|
||||
let files = Object.assign({}, stats.assetsByChunkName)
|
||||
|
||||
Object.keys(files).forEach((key) => {
|
||||
files[key].forEach((file) => {
|
||||
this._results[stats.publicPath + file] = stats.publicPath + file + '?version=' + this._hash(file, stats.outputPath)
|
||||
})
|
||||
})
|
||||
|
||||
fs.writeFileSync(this._context + '/' + this._filename, JSON.stringify(this._results, null, 4))
|
||||
}
|
||||
|
||||
/**
|
||||
* hash content of file
|
||||
*
|
||||
* @param {string} file
|
||||
* @param {sring} stats
|
||||
* @return {string}
|
||||
*
|
||||
*/
|
||||
_hash(file, outputPath) {
|
||||
|
||||
const data = fs.readFileSync(outputPath + '/' + file, 'utf8')
|
||||
const hash = crypto
|
||||
.createHash('md5')
|
||||
.update(data)
|
||||
.digest('hex')
|
||||
|
||||
return hash
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Manifest
|
@ -0,0 +1,43 @@
|
||||
const Manifest = require('./../manifest.js')
|
||||
const fs = require('fs')
|
||||
|
||||
/**
|
||||
* Plugin for Manifest
|
||||
*
|
||||
* @author Björn Hase <me@herr-hase.wtf>
|
||||
* @license http://opensource.org/licenses/MIT The MIT License
|
||||
* @link https://git.node001.net/tiny-components/webpack.git
|
||||
*
|
||||
*/
|
||||
class ManifestPlugin {
|
||||
|
||||
constructor(options) {
|
||||
this._filename = options.filename,
|
||||
this._context = options.context
|
||||
}
|
||||
|
||||
apply(compiler) {
|
||||
|
||||
const pluginName = ManifestPlugin.name
|
||||
|
||||
// webpack module instance can be accessed from the compiler object,
|
||||
// this ensures that correct version of the module is used
|
||||
// (do not require/import the webpack or any symbols from it directly).
|
||||
const { webpack } = compiler
|
||||
|
||||
// Compilation object gives us reference to some useful constants.
|
||||
const { Compilation } = webpack
|
||||
|
||||
// create manifest object
|
||||
const manifest = new Manifest(this._filename, this._context)
|
||||
|
||||
compiler.hooks.emit.tapAsync(pluginName, (compilation, callback) => {
|
||||
const stats = compilation.getStats().toJson()
|
||||
|
||||
manifest.run(stats)
|
||||
callback()
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = ManifestPlugin
|
Loading…
Reference in new issue