@@ -891,7 +891,7 @@ async function compileCSS(
891
891
892
892
if ( needInlineImport ) {
893
893
postcssPlugins . unshift (
894
- ( await import ( 'postcss-import' ) ) . default ( {
894
+ ( await importPostcssImport ( ) ) . default ( {
895
895
async resolve ( id , basedir ) {
896
896
const publicFile = checkPublicFile ( id , config )
897
897
if ( publicFile ) {
@@ -926,7 +926,7 @@ async function compileCSS(
926
926
927
927
if ( isModule ) {
928
928
postcssPlugins . unshift (
929
- ( await import ( 'postcss-modules' ) ) . default ( {
929
+ ( await importPostcssModules ( ) ) . default ( {
930
930
...modulesOptions ,
931
931
localsConvention : modulesOptions ?. localsConvention ,
932
932
getJSON (
@@ -963,31 +963,30 @@ async function compileCSS(
963
963
let postcssResult : PostCSS . Result
964
964
try {
965
965
const source = removeDirectQuery ( id )
966
+ const postcss = await importPostcss ( )
966
967
// 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
+ } )
991
990
992
991
// record CSS dependencies from @imports
993
992
for ( const message of postcssResult . messages ) {
@@ -1055,6 +1054,33 @@ async function compileCSS(
1055
1054
}
1056
1055
}
1057
1056
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
+
1058
1084
export interface PreprocessCSSResult {
1059
1085
code : string
1060
1086
map ?: SourceMapInput
0 commit comments