-
Notifications
You must be signed in to change notification settings - Fork 12.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Synchronous dynamic module loading? #19495
Comments
We're trying to follow the ES modules spec, which means only allowing import AbsType = require("abs"); // Not a runtime import
let abs: typeof AbsType | undefined;
try {
// tslint:disable-next-line no-var-requires
abs = require("abs");
} catch (_) {}
if (abs) {
console.log(abs("abc"));
} Of course this won't work in a browser. |
Unless you post-process it via some sort of bundler. 😁 But yeah, synchronous module loading is the bane of browsers. |
@andy-ms Won't that code try to |
So long as you only use the first |
Oh right I guess that works. |
Sometimes I needed to have synchronous loading to load only required modules depending on some flags. declare function require("./mymodule"): typeof module "./mymodule"; |
@NN--- See above with the |
@andy-ms We should consider handling |
@weswigham my only comment is that is potentially a 👣 🔫 in authoring code that is specifically not portable and won't get re-written... It feels like it is a necessity in JavaScript, but something that shouldn't be easy to do in TypeScript (or something that should be behind a flag). |
@kitsonk Feels like a pretty small foot gun if your output format is |
Fair point... I guess module format |
There is a lint rule to remind people to use typed
Right, that is probably a better solution. |
I said you get the right types... I believe the emit is broken because we only seek to transform top-level import statements (we just have no such optimization in the type checker). And again, if the node That's a good indicator we should change our behavior to accommodate our users since we no longer have any technical barriers to doing so. |
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed. |
In Node.JS the following pattern can be used to load optional dependencies:
This doesn't work with an
import foo = require()
becauseimport
cannot be inside atry
block. Dynamicimport
s have to be async, which unlike the above, requires the calling code to be async. How about having synchronous dynamic module importing which would allow animport
inside atry
block but load synchronously?The text was updated successfully, but these errors were encountered: