jlouns / gulp-requirejs-optimize

Wrapper for the requirejs optimizer for use in gulp
MIT License
62 stars 9 forks source link

Question on concatenation / r.js behavior #15

Closed davisford closed 8 years ago

davisford commented 8 years ago

@jlouns when I run the optimizer on my source, it seems to duplicate modules where a reference is desired, and I'm wondering if I'm configuring it wrong?

Here's a simple example:

// in src/app/foo.js
export default class Foo { }
// in src/app/bar.js 
import Foo as './Foo';
export default class Bar extends Foo { }

I run this through babel using the amd plugin and we get something like this:

// in build/app/foo.js
define(['exports'], function (exports) {
  Object.defineProperty(exports, "__esModule", { value: true });

  var Foo = function () { }
  // more stuff here
  exports.default = Foo;
});
// in build/app/bar.js
define(['exports', './Foo'], function (exports, Foo) {
  Object.defineProperty(exports, "__esModule", { value: true });

  var Bar = function () { }
  // more stuff here
  exports.default = Bar;
});

But now when I run this through the optimizer, I get the contents of foo.js embedded in bar.js instead of referenced. So, I end up with a module for Foo defined in foo.js and another one in bar.js.

// in dist/app/bar.js
define(['exports', function (exports) { Object.defineProperty(exports,"__esModule", {value: true}); var Foo=function() { } /* etc */ exports.default=Foo; }); define(['exports', './Foo', function(exports, Foo) { /* etc */ });

If I concat these all together, I get the same module defined all over the place. What am I missing?

davisford commented 8 years ago

I see now that I had not yet specified the main entry point, so the optimizer couldn't properly walk the dependency graph and all files were processed standalone.