perf: avoid loadFullConfig when creating block hoist plugin (#13223)

* perf: avoid loadFullConfig when creating block hoist plugin

* address code review comments
This commit is contained in:
Huáng Jùnliàng 2021-04-27 14:39:18 -04:00 committed by GitHub
parent a0e20ac5df
commit 88da2e80ed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,22 +1,18 @@
import loadConfig from "../config";
import type { Plugin } from "../config";
import traverse from "@babel/traverse";
import Plugin from "../config/plugin";
let LOADED_PLUGIN: 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,
// which loads this file, and this 'loadConfig' loading plugins.
const config = loadConfig.sync({
babelrc: false,
configFile: false,
browserslistConfigFile: false,
plugins: [blockHoistPlugin],
});
LOADED_PLUGIN = config ? config.passes[0][0] : undefined;
if (!LOADED_PLUGIN) throw new Error("Assertion failure");
// cache the loaded blockHoist plugin plugin
LOADED_PLUGIN = new Plugin(
{
...blockHoistPlugin,
visitor: traverse.explode(blockHoistPlugin.visitor),
},
{},
);
}
return LOADED_PLUGIN;