add resolveModuleSource option - closes #471

This commit is contained in:
Sebastian McKenzie 2015-01-30 18:04:54 +11:00
parent b2ec15accc
commit 3b783979d8
9 changed files with 42 additions and 6 deletions

View File

@ -74,6 +74,7 @@ File.validOptions = [
"format",
"playground",
"experimental",
"resolveModuleSource",
// these are used by plugins
"ignore",
@ -92,6 +93,7 @@ File.normaliseOptions = function (opts) {
defaults(opts, {
keepModuleIdExtensions: false,
resolveModuleSource: null,
experimental: false,
reactCompat: false,
playground: false,
@ -332,7 +334,11 @@ File.prototype.transform = function (ast) {
this.ast = ast;
this.lastStatements = t.getLastStatements(ast.program);
this.scope = new Scope(ast.program, ast, null, this);
this.moduleFormatter = this.getModuleFormatter(this.opts.modules);
var modFormatter = this.moduleFormatter = this.getModuleFormatter(this.opts.modules);
if (modFormatter.init && this.transformers["es6.modules"].canRun()) {
modFormatter.init();
}
var astRun = function (key) {
each(self.transformerStack, function (pass) {

View File

@ -15,6 +15,8 @@ function AMDFormatter() {
util.inherits(AMDFormatter, DefaultFormatter);
AMDFormatter.prototype.init = CommonFormatter.prototype.init;
AMDFormatter.prototype.buildDependencyLiterals = function () {
var names = [];
for (var name in this.ids) {

View File

@ -9,14 +9,16 @@ var contains = require("lodash/collection/contains");
function CommonJSFormatter(file) {
DefaultFormatter.apply(this, arguments);
if (this.hasNonDefaultExports) {
file.ast.program.body.push(util.template("exports-module-declaration", true));
}
}
util.inherits(CommonJSFormatter, DefaultFormatter);
CommonJSFormatter.prototype.init = function () {
if (this.hasNonDefaultExports) {
this.file.ast.program.body.push(util.template("exports-module-declaration", true));
}
};
CommonJSFormatter.prototype.importSpecifier = function (specifier, node, nodes) {
var variableName = t.getSpecifierName(specifier);

View File

@ -21,6 +21,8 @@ function SystemFormatter(file) {
util.inherits(SystemFormatter, AMDFormatter);
SystemFormatter.prototype.init = function () {};
SystemFormatter.prototype._addImportSource = function (node, exportNode) {
node._importSource = exportNode.source && exportNode.source.value;
return node;

View File

@ -13,7 +13,7 @@ module.exports = {
react: require("./other/react"),
_modulesSplit: require("./internal/modules-split"),
_modules: require("./internal/modules"),
// needs to be before `regenerator` due to generator comprehensions
// needs to be before `_aliasFunction`

View File

@ -8,7 +8,18 @@
var t = require("../../../types");
var resolveModuleSource = function (node, parent, scope, context, file) {
var resolveModuleSource = file.opts.resolveModuleSource;
if (node.source && resolveModuleSource) {
node.source.value = resolveModuleSource(node.source.value);
}
};
exports.ImportDeclaration = resolveModuleSource;
exports.ExportDeclaration = function (node, parent, scope) {
resolveModuleSource.apply(null, arguments);
var declar = node.declaration;
if (node.default) {

View File

@ -0,0 +1,3 @@
export { foo } from "foo-export-named";
import foo from "foo-import-default";
import "foo-import-bare";

View File

@ -0,0 +1,5 @@
"use strict";
export { foo } from "resolved/foo-export-named";
import foo from "resolved/foo-import-default";
import "resolved/foo-import-bare";

View File

@ -0,0 +1,5 @@
exports.blacklist = ["es6.modules"];
exports.resolveModuleSource = function (originalSource) {
return "resolved/" + originalSource;
};