add umd globals export - fixes #375

This commit is contained in:
Sebastian McKenzie
2015-04-01 23:02:03 +11:00
parent 22b88487ee
commit 76573093bd
20 changed files with 161 additions and 52 deletions

View File

@@ -78,9 +78,10 @@ var exportsVisitor = traverse.explode({
export default class DefaultFormatter {
constructor(file) {
this.scope = file.scope;
this.file = file;
this.ids = object();
this.defaultIds = object();
this.scope = file.scope;
this.file = file;
this.ids = object();
this.hasNonDefaultExports = false;

View File

@@ -69,8 +69,13 @@ export default class AMDFormatter extends DefaultFormatter {
}
importSpecifier(specifier, node, nodes) {
var key = node.source.value;
var ref = this.getExternalReference(node);
if (t.isImportNamespaceSpecifier(specifier) || t.isImportDefaultSpecifier(specifier)) {
this.defaultIds[key] = specifier.local;
}
if (includes(this.file.dynamicImportedNoDefault, node)) {
// Prevent unnecessary renaming of dynamic imports.
this.ids[node.source.value] = ref;

View File

@@ -10,7 +10,7 @@ export default class UMDFormatter extends AMDFormatter {
// build an array of module names
var names = [];
for (var name in this.ids) {
for (let name in this.ids) {
names.push(t.literal(name));
}
@@ -44,7 +44,13 @@ export default class UMDFormatter extends AMDFormatter {
// globals
//var umdArgs = [];
var browserArgs = [t.memberExpression(t.identifier("module"), t.identifier("exports"))];
if (this.passModuleArg) browserArgs.push(t.identifier("module"));
for (let name in this.ids) {
var id = this.defaultIds[name] || t.identifier(t.toIdentifier(name));
browserArgs.push(t.memberExpression(t.identifier("global"), id));
}
//
@@ -54,12 +60,15 @@ export default class UMDFormatter extends AMDFormatter {
var runner = util.template("umd-runner-body", {
AMD_ARGUMENTS: defineArgs,
COMMON_TEST: commonTests,
COMMON_ARGUMENTS: commonArgs
COMMON_ARGUMENTS: commonArgs,
BROWSER_ARGUMENTS: browserArgs,
GLOBAL_ARG: t.identifier(t.toIdentifier(this.file.opts.basename))
});
//
var call = t.callExpression(runner, [factory]);
program.body = [t.expressionStatement(call)];
program.body = [t.expressionStatement(
t.callExpression(runner, [t.thisExpression(), factory])
)];
}
}

View File

@@ -1,7 +1,7 @@
(function (root, factory) {
if (typeof define === "function" && define.amd) {
define(AMD_ARGUMENTS, factory);
} else if (typeof exports === 'object') {
} else if (typeof exports === "object") {
factory(COMMON_ARGUMENTS);
} else {
factory(BROWSER_ARGUMENTS);

View File

@@ -1,7 +1,11 @@
(function (factory) {
(function (global, factory) {
if (typeof define === "function" && define.amd) {
define(AMD_ARGUMENTS, factory);
} else if (COMMON_TEST) {
factory(COMMON_ARGUMENTS);
} else {
var module = { exports: {} };
factory(BROWSER_ARGUMENTS);
global.GLOBAL_ARG = module.exports;
}
});