From 88da2e80ede26b89b3d591fae53551f55fe081e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Tue, 27 Apr 2021 14:39:18 -0400 Subject: [PATCH] perf: avoid loadFullConfig when creating block hoist plugin (#13223) * perf: avoid loadFullConfig when creating block hoist plugin * address code review comments --- .../src/transformation/block-hoist-plugin.ts | 24 ++++++++----------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/packages/babel-core/src/transformation/block-hoist-plugin.ts b/packages/babel-core/src/transformation/block-hoist-plugin.ts index dbe602301c..445e1866de 100644 --- a/packages/babel-core/src/transformation/block-hoist-plugin.ts +++ b/packages/babel-core/src/transformation/block-hoist-plugin.ts @@ -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;