clean up register module formatter and rename it to system
This commit is contained in:
@@ -10,9 +10,7 @@ var t = require("./types");
|
||||
var _ = require("lodash");
|
||||
|
||||
function File(opts) {
|
||||
this.opts = File.normaliseOptions(opts);
|
||||
this.moduleFormatter = this.getModuleFormatter(this.opts.modules);
|
||||
|
||||
this.opts = File.normaliseOptions(opts);
|
||||
this.uids = {};
|
||||
this.ast = {};
|
||||
}
|
||||
@@ -168,6 +166,7 @@ File.prototype.parse = function (code) {
|
||||
File.prototype.transform = function (ast) {
|
||||
this.ast = ast;
|
||||
this.scope = new Scope(ast.program);
|
||||
this.moduleFormatter = this.getModuleFormatter(this.opts.modules);
|
||||
|
||||
var self = this;
|
||||
|
||||
|
||||
@@ -41,34 +41,11 @@ AMDFormatter.prototype.transform = function (ast) {
|
||||
};
|
||||
|
||||
AMDFormatter.prototype.getModuleName = function () {
|
||||
var opts = this.file.opts;
|
||||
var filenameRelative = opts.filenameRelative;
|
||||
var moduleName = "";
|
||||
|
||||
if (!opts.amdModuleIds) {
|
||||
if (this.file.opts.amdModuleIds) {
|
||||
return CommonJSFormatter.prototype.getModuleName.apply(this, arguments);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (opts.moduleRoot) {
|
||||
moduleName = opts.moduleRoot + "/";
|
||||
}
|
||||
|
||||
if (!opts.filenameRelative) {
|
||||
return moduleName + opts.filename.replace(/^\//, "");
|
||||
}
|
||||
|
||||
if (opts.sourceRoot) {
|
||||
// remove sourceRoot from filename
|
||||
var sourceRootRegEx = new RegExp("^" + opts.sourceRoot + "\/?");
|
||||
filenameRelative = filenameRelative.replace(sourceRootRegEx, "");
|
||||
}
|
||||
|
||||
// remove extension
|
||||
filenameRelative = filenameRelative.replace(/\.(.*?)$/, "");
|
||||
|
||||
moduleName += filenameRelative;
|
||||
|
||||
return moduleName;
|
||||
};
|
||||
|
||||
AMDFormatter.prototype._push = function (node) {
|
||||
|
||||
@@ -7,6 +7,33 @@ function CommonJSFormatter(file) {
|
||||
this.file = file;
|
||||
}
|
||||
|
||||
CommonJSFormatter.prototype.getModuleName = function () {
|
||||
var opts = this.file.opts;
|
||||
var filenameRelative = opts.filenameRelative;
|
||||
var moduleName = "";
|
||||
|
||||
if (opts.moduleRoot) {
|
||||
moduleName = opts.moduleRoot + "/";
|
||||
}
|
||||
|
||||
if (!opts.filenameRelative) {
|
||||
return moduleName + opts.filename.replace(/^\//, "");
|
||||
}
|
||||
|
||||
if (opts.sourceRoot) {
|
||||
// remove sourceRoot from filename
|
||||
var sourceRootRegEx = new RegExp("^" + opts.sourceRoot + "\/?");
|
||||
filenameRelative = filenameRelative.replace(sourceRootRegEx, "");
|
||||
}
|
||||
|
||||
// remove extension
|
||||
filenameRelative = filenameRelative.replace(/\.(.*?)$/, "");
|
||||
|
||||
moduleName += filenameRelative;
|
||||
|
||||
return moduleName;
|
||||
};
|
||||
|
||||
CommonJSFormatter.prototype.import = function (node, nodes) {
|
||||
// import "foo";
|
||||
nodes.push(util.template("require", {
|
||||
|
||||
@@ -1,27 +1,24 @@
|
||||
module.exports = RegisterFormatter;
|
||||
module.exports = SystemFormatter;
|
||||
|
||||
var util = require("../../util");
|
||||
var t = require("../../types");
|
||||
var _ = require("lodash");
|
||||
var t = require("../../types");
|
||||
var _ = require("lodash");
|
||||
|
||||
var EXPORT_IDENTIFIER = t.identifier("$__export");
|
||||
var DEFAULT_IDENTIFIER = t.identifier("default");
|
||||
var SETTER_MODULE_NAMESPACE = t.identifier("m");
|
||||
var NULL_SETTER = {
|
||||
"type": "Literal",
|
||||
"value": null,
|
||||
"raw": "null"
|
||||
};
|
||||
var DEFAULT_IDENTIFIER = t.identifier("default");
|
||||
var NULL_SETTER = t.literal(null);
|
||||
|
||||
function RegisterFormatter(file) {
|
||||
this.file = file;
|
||||
this.importedModule = {};
|
||||
function SystemFormatter(file) {
|
||||
this.exportedStatements = [];
|
||||
this.importedModule = {};
|
||||
|
||||
this.exportIdentifier = file.generateUidIdentifier("export");
|
||||
this.file = file;
|
||||
}
|
||||
|
||||
RegisterFormatter.prototype.transform = function(ast) {
|
||||
SystemFormatter.prototype.transform = function (ast) {
|
||||
var program = ast.program;
|
||||
var body = program.body;
|
||||
var body = program.body;
|
||||
|
||||
// extract the module name
|
||||
var moduleName = this.file.opts.filename
|
||||
@@ -44,11 +41,11 @@ RegisterFormatter.prototype.transform = function(ast) {
|
||||
var declaredSetters = _(this.importedModule)
|
||||
.map()
|
||||
.flatten()
|
||||
.pluck("VARIABLE_NAME")
|
||||
.pluck("variableName")
|
||||
.pluck("name")
|
||||
.uniq()
|
||||
.map(t.identifier)
|
||||
.map(function(name) {
|
||||
.map(function (name) {
|
||||
return t.variableDeclarator(name);
|
||||
})
|
||||
.value();
|
||||
@@ -72,14 +69,13 @@ RegisterFormatter.prototype.transform = function(ast) {
|
||||
]));
|
||||
body.push(moduleReturnStatement);
|
||||
|
||||
|
||||
// runner
|
||||
var runner = util.template("register", {
|
||||
MODULE_NAME: t.literal(moduleName),
|
||||
MODULE_DEPENDENCIES: t.arrayExpression(dependencies),
|
||||
MODULE_BODY: t.functionExpression(
|
||||
null,
|
||||
[EXPORT_IDENTIFIER],
|
||||
[this.exportIdentifier],
|
||||
t.blockStatement(body)
|
||||
)
|
||||
});
|
||||
@@ -88,44 +84,37 @@ RegisterFormatter.prototype.transform = function(ast) {
|
||||
};
|
||||
|
||||
|
||||
RegisterFormatter.prototype._buildSetters = function() {
|
||||
SystemFormatter.prototype._buildSetters = function () {
|
||||
// generate setters array expression elements
|
||||
return _(this.importedModule)
|
||||
.map(function(specifications) {
|
||||
return _.map(this.importedModule, function (specs) {
|
||||
if (!specs.length) {
|
||||
return NULL_SETTER;
|
||||
}
|
||||
|
||||
if (!specifications.length) {
|
||||
return NULL_SETTER;
|
||||
var expressionStatements = _.map(specs, function (spec) {
|
||||
var right = SETTER_MODULE_NAMESPACE;
|
||||
if (!spec.isBatch) {
|
||||
right = t.memberExpression(right, spec.key);
|
||||
}
|
||||
|
||||
var expressionStatements = _.map(specifications, function(specification) {
|
||||
return t.expressionStatement(
|
||||
t.assignmentExpression(
|
||||
"=",
|
||||
specification.VARIABLE_NAME,
|
||||
specification.IS_BATCH ?
|
||||
SETTER_MODULE_NAMESPACE :
|
||||
t.memberExpression(
|
||||
SETTER_MODULE_NAMESPACE,
|
||||
specification.KEY
|
||||
)
|
||||
)
|
||||
);
|
||||
});
|
||||
|
||||
return t.functionExpression(
|
||||
null, [SETTER_MODULE_NAMESPACE], t.blockStatement(expressionStatements)
|
||||
return t.expressionStatement(
|
||||
t.assignmentExpression("=", spec.variableName, right
|
||||
)
|
||||
);
|
||||
}.bind(this))
|
||||
.value();
|
||||
});
|
||||
|
||||
return t.functionExpression(
|
||||
null, [SETTER_MODULE_NAMESPACE], t.blockStatement(expressionStatements)
|
||||
);
|
||||
});
|
||||
};
|
||||
|
||||
RegisterFormatter.prototype.import = function(node) {
|
||||
SystemFormatter.prototype.import = function (node) {
|
||||
var MODULE_NAME = node.source.value;
|
||||
this.importedModule[MODULE_NAME] =
|
||||
this.importedModule[MODULE_NAME] || [];
|
||||
this.importedModule[MODULE_NAME] = this.importedModule[MODULE_NAME] || [];
|
||||
};
|
||||
|
||||
RegisterFormatter.prototype.importSpecifier = function(specifier, node) {
|
||||
SystemFormatter.prototype.importSpecifier = function (specifier, node) {
|
||||
var variableName = t.getSpecifierName(specifier);
|
||||
|
||||
// import foo from "foo";
|
||||
@@ -135,26 +124,22 @@ RegisterFormatter.prototype.importSpecifier = function(specifier, node) {
|
||||
|
||||
var MODULE_NAME = node.source.value;
|
||||
|
||||
this.importedModule[MODULE_NAME] =
|
||||
this.importedModule[MODULE_NAME] || [];
|
||||
this.importedModule[MODULE_NAME] = this.importedModule[MODULE_NAME] || [];
|
||||
|
||||
this.importedModule[MODULE_NAME].push(
|
||||
{
|
||||
VARIABLE_NAME: variableName,
|
||||
KEY: specifier.id,
|
||||
IS_BATCH: specifier.type === "ImportBatchSpecifier"
|
||||
}
|
||||
);
|
||||
this.importedModule[MODULE_NAME].push({
|
||||
variableName: variableName,
|
||||
isBatch: specifier.type === "ImportBatchSpecifier",
|
||||
key: specifier.id
|
||||
});
|
||||
};
|
||||
|
||||
RegisterFormatter.prototype._export = function(name, identifier) {
|
||||
SystemFormatter.prototype._export = function (name, identifier) {
|
||||
this.exportedStatements.push(t.expressionStatement(
|
||||
t.callExpression(EXPORT_IDENTIFIER, [t.literal(name), identifier])
|
||||
t.callExpression(this.exportIdentifier, [t.literal(name), identifier])
|
||||
));
|
||||
};
|
||||
|
||||
RegisterFormatter.prototype.export = function(node, nodes) {
|
||||
|
||||
SystemFormatter.prototype.export = function (node, nodes) {
|
||||
var declar = node.declaration;
|
||||
var variableName, identifier;
|
||||
|
||||
@@ -162,61 +147,48 @@ RegisterFormatter.prototype.export = function(node, nodes) {
|
||||
// export default foo
|
||||
variableName = DEFAULT_IDENTIFIER.name;
|
||||
if (t.isClass(declar) || t.isFunction(declar)) {
|
||||
|
||||
if (!declar.id) {
|
||||
declar.id = this.file.generateUidIdentifier("anonymousFct");
|
||||
declar.id = this.file.generateUidIdentifier("anonymous");
|
||||
}
|
||||
|
||||
nodes.push(t.toStatement(declar));
|
||||
declar = declar.id;
|
||||
|
||||
}
|
||||
|
||||
identifier = declar;
|
||||
|
||||
} else if (t.isVariableDeclaration(declar)) {
|
||||
// export var foo
|
||||
variableName = declar.declarations[0].id.name;
|
||||
identifier = declar.declarations[0].id;
|
||||
|
||||
nodes.push((declar));
|
||||
|
||||
nodes.push(declar);
|
||||
} else {
|
||||
// export function foo () {}
|
||||
variableName = declar.id.name;
|
||||
identifier = declar.id;
|
||||
|
||||
nodes.push(declar);
|
||||
|
||||
}
|
||||
|
||||
this._export(variableName, identifier);
|
||||
|
||||
};
|
||||
|
||||
RegisterFormatter.prototype.exportSpecifier = function(specifier, node) {
|
||||
SystemFormatter.prototype.exportSpecifier = function (specifier, node) {
|
||||
var variableName = t.getSpecifierName(specifier);
|
||||
|
||||
if (node.source) {
|
||||
|
||||
if (t.isExportBatchSpecifier(specifier)) {
|
||||
// export * from "foo";
|
||||
var exportIdentifier = t.identifier("exports");
|
||||
this.exportedStatements.push(
|
||||
t.variableDeclaration("var",
|
||||
[
|
||||
t.variableDeclarator(
|
||||
exportIdentifier,
|
||||
EXPORT_IDENTIFIER
|
||||
)
|
||||
]
|
||||
)
|
||||
t.variableDeclaration("var", [
|
||||
t.variableDeclarator(exportIdentifier, this.exportIdentifier)
|
||||
])
|
||||
);
|
||||
|
||||
this.exportedStatements.push(util.template("exports-wildcard", {
|
||||
OBJECT: t.identifier(node.source.value)
|
||||
}, true));
|
||||
|
||||
} else {
|
||||
// export { foo } from "test";
|
||||
this._export(variableName.name, t.memberExpression(
|
||||
@@ -228,6 +200,4 @@ RegisterFormatter.prototype.exportSpecifier = function(specifier, node) {
|
||||
// export { foo };
|
||||
this._export(variableName.name, specifier.id);
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
@@ -20,9 +20,9 @@ transform._ensureTransformerNames = function (type, keys) {
|
||||
transform.transformers = {};
|
||||
|
||||
transform.moduleFormatters = {
|
||||
register: require("./modules/register"),
|
||||
common: require("./modules/common"),
|
||||
commonInterop: require("./modules/common-interop"),
|
||||
system: require("./modules/system"),
|
||||
ignore: require("./modules/ignore"),
|
||||
amd: require("./modules/amd"),
|
||||
umd: require("./modules/umd")
|
||||
|
||||
Reference in New Issue
Block a user