-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Explore supporting ES import() proposal #12364
Comments
This is a proposal that we are following in the committee. I would expect us to start looking into this early next year. also referenced in #11611. i think we just need one issue to track this. |
Stubbing it such that it is not a syntax error, has apparent type |
@Kovensky anyone can easily write that stub should they need it so I don't think its declaration belongs in the language any more than |
@aluanhaddad not quite. Just writing a |
@Kovensky I don't think I have actually tried to write it like that, thanks for explaining that this isn't possible. |
Dynamic |
That means SystemJS may update their API to match it! :D |
SystemJS can't update their API to match proposal because |
it's |
Can we have a compiler option to support this syntax? |
Sadly we'll loose type information when specifier should be computed :( |
There are talks about attaching meta-properties to |
airbnb/babel-plugin-dynamic-import-webpack#12 see concerns |
@mhegazy is this going to be in the TS "Future" (2.3?) roadmap? |
I have a solution if anyone wants a hack to use while waiting for the feature to be properly implemented in TS. You'll need
Then simply create a definition like so...
You can change the _import to really be anything but I felt the _ was pretty safe and would be easy enough to find and replace when typescript adds support. My test was a simple react-router v4 app with two routes that were added with _import. An example of it in use is below
|
Support the [Dynamic import proposal](https://github.com/tc39/proposal-dynamic-import) [TC39 Stage 3] as a loader (codegen). The import() construct is supported in webpack from 2.1.0-beta28 Also adds decprecation message then using async-system (System.import) as it will be removed from webpack 3+ BREAKING CHANGES: Typescript transform `import(...)` syntax into something. To use `ng-router-loader` with `async-import` code generator the `ng-router-loader` must run **AFTER** the TS compiler (e.g: awesome-typescript-loader), that is in a lower index in the loaders array. This also requires the code generators to emit ES5 code. SEE: microsoft/TypeScript#12364
It's hard to use such hack without native support if we want types. We should do things like: import * as ModuleInit from './init';
async function main(): Promise<void>
{
const Init: typeof ModuleInit = await _import( './init' );
Init.default();
} |
I'll note that the proposal is currently at stage 3, and only string literals should be checked (anything else should return Also, it'd be nice to see it transpiled in CommonJS/AMD (Webpack 2 works with ES module syntax): // Original
import("specifier").then(...)
// CommonJS
Promise.resolve()
.then(() => require("specifier"))
.then(...)
// AMD
Promise.resolve()
.then(() => new Promise(resolve => require(["specifier"], resolve)))
.then(...) In particular, lazy loading in Node would be very nice for end-user apps with optional features, and |
Promise will resolve to module namespace not import * as path from 'path';
console.log(path.resolve('foo')); and import('path').then(path => {
console.log(path.resolve('foo'));
}); I'm assuming TypeScript will not allow dynamic module specifier ( |
@mohsen1 I referred to |
It looks like the result will be typed as |
Is there any plan to support |
I'm also interested in this for code splitting. This doesn't work at the moment because of
|
For now, you can work around it by having declare global {
interface System {
import (request: string): Promise<any>
}
var System: System
} and using |
@Kovensky that worked great. Thank you. A few gotchas - webpack 2 needs a chunkFilename to load the chunk script correctly:
Also used the If you are getting |
Also for |
@Kovensky cheers for the workaround! I've combined with react-loadable here if anyone is interested in a demo: https://github.com/jch254/starter-pack/tree/typescript. |
@Kovensky while this post is largely around react, I am attempting your interface approach via Angular 2 and still getting the import property is undefined? Any thoughts:
Generated code looks as follows I am wondering if the interface is not being ignored in this case causing the actual import() function to go un-recognized.
|
Fixed by #14774 |
It was reverted 😢 #16281 should actually fix it. EDIT: now it's merged 🎉 |
I am still having this issue - using [email protected] |
@misantronic You might need to change your tsconfig's "module" option to "esnext" |
Dynamic import proposal is in early stages but it will require TypeScript to support it in order to be usable in TypeScript based apps.
Basic syntax:
The text was updated successfully, but these errors were encountered: