-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
[Design Spec] ESNext import() #14495
Comments
|
@mhegazy Maybe you should let This behavior is useful and we need a way to generate output with My interpretation is that |
Shouldn't the 'if not known module' reflect the possibility of missing module and do not expose |
Ah, sorry, forgot it would go to failure channel of the promise. Then it should be |
this is not valid ES2015 code. even if the engine supports ES2015 modules, I guess what you are looking for is a |
|
The argument to The emit for Node is slightly incorrect. The Promise.resolve().then(() => require('blah')) This probably also applies to the other emits. |
@mhegazy the main (existing) implementer of |
Would |
Yeah, that would definitely do. |
If spec is scheduled for es2018 I think Also tracked at #12364 |
@mhegazy I think whether you graft it to |
Note: import() needs to wait until the stack unwinds prior to doing any eval. For Node it is also important to note that CJS modules only produce a Promise.resolve().then(() => {
return {default: require('blah')}
}); |
CJS modules returning a |
Thanks all for the feedback. I have updated the spec accordingly. I decided that to introduce new flag since the syntax is not ES2015 valid |
@yuit, For UMD modules, |
Just to mention here, the transform for System.register should be the following: import x from 'x';
import('dynamic').then(...);
// ->
System.register(['x'], function (_export, _context) {
var x;
return {
setters: [X => x = X.default],
execute: () => {
_context.import('dynamic').then(...);
}
};
}); This has been supported as of SystemJS 0.20 and is included in the Babel transformer. |
@mhegazy EDIT: oh, right, browserify on old IEs 😢 EDIT2: It's there since the first edition of ECMAScript, section 15.3.5.1 at the end of page 64. What browsers specifically would not have |
@Kovensky you are correct. i was wrong. |
Sorry for bringing this once again, but I didn't really get the reason why unresolved module will end up being
My claim is that supplying unknown modules with type |
this is already the behavior today, a module that does not exist, but a .js file for it existed is resolved as |
I do see, actually. The behavior today assumes the static binding to be done where the module specifier is known at compile time. With So, to be more practical, it comes up to a question: how can I avoid "implicit any" error under const module = await import(dynamicallyReceivedPluginModuleName); |
Ah, you should be able to cast to suppress the noImplicitAny error: const module = await import(dynamic) as any; Potentially we could allow Another part of this change would be to enable some sort of syntactic form to express a module shape.. e.g. |
ok, got it. It may work. Thanks for clarification. |
The following seems more natural? const module: any = await import(dynamic); That's how I fix "implicitely any" errors when they are intended: by adding the type declaration. |
if we assume it is a contextual-type-able then sure. |
I think using // older-namespace.ts
// namespace-based code which will eventually migrate to module-based code
namespace Foo {
export async function foo() {
const blah = await import('blah.js'); // should be System.import
}
}
// blah.ts
// already migrated module
export function blah() {} |
Hi guys, I'm having problems using This is the issue: #16820 |
import(specifier)
is a newly proposed syntax for dynamically module loading.import(specifier)
takes an assignment expression as an input (Note: unlike ES6 static import which only allow string literal as module specifier) and returns a promise of module namespace object.The full proposal can be found here
Original issue: #12364
This issue will discuss design proposal for implementing
import()
: parsing, type-checking, and emitting.The proposal will NOT include "module namespace object" type and expressing the shape of the loaded module (To be able to express such type will allow users to cast the result instead of "any" to be what they expected at runtime)...The issue will be discussed and implemented separately.
Parsing
import(specifier)
will be parsed as CallExpression. andspecifier
will be parsed as AssignmentExpressionError
Error if the
module
flag is ES2015.Error if
specifier
is not assignable to string typeType-checking
specifier
is type checked as string type. (Clarification: the only limitation forspecifier
in original proposal is that it can calledtoString()
)The return of
import(specifier)
will be as following-- if known module can be resolved from
specifier
, then we return Promise of module namespace object-- if not known module, then we return Promise of any.
This will give an error inAfter offline discussion with @mhegazy @bowdenk7 @DanielRosenwasser, we think reporting an error without providing a way to easily get out of it (see [Design] Syntax to represent module namespace object in dynamic import #14844 for more detail) is undesirable. By casting tonoImplicitAny
mode andimport()
is used as an expressionPromise<any>
to satisfy the compiler will not provide users any benefit and arguably is a drawback because if in the future, there is a way to type the module easily, upgrading to use such syntax will not be convenient. Therefore, we will not issue annoImplicitAny
error with this feature but with [Design] Syntax to represent module namespace object in dynamic import #14844 instead.We will use existed module resolution logic to try to figure out the module of the dynamic import.
Emit
Emit for each different
module
kind emit is outlined belowThe text was updated successfully, but these errors were encountered: