Skip to content

Automatic generated require.ensure() wrapper #105

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

Closed
pksjce opened this issue Sep 7, 2016 · 6 comments
Closed

Automatic generated require.ensure() wrapper #105

pksjce opened this issue Sep 7, 2016 · 6 comments
Labels

Comments

@pksjce
Copy link

pksjce commented Sep 7, 2016

From @cowwoc on January 28, 2015 23:52

Looking at the require.ensure() code found at https://github.com/webpack/docs/wiki/code-splitting, is there a tool that will automatically generate a require.ensure() wrapper on behalf of the user?

Meaning, given:

var a = require("module-a");
var b = require("module-b");

the tool would scan for require() statements, and generate the following output.js:

require.ensure(["module-a", "module-b"], function(require) {
    var a = require("module-a");
    var b = require("module-b");
});

This way, the end-user codes in terms of simple synchronous require() and webpack generates the correct bundles with asynchronous loads under the hood.

Copied from original issue: webpack/webpack#734

@pksjce
Copy link
Author

pksjce commented Sep 7, 2016

From @sirlancelot on January 29, 2015 1:15

Webpack does it automatically, you can write require.ensure([], function() { var a = require("module-a") }) and it will work just fine.

@pksjce
Copy link
Author

pksjce commented Sep 7, 2016

From @cowwoc on January 29, 2015 1:18

Excellent. Please add this to the documentation (ideally on https://github.com/webpack/docs/wiki/code-splitting).

On a sidenote, is it possible to omit require.ensure() altogether and have Webpack add that? In other words, what is the significance of the user adding this code himself/herself? Does it denote something to Webpack?

@pksjce
Copy link
Author

pksjce commented Sep 7, 2016

From @sirlancelot on January 29, 2015 3:20

You have to tell webpack where to split the bundle.

@pksjce
Copy link
Author

pksjce commented Sep 7, 2016

From @cowwoc on January 29, 2015 4:42

@sirlancelot Okay. Why/when would users want to populate the dependencies array of require.ensure() manually (instead of leaving it empty)? Is there a legitimate use-case for doing so?

@sirlancelot
Copy link

I would imagine pre-populating the require.ensure array is for when you have deeply nested/split bundles and you want to tell webpack that certain modules should go in to a certain bundle. You would pre-populate the require.ensure array for the same scenarios you would use require.include; it just includes it in the bundle without evaluating it.

@SpaceK33z
Copy link
Member

This would be covered by #3.

hzuhyb pushed a commit to hzuhyb/webpack.js.org that referenced this issue Feb 18, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants