SPLATFileLoader: possible to avoid global vars and externals

In SPLATFileLoader, it would try to load external https://unpkg.com/fflate/umd/index.js if window.fflate does not exist, but is it possible that bundler users can provide their own instance of fflate without the need to inroduce a global var, or load external scripts.

Let me take care of that quickly

2 Likes

Thanks, that’s great, but is it possible to have a user-provided fflate instance other than global vars? Like for bundler users, dependencies would be imported and packed, and not exported to global.

I’m not sure how to achieve that. Maybe @docEdub or @AlitarSemiramis know?

I would expect something like this:

diff --git a/packages/dev/loaders/src/SPLAT/splatFileLoader.ts b/packages/dev/loaders/src/SPLAT/splatFileLoader.ts
--- a/packages/dev/loaders/src/SPLAT/splatFileLoader.ts	(revision 60497cdfb175e34a2588f0aebada89bd47d816f6)
+++ b/packages/dev/loaders/src/SPLAT/splatFileLoader.ts	(date 1760488351964)
@@ -174,16 +174,22 @@
         return mesh;
     }
 
+    // eslint-disable-next-line @typescript-eslint/ban-ts-comment
+    // @ts-ignore
+    static fflate: Nullable<typeof import("fflate")>;
+
     // eslint-disable-next-line @typescript-eslint/promise-function-async, no-restricted-syntax, @typescript-eslint/naming-convention
     private async _unzipWithFFlateAsync(data: Uint8Array): Promise<Map<string, Uint8Array>> {
         // ensure fflate is loaded
-        if (typeof (window as any).fflate === "undefined") {
+        let fflate = SPLATFileLoader.fflate;
+        if (!fflate && typeof (window as any).fflate === "undefined") {
             await Tools.LoadScriptAsync("https://unpkg.com/fflate/umd/index.js");
-        }
-
-        // eslint-disable-next-line @typescript-eslint/ban-ts-comment
-        // @ts-ignore
-        const { unzipSync } = (window as any).fflate as typeof import("fflate");
+            // eslint-disable-next-line @typescript-eslint/ban-ts-comment
+            // @ts-ignore
+            fflate = (window as any).fflate as typeof import("fflate");
+        }
+
+        const { unzipSync } = fflate;
 
         const unzipped = unzipSync(data); // { [filename: string]: Uint8Array }
 

And bundler users can just import SPLATFileLoader and set SPLATFileLoader.fflate to what they want.

Would it be alright if fflateis part of the plugin options as optional?

Yeah, I think this would also make it

Perfect, feel free to open a PR for that, I’ll be glad to review it!

1 Like