Skip to content

Commit

Permalink
perf: cache load plugin (#141)
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red authored Apr 4, 2023
1 parent 2c4bf35 commit fbf722f
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions packages/plugin-react/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,8 +306,18 @@ export default function viteReact(opts: Options = {}): PluginOption[] {

viteReact.preambleCode = preambleCode

function loadPlugin(path: string): Promise<any> {
return import(path).then((module) => module.default || module)
const loadedPlugin = new Map<string, any>()
function loadPlugin(path: string): any {
const cached = loadedPlugin.get(path)
if (cached) return cached

const promise = import(path).then((module) => {
const value = module.default || module
loadedPlugin.set(path, value)
return value
})
loadedPlugin.set(path, promise)
return promise
}

function createBabelOptions(rawOptions?: BabelOptions) {
Expand Down

0 comments on commit fbf722f

Please sign in to comment.