2024-08-16 12:03:39 +02:00
|
|
|
const path = require("path");
|
|
|
|
const xxhash = require("xxhashjs");
|
2021-10-06 20:34:16 +02:00
|
|
|
|
|
|
|
function contentHash(str) {
|
|
|
|
var hasher = new xxhash.h32(0);
|
|
|
|
hasher.update(str);
|
|
|
|
return hasher.digest();
|
|
|
|
}
|
|
|
|
|
2024-08-16 12:03:39 +02:00
|
|
|
function injectServiceWorker(
|
|
|
|
swFile,
|
|
|
|
findUnhashedFileNamesFromBundle,
|
|
|
|
placeholdersPerChunk
|
|
|
|
) {
|
2021-12-09 11:39:28 +01:00
|
|
|
const swName = path.basename(swFile);
|
2021-10-07 15:09:16 +02:00
|
|
|
let version;
|
2021-12-17 14:28:25 +01:00
|
|
|
let logger;
|
2024-08-16 12:03:39 +02:00
|
|
|
let mode;
|
2021-12-09 11:39:28 +01:00
|
|
|
|
2021-10-06 20:34:16 +02:00
|
|
|
return {
|
2021-10-11 17:59:31 +02:00
|
|
|
name: "hydrogen:injectServiceWorker",
|
2021-10-06 20:34:16 +02:00
|
|
|
apply: "build",
|
|
|
|
enforce: "post",
|
2024-08-16 12:03:39 +02:00
|
|
|
|
2021-12-09 11:39:28 +01:00
|
|
|
buildStart() {
|
|
|
|
this.emitFile({
|
|
|
|
type: "chunk",
|
|
|
|
fileName: swName,
|
|
|
|
id: swFile,
|
|
|
|
});
|
|
|
|
},
|
2024-08-16 12:03:39 +02:00
|
|
|
|
|
|
|
configResolved: (config) => {
|
|
|
|
mode = config.mode;
|
2021-12-09 16:37:31 +01:00
|
|
|
version = JSON.parse(config.define.DEFINE_VERSION); // unquote
|
2021-12-17 14:28:25 +01:00
|
|
|
logger = config.logger;
|
2021-10-06 20:34:16 +02:00
|
|
|
},
|
2024-08-16 12:03:39 +02:00
|
|
|
|
|
|
|
generateBundle: async function (options, bundle) {
|
2022-05-18 14:01:17 +02:00
|
|
|
const otherUnhashedFiles = findUnhashedFileNamesFromBundle(bundle);
|
2021-12-09 14:36:12 +01:00
|
|
|
const unhashedFilenames = [swName].concat(otherUnhashedFiles);
|
2024-08-16 12:03:39 +02:00
|
|
|
const unhashedFileContentMap = unhashedFilenames.reduce(
|
|
|
|
(map, fileName) => {
|
|
|
|
const chunkOrAsset = bundle[fileName];
|
|
|
|
if (!chunkOrAsset) {
|
|
|
|
throw new Error(
|
|
|
|
"could not get content for uncached asset or chunk " +
|
|
|
|
fileName
|
|
|
|
);
|
|
|
|
}
|
|
|
|
map[fileName] = chunkOrAsset.source || chunkOrAsset.code;
|
|
|
|
return map;
|
|
|
|
},
|
|
|
|
{}
|
|
|
|
);
|
2021-12-06 15:34:39 +01:00
|
|
|
const assets = Object.values(bundle);
|
2024-08-16 12:03:39 +02:00
|
|
|
const hashedFileNames = assets
|
|
|
|
.map((o) => o.fileName)
|
|
|
|
.filter((fileName) => !unhashedFileContentMap[fileName]);
|
|
|
|
const globalHash = getBuildHash(
|
|
|
|
hashedFileNames,
|
|
|
|
unhashedFileContentMap
|
|
|
|
);
|
2021-12-09 16:37:31 +01:00
|
|
|
const placeholderValues = {
|
|
|
|
DEFINE_GLOBAL_HASH: `"${globalHash}"`,
|
2024-08-16 12:03:39 +02:00
|
|
|
...getCacheFileNamePlaceholderValues(
|
|
|
|
swName,
|
|
|
|
unhashedFilenames,
|
|
|
|
assets,
|
|
|
|
mode
|
|
|
|
),
|
2021-12-09 16:37:31 +01:00
|
|
|
};
|
2024-08-16 12:03:39 +02:00
|
|
|
replacePlaceholdersInChunks(
|
|
|
|
assets,
|
|
|
|
placeholdersPerChunk,
|
|
|
|
placeholderValues
|
|
|
|
);
|
2021-12-17 14:28:25 +01:00
|
|
|
logger.info(`\nBuilt ${version} (${globalHash})`);
|
2024-08-16 12:03:39 +02:00
|
|
|
},
|
2021-10-06 20:34:16 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-12-09 14:36:12 +01:00
|
|
|
function getBuildHash(hashedFileNames, unhashedFileContentMap) {
|
2024-08-16 12:03:39 +02:00
|
|
|
const unhashedHashes = Object.entries(unhashedFileContentMap).map(
|
|
|
|
([fileName, content]) => {
|
|
|
|
return `${fileName}-${contentHash(Buffer.from(content))}`;
|
|
|
|
}
|
|
|
|
);
|
2021-12-09 14:36:12 +01:00
|
|
|
const globalHashAssets = hashedFileNames.concat(unhashedHashes);
|
2021-10-06 20:34:16 +02:00
|
|
|
globalHashAssets.sort();
|
|
|
|
return contentHash(globalHashAssets.join(",")).toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
const NON_PRECACHED_JS = [
|
2021-10-11 17:59:31 +02:00
|
|
|
"hydrogen-legacy",
|
2021-10-06 20:34:16 +02:00
|
|
|
"olm_legacy.js",
|
2024-08-16 12:03:39 +02:00
|
|
|
// most environments don't need the worker
|
|
|
|
"main.js",
|
2021-10-06 20:34:16 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
function isPreCached(asset) {
|
2024-08-16 12:03:39 +02:00
|
|
|
const { name, fileName } = asset;
|
|
|
|
return (
|
|
|
|
name?.endsWith(".svg") ||
|
|
|
|
name?.endsWith(".png") ||
|
|
|
|
name?.endsWith(".css") ||
|
|
|
|
name?.endsWith(".wasm") ||
|
|
|
|
name?.endsWith(".html") ||
|
|
|
|
// the index and vendor chunks don't have an extension in `name`, so check extension on `fileName`
|
|
|
|
(fileName.endsWith(".js") &&
|
|
|
|
!NON_PRECACHED_JS.includes(path.basename(name)))
|
|
|
|
);
|
2021-10-06 20:34:16 +02:00
|
|
|
}
|
|
|
|
|
2024-08-16 12:03:39 +02:00
|
|
|
function getCacheFileNamePlaceholderValues(
|
|
|
|
swName,
|
|
|
|
unhashedFilenames,
|
|
|
|
assets,
|
|
|
|
mode
|
|
|
|
) {
|
2021-10-06 20:34:16 +02:00
|
|
|
const unhashedPreCachedAssets = [];
|
|
|
|
const hashedPreCachedAssets = [];
|
|
|
|
const hashedCachedOnRequestAssets = [];
|
|
|
|
|
2024-08-16 12:03:39 +02:00
|
|
|
if (mode === "production") {
|
|
|
|
for (const asset of assets) {
|
|
|
|
const { name, fileName } = asset;
|
|
|
|
// the service worker should not be cached at all,
|
|
|
|
// it's how updates happen
|
|
|
|
if (fileName === swName) {
|
|
|
|
continue;
|
|
|
|
} else if (unhashedFilenames.includes(fileName)) {
|
|
|
|
unhashedPreCachedAssets.push(fileName);
|
|
|
|
} else if (isPreCached(asset)) {
|
|
|
|
hashedPreCachedAssets.push(fileName);
|
|
|
|
} else {
|
|
|
|
hashedCachedOnRequestAssets.push(fileName);
|
|
|
|
}
|
2021-10-06 20:34:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-09 16:37:31 +01:00
|
|
|
return {
|
2024-08-16 12:03:39 +02:00
|
|
|
DEFINE_UNHASHED_PRECACHED_ASSETS: JSON.stringify(
|
|
|
|
unhashedPreCachedAssets
|
|
|
|
),
|
2021-12-09 16:37:31 +01:00
|
|
|
DEFINE_HASHED_PRECACHED_ASSETS: JSON.stringify(hashedPreCachedAssets),
|
2024-08-16 12:03:39 +02:00
|
|
|
DEFINE_HASHED_CACHED_ON_REQUEST_ASSETS: JSON.stringify(
|
|
|
|
hashedCachedOnRequestAssets
|
|
|
|
),
|
|
|
|
};
|
2021-10-06 20:34:16 +02:00
|
|
|
}
|
2021-12-09 12:15:17 +01:00
|
|
|
|
2024-08-16 12:03:39 +02:00
|
|
|
function replacePlaceholdersInChunks(
|
|
|
|
assets,
|
|
|
|
placeholdersPerChunk,
|
|
|
|
placeholderValues
|
|
|
|
) {
|
2021-12-09 16:37:31 +01:00
|
|
|
for (const [name, placeholderMap] of Object.entries(placeholdersPerChunk)) {
|
2024-08-16 12:03:39 +02:00
|
|
|
const chunk = assets.find((a) => a.type === "chunk" && a.name === name);
|
2021-12-09 12:15:17 +01:00
|
|
|
if (!chunk) {
|
2024-08-16 12:03:39 +02:00
|
|
|
throw new Error(
|
|
|
|
`could not find chunk ${name} to replace placeholders`
|
|
|
|
);
|
2021-12-09 16:37:31 +01:00
|
|
|
}
|
2024-08-16 12:03:39 +02:00
|
|
|
for (const [placeholderName, placeholderLiteral] of Object.entries(
|
|
|
|
placeholderMap
|
|
|
|
)) {
|
2021-12-09 16:37:31 +01:00
|
|
|
const replacedValue = placeholderValues[placeholderName];
|
|
|
|
const oldCode = chunk.code;
|
2024-08-16 12:03:39 +02:00
|
|
|
chunk.code = chunk.code.replaceAll(
|
|
|
|
placeholderLiteral,
|
|
|
|
replacedValue
|
|
|
|
);
|
2021-12-09 16:37:31 +01:00
|
|
|
if (chunk.code === oldCode) {
|
2024-08-16 12:03:39 +02:00
|
|
|
throw new Error(
|
|
|
|
`Could not replace ${placeholderName} in ${name}, looking for literal ${placeholderLiteral}`
|
|
|
|
);
|
2021-12-09 16:37:31 +01:00
|
|
|
}
|
2021-12-09 12:15:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-12-09 16:37:31 +01:00
|
|
|
|
|
|
|
/** creates a value to be include in the `define` build settings,
|
|
|
|
* but can be replace at the end of the build in certain chunks.
|
|
|
|
* We need this for injecting the global build hash and the final
|
|
|
|
* filenames in the service worker and index chunk.
|
|
|
|
* These values are only known in the generateBundle step, so we
|
|
|
|
* replace them by unique strings wrapped in a prompt call so no
|
|
|
|
* transformation will touch them (minifying, ...) and we can do a
|
|
|
|
* string replacement still at the end of the build. */
|
|
|
|
function definePlaceholderValue(mode, name, devValue) {
|
2024-08-16 12:03:39 +02:00
|
|
|
if (mode === "production" || mode === "sdk") {
|
2021-12-09 16:37:31 +01:00
|
|
|
// note that `prompt(...)` will never be in the final output, it's replaced by the final value
|
|
|
|
// once we know at the end of the build what it is and just used as a temporary value during the build
|
|
|
|
// as something that will not be transformed.
|
|
|
|
// I first considered Symbol but it's not inconceivable that babel would transform this.
|
|
|
|
return `prompt(${JSON.stringify(name)})`;
|
|
|
|
} else {
|
|
|
|
return JSON.stringify(devValue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-16 12:03:39 +02:00
|
|
|
/**
|
|
|
|
* Returns the short sha for the latest git commit
|
|
|
|
* @see https://stackoverflow.com/a/35778030
|
|
|
|
*/
|
|
|
|
function getLatestGitCommitHash() {
|
|
|
|
return require("child_process")
|
|
|
|
.execSync("git rev-parse --short HEAD")
|
|
|
|
.toString()
|
|
|
|
.trim();
|
|
|
|
}
|
|
|
|
|
2021-12-09 16:37:31 +01:00
|
|
|
function createPlaceholderValues(mode) {
|
|
|
|
return {
|
2024-08-16 12:03:39 +02:00
|
|
|
DEFINE_GLOBAL_HASH: definePlaceholderValue(
|
|
|
|
mode,
|
|
|
|
"DEFINE_GLOBAL_HASH",
|
|
|
|
`git commit: ${getLatestGitCommitHash()}`
|
|
|
|
),
|
|
|
|
DEFINE_UNHASHED_PRECACHED_ASSETS: definePlaceholderValue(
|
|
|
|
mode,
|
|
|
|
"UNHASHED_PRECACHED_ASSETS",
|
|
|
|
[]
|
|
|
|
),
|
|
|
|
DEFINE_HASHED_PRECACHED_ASSETS: definePlaceholderValue(
|
|
|
|
mode,
|
|
|
|
"HASHED_PRECACHED_ASSETS",
|
|
|
|
[]
|
|
|
|
),
|
|
|
|
DEFINE_HASHED_CACHED_ON_REQUEST_ASSETS: definePlaceholderValue(
|
|
|
|
mode,
|
|
|
|
"HASHED_CACHED_ON_REQUEST_ASSETS",
|
|
|
|
[]
|
|
|
|
),
|
2021-12-09 16:37:31 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-08-16 12:03:39 +02:00
|
|
|
module.exports = { injectServiceWorker, createPlaceholderValues };
|