Skip to content

Module Loading #8

@zicklag

Description

@zicklag

It could be pretty handy to be able to load modules, but it's not necessarily simple to implement. Opening this issue as sort of a discussion placeholder and tracking issue ( if you don't mind ). Here are my thoughts so far.

Deno has awesome module loading capabilities that we could make do anything we want. Module loading in Deno is async, and not easily integrated with Bevy's asset loader because of it's non-Send requirements on the JsRuntime, but I managed a simple approach with an async event loop separate from the asset server that listened on a channel for module load events and that worked pretty well.

Then I ran into the issue that the browser could load javascript modules, but not typescript modules, because we can't hook into the import process that the browser uses like we can when we're running on Deno.

It seems like the best user experience would be:

  1. When an import statement is found in a script, during the script loading/transpiling process, we download any dependencies recursively.
  2. We load any loaded dependencies, indexed by their resolved URL, into a global in globalThis so that the scripts can access all the dependencies when running.
  3. During transpiling, we re-write the import statement to something like:
const mylib = globalThis.dependencies['https://deno.land/x/some_modue/mod.ts']`;
  1. We would also have to re-write export statements, maybe wrapping the whole module in an IIFE and then return-ing the export's so that we can extract the exports in the browser by using something like:
const module_exports = new Function(transpiled_module_source_code)();

Alternative: Bundling

Another compelling option would be to provide a CLI that could be used to bundle all of the scripts into a single JS file that would be more efficiently loaded in the browser.

But I would really like to avoid requiring a build step for scripts, because that's part of the point of scripting, is to avoid a build/bundle step, even if it is fast.

Still, if you only needed the build step for web, it could be fine.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions