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

Add option to code split dynamic imports in bundles #5562

Closed
jimsimon opened this issue May 17, 2020 · 6 comments
Closed

Add option to code split dynamic imports in bundles #5562

jimsimon opened this issue May 17, 2020 · 6 comments
Labels
cli related to cli/ dir feat new feature (which has been agreed to/accepted)

Comments

@jimsimon
Copy link

I suspect this is probably out of scope given Deno appears to be only targeting server runtimes where this matters less, but it would be great for web usage if Deno's bundle command had an option to code split for dynamic imports. Without this feature, we'll likely see a webpack equivalent for Deno. It would be unfortunate to have separate bundlers for targeting server vs web. Additionally, servers can benefit from faster startup times if dynamic imports were code split.

@kitsonk
Copy link
Contributor

kitsonk commented May 17, 2020

deno bundle would unlikely ever include something like this. For Deno.bundle() that might be a different story, but what would your expectations be? Every dynamic import is broken out to a seperate chunk? Ultimately even Deno.bundle() isn't the best for general purpose bundling for a browser. I am afraid there will always be seperate bundlers for targeting server vs. the web, because they have quite different concerns.

@mischkl
Copy link

mischkl commented May 17, 2020

I'd imagine this is actually quite relevant for the server use case as well, not for "applications" but for reusable libraries. Beyond a certain library size it's pretty common practice to split it up into sub-modules, some of which may indeed still depend on each other, but with the difference that the consumer of the library has the option to only import the sub-modules he needs. This is IMHO an important feature for publishing libraries, if you want to truly avoid the need for additional build tooling.

It wouldn't have to be limited to dynamic imports, of course - just like with webpack it should be possible to specify multiple entry points as a list of files.

@kitsonk
Copy link
Contributor

kitsonk commented May 17, 2020

@mischkl there is nothing preventing someone from creating deno bundle on various entry points at the moment and then having a server script the imports multiple bundles.

I can see the point about potentially not bundling bundles, and simply leaving them as an external import, as well as supporting a more complex dependency analysis so that deno bundle a.ts b.ts c.ts would ensure that if a.ts depended on b.ts that it imported the bundle of b.ts instead of inlining b.ts and its dependencies again. Getting more complex than that would be potentially an enhancement for Deno.bundle().

@kitsonk kitsonk mentioned this issue May 17, 2020
18 tasks
@jimsimon
Copy link
Author

I think enhancing the compiler api for Deno.bundle() would be a great way to approach this. That keeps the deno bundle cli command that same but would also allow for more tools to be built on top of the bundle logic. This combined with the concepts being discussed about exposing typescript transformers (#3354) could potentially allow for the creation of something akin to webpack that's built on top of Deno's core tooling.

My expectations would be that Deno.bundle() can do the advanced dependency analysis you mentioned, including splitting out multiple vendor chunks much like webpack can. The general idea in my mind is that we want to load as little code as needed whether we're on a server or on the web. The bundle logic right now forces us to load and parse everything even if we're only going to use one small code path. This is fine for smaller scripts and web apps but starts to have a real performance impact once on larger projects. Server cold start time is definitely a concern with the current bundle logic (though I'll be honest that my personal focus is on web frontends).

@kitsonk
Copy link
Contributor

kitsonk commented May 18, 2020

@jimsimon take a look at #4752. From your perspective of your use case, how would you expect the API to look (please comment in that issue).

@bartlomieju bartlomieju added cli related to cli/ dir feat new feature (which has been agreed to/accepted) labels May 18, 2020
@kitsonk
Copy link
Contributor

kitsonk commented Nov 5, 2020

As of 1.5, dynamic imports are no longer inlined under any conditions and bundles are simply ESM files, so they become really difficult to detect as a unique file. So effectively it is up to the end user to figure out if they want to import a bundle (and get further bundling) or use dynamic import() and have that lazily loaded at runtime.

@kitsonk kitsonk closed this as completed Nov 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cli related to cli/ dir feat new feature (which has been agreed to/accepted)
Projects
None yet
Development

No branches or pull requests

4 participants