You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
85 lines
1.7 KiB
85 lines
1.7 KiB
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) => {
|
|
|
|
const filePath = stats.outputPath + '/' + file
|
|
|
|
if (fs.existsSync(filePath)) {
|
|
this._results[stats.publicPath + file] = stats.publicPath + file + '?version=' + this._hash(filePath)
|
|
}
|
|
})
|
|
})
|
|
|
|
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(filePath) {
|
|
|
|
const data = fs.readFileSync(filePath, 'utf8')
|
|
const hash = crypto
|
|
.createHash('md5')
|
|
.update(data)
|
|
.digest('hex')
|
|
|
|
return hash
|
|
}
|
|
}
|
|
|
|
module.exports = Manifest
|