Track options on the plugin instance to avoid array pair usage.
This commit is contained in:
parent
f9bac2a358
commit
35312dc3d2
@ -9,7 +9,7 @@ export type ResolvedConfig = {
|
||||
};
|
||||
|
||||
export type { Plugin };
|
||||
export type PluginPassList = Array<[Plugin, ?{}]>;
|
||||
export type PluginPassList = Array<Plugin>;
|
||||
export type PluginPasses = Array<PluginPassList>;
|
||||
|
||||
/**
|
||||
|
||||
@ -79,7 +79,7 @@ const ALLOWED_PLUGIN_KEYS = new Set([
|
||||
|
||||
export default function manageOptions(opts: {}): {
|
||||
options: Object,
|
||||
passes: Array<Array<[Plugin, ?{}]>>,
|
||||
passes: Array<Array<Plugin>>,
|
||||
} | null {
|
||||
return new OptionManager().init(opts);
|
||||
}
|
||||
@ -91,7 +91,7 @@ class OptionManager {
|
||||
}
|
||||
|
||||
options: Object;
|
||||
passes: Array<Array<[Plugin, ?{}]>>;
|
||||
passes: Array<Array<Plugin>>;
|
||||
|
||||
/**
|
||||
* This is called when we want to merge the input `opts` into the
|
||||
@ -102,7 +102,7 @@ class OptionManager {
|
||||
* - `dirname` is used to resolve plugins relative to it.
|
||||
*/
|
||||
|
||||
mergeOptions(config: MergeOptions, pass?: Array<[Plugin, ?{}]>) {
|
||||
mergeOptions(config: MergeOptions, pass?: Array<Plugin>) {
|
||||
const result = loadConfig(config);
|
||||
|
||||
const plugins = result.plugins.map(descriptor =>
|
||||
@ -315,12 +315,16 @@ const loadDescriptor = makeWeakCache((descriptor, cache) => {
|
||||
*/
|
||||
function loadPluginDescriptor(descriptor: BasicDescriptor) {
|
||||
if (descriptor.value instanceof Plugin) {
|
||||
return [descriptor.value, descriptor.options];
|
||||
if (descriptor.options) {
|
||||
throw new Error(
|
||||
"Passed options to an existing Plugin instance will not work.",
|
||||
);
|
||||
}
|
||||
|
||||
return descriptor.value;
|
||||
}
|
||||
|
||||
const result = instantiatePlugin(loadDescriptor(descriptor));
|
||||
|
||||
return [result, descriptor.options];
|
||||
return instantiatePlugin(loadDescriptor(descriptor));
|
||||
}
|
||||
|
||||
const instantiatePlugin = makeWeakCache(
|
||||
@ -360,8 +364,8 @@ const instantiatePlugin = makeWeakCache(
|
||||
};
|
||||
|
||||
// If the inherited plugin changes, reinstantiate this plugin.
|
||||
inherits = cache.invalidate(
|
||||
() => loadPluginDescriptor(inheritsDescriptor)[0],
|
||||
inherits = cache.invalidate(() =>
|
||||
loadPluginDescriptor(inheritsDescriptor),
|
||||
);
|
||||
|
||||
plugin.pre = chain(inherits.pre, plugin.pre);
|
||||
@ -376,7 +380,7 @@ const instantiatePlugin = makeWeakCache(
|
||||
]);
|
||||
}
|
||||
|
||||
return new Plugin(plugin, descriptor.alias);
|
||||
return new Plugin(plugin, descriptor.options, descriptor.alias);
|
||||
},
|
||||
);
|
||||
|
||||
|
||||
@ -7,7 +7,9 @@ export default class Plugin {
|
||||
pre: ?Function;
|
||||
visitor: ?{};
|
||||
|
||||
constructor(plugin: {}, key?: string) {
|
||||
options: {} | void;
|
||||
|
||||
constructor(plugin: {}, options: ?{}, key?: string) {
|
||||
if (plugin.name != null && typeof plugin.name !== "string") {
|
||||
throw new Error("Plugin .name must be a string, null, or undefined");
|
||||
}
|
||||
@ -35,5 +37,6 @@ export default class Plugin {
|
||||
this.post = plugin.post;
|
||||
this.pre = plugin.pre;
|
||||
this.visitor = plugin.visitor;
|
||||
this.options = options || undefined;
|
||||
}
|
||||
}
|
||||
|
||||
@ -6,7 +6,7 @@ import loadConfig, { type Plugin } from "../config";
|
||||
|
||||
let LOADED_PLUGIN: Plugin | void;
|
||||
|
||||
export default function loadBlockHoistPlugin(): [Plugin, void] {
|
||||
export default function loadBlockHoistPlugin(): Plugin {
|
||||
if (!LOADED_PLUGIN) {
|
||||
// Lazy-init the internal plugin to remove the init-time circular
|
||||
// dependency between plugins being passed babel-core's export object,
|
||||
@ -15,11 +15,11 @@ export default function loadBlockHoistPlugin(): [Plugin, void] {
|
||||
babelrc: false,
|
||||
plugins: [blockHoistPlugin],
|
||||
});
|
||||
LOADED_PLUGIN = config ? config.passes[0][0][0] : undefined;
|
||||
LOADED_PLUGIN = config ? config.passes[0][0] : undefined;
|
||||
if (!LOADED_PLUGIN) throw new Error("Assertion failure");
|
||||
}
|
||||
|
||||
return [LOADED_PLUGIN, undefined];
|
||||
return LOADED_PLUGIN;
|
||||
}
|
||||
|
||||
const blockHoistPlugin = {
|
||||
|
||||
@ -49,10 +49,8 @@ function transformFile(file: File, pluginPasses: PluginPasses): void {
|
||||
const passes = [];
|
||||
const visitors = [];
|
||||
|
||||
for (const [plugin, pluginOpts] of pluginPairs.concat([
|
||||
loadBlockHoistPlugin(),
|
||||
])) {
|
||||
const pass = new PluginPass(file, plugin.key, pluginOpts);
|
||||
for (const plugin of pluginPairs.concat([loadBlockHoistPlugin()])) {
|
||||
const pass = new PluginPass(file, plugin.key, plugin.options);
|
||||
|
||||
passPairs.push([plugin, pass]);
|
||||
passes.push(pass);
|
||||
|
||||
@ -15,7 +15,7 @@ export default function normalizeOptions(config: ResolvedConfig): {} {
|
||||
});
|
||||
|
||||
for (const pluginPairs of config.passes) {
|
||||
for (const [plugin] of pluginPairs) {
|
||||
for (const plugin of pluginPairs) {
|
||||
if (plugin.manipulateOptions) {
|
||||
plugin.manipulateOptions(options, options.parserOpts);
|
||||
}
|
||||
|
||||
@ -154,9 +154,8 @@ describe("api", function() {
|
||||
plugins: [__dirname + "/../../babel-plugin-syntax-jsx"],
|
||||
}).then(function(result) {
|
||||
assert.ok(
|
||||
result.options.plugins[0][0].manipulateOptions
|
||||
.toString()
|
||||
.indexOf("jsx") >= 0,
|
||||
result.options.plugins[0].manipulateOptions.toString().indexOf("jsx") >=
|
||||
0,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user