fix plugin api
This commit is contained in:
parent
ab55ec4ea2
commit
cb0026edfe
@ -29,6 +29,7 @@ See [CHANGELOG - 6to5](CHANGELOG-6to5.md) for the pre-4.0.0 version changelog.
|
|||||||
* Add `jsxPragma` option.
|
* Add `jsxPragma` option.
|
||||||
* Automatically generate CLI options based on internal API options.
|
* Automatically generate CLI options based on internal API options.
|
||||||
* Add support for `.babelrc` on absolute paths.
|
* Add support for `.babelrc` on absolute paths.
|
||||||
|
* Plugin API!
|
||||||
* **Internal**
|
* **Internal**
|
||||||
* Export `options` in browser API.
|
* Export `options` in browser API.
|
||||||
* Rewritten parser.
|
* Rewritten parser.
|
||||||
|
|||||||
@ -6,7 +6,6 @@ import isFunction from "lodash/lang/isFunction";
|
|||||||
import isAbsolute from "path-is-absolute";
|
import isAbsolute from "path-is-absolute";
|
||||||
import resolveRc from "../../tools/resolve-rc";
|
import resolveRc from "../../tools/resolve-rc";
|
||||||
import sourceMap from "source-map";
|
import sourceMap from "source-map";
|
||||||
import Transformer from "../transformer";
|
|
||||||
import transform from "./../index";
|
import transform from "./../index";
|
||||||
import generate from "../../generation";
|
import generate from "../../generation";
|
||||||
import defaults from "lodash/object/defaults";
|
import defaults from "lodash/object/defaults";
|
||||||
@ -177,7 +176,7 @@ export default class File {
|
|||||||
buildTransformers() {
|
buildTransformers() {
|
||||||
var file = this;
|
var file = this;
|
||||||
|
|
||||||
var transformers = {};
|
var transformers = this.transformers = {};
|
||||||
|
|
||||||
var secondaryStack = [];
|
var secondaryStack = [];
|
||||||
var stack = [];
|
var stack = [];
|
||||||
@ -202,14 +201,13 @@ export default class File {
|
|||||||
// init plugins!
|
// init plugins!
|
||||||
var beforePlugins = [];
|
var beforePlugins = [];
|
||||||
var afterPlugins = [];
|
var afterPlugins = [];
|
||||||
for (var i = 0; i < file.opts.plugins; i++) {
|
for (var i = 0; i < file.opts.plugins.length; i++) {
|
||||||
this.addPlugin(file.opts.plugins[i]);
|
this.addPlugin(file.opts.plugins[i], beforePlugins, afterPlugins);
|
||||||
}
|
}
|
||||||
stack = beforePlugins.concat(stack, afterPlugins);
|
stack = beforePlugins.concat(stack, afterPlugins);
|
||||||
|
|
||||||
// register
|
// register
|
||||||
this.transformerStack = stack.concat(secondaryStack);
|
this.transformerStack = stack.concat(secondaryStack);
|
||||||
this.transformers = transformers;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
getModuleFormatter(type: string) {
|
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)}`);
|
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
|
// validate transformer key
|
||||||
var key = plugin.key;
|
var key = plugin.key;
|
||||||
if (this.transformers[key]) {
|
if (this.transformers[key]) {
|
||||||
throw new ReferenceError(`The key for plugin ${JSON.stringify(name)} of ${key} collides with an existing plugin`);
|
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!
|
// build!
|
||||||
var pass = this.transformers[key] = plugin.buildPass(this);
|
var pass = this.transformers[key] = plugin.buildPass(this);
|
||||||
if (pass.canTransform()) {
|
if (pass.canTransform()) {
|
||||||
|
|||||||
@ -9,7 +9,7 @@ import each from "lodash/collection/each";
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* This is the class responsible for normalising a transformers handlers
|
* 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`.
|
* actually running the transformer over the provided `File`.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@ -69,6 +69,11 @@ export default class Transformer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
buildPass(file: File): TransformerPass {
|
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);
|
return new TransformerPass(file, this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user