Skip to content

A case for keeping around require.ensure #5909

@noahgrant

Description

@noahgrant

Do you want to request a feature or report a bug?
I'm requesting not deprecating an existing feature, require.ensure.

What is the current behavior?

If the current behavior is a bug, please provide the steps to reproduce.

What is the expected behavior?

If this is a feature request, what is motivation or use case for changing the behavior?
require.ensure is listed as the legacy method of code-splitting in the documentation, favoring instead the new dynamic imports spec. Currently, both work, but as require.ensure is legacy, it's just a matter of time before it gets deprecated. But dynamic imports have no ability to batch 'sibling' imports together into a common split (something I personally do often).

For example, let's say I have a Profile Page, a Billing Page, and a Settings Page that all belong under an Account Navigation tab. Each individual page is fairly small (let's say ~10kB gzipped), so I'd prefer to batch them together so that the user doesn't have to request a new javascript file on every page click—just once when they go anywhere in the Account tab (since we can likely fit all three within the 1 second load time we have per the RAIL model).

I might organize them like this:

require.ensure([
  // all three of these are siblings
  'path/to/profile_page',
  'path/to/billing_page',
  'path/to/settings_page'
], () => {
  // require currently-requested page
});

I could create a new parent file, account.js, that would simply import the three siblings and become a single parent node:

// account.js
import ProfilePage from 'path/to/profile_page';
import BillingPage from 'path/to/billing_page';
import SettingsPage from 'path/to/settings_page';

which would allow me to use:

import('path/to/account').then(() => {
  // set currently-requested page
});

but I'd prefer not to create a new file just for this purpose every time I want to batch imports. @TheLarkInn, I believe you mentioned that something like this would work by using magic comments to force the siblings into the same split:

Promise.all([
  import(/* webpackChunkName: 'account' */ 'path/to/profile_page'),
  import(/* webpackChunkName: 'account' */ 'path/to/billing_page'),
  import(/* webpackChunkName: 'account' */ 'path/to/settings_page')
]).then(([ProfilePage, BillingPage, SettingsPage]) => {
});

which I am happy to do, but at that point aren't we still relying on webpack-specific wrappers around dynamic importing (to support the magic comment) as opposed to adhering to spec?

And so, my real question then is: what is the best long-term solution for batching dynamic imports?

Thank you all for your time.

Please mention other relevant information such as the browser version, Node.js version, webpack version and Operating System.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions