mirror of
https://github.com/vector-im/hydrogen-web.git
synced 2024-12-23 03:25:12 +01:00
transpile service worker and cleanup build plugin
This commit is contained in:
parent
180681b602
commit
c344032c0a
@ -8,42 +8,42 @@ function contentHash(str) {
|
|||||||
return hasher.digest();
|
return hasher.digest();
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = function injectServiceWorker(swFile) {
|
module.exports = function injectServiceWorker(swFile, otherUncachedFiles) {
|
||||||
|
const swName = path.basename(swFile);
|
||||||
let root;
|
let root;
|
||||||
let version;
|
let version;
|
||||||
let manifestHref;
|
|
||||||
return {
|
return {
|
||||||
name: "hydrogen:injectServiceWorker",
|
name: "hydrogen:injectServiceWorker",
|
||||||
apply: "build",
|
apply: "build",
|
||||||
enforce: "post",
|
enforce: "post",
|
||||||
|
buildStart() {
|
||||||
|
this.emitFile({
|
||||||
|
type: "chunk",
|
||||||
|
fileName: swName,
|
||||||
|
id: swFile,
|
||||||
|
});
|
||||||
|
},
|
||||||
configResolved: config => {
|
configResolved: config => {
|
||||||
root = config.root;
|
root = config.root;
|
||||||
version = JSON.parse(config.define.HYDROGEN_VERSION); // unquote
|
version = JSON.parse(config.define.HYDROGEN_VERSION); // unquote
|
||||||
},
|
},
|
||||||
generateBundle: async function(_, bundle) {
|
generateBundle: async function(options, bundle) {
|
||||||
const absoluteSwFile = path.resolve(root, swFile);
|
const uncachedFileNames = [swName].concat(otherUncachedFiles);
|
||||||
let swSource = await fs.readFile(absoluteSwFile, {encoding: "utf8"});
|
const uncachedFileContentMap = uncachedFileNames.reduce((map, fileName) => {
|
||||||
const index = bundle["index.html"];
|
const chunkOrAsset = bundle[fileName];
|
||||||
if (!index) {
|
if (!chunkOrAsset) {
|
||||||
console.log("index not found", index);
|
throw new Error("could not get content for uncached asset or chunk " + fileName);
|
||||||
}
|
}
|
||||||
const uncachedFileContentMap = {
|
map[fileName] = chunkOrAsset.source || chunkOrAsset.code;
|
||||||
"index.html": index.source,
|
return map;
|
||||||
"sw.js": swSource
|
}, {});
|
||||||
};
|
|
||||||
const assets = Object.values(bundle);
|
const assets = Object.values(bundle);
|
||||||
const cachedFileNames = assets.map(o => o.fileName).filter(fileName => fileName !== "index.html");
|
const cachedFileNames = assets.map(o => o.fileName).filter(fileName => !uncachedFileContentMap[fileName]);
|
||||||
const globalHash = getBuildHash(cachedFileNames, uncachedFileContentMap);
|
const globalHash = getBuildHash(cachedFileNames, uncachedFileContentMap);
|
||||||
swSource = await buildServiceWorker(swSource, version, globalHash, assets);
|
const sw = bundle[swName];
|
||||||
const outputName = path.basename(absoluteSwFile);
|
sw.code = replaceConstsInServiceWorker(sw.code, version, globalHash, assets);
|
||||||
// TODO: do normal build transformations for service worker too,
|
console.log(`\nBuilt ${version} (${globalHash})`);
|
||||||
// I think if we emit it as a chunk rather than an asset it would
|
|
||||||
// but we can't emit chunks anymore in generateBundle I think ...
|
|
||||||
this.emitFile({
|
|
||||||
type: "asset",
|
|
||||||
fileName: outputName,
|
|
||||||
source: swSource
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@ -75,7 +75,7 @@ function isPreCached(asset) {
|
|||||||
fileName.endsWith(".js") && !NON_PRECACHED_JS.includes(path.basename(name));
|
fileName.endsWith(".js") && !NON_PRECACHED_JS.includes(path.basename(name));
|
||||||
}
|
}
|
||||||
|
|
||||||
async function buildServiceWorker(swSource, version, globalHash, assets) {
|
function replaceConstsInServiceWorker(swSource, version, globalHash, assets) {
|
||||||
const unhashedPreCachedAssets = [];
|
const unhashedPreCachedAssets = [];
|
||||||
const hashedPreCachedAssets = [];
|
const hashedPreCachedAssets = [];
|
||||||
const hashedCachedOnRequestAssets = [];
|
const hashedCachedOnRequestAssets = [];
|
||||||
|
@ -50,7 +50,7 @@ export default {
|
|||||||
// important this comes before service worker
|
// important this comes before service worker
|
||||||
// otherwise the manifest and the icons it refers to won't be cached
|
// otherwise the manifest and the icons it refers to won't be cached
|
||||||
injectWebManifest("assets/manifest.json"),
|
injectWebManifest("assets/manifest.json"),
|
||||||
injectServiceWorker("sw.js"),
|
injectServiceWorker("./src/platform/web/sw.js", ["index.html"]),
|
||||||
],
|
],
|
||||||
define: {
|
define: {
|
||||||
"HYDROGEN_VERSION": JSON.stringify(version)
|
"HYDROGEN_VERSION": JSON.stringify(version)
|
||||||
|
Loading…
Reference in New Issue
Block a user