Skip to content

require.ensure code splitting doesn't work with closures #407

@msonnabaum

Description

@msonnabaum

Using require.ensure like so seems to work fine, and d3 gets split into a separate bundle file:

    Router.prototype.someMethod = function() {};

    Router.prototype.foo = function() {
      require.ensure([], function() {
        require("d3");
        return this.someMethod();
      });
    };

However, that call to this.someMethod() will fail, so I changed it to return a closure (switched -> to => in coffeescript):

    Router.prototype.foo = function() {
      require.ensure([], (function(_this) {
        return function() {
          require("d3");
          return _this.someMethod();
        };
      })(this));
    };

This change seems to break the code splitting, producing a single bundle.js file.

I've worked around the issue for now by using Function.prototype.bind() instead of the closure, but the current behavior was very surprising and took me a while to track down, so it seems worthwhile to fix so others don't run into it as well.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions