fix plugin api

This commit is contained in:
Sebastian McKenzie 2015-04-03 01:53:25 +11:00
parent ab55ec4ea2
commit cb0026edfe
3 changed files with 15 additions and 15 deletions

View File

@ -29,6 +29,7 @@ See [CHANGELOG - 6to5](CHANGELOG-6to5.md) for the pre-4.0.0 version changelog.
* Add `jsxPragma` option.
* Automatically generate CLI options based on internal API options.
* Add support for `.babelrc` on absolute paths.
* Plugin API!
* **Internal**
* Export `options` in browser API.
* Rewritten parser.

View File

@ -6,7 +6,6 @@ import isFunction from "lodash/lang/isFunction";
import isAbsolute from "path-is-absolute";
import resolveRc from "../../tools/resolve-rc";
import sourceMap from "source-map";
import Transformer from "../transformer";
import transform from "./../index";
import generate from "../../generation";
import defaults from "lodash/object/defaults";
@ -177,7 +176,7 @@ export default class File {
buildTransformers() {
var file = this;
var transformers = {};
var transformers = this.transformers = {};
var secondaryStack = [];
var stack = [];
@ -202,14 +201,13 @@ export default class File {
// init plugins!
var beforePlugins = [];
var afterPlugins = [];
for (var i = 0; i < file.opts.plugins; i++) {
this.addPlugin(file.opts.plugins[i]);
for (var i = 0; i < file.opts.plugins.length; i++) {
this.addPlugin(file.opts.plugins[i], beforePlugins, afterPlugins);
}
stack = beforePlugins.concat(stack, afterPlugins);
// register
this.transformerStack = stack.concat(secondaryStack);
this.transformers = transformers;
}
getModuleFormatter(type: string) {
@ -258,21 +256,17 @@ export default class File {
throw new TypeError(`Plugin ${JSON.stringify(name)} has an illegal position of ${JSON.stringify(position)}`);
}
// validate Transformer instance
if (!(plugin instanceof Transformer)) {
if (plugin && plugin.constructor.name === "Transformer") {
throw new TypeError(`Plugin ${JSON.stringify(name)} exported a Transformer instance but it resolved to a different version of Babel`);
} else {
throw new TypeError(`Plugin ${JSON.stringify(name)} didn't export default a Transformer instance`);
}
}
// validate transformer key
var key = plugin.key;
if (this.transformers[key]) {
throw new ReferenceError(`The key for plugin ${JSON.stringify(name)} of ${key} collides with an existing plugin`);
}
// validate Transformer instance
if (!plugin.buildPass || plugin.constructor.name !== "Transformer") {
throw new TypeError(`Plugin ${JSON.stringify(name)} didn't export default a Transformer instance`);
}
// build!
var pass = this.transformers[key] = plugin.buildPass(this);
if (pass.canTransform()) {

View File

@ -9,7 +9,7 @@ import each from "lodash/collection/each";
/**
* This is the class responsible for normalising a transformers handlers
* as well as constructing a `TransformerPass` that is repsonsible for
* as well as constructing a `TransformerPass` that is responsible for
* actually running the transformer over the provided `File`.
*/
@ -69,6 +69,11 @@ export default class Transformer {
}
buildPass(file: File): TransformerPass {
// validate Transformer instance
if (!(file instanceof File)) {
throw new TypeError(`Transformer ${this.key} is resolving to a different Babel version to what is doing the actual transformation...`);
}
return new TransformerPass(file, this);
}
}