add umd module formatter - closes #82
This commit is contained in:
@@ -10,6 +10,8 @@ function AMDFormatter(file) {
|
||||
this.ids = {};
|
||||
}
|
||||
|
||||
util.inherits(AMDFormatter, CommonJSFormatter);
|
||||
|
||||
AMDFormatter.prototype.transform = function (ast) {
|
||||
var program = ast.program;
|
||||
var body = program.body;
|
||||
@@ -76,8 +78,6 @@ AMDFormatter.prototype.importSpecifier = function (specifier, node, nodes) {
|
||||
]));
|
||||
};
|
||||
|
||||
AMDFormatter.prototype.export = CommonJSFormatter.prototype.export;
|
||||
|
||||
AMDFormatter.prototype.exportSpecifier = function (specifier, node, nodes) {
|
||||
var variableName = util.getSpecifierName(specifier);
|
||||
|
||||
|
||||
48
lib/6to5/modules/umd.js
Normal file
48
lib/6to5/modules/umd.js
Normal file
@@ -0,0 +1,48 @@
|
||||
module.exports = UMDFormatter;
|
||||
|
||||
var CommonJSFormatter = require("./common");
|
||||
var AMDFormatter = require("./amd");
|
||||
var util = require("../util");
|
||||
var b = require("ast-types").builders;
|
||||
var _ = require("lodash");
|
||||
|
||||
function UMDFormatter(file) {
|
||||
this.file = file;
|
||||
this.ids = {};
|
||||
}
|
||||
|
||||
util.inherits(UMDFormatter, AMDFormatter);
|
||||
|
||||
UMDFormatter.prototype.transform = function (ast) {
|
||||
var program = ast.program;
|
||||
var body = program.body;
|
||||
|
||||
// build an array of module names
|
||||
|
||||
var names = [];
|
||||
_.each(this.ids, function (id, name) {
|
||||
names.push(b.literal(name));
|
||||
});
|
||||
|
||||
// factory
|
||||
|
||||
var ids = _.values(this.ids);
|
||||
var args = [b.identifier("exports")].concat(ids);
|
||||
|
||||
var factory = b.functionExpression(null, args, b.blockStatement(body));
|
||||
|
||||
// runner
|
||||
|
||||
var runner = util.template("umd-runner-body", {
|
||||
AMD_ARGUMENTS: b.arrayExpression([b.literal("exports")].concat(names)),
|
||||
|
||||
COMMON_ARGUMENTS: names.map(function (name) {
|
||||
return b.callExpression(b.identifier("require"), [name])
|
||||
})
|
||||
});
|
||||
|
||||
//
|
||||
|
||||
var call = b.callExpression(runner, [b.thisExpression(), factory]);
|
||||
program.body = [b.expressionStatement(call)];
|
||||
};
|
||||
7
lib/6to5/templates/umd-runner-body.js
Normal file
7
lib/6to5/templates/umd-runner-body.js
Normal file
@@ -0,0 +1,7 @@
|
||||
(function (root, factory) {
|
||||
if (typeof define === "function" && define.amd) {
|
||||
define(AMD_ARGUMENTS, factory);
|
||||
} else if (typeof exports !== "undefined") {
|
||||
factory(exports, COMMON_ARGUMENTS);
|
||||
}
|
||||
});
|
||||
@@ -106,7 +106,8 @@ transform.transformers = {
|
||||
|
||||
transform.moduleFormatters = {
|
||||
common: require("./modules/common"),
|
||||
amd: require("./modules/amd")
|
||||
amd: require("./modules/amd"),
|
||||
umd: require("./modules/umd")
|
||||
};
|
||||
|
||||
_.each(transform.transformers, function (transformer, key) {
|
||||
|
||||
@@ -3,12 +3,15 @@ var astTypes = require("recast").types;
|
||||
var recast = require("recast");
|
||||
var acorn = require("acorn");
|
||||
var path = require("path");
|
||||
var util = require("util");
|
||||
var fs = require("fs");
|
||||
var _ = require("lodash");
|
||||
|
||||
var n = astTypes.namedTypes;
|
||||
var b = astTypes.builders;
|
||||
|
||||
exports.inherits = util.inherits;
|
||||
|
||||
exports.ensureBlock = function (node) {
|
||||
var block = node.body;
|
||||
if (block.type === "BlockStatement") return;
|
||||
|
||||
8
test/fixtures/syntax/modules-umd/exports-default/actual.js
vendored
Normal file
8
test/fixtures/syntax/modules-umd/exports-default/actual.js
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
export default 42;
|
||||
export default {};
|
||||
export default [];
|
||||
export default foo;
|
||||
export default function () {}
|
||||
export default class {}
|
||||
export default function foo () {}
|
||||
export default class foo {}
|
||||
27
test/fixtures/syntax/modules-umd/exports-default/expected.js
vendored
Normal file
27
test/fixtures/syntax/modules-umd/exports-default/expected.js
vendored
Normal file
@@ -0,0 +1,27 @@
|
||||
"use strict";
|
||||
|
||||
(function (root, factory) {
|
||||
if (typeof define === "function" && define.amd) {
|
||||
define(["exports"], factory);
|
||||
} else if (typeof exports !== "undefined") {
|
||||
factory(exports);
|
||||
}
|
||||
})(this, function (exports) {
|
||||
exports.default = 42;
|
||||
exports.default = {};
|
||||
exports.default = [];
|
||||
exports.default = foo;
|
||||
exports.default = function() {};
|
||||
|
||||
exports.default = function() {
|
||||
var _class = function() {};
|
||||
return _class;
|
||||
}();
|
||||
|
||||
exports.default = function foo() {};
|
||||
|
||||
exports.default = function() {
|
||||
var foo = function foo() {};
|
||||
return foo;
|
||||
}();
|
||||
});
|
||||
6
test/fixtures/syntax/modules-umd/exports-from/actual.js
vendored
Normal file
6
test/fixtures/syntax/modules-umd/exports-from/actual.js
vendored
Normal file
@@ -0,0 +1,6 @@
|
||||
export * from "foo";
|
||||
export {foo} from "foo";
|
||||
export {foo, bar} from "foo";
|
||||
export {foo as bar} from "foo";
|
||||
export {foo as default} from "foo";
|
||||
export {foo as default, bar} from "foo";
|
||||
23
test/fixtures/syntax/modules-umd/exports-from/expected.js
vendored
Normal file
23
test/fixtures/syntax/modules-umd/exports-from/expected.js
vendored
Normal file
@@ -0,0 +1,23 @@
|
||||
"use strict";
|
||||
|
||||
(function (root, factory) {
|
||||
if (typeof define === "function" && define.amd) {
|
||||
define(["exports", "foo"], factory);
|
||||
} else if (typeof exports !== "undefined") {
|
||||
factory(exports, require("foo"));
|
||||
}
|
||||
})(this, function (exports, _foo) {
|
||||
(function(obj) {
|
||||
for (var i in obj) {
|
||||
exports[i] = obj[i];
|
||||
}
|
||||
})(_foo);
|
||||
|
||||
exports.foo = _foo.foo;
|
||||
exports.foo = _foo.foo;
|
||||
exports.bar = _foo.bar;
|
||||
exports.bar = _foo.foo;
|
||||
exports.default = _foo.foo;
|
||||
exports.default = _foo.foo;
|
||||
exports.bar = _foo.bar;
|
||||
});
|
||||
5
test/fixtures/syntax/modules-umd/exports-named/actual.js
vendored
Normal file
5
test/fixtures/syntax/modules-umd/exports-named/actual.js
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
export {foo};
|
||||
export {foo, bar};
|
||||
export {foo as bar};
|
||||
export {foo as default};
|
||||
export {foo as default, bar};
|
||||
17
test/fixtures/syntax/modules-umd/exports-named/expected.js
vendored
Normal file
17
test/fixtures/syntax/modules-umd/exports-named/expected.js
vendored
Normal file
@@ -0,0 +1,17 @@
|
||||
"use strict";
|
||||
|
||||
(function (root, factory) {
|
||||
if (typeof define === "function" && define.amd) {
|
||||
define(["exports"], factory);
|
||||
} else if (typeof exports !== "undefined") {
|
||||
factory(exports);
|
||||
}
|
||||
})(this, function (exports) {
|
||||
exports.foo = foo;
|
||||
exports.foo = foo;
|
||||
exports.bar = bar;
|
||||
exports.bar = foo;
|
||||
exports.default = foo;
|
||||
exports.default = foo;
|
||||
exports.bar = bar;
|
||||
});
|
||||
8
test/fixtures/syntax/modules-umd/exports-variable/actual.js
vendored
Normal file
8
test/fixtures/syntax/modules-umd/exports-variable/actual.js
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
export var foo = 1;
|
||||
export var foo2 = function () {};
|
||||
export var foo3;
|
||||
export let foo4 = 2;
|
||||
export let foo5;
|
||||
export const foo6 = 3;
|
||||
export function foo7 () {}
|
||||
export class foo8 {}
|
||||
31
test/fixtures/syntax/modules-umd/exports-variable/expected.js
vendored
Normal file
31
test/fixtures/syntax/modules-umd/exports-variable/expected.js
vendored
Normal file
@@ -0,0 +1,31 @@
|
||||
"use strict";
|
||||
|
||||
(function (root, factory) {
|
||||
if (typeof define === "function" && define.amd) {
|
||||
define(["exports"], factory);
|
||||
} else if (typeof exports !== "undefined") {
|
||||
factory(exports);
|
||||
}
|
||||
})(this, function (exports) {
|
||||
exports.foo7 = foo7;
|
||||
var foo = 1;
|
||||
exports.foo = foo;
|
||||
var foo2 = function() {};
|
||||
exports.foo2 = foo2;
|
||||
var foo3;
|
||||
exports.foo3 = foo3;
|
||||
var _foo4 = 2;
|
||||
exports.foo4 = _foo4;
|
||||
var _foo5;
|
||||
exports.foo5 = _foo5;
|
||||
var _foo6 = 3;
|
||||
exports.foo6 = _foo6;
|
||||
function foo7() {}
|
||||
|
||||
var foo8 = function() {
|
||||
var foo8 = function foo8() {};
|
||||
return foo8;
|
||||
}();
|
||||
|
||||
exports.foo8 = foo8;
|
||||
});
|
||||
11
test/fixtures/syntax/modules-umd/hoist-function-exports/actual.js
vendored
Normal file
11
test/fixtures/syntax/modules-umd/hoist-function-exports/actual.js
vendored
Normal file
@@ -0,0 +1,11 @@
|
||||
import { isEven } from "./evens";
|
||||
|
||||
export function nextOdd(n) {
|
||||
return isEven(n) ? n + 1 : n + 2;
|
||||
}
|
||||
|
||||
export var isOdd = (function(isEven) {
|
||||
return function(n) {
|
||||
return !isEven(n);
|
||||
};
|
||||
})(isEven);
|
||||
24
test/fixtures/syntax/modules-umd/hoist-function-exports/expected.js
vendored
Normal file
24
test/fixtures/syntax/modules-umd/hoist-function-exports/expected.js
vendored
Normal file
@@ -0,0 +1,24 @@
|
||||
"use strict";
|
||||
|
||||
(function (root, factory) {
|
||||
if (typeof define === "function" && define.amd) {
|
||||
define(["exports", "./evens"], factory);
|
||||
} else if (typeof exports !== "undefined") {
|
||||
factory(exports, require("./evens"));
|
||||
}
|
||||
})(this, function (exports, _evens) {
|
||||
exports.nextOdd = nextOdd;
|
||||
var isEven = _evens.isEven;
|
||||
|
||||
function nextOdd(n) {
|
||||
return (isEven(n) ? n + 1 : n + 2);
|
||||
}
|
||||
|
||||
var isOdd = function(isEven) {
|
||||
return function(n) {
|
||||
return !isEven(n);
|
||||
};
|
||||
}(isEven);
|
||||
|
||||
exports.isOdd = isOdd;
|
||||
});
|
||||
2
test/fixtures/syntax/modules-umd/imports-default/actual.js
vendored
Normal file
2
test/fixtures/syntax/modules-umd/imports-default/actual.js
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
import foo from "foo";
|
||||
import {default as foo} from "foo";
|
||||
12
test/fixtures/syntax/modules-umd/imports-default/expected.js
vendored
Normal file
12
test/fixtures/syntax/modules-umd/imports-default/expected.js
vendored
Normal file
@@ -0,0 +1,12 @@
|
||||
"use strict";
|
||||
|
||||
(function (root, factory) {
|
||||
if (typeof define === "function" && define.amd) {
|
||||
define(["exports", "foo"], factory);
|
||||
} else if (typeof exports !== "undefined") {
|
||||
factory(exports, require("foo"));
|
||||
}
|
||||
})(this, function (exports, _foo) {
|
||||
var foo = _foo.default;
|
||||
var foo = _foo.default;
|
||||
});
|
||||
1
test/fixtures/syntax/modules-umd/imports-glob/actual.js
vendored
Normal file
1
test/fixtures/syntax/modules-umd/imports-glob/actual.js
vendored
Normal file
@@ -0,0 +1 @@
|
||||
import * as foo from "foo";
|
||||
11
test/fixtures/syntax/modules-umd/imports-glob/expected.js
vendored
Normal file
11
test/fixtures/syntax/modules-umd/imports-glob/expected.js
vendored
Normal file
@@ -0,0 +1,11 @@
|
||||
"use strict";
|
||||
|
||||
(function (root, factory) {
|
||||
if (typeof define === "function" && define.amd) {
|
||||
define(["exports", "foo"], factory);
|
||||
} else if (typeof exports !== "undefined") {
|
||||
factory(exports, require("foo"));
|
||||
}
|
||||
})(this, function (exports, _foo) {
|
||||
var foo = _foo;
|
||||
});
|
||||
1
test/fixtures/syntax/modules-umd/imports-mixing/actual.js
vendored
Normal file
1
test/fixtures/syntax/modules-umd/imports-mixing/actual.js
vendored
Normal file
@@ -0,0 +1 @@
|
||||
import foo, {baz as xyz} from "foo";
|
||||
12
test/fixtures/syntax/modules-umd/imports-mixing/expected.js
vendored
Normal file
12
test/fixtures/syntax/modules-umd/imports-mixing/expected.js
vendored
Normal file
@@ -0,0 +1,12 @@
|
||||
"use strict";
|
||||
|
||||
(function (root, factory) {
|
||||
if (typeof define === "function" && define.amd) {
|
||||
define(["exports", "foo"], factory);
|
||||
} else if (typeof exports !== "undefined") {
|
||||
factory(exports, require("foo"));
|
||||
}
|
||||
})(this, function (exports, _foo) {
|
||||
var foo = _foo.default;
|
||||
var xyz = _foo.baz;
|
||||
});
|
||||
4
test/fixtures/syntax/modules-umd/imports-named/actual.js
vendored
Normal file
4
test/fixtures/syntax/modules-umd/imports-named/actual.js
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
import {bar} from "foo";
|
||||
import {bar, baz} from "foo";
|
||||
import {bar as baz} from "foo";
|
||||
import {bar as baz, xyz} from "foo";
|
||||
16
test/fixtures/syntax/modules-umd/imports-named/expected.js
vendored
Normal file
16
test/fixtures/syntax/modules-umd/imports-named/expected.js
vendored
Normal file
@@ -0,0 +1,16 @@
|
||||
"use strict";
|
||||
|
||||
(function (root, factory) {
|
||||
if (typeof define === "function" && define.amd) {
|
||||
define(["exports", "foo"], factory);
|
||||
} else if (typeof exports !== "undefined") {
|
||||
factory(exports, require("foo"));
|
||||
}
|
||||
})(this, 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;
|
||||
});
|
||||
3
test/fixtures/syntax/modules-umd/imports/actual.js
vendored
Normal file
3
test/fixtures/syntax/modules-umd/imports/actual.js
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
import "foo";
|
||||
import "foo-bar";
|
||||
import "./directory/foo-bar";
|
||||
11
test/fixtures/syntax/modules-umd/imports/expected.js
vendored
Normal file
11
test/fixtures/syntax/modules-umd/imports/expected.js
vendored
Normal file
@@ -0,0 +1,11 @@
|
||||
"use strict";
|
||||
|
||||
(function (root, factory) {
|
||||
if (typeof define === "function" && define.amd) {
|
||||
define(["exports", "foo", "foo-bar", "./directory/foo-bar"], factory);
|
||||
} else if (typeof exports !== "undefined") {
|
||||
factory(exports, require("foo"), require("foo-bar"), require("./directory/foo-bar"));
|
||||
}
|
||||
})(this, function (exports, _foo, _fooBar, _directoryFooBar) {
|
||||
|
||||
});
|
||||
12
test/fixtures/syntax/modules-umd/overview/actual.js
vendored
Normal file
12
test/fixtures/syntax/modules-umd/overview/actual.js
vendored
Normal file
@@ -0,0 +1,12 @@
|
||||
import "foo";
|
||||
import "foo-bar";
|
||||
import "./directory/foo-bar";
|
||||
import foo from "foo";
|
||||
import * as foo from "foo";
|
||||
import {bar} from "foo";
|
||||
import {foo as bar} from "foo";
|
||||
|
||||
export {test};
|
||||
export var test = 5;
|
||||
|
||||
export default test;
|
||||
18
test/fixtures/syntax/modules-umd/overview/expected.js
vendored
Normal file
18
test/fixtures/syntax/modules-umd/overview/expected.js
vendored
Normal file
@@ -0,0 +1,18 @@
|
||||
"use strict";
|
||||
|
||||
(function (root, factory) {
|
||||
if (typeof define === "function" && define.amd) {
|
||||
define(["exports", "foo", "foo-bar", "./directory/foo-bar"], factory);
|
||||
} else if (typeof exports !== "undefined") {
|
||||
factory(exports, require("foo"), require("foo-bar"), require("./directory/foo-bar"));
|
||||
}
|
||||
})(this, function (exports, _foo, _fooBar, _directoryFooBar) {
|
||||
var foo = _foo.default;
|
||||
var foo = _foo;
|
||||
var bar = _foo.bar;
|
||||
var bar = _foo.foo;
|
||||
exports.test = test;
|
||||
var test = 5;
|
||||
exports.test = test;
|
||||
exports.default = test;
|
||||
});
|
||||
Reference in New Issue
Block a user