add custom module formatters

This commit is contained in:
Sebastian McKenzie
2014-10-22 21:43:43 +11:00
parent 2b70df4141
commit 035829e726
2 changed files with 38 additions and 1 deletions

View File

@@ -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
};
```

View File

@@ -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);
};