-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
[FEATURE] Support dynamic import #2770
Comments
Thanks for filing the tracking bug. Just want to confirm that the feature we're tracking here should be the currently stage 3 dynamic import syntax, not top-level await syntax. |
Yes ! |
@blickly There is already both Chrome and Safari support for this feature. The hard part is figuring out exactly what the compiler should do here. But parsing would be a good first step... |
Any news on this ? |
Would be amazing to see parser support for Spent the morning trying to add Closure Compiler as the minifier in https://github.com/MaxMilton/sapper-template-rollup/tree/feat/use-closure-compiler but in the end it turns out the import() can't be parsed and the compiler bails completely. In this project the import function is polyfilled so we don't need the compiler to do anything special. This is now a blocker for adding Closure Compiler support 😓 |
Created Google internal issue http://b/122961838 for this request. |
Has there been any movement on this recently? Asking because we're getting into a project for which it would be extremely useful. I figured I'd ask before we decide to fork and fix it ourselves. |
Most of JS developers now use Rollup instead of Closure Compiler. Rollup does a great job in handling await import instructions and moving all your imports at the same level on the disk. As a result, code splitting leads to faster load times. File size matters, but code splitting really is on another level. The benefits are impressive. In addition, Rollup understands ESNext and respects property names, which leads to fewer bugs. As of July 2019, I would recommend Rollup over Closure Compiler. |
Although rollup and closure compiler do have some overlap in that they can both handle JS bundling, their functionality is not mutually exclusive. If you use rollup you can also use closure compiler with a plugin like @ampproject/rollup-plugin-closure-compiler. Rollup is an excelent module bundler and closure compiler is an excelent JS minifier. If you want the most performant code, why not use both? |
@Tonsil, did you fork? |
No, we punted on it. We wanted to dynamically import a library because it's on the large side and not needed in all cases. For now, we decided instead to keep that code as a separate file (for other reasons, like not wanting to have to rewrite it just yet). Our workaround was to have our modular code write out a script tag on the page and pull it in that way. If/when we do decide to fork, I'll let you know. |
"Support" can mean at least 2 different things for dynamic import here.
People building big applications definitely want the code splitting, but there's a lot to work out there. I'd really like to know how much people care about importing foreign ES modules (number 1). I would like to advocate for implementing import of foreign ES modules, but that will be a hard sell if I cannot show that a significant number of people really want it and would benefit from it. |
FWIW, the Scala.js compiler cares, and therefore, indirectly, a lot of Scala.js users. That's because Scala.js uses Closure to optimize the Scala.js code but not its potential JS dependencies. |
I also have this related question: Do people want closure-compiler to be able to generate output that functions as an ES module and can be imported JS code the compiler never sees? |
Yes. Scala.js would also like that. I'd love to be able to remove this line of code: |
@brad4d wrote:
My two cents (as a longtime Closure Compiler user, and somebody who would like it to support dynamic module import): Number 2 is much more important. I wouldn't personally need to dynamically load JS that's not being compiled by my own workflow. |
@maxmilton A brute-force workaround for including dynamic import() in JavaScript to be compiled with the Closure Compiler is temporarily to rename every import() statement to dynamicImport() and then compile, then convert every dynamicImport() back to import() in the resulting compilation. This is admittedly incredibly dreadful, but IT WORKS (at least in my experiments with "SIMPLE OPTIMIZATIONS"). The compiler leaves dynamicImport() as-is (i.e., unrenamed), and so you can search-for and replace it. (Yes, this is not an ideal solution, but it works in my tests. See what you get.) |
erm, you could also just wrap the dynamic import if you have control over all the code you are building like |
Design document for support: |
This is fully supported as of the 20210505 version |
The online interface to the Closure Compiler (https://closure-compiler.appspot.com) is still throwing errors when dynamic import statements are encountered:
When will it be updated to the 20210505 version? |
Allowing dynamic imports is enabled by default in the command line version, but the option is off by default for the compiler options. This would have to be specifically enabled for the appspot instance. |
@ChadKillingsworth When I run the 20210505 version locally at the command line, the closure compiler still fails to work with dynamic imports. Using this command:
...I get the following error:
The same error plus an additional "Failed to load module" error occurs if I provide full path info in the import argument, i.e., Per the error message, I'm unclear on how to "alias" a dynamic import expression for language level. Also, the documentation you linked previously contains this text:
This suggests that the import in my code is not resolvable. It doesn't look like the closure compiler is really supporting dynamic imports as they are used in actual code (with promise-style Many thanks! EDIT: In reviewing my code, |
Take a look at https://github.com/google/closure-compiler/wiki/JS-Modules#dynamic-import-expressions and see if that helps. If not, let me know and I'll clarify the documentation. |
@ChadKillingsworth No such luck for me. The text that states this gives hope:
But even if I save the name of the imported module in a variable and code If I understand the My modules typically have this format:
After renaming
I want it to preserve my variable names and object.freeze at the end. I've worked around this previously by renaming my Many thanks in advance for your help. |
That's backwards. The compiler will rename dynamic imports for you with this flag. Your source code shouldn't need touched. So with source: import('./my-module.js') becomes: dynamicImport('./my-module.js') As for preserving your exported names, that's a use case that's not yet supported |
@ChadKillingsworth Thanks! Okay, understanding that, here's the error I now get. Provided this for input.mjs:
And provided this execution:
Then the error produced is:
Is there a missing step or CLI flag? |
Provide a definition for |
@ChadKillingsworth What would go in the extern file for this simple case, and how would that file be passed to the executable on the command line? I see the documentation at https://developers.google.com/closure/compiler/docs/externs-and-exports, but I'm unclear how to apply it to the code provided above. |
This would be a good question for the mailing list or for stackoverflow.com |
Support dynamic import :
https://developers.google.com/web/updates/2017/11/dynamic-import
module.js :
main.js :
Should display :
The thing is about supporting 'export' and 'import' keywords.
The text was updated successfully, but these errors were encountered: