Skip to content

Commit fedb080

Browse files
authoredApr 3, 2023
perf(css): cache lazy import (#12721)
1 parent c90b46e commit fedb080

File tree

1 file changed

+52
-26
lines changed
  • packages/vite/src/node/plugins

1 file changed

+52
-26
lines changed
 

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

+52-26
Original file line numberDiff line numberDiff line change
@@ -891,7 +891,7 @@ async function compileCSS(
891891

892892
if (needInlineImport) {
893893
postcssPlugins.unshift(
894-
(await import('postcss-import')).default({
894+
(await importPostcssImport()).default({
895895
async resolve(id, basedir) {
896896
const publicFile = checkPublicFile(id, config)
897897
if (publicFile) {
@@ -926,7 +926,7 @@ async function compileCSS(
926926

927927
if (isModule) {
928928
postcssPlugins.unshift(
929-
(await import('postcss-modules')).default({
929+
(await importPostcssModules()).default({
930930
...modulesOptions,
931931
localsConvention: modulesOptions?.localsConvention,
932932
getJSON(
@@ -963,31 +963,30 @@ async function compileCSS(
963963
let postcssResult: PostCSS.Result
964964
try {
965965
const source = removeDirectQuery(id)
966+
const postcss = await importPostcss()
966967
// postcss is an unbundled dep and should be lazy imported
967-
postcssResult = await (await import('postcss'))
968-
.default(postcssPlugins)
969-
.process(code, {
970-
...postcssOptions,
971-
parser:
972-
lang === 'sss'
973-
? loadPreprocessor(PostCssDialectLang.sss, config.root)
974-
: postcssOptions.parser,
975-
to: source,
976-
from: source,
977-
...(devSourcemap
978-
? {
979-
map: {
980-
inline: false,
981-
annotation: false,
982-
// postcss may return virtual files
983-
// we cannot obtain content of them, so this needs to be enabled
984-
sourcesContent: true,
985-
// when "prev: preprocessorMap", the result map may include duplicate filename in `postcssResult.map.sources`
986-
// prev: preprocessorMap,
987-
},
988-
}
989-
: {}),
990-
})
968+
postcssResult = await postcss.default(postcssPlugins).process(code, {
969+
...postcssOptions,
970+
parser:
971+
lang === 'sss'
972+
? loadPreprocessor(PostCssDialectLang.sss, config.root)
973+
: postcssOptions.parser,
974+
to: source,
975+
from: source,
976+
...(devSourcemap
977+
? {
978+
map: {
979+
inline: false,
980+
annotation: false,
981+
// postcss may return virtual files
982+
// we cannot obtain content of them, so this needs to be enabled
983+
sourcesContent: true,
984+
// when "prev: preprocessorMap", the result map may include duplicate filename in `postcssResult.map.sources`
985+
// prev: preprocessorMap,
986+
},
987+
}
988+
: {}),
989+
})
991990

992991
// record CSS dependencies from @imports
993992
for (const message of postcssResult.messages) {
@@ -1055,6 +1054,33 @@ async function compileCSS(
10551054
}
10561055
}
10571056

1057+
const lazyImportCache = new Map()
1058+
function createCachedImport<T>(
1059+
name: string,
1060+
imp: () => Promise<T>,
1061+
): () => T | Promise<T> {
1062+
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
1072+
}
1073+
}
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'))
1083+
10581084
export interface PreprocessCSSResult {
10591085
code: string
10601086
map?: SourceMapInput

0 commit comments

Comments
 (0)
Please sign in to comment.