diff --git a/lib/6to5/file.js b/lib/6to5/file.js index 994fc344d5..fd8117e65f 100644 --- a/lib/6to5/file.js +++ b/lib/6to5/file.js @@ -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) { diff --git a/lib/6to5/transformation/modules/amd.js b/lib/6to5/transformation/modules/amd.js index 00d60a7077..eca421f199 100644 --- a/lib/6to5/transformation/modules/amd.js +++ b/lib/6to5/transformation/modules/amd.js @@ -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) { diff --git a/lib/6to5/transformation/modules/common.js b/lib/6to5/transformation/modules/common.js index 8238f304bc..c59d512248 100644 --- a/lib/6to5/transformation/modules/common.js +++ b/lib/6to5/transformation/modules/common.js @@ -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); diff --git a/lib/6to5/transformation/modules/system.js b/lib/6to5/transformation/modules/system.js index feb17fe479..794616c0f3 100644 --- a/lib/6to5/transformation/modules/system.js +++ b/lib/6to5/transformation/modules/system.js @@ -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; diff --git a/lib/6to5/transformation/transformers/index.js b/lib/6to5/transformation/transformers/index.js index ec9698c2ce..5ca676a1ad 100644 --- a/lib/6to5/transformation/transformers/index.js +++ b/lib/6to5/transformation/transformers/index.js @@ -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` diff --git a/lib/6to5/transformation/transformers/internal/modules-split.js b/lib/6to5/transformation/transformers/internal/modules.js similarity index 78% rename from lib/6to5/transformation/transformers/internal/modules-split.js rename to lib/6to5/transformation/transformers/internal/modules.js index d15d189f09..1477c94b0f 100644 --- a/lib/6to5/transformation/transformers/internal/modules-split.js +++ b/lib/6to5/transformation/transformers/internal/modules.js @@ -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) { diff --git a/test/fixtures/transformation/api/resolve-module-source/actual.js b/test/fixtures/transformation/api/resolve-module-source/actual.js new file mode 100644 index 0000000000..3cde6441d4 --- /dev/null +++ b/test/fixtures/transformation/api/resolve-module-source/actual.js @@ -0,0 +1,3 @@ +export { foo } from "foo-export-named"; +import foo from "foo-import-default"; +import "foo-import-bare"; diff --git a/test/fixtures/transformation/api/resolve-module-source/expected.js b/test/fixtures/transformation/api/resolve-module-source/expected.js new file mode 100644 index 0000000000..98b45f51e6 --- /dev/null +++ b/test/fixtures/transformation/api/resolve-module-source/expected.js @@ -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"; diff --git a/test/fixtures/transformation/api/resolve-module-source/options.js b/test/fixtures/transformation/api/resolve-module-source/options.js new file mode 100644 index 0000000000..e54ae43bfa --- /dev/null +++ b/test/fixtures/transformation/api/resolve-module-source/options.js @@ -0,0 +1,5 @@ +exports.blacklist = ["es6.modules"]; + +exports.resolveModuleSource = function (originalSource) { + return "resolved/" + originalSource; +};