Skip to content

Commit

Permalink
Added stable sorting of generated modules list according to their paths.
Browse files Browse the repository at this point in the history
  • Loading branch information
RReverser committed Mar 18, 2014
1 parent c97ad23 commit 5a1fe63
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 21 deletions.
20 changes: 18 additions & 2 deletions lib/replacer.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ var fs = require('fs'),
n = astUtils.namedTypes;

function Replacer(options) {
this.id = options.id;
this.id = undefined;
this.map = options.map;
this.path = options.path;
this.refs = [];

var pipeline = this.map.transform.reduce(function (stream, transform) {
return stream.pipe(transform(this.path));
Expand All @@ -34,6 +35,21 @@ Replacer.prototype.getDependency = function (path) {
return this.map.get(pathUtils.getNodePath(this.path, path));
};

Replacer.prototype.referenceFrom = function (ref) {
this.refs.push(ref);
return this;
};

Replacer.prototype.resolveAs = function (id) {
this.id = id;

this.refs.forEach(function (ref) {
ref.value = this.id;
}, this);

return this;
};

Replacer.prototype.visit = function (ast) {
return astUtils.traverse(ast, function (node) {
if (n.CallExpression.check(node)) {
Expand All @@ -42,7 +58,7 @@ Replacer.prototype.visit = function (ast) {

if (n.Identifier.check(func) && func.name === 'require' && n.Literal.check(arg) && !isCoreModule(arg.value)) {
func.name = astConsts.require.name;
arg.value = this.getDependency(arg.value).id;
this.getDependency(arg.value).referenceFrom(arg);
}
}
}.bind(this));
Expand Down
27 changes: 15 additions & 12 deletions lib/replacerMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ ReplacerMap.prototype.get = function (path) {

var id = this.promises.length++,
replacer = this.replacers[path] = new Replacer({
id: id,
map: this,
path: path
});
Expand All @@ -25,17 +24,21 @@ ReplacerMap.prototype.get = function (path) {
};

ReplacerMap.prototype.whenAll = function (startIndex) {
var currentLength = this.promises.length;

return Promise.all(this.promises.slice(startIndex || 0)).then(function (firstPart) {
if (this.promises.length > currentLength) {
return this.whenAll(currentLength).then(function (secondPart) {
return firstPart.concat(secondPart);
});
} else {
return firstPart;
}
}.bind(this));
var map = this, currentLength = this.promises.length;

return (
Promise.all(this.promises.slice(startIndex || 0))
.then(function () {
if (map.promises.length > currentLength) {
return map.whenAll(currentLength);
}
})
.then(function () {
return Promise.all(Object.keys(map.replacers).sort().map(function (path, id) {
return this[path].resolveAs(id).promise;
}, map.replacers));
})
);
};

module.exports = ReplacerMap;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pure-cjs",
"version": "1.9.1",
"version": "1.9.2",
"description": "Pure minimalistic CommonJS builder",
"bin": "./bin/pure-cjs",
"main": "./lib/pureCjs",
Expand Down
12 changes: 6 additions & 6 deletions test/suites/c (no exports)/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@
_require.cache = [];
_require.modules = [
function (module, exports) {
var a = _require(1);
exports.value = 3;
var c = _require(1), url = require('url');
this.topValue = _require(2) * 2;
},
function (module, exports) {
var c = _require(0), url = require('url');
this.topValue = _require(2) * 2;
var a = _require(0);
exports.value = 3;
},
function (module, exports) {
module.exports = _require(0).value * 7;
module.exports = _require(1).value * 7;
}
];
_require(0);
_require(1);
}());

0 comments on commit 5a1fe63

Please sign in to comment.