Move Plugin class to be part of config loading, independent of File.
This commit is contained in:
@@ -389,16 +389,25 @@ export default class File extends Store {
|
||||
|
||||
transform(): BabelFileResult {
|
||||
for (const pluginPairs of this.pluginPasses) {
|
||||
const passPairs = [];
|
||||
const passes = [];
|
||||
const visitors = [];
|
||||
|
||||
for (const [ plugin, pluginOpts ] of pluginPairs.concat(INTERNAL_PLUGINS)) {
|
||||
const pass = new PluginPass(this, plugin, pluginOpts);
|
||||
const pass = new PluginPass(this, plugin.key, pluginOpts);
|
||||
|
||||
passPairs.push([ plugin, pass ]);
|
||||
passes.push(pass);
|
||||
visitors.push(plugin.visitor);
|
||||
|
||||
if (plugin.pre) plugin.pre.call(pass, this);
|
||||
}
|
||||
|
||||
for (const [ plugin, pass ] of passPairs) {
|
||||
const fn = plugin.pre;
|
||||
if (fn) fn.call(pass, this);
|
||||
}
|
||||
|
||||
this.call("pre", passes);
|
||||
util.debug(this.opts, "Start transform traverse");
|
||||
|
||||
// merge all plugin visitors into a single visitor
|
||||
@@ -406,7 +415,12 @@ export default class File extends Store {
|
||||
traverse(this.ast, visitor, this.scope);
|
||||
|
||||
util.debug(this.opts, "End transform traverse");
|
||||
this.call("post", passes);
|
||||
|
||||
for (const [ plugin, pass ] of passPairs) {
|
||||
const fn = plugin.post;
|
||||
if (fn) fn.call(pass, this);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return this.generate();
|
||||
@@ -459,14 +473,6 @@ export default class File extends Store {
|
||||
this.addAst(ast);
|
||||
}
|
||||
|
||||
call(key: "pre" | "post", pluginPasses: Array<PluginPass>) {
|
||||
for (const pass of pluginPasses) {
|
||||
const plugin = pass.plugin;
|
||||
const fn = plugin[key];
|
||||
if (fn) fn.call(pass, this);
|
||||
}
|
||||
}
|
||||
|
||||
parseInputSourceMap(code: string): string {
|
||||
const opts = this.opts;
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import * as context from "../../../index";
|
||||
import Plugin from "../../plugin";
|
||||
import Plugin from "./plugin";
|
||||
import * as messages from "babel-messages";
|
||||
import resolvePlugin from "../../../helpers/resolve-plugin";
|
||||
import resolvePreset from "../../../helpers/resolve-preset";
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import OptionManager from "./file/options/option-manager";
|
||||
import OptionManager from "./option-manager";
|
||||
import * as messages from "babel-messages";
|
||||
import traverse from "babel-traverse";
|
||||
import clone from "lodash/clone";
|
||||
@@ -2,7 +2,6 @@
|
||||
import fs from "fs";
|
||||
|
||||
import normalizeAst from "../helpers/normalize-ast";
|
||||
import Plugin from "./plugin";
|
||||
import File from "./file";
|
||||
import OptionManager from "./file/options/option-manager";
|
||||
|
||||
@@ -10,7 +9,7 @@ export function analyse(code: string, opts: Object = {}, visitor?: Object): ?Bab
|
||||
opts.code = false;
|
||||
if (visitor) {
|
||||
opts.plugins = opts.plugins || [];
|
||||
opts.plugins.push(new Plugin({ visitor }));
|
||||
opts.plugins.push({ visitor });
|
||||
}
|
||||
return transform(code, opts).metadata;
|
||||
}
|
||||
|
||||
@@ -1,18 +1,16 @@
|
||||
import type Plugin from "./plugin";
|
||||
import Store from "../store";
|
||||
import File from "./file";
|
||||
|
||||
export default class PluginPass extends Store {
|
||||
constructor(file: File, plugin: Plugin, options: Object = {}) {
|
||||
constructor(file: File, key: string, options: Object = {}) {
|
||||
super();
|
||||
this.plugin = plugin;
|
||||
this.key = plugin.key;
|
||||
|
||||
this.key = key;
|
||||
this.file = file;
|
||||
this.opts = options;
|
||||
}
|
||||
|
||||
key: string;
|
||||
plugin: Plugin;
|
||||
file: File;
|
||||
opts: Object;
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@ import * as babel from "../lib/index";
|
||||
import buildExternalHelpers from "../lib/tools/build-external-helpers";
|
||||
import sourceMap from "source-map";
|
||||
import assert from "assert";
|
||||
import Plugin from "../lib/transformation/plugin";
|
||||
import Plugin from "../lib/transformation/file/options/plugin";
|
||||
import generator from "babel-generator";
|
||||
|
||||
function assertIgnored(result) {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { transform } from "../lib/index";
|
||||
import Plugin from "../lib/transformation/plugin";
|
||||
import Plugin from "../lib/transformation/file/options/plugin";
|
||||
import chai from "chai";
|
||||
|
||||
describe("traversal path", function () {
|
||||
|
||||
Reference in New Issue
Block a user