Bläddra i källkod

ref: postbuild

Sv443 1 år sedan
förälder
incheckning
f9c801bc28
3 ändrade filer med 22 tillägg och 44 borttagningar
  1. 2 37
      dist/BetterYTM.user.js
  2. 1 1
      src/index.ts
  3. 19 6
      src/tools/post-build.ts

+ 2 - 37
dist/BetterYTM.user.js

@@ -33,41 +33,6 @@
 /* Disclaimer: I am not affiliated with YouTube, Google, Alphabet, Genius or anyone else */
 /* C&D this 🖕 */
 
-// ==UserScript==
-// @name            BetterYTM
-// @homepageURL     https://github.com/Sv443/BetterYTM#readme
-// @namespace       https://github.com/Sv443/BetterYTM
-// @version         1.0.0
-// @description     Configurable layout and UX improvements for YouTube Music
-// @description:de  Konfigurierbares Layout und UX-Verbesserungen für YouTube Music
-// @license         MIT
-// @author          Sv443
-// @copyright       Sv443 (https://github.com/Sv443)
-// @icon            https://raw.githubusercontent.com/Sv443/BetterYTM/develop/assets/icon/icon.png
-// @match           https://music.youtube.com/*
-// @match           https://www.youtube.com/*
-// @run-at          document-start
-// @downloadURL     https://raw.githubusercontent.com/Sv443/BetterYTM/develop/dist/BetterYTM.user.js
-// @updateURL       https://raw.githubusercontent.com/Sv443/BetterYTM/develop/dist/BetterYTM.user.js
-// @connect         api.sv443.net
-// @grant           GM.getValue
-// @grant           GM.setValue
-// @grant           unsafeWindow
-// ==/UserScript==
-/*
- ▄▄▄                    ▄   ▄▄▄▄▄▄   ▄
- █  █ ▄▄▄ █   █   ▄▄▄ ▄ ▄█ █  █  █▀▄▀█
- █▀▀▄ █▄█ █▀  █▀  █▄█ █▀  █   █  █   █
- █▄▄▀ ▀▄▄ ▀▄▄ ▀▄▄ ▀▄▄ █   █   █  █   █
-
-         Made with ❤️ by Sv443
- I welcome every contribution on GitHub!
-   https://github.com/Sv443/BetterYTM
-*/
-
-/* Disclaimer: I am not affiliated with YouTube, Google, Alphabet, Genius or anyone else */
-/* C&D this 🖕 */
-
 /******/ var __webpack_modules__ = ({
 
 /***/ "./node_modules/@billjs/event-emitter/lib/index.js":
@@ -512,7 +477,7 @@ const scriptInfo = Object.freeze({
     name: GM.info.script.name,
     version: GM.info.script.version,
     namespace: GM.info.script.namespace,
-    lastCommit: "fae706c", // assert as generic string instead of union
+    lastCommit: "9360180", // assert as generic string instead of union
 });
 
 
@@ -2217,7 +2182,7 @@ function init() {
 /** Called when the DOM has finished loading and can be queried and altered by the userscript */
 function onDomLoad() {
     return __awaiter(this, void 0, void 0, function* () {
-        // post-build these double quotes are replaced by backticks
+        // post-build these double quotes are replaced by backticks (if backticks are used here, webpack converts them to double quotes)
         (0,_utils__WEBPACK_IMPORTED_MODULE_2__.addGlobalStyle)(`/*!***************************************************************************!*\
   !*** css ./node_modules/css-loader/dist/cjs.js!./src/features/layout.css ***!
   \***************************************************************************/

+ 1 - 1
src/index.ts

@@ -52,7 +52,7 @@ async function init() {
 
 /** Called when the DOM has finished loading and can be queried and altered by the userscript */
 async function onDomLoad() {
-  // post-build these double quotes are replaced by backticks
+  // post-build these double quotes are replaced by backticks (if backticks are used here, webpack converts them to double quotes)
   addGlobalStyle("{{GLOBAL_STYLE}}", "global");
 
   const features = await loadFeatureConf();

+ 19 - 6
src/tools/post-build.ts

@@ -69,10 +69,15 @@ const header = `\
       globalStyle = remSourcemapComments(globalStyle);
 
     // read userscript and inject build number and global CSS
-    let userscript = String(await readFile(scriptPath))
-      .replace(/\/?\*?{{BRANCH}}\*?\/?/gm, branch)
-      .replace(/\/?\*?{{BUILD_NUMBER}}\*?\/?/gm, lastCommitSha)
-      .replace(/"\/?\*?{{GLOBAL_STYLE}}\*?\/?"/gm, `\`${globalStyle}\``);
+    let userscript = insertValues(
+      String(await readFile(scriptPath)),
+      {
+        BRANCH: branch,
+        BUILD_NUMBER: lastCommitSha,
+      },
+    )
+      // needs special treatment because the double quotes need to be replaced with backticks
+      .replace(/"(\/\*)?{{GLOBAL_STYLE}}(\*\/)?"/gm, `\`${globalStyle}\``);
 
     if(mode === "production")
       userscript = remSourcemapComments(userscript);
@@ -102,11 +107,19 @@ const header = `\
   }
 })();
 
+type Stringifiable = { toString(): string; } | string;
+
+/** Replaces tokens in the format `{{key}}` or `/⋆{{key}}⋆/` of the `replacements` param with their respective value */
+function insertValues(userscript: string, replacements: Record<string, Stringifiable>) {
+  for(const key in replacements)
+    userscript = userscript.replace(new RegExp(`(\\/\\*)?{{${key}}}(\\*\\/)?`), String(replacements[key]));
+  return userscript;
+}
+
 /** Removes sourcemapping comments */
 function remSourcemapComments(input: string) {
   return input
-    .replace(/\n\s*\/\*\s?#.+\*\//gm, "")
-    .replace(/\n\s*\/\/\s?#.+$/gm, "");
+    .replace(/\n\s*\/(\*|\/)\s?#.+(\*\/)?$/gm, "");
 }
 
 /**