Skip to content

Commit 0646754

Browse files
authoredApr 4, 2023
refactor(css): simplify cached import code (#12730)
1 parent 889eebe commit 0646754

File tree

1 file changed

+12
-23
lines changed
  • packages/vite/src/node/plugins

1 file changed

+12
-23
lines changed
 

‎packages/vite/src/node/plugins/css.ts

+12-23
Original file line numberDiff line numberDiff line change
@@ -1054,32 +1054,21 @@ async function compileCSS(
10541054
}
10551055
}
10561056

1057-
const lazyImportCache = new Map()
1058-
function createCachedImport<T>(
1059-
name: string,
1060-
imp: () => Promise<T>,
1061-
): () => T | Promise<T> {
1057+
function createCachedImport<T>(imp: () => Promise<T>): () => T | Promise<T> {
1058+
let cached: T | Promise<T>
10621059
return () => {
1063-
const cached = lazyImportCache.get(name)
1064-
if (cached) return cached
1065-
1066-
const promise = imp().then((module) => {
1067-
lazyImportCache.set(name, module)
1068-
return module
1069-
})
1070-
lazyImportCache.set(name, promise)
1071-
return promise
1060+
if (!cached) {
1061+
cached = imp().then((module) => {
1062+
cached = module
1063+
return module
1064+
})
1065+
}
1066+
return cached
10721067
}
10731068
}
1074-
const importPostcssImport = createCachedImport(
1075-
'postcss-import',
1076-
() => import('postcss-import'),
1077-
)
1078-
const importPostcssModules = createCachedImport(
1079-
'postcss-modules',
1080-
() => import('postcss-modules'),
1081-
)
1082-
const importPostcss = createCachedImport('postcss', () => import('postcss'))
1069+
const importPostcssImport = createCachedImport(() => import('postcss-import'))
1070+
const importPostcssModules = createCachedImport(() => import('postcss-modules'))
1071+
const importPostcss = createCachedImport(() => import('postcss'))
10831072

10841073
export interface PreprocessCSSResult {
10851074
code: string

0 commit comments

Comments
 (0)
Please sign in to comment.