Skip to content
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

Support dynamic lazy imports #417

Closed
petebacondarwin opened this issue Feb 8, 2022 · 7 comments
Closed

Support dynamic lazy imports #417

petebacondarwin opened this issue Feb 8, 2022 · 7 comments
Labels
enhancement New feature or request

Comments

@petebacondarwin
Copy link
Contributor

Cloudflare workers support imports natively, including runtime import() expressions.

So potentially we could lazy load files by uploading such modules as "external" alongside the entry-point file.

Currently, our build process will bundle all static imports (and statically analysable dynamic imports) into the main entry-point bundle: import()-ed modules are wrapped in a function, actually executing the import() executes the inlined code.

One thing this approach doesn't currently support is imports where the module specifier is not known at build time. For example:

a.js

export const foo = 'A';

b.js

export const foo = 'B';

index.js

async function main() {
  const p = Math.random() > 0.5 ? 'a' : 'b';
  const foo = (await import(`./${p}.js`)).foo;
}
main();

This issues tracks the support for being able to do this via Wrangler 2.

@petebacondarwin petebacondarwin added the enhancement New feature or request label Feb 8, 2022
@petebacondarwin petebacondarwin modified the milestone: Backlog May 12, 2022
@petebacondarwin
Copy link
Contributor Author

Closing as a duplicate of #637, which is a superset of this really.

@threepointone
Copy link
Contributor

Actually, it's different. That's about lazy imports (await import(<string literal>)), while this is about dynamic imports (await import(variable))

@threepointone threepointone reopened this May 30, 2022
@bhvngt
Copy link

bhvngt commented Jun 21, 2022

Would love to have this feature. Currently, I am stuck here. I have few variable based dynamic imports that needs to run inside worker.

Is there any workaround or would I have to hard code all the statically analysable dynamic imports upfront for the time being?

@petebacondarwin
Copy link
Contributor Author

One solution, we could implement, is to add a configuration option to specify additional entry-points for a Worker. These would be bundled using the same tooling as the main entry-point but uploaded as additional modules alongside the main one.
Before we bother to do this, we need to check whether the runtime is now able to support loading these dynamically - I think it does in some form.

@penalosa
Copy link
Contributor

Closing for now, since --no-bundle supports this use case. If there isn't an issue already open for tracking the new module work that's in-progress, we should make one.

@irvinebroque
Copy link
Contributor

Should the docs for --no-bundle be updated to reflect that using --no-bundle supports dynamic imports?

https://developers.cloudflare.com/workers/wrangler/commands/

@penalosa
Copy link
Contributor

@irvinebroque Yes, they probably should

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Archived in project
Development

No branches or pull requests

5 participants