-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #99 from stealjs/circular
Fix default imports in ES6 to AMD circular references
- Loading branch information
Showing
7 changed files
with
229 additions
and
43 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,17 +1,28 @@ | ||
var amd_amd = require('./amd_amd'); | ||
var getCompile = require('./es6_compiler'); | ||
var getAst = require("./get_ast"); | ||
var amdToAmd = require("./amd_amd"); | ||
var getCompile = require("./es6_compiler"); | ||
var patchCircularDependencies = require("./patch_circular_dependencies"); | ||
|
||
module.exports = function(load, options){ | ||
module.exports = function(load, options) { | ||
var compile = getCompile(options); | ||
|
||
var result = compile(load.source.toString(), { | ||
filename: options.sourceMapFileName || load.address, | ||
modules: 'amd', | ||
sourceMaps: true | ||
}, options); | ||
var result = compile( | ||
load.source.toString(), | ||
{ | ||
filename: options.sourceMapFileName || load.address, | ||
modules: "amd", | ||
sourceMaps: true | ||
}, | ||
options | ||
); | ||
|
||
load.source = result.code; | ||
load.map = result.map; | ||
load.ast = getAst(load, options.sourceMapFileName); | ||
return amd_amd(load, options); | ||
|
||
if (load.circular && options.patchCircularDependencies) { | ||
load.ast = patchCircularDependencies(load.ast); | ||
} | ||
|
||
return amdToAmd(load, options); | ||
}; |
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,114 @@ | ||
var types = require("ast-types"); | ||
var first = require("lodash/first"); | ||
var last = require("lodash/last"); | ||
var concat = require("lodash/concat"); | ||
var estemplate = require("estemplate"); | ||
|
||
var n = types.namedTypes; | ||
|
||
module.exports = function(ast) { | ||
var variableNames = collectVariableNames(ast); | ||
|
||
appendToDefineFactory( | ||
ast, | ||
concat(variableNames.map(patchHelperCallTemplate), patchHelperTemplate()) | ||
); | ||
|
||
return ast; | ||
}; | ||
|
||
/** | ||
* Collects the identifiers of Babel generated import assignments | ||
* | ||
* E.g, given the AST of the code below: | ||
* | ||
* var _bar2 = _interopRequireDefault('bar'); | ||
* var _foo2 = _interopRequireDefault('foo'); | ||
* | ||
* this function will return an array with the AST nodes of the _bar2 and | ||
* _foo2 identifiers. | ||
* | ||
* @param {Object} ast - The code's AST | ||
* @return {Array.<Object>} A list of identifiers (AST nodes) | ||
*/ | ||
function collectVariableNames(ast) { | ||
var names = []; | ||
|
||
types.visit(ast, { | ||
visitVariableDeclarator: function(path) { | ||
var node = path.node; | ||
|
||
if (this.isBabelRequireInteropCall(node.init)) { | ||
names.push(node.id); | ||
} | ||
|
||
this.traverse(path); | ||
}, | ||
|
||
isBabelRequireInteropCall: function(node) { | ||
return ( | ||
n.CallExpression.check(node) && | ||
node.callee.name === "_interopRequireDefault" | ||
); | ||
} | ||
}); | ||
|
||
return names; | ||
} | ||
|
||
/** | ||
* Appends the given node to the `define` factory function body | ||
* | ||
* MUTATES THE AST | ||
* | ||
* @param {Object} ast - The AMD module AST | ||
* @param {Array.<Object>} nodes - The AST nodes to be appended | ||
*/ | ||
function appendToDefineFactory(ast, nodes) { | ||
types.visit(ast, { | ||
visitCallExpression: function(path) { | ||
var node = path.node; | ||
|
||
if (this.isDefineIdentifier(node.callee)) { | ||
var factory = last(node.arguments); | ||
factory.body.body = concat(factory.body.body, nodes); | ||
this.abort(); | ||
} | ||
|
||
this.traverse(path); | ||
}, | ||
|
||
isDefineIdentifier: function(node) { | ||
return n.Identifier.check(node) && node.name === "define"; | ||
} | ||
}); | ||
} | ||
|
||
function patchHelperCallTemplate(identifier) { | ||
return first( | ||
estemplate.compile("_patchCircularDependency(%= name %)")({ | ||
name: identifier | ||
}).body | ||
); | ||
} | ||
|
||
function patchHelperTemplate() { | ||
return first( | ||
estemplate.compile(` | ||
function _patchCircularDependency(obj) { | ||
var defaultExport; | ||
Object.defineProperty(obj.default, "default", { | ||
set: function(value) { | ||
if (obj.default.__esModule) { | ||
obj.default = value; | ||
} | ||
defaultExport = value; | ||
}, | ||
get: function() { | ||
return defaultExport; | ||
} | ||
}); | ||
} | ||
`)({}).body | ||
); | ||
} |
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
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
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,32 @@ | ||
define([ | ||
'exports', | ||
'basics/amdmodule' | ||
], function (exports, _amdmodule) { | ||
'use strict'; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
exports.__useDefault = undefined; | ||
var _amdmodule2 = _interopRequireDefault(_amdmodule); | ||
function _interopRequireDefault(obj) { | ||
return obj && obj.__esModule ? obj : { default: obj }; | ||
} | ||
exports.default = { | ||
amdModule: _amdmodule2.default, | ||
name: 'es6Module' | ||
}; | ||
var __useDefault = exports.__useDefault = true; | ||
_patchCircularDependency(_amdmodule2); | ||
function _patchCircularDependency(obj) { | ||
var defaultExport; | ||
Object.defineProperty(obj.default, 'default', { | ||
set: function (value) { | ||
if (obj.default.__esModule) { | ||
obj.default = value; | ||
} | ||
defaultExport = value; | ||
}, | ||
get: function () { | ||
return defaultExport; | ||
} | ||
}); | ||
} | ||
}); |