Browse Source

feat: add manual package linking

Sven 9 months ago
parent
commit
abc2ad6d62
2 changed files with 25 additions and 8 deletions
  1. 3 5
      rollup.config.mjs
  2. 22 3
      src/tools/post-build.ts

+ 3 - 5
rollup.config.mjs

@@ -35,7 +35,7 @@ export default (/**@type {import("./src/types").RollupArgs}*/ args) => (async ()
 
   const { mode, suffix } = passCliArgs;
 
-  const linkedPkgs = requireJson.filter((pkg) => pkg.link === true);
+  const linkedPkgs = requireJson.filter((pkg) => typeof pkg.link === "string");
 
   /** @type {import("rollup").RollupOptions} */
   const config = {
@@ -62,9 +62,7 @@ export default (/**@type {import("./src/types").RollupArgs}*/ args) => (async ()
       format: "iife",
       sourcemap: mode === "development",
       compact: mode === "development",
-      globals: linkedPkgs.length > 0 ? Object.fromEntries(
-        Object.entries(globalPkgs).filter(([key]) => !linkedPkgs.some((pkg) => pkg.pkgName === key))
-      ) : globalPkgs,
+      globals: linkedPkgs.length > 0 ? Object.fromEntries(Object.entries(globalPkgs)) : globalPkgs,
     },
     onwarn(warning) {
       // ignore circular dependency warnings
@@ -73,7 +71,7 @@ export default (/**@type {import("./src/types").RollupArgs}*/ args) => (async ()
         console.error(`\x1b[33m(!)\x1b[0m ${message}\n`, rest);
       }
     },
-    external: linkedPkgs.length > 0 ? externalPkgs.filter(p => !linkedPkgs.map(lp => lp.pkgName).includes(p)) : externalPkgs,
+    external: externalPkgs,
   };
 
   return config;

+ 22 - 3
src/tools/post-build.ts

@@ -1,5 +1,5 @@
 import { access, readFile, writeFile, constants as fsconst } from "node:fs/promises";
-import { dirname, join, relative } from "node:path";
+import { dirname, join, relative, resolve } from "node:path";
 import { fileURLToPath } from "node:url";
 import { randomUUID } from "node:crypto";
 import { exec } from "node:child_process";
@@ -149,7 +149,7 @@ I welcome every contribution on GitHub!
       userscript = userscript.replace(/sourceMappingURL=/gm, `sourceMappingURL=http://localhost:${devServerPort}/`);
 
     // insert userscript header and final newline
-    const finalUserscript = `${header}\n${userscript}${userscript.endsWith("\n") ? "" : "\n"}`;
+    const finalUserscript = `${header}\n${await getLinkedPkgs()}${userscript}${userscript.endsWith("\n") ? "" : "\n"}`;
 
     await writeFile(scriptPath, finalUserscript);
 
@@ -276,7 +276,7 @@ async function getRequireDirectives() {
   const require = JSON.parse(requireFile) as RequireObj[];
 
   for(const entry of require) {
-    if("link" in entry && entry.link === true)
+    if("link" in entry && typeof entry.link === "string")
       continue;
     "pkgName" in entry && directives.push(getRequireEntry(entry));
     "url" in entry && directives.push(`// @require           ${entry.url}`);
@@ -343,3 +343,22 @@ function getCliArg<TReturn extends string = string>(name: string, defaultVal?: T
   const val = arg?.split("=")?.[1];
   return (val && val.length > 0 ? val : defaultVal)?.trim() as TReturn | undefined;
 }
+
+async function getLinkedPkgs() {
+  const requireFile = String(await readFile(join(assetFolderPath, "require.json")));
+  const require = (JSON.parse(requireFile) as RequireObj[]);
+
+  let retStr = "";
+
+  for(const entry of require) {
+    if(!("link" in entry) || typeof entry.link !== "string")
+      continue;
+
+    const scriptCont = String(await readFile(resolve(entry.link)));
+    const trimmedScript = scriptCont
+      .replace(/\/\/ ==.*==[\s\S]*?\/\/ ==\/.*==/gm, "");
+    retStr += `\n// >> Linked package: ${entry.link}\n${trimmedScript}\n// << End of linked package: ${entry.link}\n`;
+  }
+
+  return retStr;
+}