simplify amd module formatter
This commit is contained in:
parent
b8a80364df
commit
e263757509
@ -66,8 +66,7 @@ export function bar() {
|
||||
**Out**
|
||||
|
||||
```javascript
|
||||
define(["foo"], function (_foo) {
|
||||
var exports = {};
|
||||
define(["exports", "foo"], function (exports, _foo) {
|
||||
exports.bar = bar;
|
||||
|
||||
var foo = _foo.default;
|
||||
@ -75,8 +74,6 @@ define(["foo"], function (_foo) {
|
||||
function bar() {
|
||||
return foo("foobar");
|
||||
}
|
||||
|
||||
return exports;
|
||||
});
|
||||
```
|
||||
|
||||
|
||||
@ -18,22 +18,18 @@ AMDFormatter.prototype.transform = function (ast) {
|
||||
|
||||
// build an array of module names
|
||||
|
||||
var names = [];
|
||||
var names = [b.literal("exports")];
|
||||
_.each(this.ids, function (id, name) {
|
||||
names.push(b.literal(name));
|
||||
});
|
||||
names = b.arrayExpression(names);
|
||||
|
||||
// add exports
|
||||
|
||||
body.unshift(b.variableDeclaration("var", [
|
||||
b.variableDeclarator(b.identifier("exports"), b.objectExpression([]))
|
||||
]));
|
||||
body.push(b.returnStatement(b.identifier("exports")));
|
||||
|
||||
// build up define container
|
||||
|
||||
var container = b.functionExpression(null, _.values(this.ids), b.blockStatement(body));
|
||||
var params = _.values(this.ids);
|
||||
params.unshift(b.identifier("exports"));
|
||||
|
||||
var container = b.functionExpression(null, params, b.blockStatement(body));
|
||||
var call = b.callExpression(b.identifier("define"), [names, container]);
|
||||
|
||||
program.body = [b.expressionStatement(call)];
|
||||
|
||||
@ -1,8 +1,6 @@
|
||||
"use strict";
|
||||
|
||||
define([], function () {
|
||||
var exports = {};
|
||||
|
||||
define(["exports"], function (exports) {
|
||||
exports.default = 42;
|
||||
exports.default = {};
|
||||
exports.default = [];
|
||||
@ -20,6 +18,4 @@ define([], function () {
|
||||
var foo = function foo() {};
|
||||
return foo;
|
||||
}();
|
||||
|
||||
return exports;
|
||||
});
|
||||
|
||||
@ -1,8 +1,6 @@
|
||||
"use strict";
|
||||
|
||||
define(["foo"], function (_foo) {
|
||||
var exports = {};
|
||||
|
||||
define(["exports", "foo"], function (exports, _foo) {
|
||||
(function(obj) {
|
||||
for (var i in obj) {
|
||||
exports[i] = obj[i];
|
||||
@ -16,6 +14,4 @@ define(["foo"], function (_foo) {
|
||||
exports.default = _foo.foo;
|
||||
exports.default = _foo.foo;
|
||||
exports.bar = _foo.bar;
|
||||
|
||||
return exports;
|
||||
});
|
||||
|
||||
@ -1,8 +1,6 @@
|
||||
"use strict";
|
||||
|
||||
define([], function () {
|
||||
var exports = {};
|
||||
|
||||
define(["exports"], function (exports) {
|
||||
exports.foo = foo;
|
||||
exports.foo = foo;
|
||||
exports.bar = bar;
|
||||
@ -10,6 +8,4 @@ define([], function () {
|
||||
exports.default = foo;
|
||||
exports.default = foo;
|
||||
exports.bar = bar;
|
||||
|
||||
return exports;
|
||||
});
|
||||
|
||||
@ -1,8 +1,6 @@
|
||||
"use strict";
|
||||
|
||||
define([], function () {
|
||||
var exports = {};
|
||||
|
||||
define(["exports"], function (exports) {
|
||||
exports.foo7 = foo7;
|
||||
var foo = 1;
|
||||
exports.foo = foo;
|
||||
@ -24,6 +22,4 @@ define([], function () {
|
||||
}();
|
||||
|
||||
exports.foo8 = foo8;
|
||||
|
||||
return exports;
|
||||
});
|
||||
|
||||
@ -1,8 +1,6 @@
|
||||
"use strict";
|
||||
|
||||
define(["./evens"], function (_evens) {
|
||||
var exports = {};
|
||||
|
||||
define(["exports", "./evens"], function (exports, _evens) {
|
||||
exports.nextOdd = nextOdd;
|
||||
var isEven = _evens.isEven;
|
||||
|
||||
@ -17,6 +15,4 @@ define(["./evens"], function (_evens) {
|
||||
}(isEven);
|
||||
|
||||
exports.isOdd = isOdd;
|
||||
|
||||
return exports;
|
||||
});
|
||||
|
||||
@ -1,10 +1,6 @@
|
||||
"use strict";
|
||||
|
||||
define(["foo"], function (_foo) {
|
||||
var exports = {};
|
||||
|
||||
define(["exports", "foo"], function (exports, _foo) {
|
||||
var foo = _foo.default;
|
||||
var foo = _foo.default;
|
||||
|
||||
return exports;
|
||||
});
|
||||
|
||||
@ -1,9 +1,5 @@
|
||||
"use strict";
|
||||
|
||||
define(["foo"], function (_foo) {
|
||||
var exports = {};
|
||||
|
||||
define(["exports", "foo"], function (exports, _foo) {
|
||||
var foo = _foo;
|
||||
|
||||
return exports;
|
||||
});
|
||||
|
||||
@ -1,10 +1,6 @@
|
||||
"use strict";
|
||||
|
||||
define(["foo"], function (_foo) {
|
||||
var exports = {};
|
||||
|
||||
define(["exports", "foo"], function (exports, _foo) {
|
||||
var foo = _foo.default;
|
||||
var xyz = _foo.baz;
|
||||
|
||||
return exports;
|
||||
});
|
||||
|
||||
@ -1,14 +1,10 @@
|
||||
"use strict";
|
||||
|
||||
define(["foo"], function (_foo) {
|
||||
var exports = {};
|
||||
|
||||
define(["exports", "foo"], function (exports, _foo) {
|
||||
var bar = _foo.bar;
|
||||
var bar = _foo.bar;
|
||||
var baz = _foo.baz;
|
||||
var baz = _foo.bar;
|
||||
var baz = _foo.bar;
|
||||
var xyz = _foo.xyz;
|
||||
|
||||
return exports;
|
||||
});
|
||||
|
||||
@ -1,7 +1,5 @@
|
||||
"use strict";
|
||||
|
||||
define(["foo", "foo-bar", "./directory/foo-bar"], function (_foo, _fooBar, _directoryFooBar) {
|
||||
var exports = {};
|
||||
define(["exports", "foo", "foo-bar", "./directory/foo-bar"], function (exports, _foo, _fooBar, _directoryFooBar) {
|
||||
|
||||
return exports;
|
||||
});
|
||||
|
||||
@ -1,8 +1,6 @@
|
||||
"use strict";
|
||||
|
||||
define(["foo", "foo-bar", "./directory/foo-bar"], function (_foo, _fooBar, _directoryFooBar) {
|
||||
var exports = {};
|
||||
|
||||
define(["exports", "foo", "foo-bar", "./directory/foo-bar"], function (exports, _foo, _fooBar, _directoryFooBar) {
|
||||
var foo = _foo.default;
|
||||
var foo = _foo;
|
||||
var bar = _foo.bar;
|
||||
@ -11,6 +9,4 @@ define(["foo", "foo-bar", "./directory/foo-bar"], function (_foo, _fooBar, _dire
|
||||
var test = 5;
|
||||
exports.test = test;
|
||||
exports.default = test;
|
||||
|
||||
return exports;
|
||||
});
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user