Import Sync [JS/TS]
So, once can programmatically import like this:
const a = await import("urmom-v"+15)
but what if I don't want to await? What if I wanted replicate something like:
const a = require("urmom-v"+15)
You cannot synchronously import a file on the web.
The only reason that Node.js can do that is that it has direct access to the user's filesystem.
On the web, blocking the main thread while waiting for an HTTP response would be terrible.
Think of this:
const a = require("./a.js"); const b = require("./b.js"); const c = require("./c.js");
Fire HTTP request for
a.js, read the response, parse it, check it's dependencies, then execute the code, then repeat for
Like, dang that would be terrible even if you were on a 5G connection.
import a from "./a.js"; import b from "./b.js"; import c from "./c.js";
fetches and parses everything in parallel.
Btw, appending an (X)HTML
script element with
Imperfect, but the closest you'll get.
const import_file = document.createElement("script"); const remove = "removeAttribute"; import_file[remove]("async"); import_file[remove]("defer"); import_file[remove]("type"); const main = import_file.cloneNode(false); const set = "setAttribute"; import_file[set]("src", "./import.js"); main[set]("src", "./main.js");
That was painful to write, don't actually do that.