Skip to content

Proposal: Async imports #3100

Closed
Closed
@tinganho

Description

@tinganho

Most module bundlers bundles everything into one file e.g. browserify, requirejs etc. But if you are developing a very large scale application you might not find it suitable to have everything bundled into a single file. What you might want is to have it bundled into multiple files, i.e. sub graphs of the main dependency graph. So if you visit a page, you only want to download the contents of that page instead of contents of others. This is a very good use case for SPA:s, because in today's day and age they need to be bundled into a single file, because they need to handle navigations and actions through all pages.

But in order for it to work we need async imports. To just give you an overview of what impact this feature could have — let's have a look at a general defined router for an SPA:

class Router extends SomeRouter {
  ...
  private routes = {
     '/users/:id': 'renderUserPage'
  }
  ...
  renderUserPage() {
     async import {
       UserPageModel from './pages/user/UserPageModel';
       UserPageView from './pages/user/UserPageView';
     } {
       let model = new UserPageModel({ id: 1 });
       let view = UserPageView({ model: model });
       view.render();
     }
  }
  ...
}

Now, the above async import statement could tell a module loader to bundle its' sub-dependency graph. So all files that UserPageModel and UserPageView requires could be bundled into a single file. Now, If you want to navigate to a different page you don't want to download the already downloaded modules. So a smart module bundler will bundles different subtracted modules of a bundle. So if a user navigates all pages in an application he would have download the whole dependency graph without over fetching something.

This proposal was inspired by how Facebook does its' closed source module handling. I know that they do something similar. And it also very similar to webpack's code splitting. It would be good if TypeScript support this with all the tooling support.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Out of ScopeThis idea sits outside of the TypeScript language design constraintsSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions