From 035829e7262489d8698036e291eb8027621752da Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Wed, 22 Oct 2014 21:43:43 +1100 Subject: [PATCH] add custom module formatters --- MODULES.md | 28 ++++++++++++++++++++++++++++ lib/6to5/file.js | 11 ++++++++++- 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/MODULES.md b/MODULES.md index a75ada8cb9..8f8980169c 100644 --- a/MODULES.md +++ b/MODULES.md @@ -108,3 +108,31 @@ export function bar() { } }); ``` + +## Custom + +```javascript +module.exports = ModuleFormatter; + +function ModuleFormatter() { + +} + +ModuleFormatter.prototype.import = function (node, nodes) { + // node is an ImportDeclaration +}; + +ModuleFormatter.prototype.importSpecifier = function (specifier, node, nodes) { + // specifier is an ImportSpecifier + // node is an ImportDeclaration +}; + +ModuleFormatter.prototype.export = function (node, nodes) { + // node is an ExportDeclaration +}; + +ModuleFormatter.prototype.exportSpecifier = function (specifier, node, nodes) { + // specifier is an ExportSpecifier + // node is an ExportDeclaration +}; +``` diff --git a/lib/6to5/file.js b/lib/6to5/file.js index 8f9dcb53f3..aefbfa2903 100644 --- a/lib/6to5/file.js +++ b/lib/6to5/file.js @@ -41,7 +41,16 @@ File.normaliseOptions = function (opts) { File.prototype.getModuleFormatter = function (type) { var ModuleLoader = transform.moduleFormatters[type]; - if (!ModuleLoader) throw new ReferenceError("unknown module formatter type " + type); + + if (!ModuleLoader) { + var loc = util.resolve(type) + if (loc) ModuleLoader = require(loc); + } + + if (!ModuleLoader) { + throw new ReferenceError("unknown module formatter type " + type); + } + return new ModuleLoader(this); };