Skip to content

Commit

Permalink
refactor(css): simplify cached import code (#12730)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy committed Apr 4, 2023
1 parent 889eebe commit 0646754
Showing 1 changed file with 12 additions and 23 deletions.
35 changes: 12 additions & 23 deletions packages/vite/src/node/plugins/css.ts
Expand Up @@ -1054,32 +1054,21 @@ async function compileCSS(
}
}

const lazyImportCache = new Map()
function createCachedImport<T>(
name: string,
imp: () => Promise<T>,
): () => T | Promise<T> {
function createCachedImport<T>(imp: () => Promise<T>): () => T | Promise<T> {
let cached: T | Promise<T>
return () => {
const cached = lazyImportCache.get(name)
if (cached) return cached

const promise = imp().then((module) => {
lazyImportCache.set(name, module)
return module
})
lazyImportCache.set(name, promise)
return promise
if (!cached) {
cached = imp().then((module) => {
cached = module
return module
})
}
return cached
}
}
const importPostcssImport = createCachedImport(
'postcss-import',
() => import('postcss-import'),
)
const importPostcssModules = createCachedImport(
'postcss-modules',
() => import('postcss-modules'),
)
const importPostcss = createCachedImport('postcss', () => import('postcss'))
const importPostcssImport = createCachedImport(() => import('postcss-import'))
const importPostcssModules = createCachedImport(() => import('postcss-modules'))
const importPostcss = createCachedImport(() => import('postcss'))

export interface PreprocessCSSResult {
code: string
Expand Down

0 comments on commit 0646754

Please sign in to comment.