Procházet zdrojové kódy

feat: allow building with linked pkgs in the bundle

Sv443 před 1 rokem
rodič
revize
c41d87da46
3 změnil soubory, kde provedl 69 přidání a 13 odebrání
  1. 47 0
      assets/README.md
  2. 7 3
      assets/require.json
  3. 15 10
      rollup.config.mjs

+ 47 - 0
assets/README.md

@@ -0,0 +1,47 @@
+## Asset formats explained
+
+<br>
+
+### Images:
+- PNG images and external assets are stored in `assets/images`
+- SVG images are stored in `assets/icons`
+- CSS files in `assets/style`
+- Translations are in `assets/translations`
+
+<br>
+
+## JSON file formats:
+> [!NOTE]  
+> Note: If a property is wrapped with square brackets (`[name]`), it means that the property is optional
+
+<br>
+
+### [`locales.json`](locales.json)
+This file contains a definition of the available locales and translations.  
+The keys of the object are the locale codes, and the values are the locale objects, with the following properties:  
+  
+| Property         | Type       | Description                                                |
+| :--------------- | :--------- | :--------------------------------------------------------- |
+| `name`           | `string`   | The name of the locale in the locale's language            |
+| `nameEnglish`    | `string`   | The name of the locale in English                          |
+| `emoji`          | `string`   | The flag emoji of the locale                               |
+| `userscriptDesc` | `string`   | The description of the userscript in the locale's language |
+| `authors`        | `string[]` | The authors of the translations                            |
+
+<br>
+
+### [`plugins.json`](plugins.json)
+(Not implemented yet)
+
+<br>
+
+### [`require.json`](require.json)
+This file contains the npm libraries that are loaded into the runtime through the `@require` userscript directive.  
+It's an array of objects, which each have the following properties:  
+  
+| Property  | Type      | Description                                                                           |
+| :-------- | :-------- | :------------------------------------------------------------------------------------ |
+| `pkgName` | `string`  | The name of the npm package, as in `npm i <pkgName>`                                  |
+| `path`    | `string`  | The path to the file that should be loaded, relative to the library root dir          |
+| `global`  | `string`  | The name of the global variable that the library exports                              |
+| `[link]`  | `boolean` | Whether `npm link` is active and the library should instead be included in the bundle |

+ 7 - 3
assets/require.json

@@ -1,14 +1,18 @@
 [
   {
     "pkgName": "@sv443-network/userutils",
-    "path": "dist/index.global.js"
+    "path": "dist/index.global.js",
+    "global": "UserUtils",
+    "link": true
   },
   {
     "pkgName": "fuse.js",
-    "path": "dist/fuse.basic.js"
+    "path": "dist/fuse.basic.js",
+    "global": "Fuse"
   },
   {
     "pkgName": "marked",
-    "path": "lib/marked.umd.js"
+    "path": "lib/marked.umd.js",
+    "global": "marked"
   }
 ]

+ 15 - 10
rollup.config.mjs

@@ -6,6 +6,15 @@ import pluginCss from "rollup-plugin-import-css";
 import pluginExecute from "rollup-plugin-execute";
 import typescript from "typescript";
 
+import requireJson from "./assets/require.json" assert { type: "json" };
+
+const globalPkgs = requireJson.reduce((acc, pkg) => {
+  acc[pkg.pkgName] = pkg.global;
+  return acc;
+}, {});
+
+const externalPkgs = requireJson.map(pkg => pkg.pkgName);
+
 const outputDir = "dist";
 const outputFile = getOutputFileName();
 
@@ -26,6 +35,8 @@ export default (/**@type {import("./src/types").RollupArgs}*/ args) => (async ()
 
   const { mode, suffix } = passCliArgs;
 
+  const linkedPkgs = requireJson.filter((pkg) => pkg.link === true);
+
   /** @type {import("rollup").RollupOptions} */
   const config = {
     input: "src/index.ts",
@@ -52,11 +63,9 @@ export default (/**@type {import("./src/types").RollupArgs}*/ args) => (async ()
       format: "iife",
       sourcemap: mode === "development",
       compact: mode === "development",
-      globals: {
-        "@sv443-network/userutils": "UserUtils",
-        "fuse.js": "Fuse",
-        "marked": "marked",
-      },
+      globals: linkedPkgs.length > 0 ? Object.fromEntries(
+        Object.entries(globalPkgs).filter(([key]) => !linkedPkgs.some((pkg) => pkg.pkgName === key))
+      ) : globalPkgs,
     },
     onwarn(warning) {
       // ignore circular dependency warnings
@@ -65,11 +74,7 @@ export default (/**@type {import("./src/types").RollupArgs}*/ args) => (async ()
         console.error(`\x1b[33m(!)\x1b[0m ${message}\n`, rest);
       }
     },
-    external: [
-      "@sv443-network/userutils",
-      "fuse.js",
-      "marked",
-    ],
+    external: linkedPkgs.length > 0 ? externalPkgs.filter(p => !linkedPkgs.map(lp => lp.pkgName).includes(p)) : externalPkgs,
   };
 
   return config;