Browse Source

feat: build svg spritesheet

Sv443 2 months ago
parent
commit
fe2c0c5d6d
3 changed files with 64 additions and 1 deletions
  1. 26 0
      assets/icons/spritesheet.svg
  2. 2 1
      package.json
  3. 36 0
      src/tools/post-build.ts

File diff suppressed because it is too large
+ 26 - 0
assets/icons/spritesheet.svg


+ 2 - 1
package.json

@@ -132,7 +132,8 @@
     "ignore": [
       "dist/*",
       "dev/*",
-      "*/stories/*"
+      "*/stories/*",
+      "assets/**/spritesheet.svg"
     ]
   }
 }

+ 36 - 0
src/tools/post-build.ts

@@ -188,6 +188,8 @@ I welcome every contribution on GitHub!
       }
     }
 
+    await createSvgSpritesheet(buildNbr);
+
     console.info([
       "",
       `Successfully built for ${envText} - build number (last commit SHA): ${buildNbr}`,
@@ -501,3 +503,37 @@ function getFileHashSha256(path: string): Promise<string> {
     stream.on("error", rej);
   });
 }
+
+/** Compiles all `icon-*` assets into a single SVG spritesheet file and writes it to `assets/icons/spritesheet.svg` */
+async function createSvgSpritesheet(buildNbr: string) {
+  try {
+    const sprites: string[] = [],
+      promises: Promise<void>[] = [];
+
+    for(const [name, val] of Object.entries(resourcesJson.resources)) {
+      if(!/^icon-/.test(name))
+        continue;
+
+      promises.push(new Promise(async (res) => {
+        const iconPath = resolveResourcePath(typeof val === "string" ? val : val.path);
+        const iconSvg = String(await readFile(iconPath)).replace(/\n/g, "");
+        const viewBox = iconSvg.match(/viewBox="([^"]+)"/)?.[1] ?? "0 0 24 24";
+
+        sprites.push(`<symbol id="bytm-svg-${name}" viewBox="${viewBox}">\n    ${iconSvg}\n  </symbol>`);
+        res();
+      }));
+    }
+
+    await Promise.allSettled(promises);
+
+    await writeFile(resolveResourcePath("icons/spritesheet.svg"), `\
+<svg xmlns="http://www.w3.org/2000/svg" id="bytm-svg-icon-container" data-build="${buildNbr}" style="display: none;" inert="true">
+  ${sprites.join("\n  ")}
+</svg>`
+    );
+  }
+  catch(err) {
+    console.error(k.red("Error while creating SVG spritesheet:"), err);
+    return schedExit(1);
+  }
+}

Some files were not shown because too many files changed in this diff