Skip to content

Commit fbf722f

Browse files
authored
perf: cache load plugin (#141)
1 parent 2c4bf35 commit fbf722f

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

packages/plugin-react/src/index.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -306,8 +306,18 @@ export default function viteReact(opts: Options = {}): PluginOption[] {
306306

307307
viteReact.preambleCode = preambleCode
308308

309-
function loadPlugin(path: string): Promise<any> {
310-
return import(path).then((module) => module.default || module)
309+
const loadedPlugin = new Map<string, any>()
310+
function loadPlugin(path: string): any {
311+
const cached = loadedPlugin.get(path)
312+
if (cached) return cached
313+
314+
const promise = import(path).then((module) => {
315+
const value = module.default || module
316+
loadedPlugin.set(path, value)
317+
return value
318+
})
319+
loadedPlugin.set(path, promise)
320+
return promise
311321
}
312322

313323
function createBabelOptions(rawOptions?: BabelOptions) {

0 commit comments

Comments
 (0)