-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Massive (2.5x-4x) speedup by switching from
recast
to ast-types
+…
… `esprima` + `escodegen` (as we don't need to preserve formatting and comments in built file). Added source map test suite. Set version to 1.9.0. Closes #7.
- Loading branch information
Showing
16 changed files
with
196 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
/.idea | ||
/node_modules | ||
/npm-debug.log | ||
npm-debug.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
var recast = require('recast'), | ||
fs = require('fs'), | ||
b = recast.types.builders; | ||
var astUtils = require('./astUtils'), | ||
b = astUtils.builders, | ||
fs = require('fs'); | ||
|
||
exports.moduleArgs = [b.identifier('module'), b.identifier('exports')]; | ||
exports.require = b.identifier('_require'); | ||
exports.factoryArgs = [b.identifier('define')]; | ||
exports.localReqModules = b.memberExpression(exports.require, b.identifier('modules'), false); | ||
|
||
exports.preamble = recast.parse(fs.readFileSync(__dirname + '/templates/preamble.js'), {sourceFileName: 'preamble.js'}).program.body; | ||
exports.umdWrapper = recast.parse(fs.readFileSync(__dirname + '/templates/umdWrapper.js'), {sourceFileName: 'umdWrapper.js'}).program.body[0].expression; | ||
exports.preamble = astUtils.parse(fs.readFileSync(__dirname + '/templates/preamble.js'), {loc: true, source: 'preamble.js'}).body; | ||
exports.umdWrapper = astUtils.parse(fs.readFileSync(__dirname + '/templates/umdWrapper.js'), {loc: true, source: 'umdWrapper.js'}).body[0].expression; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
var parse = require('esprima').parse, | ||
generate = require('escodegen').generate, | ||
types = require('ast-types'), | ||
traverse = types.traverse.fast; | ||
|
||
// Polyfill until https://github.com/ariya/esprima/pull/148/files is merged into npm version | ||
|
||
exports.parse = function (code, options) { | ||
var ast = parse.apply(this, arguments); | ||
|
||
if (options.source) { | ||
traverse(ast, function (node) { | ||
node.loc.source = options; | ||
}); | ||
} | ||
|
||
return ast; | ||
}; | ||
|
||
// Workaround until https://github.com/Constellation/escodegen/issues/174 is fixed | ||
|
||
exports.generate = function (ast, options) { | ||
var output = generate(ast, options); | ||
|
||
if (options.sourceMapWithCode && typeof output === 'string') { | ||
output = {code: output}; | ||
} | ||
|
||
return output; | ||
}; | ||
|
||
exports.traverse = traverse; | ||
|
||
exports.builders = types.builders; | ||
exports.namedTypes = types.namedTypes; |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = require('davy'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,51 @@ | ||
var recast = require('recast'), | ||
fs = require('fs'), | ||
Promise = require('davy'), | ||
var fs = require('fs'), | ||
Promise = require('./promise'), | ||
es = require('event-stream'), | ||
astConsts = require('./astConsts'), | ||
pathUtils = require('./pathUtils'), | ||
isCoreModule = require('resolve').isCore, | ||
|
||
types = recast.types, | ||
b = types.builders, | ||
n = types.namedTypes; | ||
|
||
module.exports = recast.Visitor.extend({ | ||
init: function (options) { | ||
this.id = options.id; | ||
this.map = options.map; | ||
this.path = options.path; | ||
|
||
var pipeline = [fs.createReadStream(this.path, {encoding: 'utf-8'})]; | ||
|
||
this.map.transform.forEach(function (transform) { | ||
pipeline.push(transform(this.path)); | ||
}, this); | ||
|
||
var promise = new Promise(); | ||
pipeline.push(es.wait(function (err, js) { | ||
err ? promise.reject(err) : promise.fulfill(js); | ||
})); | ||
|
||
this.promise = promise.then(function (js) { | ||
var ast = recast.parse(js, {sourceFileName: this.path}).program; | ||
this.visit(ast); | ||
return b.functionExpression(null, astConsts.moduleArgs, b.blockStatement(ast.body)); | ||
}.bind(this)); | ||
|
||
es.pipeline.apply(es, pipeline); | ||
}, | ||
|
||
getDependency: function (path) { | ||
return this.map.get(pathUtils.getNodePath(this.path, path)); | ||
}, | ||
|
||
visitCallExpression: function (node) { | ||
var func = node.callee, | ||
arg = node.arguments[0]; | ||
|
||
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; | ||
astUtils = require('./astUtils'), | ||
astConsts = require('./astConsts'), | ||
b = astUtils.builders, | ||
n = astUtils.namedTypes; | ||
|
||
function Replacer(options) { | ||
this.id = options.id; | ||
this.map = options.map; | ||
this.path = options.path; | ||
|
||
var pipeline = this.map.transform.reduce(function (stream, transform) { | ||
return stream.pipe(transform(this.path)); | ||
}, fs.createReadStream(this.path, {encoding: 'utf-8'})); | ||
|
||
var promise = new Promise(); | ||
|
||
pipeline.pipe(es.wait(function (err, js) { | ||
err ? promise.reject(err) : promise.fulfill(js); | ||
})); | ||
|
||
this.promise = promise.then(function (js) { | ||
var ast = astUtils.parse(js, {loc: true, source: this.path}); | ||
this.visit(ast); | ||
return b.functionExpression(null, astConsts.moduleArgs, b.blockStatement(ast.body)); | ||
}.bind(this)); | ||
} | ||
|
||
Replacer.prototype.getDependency = function (path) { | ||
return this.map.get(pathUtils.getNodePath(this.path, path)); | ||
}; | ||
|
||
Replacer.prototype.visit = function (ast) { | ||
return astUtils.traverse(ast, function (node) { | ||
if (n.CallExpression.check(node)) { | ||
var func = node.callee, | ||
arg = node.arguments[0]; | ||
|
||
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; | ||
} | ||
} | ||
}.bind(this)); | ||
}; | ||
|
||
this.genericVisit(node); | ||
} | ||
}); | ||
module.exports = Replacer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module.exports = { | ||
input: 'fixtures/a.js', | ||
exports: 'A', | ||
map: true | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,37 @@ | ||
(function (name, factory) { | ||
if (typeof define === 'function' && define.amd) { | ||
// AMD. Register as an anonymous module. | ||
define([], factory); | ||
} else if (typeof exports === 'object') { | ||
// Node. Does not work with strict CommonJS, but | ||
// only CommonJS-like enviroments that support module.exports, | ||
// like Node. | ||
module.exports = factory(); | ||
} else { | ||
// Browser globals (root is window) | ||
this[name] = factory(); | ||
} | ||
})("A", function(define) { | ||
} | ||
}('A', function (define) { | ||
function _require(index) { | ||
var module = _require.cache[index]; | ||
|
||
if (!module) { | ||
var exports = {}; | ||
module = _require.cache[index] = {id: index, exports: exports}; | ||
module = _require.cache[index] = { | ||
id: index, | ||
exports: exports | ||
}; | ||
_require.modules[index].call(exports, module, exports); | ||
} | ||
|
||
return module.exports; | ||
} | ||
|
||
_require.cache = []; | ||
|
||
_require.modules = [function(module, exports) { | ||
var c = _require(1), | ||
url = require('url'); | ||
|
||
this.topValue = _require(2) * 2; | ||
}, function(module, exports) { | ||
var a = _require(0); | ||
exports.value = 3; | ||
}, function(module, exports) { | ||
module.exports = _require(1).value * 7; | ||
}]; | ||
|
||
_require.modules = [ | ||
function (module, exports) { | ||
var c = _require(1), url = require('url'); | ||
this.topValue = _require(2) * 2; | ||
}, | ||
function (module, exports) { | ||
var a = _require(0); | ||
exports.value = 3; | ||
}, | ||
function (module, exports) { | ||
module.exports = _require(1).value * 7; | ||
} | ||
]; | ||
return _require(0); | ||
}); | ||
})); |
Oops, something went wrong.