cramforce/splittable

Add import() support

Opened this issue · 13 comments

It should replace System.import method
https://github.com/tc39/proposal-dynamic-import

The proposal can possibly move to stage 3 next week

It is on stage 3 now

import is reserved words so dynamic imports can't be polyfilled. They should be transpiled. So we probably need Babel plugin first

There is https://github.com/babel/babel/tree/master/packages/babel-plugin-syntax-dynamic-import so I suppose we can use custom plugin to transpile it to current System.import. @cramforce are you ok with that?

Just saw the second comment. My email was delayed.

Yeah, you can insert that path in here: https://github.com/cramforce/splittable/blob/master/splittable.js#L223

And then look for System.import in the evil regex below and that might work!

I just created babel plugin which replaces import() with System.import(). I think it could be added to line you mentioned but tests look complicated. I didn't figure out how to add tests for import()

Cool. I can take it from there. Does the plugin do nothing except s/import/System.import. One very useful feature would be a hook to control rewriting of the module argument (Going from module identifier to URL).

Yes, it replaces dynamic imports with System.imports but not just s/import/System.import because we should replace only import calls, not static imports.

One very useful feature would be a hook to control rewriting of the module argument (Going from module identifier to URL).

Not sure if this logic should be in babel plugin. System.import needs this too, right?

It cannot be correctly done at runtime. The nice thing about import is that it resolves a relative module name. A regular function call cannot know what '../foo' means, because it doesn't know about filenames anymore.

That's true. Can you explain what changes do you want from transformer?

@chicoxyzzy It would either be configurable or something predictable. E.g. if the resulting path was always a relative path from the project root instead of the current file, then everything else could be done at runtime.

ok I'll try to add an option for import specifier to be exactly StringLiteral and a relative path.