2021-10-07 00:04:16 +05:30
|
|
|
const fs = require('fs/promises');
|
|
|
|
const path = require('path');
|
|
|
|
|
|
|
|
module.exports = function injectWebManifest(manifestFile) {
|
|
|
|
let root;
|
|
|
|
let manifestHref;
|
|
|
|
return {
|
2021-10-11 21:29:31 +05:30
|
|
|
name: "hydrogen:injectWebManifest",
|
2021-10-07 00:04:16 +05:30
|
|
|
apply: "build",
|
|
|
|
configResolved: config => {
|
|
|
|
root = config.root;
|
|
|
|
},
|
|
|
|
transformIndexHtml: {
|
|
|
|
transform(html) {
|
|
|
|
return [{
|
|
|
|
tag: "link",
|
|
|
|
attrs: {rel: "manifest", href: manifestHref},
|
|
|
|
injectTo: "head"
|
|
|
|
}];
|
|
|
|
},
|
|
|
|
},
|
|
|
|
generateBundle: async function() {
|
|
|
|
const absoluteManifestFile = path.resolve(root, manifestFile);
|
|
|
|
const manifestDir = path.dirname(absoluteManifestFile);
|
|
|
|
const json = await fs.readFile(absoluteManifestFile, {encoding: "utf8"});
|
|
|
|
const manifest = JSON.parse(json);
|
|
|
|
for (const icon of manifest.icons) {
|
|
|
|
const iconFileName = path.resolve(manifestDir, icon.src);
|
|
|
|
const imgData = await fs.readFile(iconFileName);
|
|
|
|
const ref = this.emitFile({
|
|
|
|
type: "asset",
|
|
|
|
name: path.basename(iconFileName),
|
|
|
|
source: imgData
|
|
|
|
});
|
2021-12-06 19:55:44 +05:30
|
|
|
// we take the basename as getFileName gives the filename
|
|
|
|
// relative to the output dir, but the manifest is an asset
|
|
|
|
// just like they icon, so we assume they end up in the same dir
|
|
|
|
icon.src = path.basename(this.getFileName(ref));
|
2021-10-07 00:04:16 +05:30
|
|
|
}
|
|
|
|
const outputName = path.basename(absoluteManifestFile);
|
|
|
|
const manifestRef = this.emitFile({
|
|
|
|
type: "asset",
|
|
|
|
name: outputName,
|
|
|
|
source: JSON.stringify(manifest)
|
|
|
|
});
|
|
|
|
manifestHref = this.getFileName(manifestRef);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|