From f30782a9bc470110c4fd670a9bbd08273e19741e Mon Sep 17 00:00:00 2001 From: Sv443 Date: Sun, 17 Nov 2024 18:45:16 +0100 Subject: [PATCH] ref: elaborate on resource value resolution --- contributing.md | 3 ++- src/tools/post-build.ts | 8 ++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/contributing.md b/contributing.md index a879120c8..483dc5aaf 100644 --- a/contributing.md +++ b/contributing.md @@ -193,7 +193,8 @@ The main files you will be working with are: - Adding an asset (image, icon, stylesheet, translation file and misc. other files): 1. Add the asset to the `assets` folder in the root of the project, under the correct subfolder 2. Add the asset to the [`assets/resources.json`](./assets/resources.json) file by following the format of the other entries. - If the path begins with a slash, it will start at the project root (where package.json is), otherwise it will start at the `assets` folder. + If the path begins with a slash, it will start at the project root (where package.json is), otherwise it will start at the `assets` folder. + The path string or all values in the object of each resource will be passed through the function `resolveResourceVal()` in [`src/tools/post-build.ts`](./src/tools/post-build.ts) to resolve placeholders like `$BRANCH`. View all replacements by looking up that function. 3. The asset will be immediately available in the userscript after the next build and the `@resource` directive will automatically point at the locally served asset or the GitHub CDN, depending on the build mode. 4. **When committing, make sure to commit the assets first, then rebuild the userscript and make a second commit.** This needs to be done because the build script at `src/tools/post-build.ts` will use the *previous* commit hash to create version-independent URLs for the assets. These will continue to work in the future, instead of pointing to an ever-changing branch where files could be moved, renamed or deleted at any time. diff --git a/src/tools/post-build.ts b/src/tools/post-build.ts index 3c7680598..6a010e260 100644 --- a/src/tools/post-build.ts +++ b/src/tools/post-build.ts @@ -254,8 +254,8 @@ async function exists(path: string) { } /** Resolves the value of an entry in resources.json */ -function resolveVal(value: string, buildNbr: string) { - if(!value.includes("$")) +function resolveResourceVal(value: string, buildNbr: string) { + if(!(/\$/.test(value))) return value; const replacements = [ @@ -284,8 +284,8 @@ async function getResourceDirectives(ref: string) { ? await getFileHashSha256(pathVal.replace(/\?.+/g, "")) : undefined; resourcesHashed[name] = typeof val === "object" - ? { path: resolveVal(val.path, ref), ref: resolveVal(val.ref, ref), hash } - : { path: getResourceUrl(resolveVal(val, ref), ref), ref, hash }; + ? { path: resolveResourceVal(val.path, ref), ref: resolveResourceVal(val.ref, ref), hash } + : { path: getResourceUrl(resolveResourceVal(val, ref), ref), ref, hash }; } const addResourceHashed = async (name: string, path: string, ref: string) => {