我正在努力完成这项吞噬任务。我有一个文件,我需要修改后期构建,该文件是使用 Angular Service Worker 的 PWA 包的一部分。作为修改的结果,我需要重新计算哈希并更新内容如下所示的ngsw.json
文件中的条目。
"hashTable": {
"/1.a634d7bd57cc0b60b07a.js": "f67c68837f048be737dcb1359072fc40c1f93144",
"/10.b18571adf198206cc401.js": "c59d8f124964b73456f22ed3a51efec0493ce7c5",
"/100.625f7b4770945172db3e.js": "da62af393973a8eb002874406e8a1dd828faecaf",
"/main.5be263c044e031156b6b.js": "5bfa4ec8a86ee2ab4a5f2f999b0742ac4a5ddbc7"
}
我知道需要哈希更新的文件的文件名,我有这个功能
let hashsum = require("gulp-hashsum");
function getHash() {
gulp.src(["www/main*.js"]).pipe(hashsum({
stream: true,
json: true
}
)).pipe(gulp.dest(
// How to replace ngsw.json 'hashTable' entry with this response? ));
}
我对gulp不是很熟悉,任何帮助将不胜感激。
您可以编写一个插件,该插件将从hashsum
中获取结果,并简单地使用fs
来修改您的 json。
这是一个幼稚的实现:
const fs = require('fs')
const path = require('path')
const through = require('through2')
const hashsum = require('gulp-hashsum')
const modifyJson = ({ fileName, src, property }) => through.obj((file, _, cb) => {
const { name } = path.parse(file.path)
if (name !== fileName) return cb(null, file)
const pathToJson = path.resolve(__dirname, src)
if (!fs.existsSync(pathToJson)) {
return cb(new Error(`${src} doesn't exist.`), file)
}
const json = JSON.parse(fs.readFileSync(pathToJson, 'utf8'))
const content = JSON.parse(file.contents)
if (typeof json[property] === 'undefined') console.warn(`${src} doesn't has any property named '${property}' A new one will be created.`)
json[property] = content
fs.writeFileSync(pathToJson, JSON.stringify(json))
return cb(null, file)
})
用法:
exports.modifyJson = () =>
src(['app/**/*.js'])
.pipe(hashsum({
stream: true,
json: true,
}))
.pipe(modifyJson({
fileName: 'SHA1SUMS',
src: './test.json',
property: 'hashTable',
}))
我在这里有一个要点上的这段代码。