@@ -3,11 +3,9 @@ import MagicString, { Bundle as MagicStringBundle } from 'magic-string';
3
3
import type { ModuleInfo , PluginContext } from 'rollup' ;
4
4
5
5
/** Generate a CSS bundle from Rollup context */
6
- export function generateCssBundle ( {
7
- getModuleIds,
8
- getModuleInfo,
9
- warn,
10
- } : Pick < PluginContext , 'getModuleIds' | 'getModuleInfo' | 'warn' > ) : {
6
+ export function generateCssBundle (
7
+ plugin : Pick < PluginContext , 'getModuleIds' | 'getModuleInfo' | 'warn' > ,
8
+ ) : {
11
9
bundle : MagicStringBundle ;
12
10
extractedCssIds : Set < string > ;
13
11
} {
@@ -16,17 +14,18 @@ export function generateCssBundle({
16
14
17
15
// 1. identify CSS files to bundle
18
16
const cssFiles : Record < string , ImportChain > = { } ;
19
- for ( const id of getModuleIds ( ) ) {
17
+ for ( const id of plugin . getModuleIds ( ) ) {
20
18
if ( cssFileFilter . test ( id ) ) {
21
- cssFiles [ id ] = buildImportChain ( id , { getModuleInfo , warn } ) ;
19
+ cssFiles [ id ] = buildImportChain ( id , plugin ) ;
22
20
}
23
21
}
24
22
25
23
// 2. build bundle from import order
26
24
for ( const id of sortModules ( cssFiles ) ) {
27
- const { importedIdResolutions } = getModuleInfo ( id ) ?? { } ;
28
- for ( const resolution of importedIdResolutions ?? [ ] ) {
29
- if ( resolution . meta . css && ! extractedCssIds . has ( resolution . id ) ) {
25
+ const { importedIds } = plugin . getModuleInfo ( id ) ?? { } ;
26
+ for ( const importedId of importedIds ?? [ ] ) {
27
+ const resolution = plugin . getModuleInfo ( importedId ) ;
28
+ if ( resolution ?. meta . css && ! extractedCssIds . has ( resolution . id ) ) {
30
29
extractedCssIds . add ( resolution . id ) ;
31
30
cssBundle . addSource ( {
32
31
filename : resolution . id ,
@@ -45,9 +44,9 @@ export type ImportChain = [id: string, order: number][];
45
44
/** Trace a file back through its importers, building an ordered list */
46
45
export function buildImportChain (
47
46
id : string ,
48
- { getModuleInfo , warn } : Pick < PluginContext , 'getModuleInfo' | 'warn' > ,
47
+ plugin : Pick < PluginContext , 'getModuleInfo' | 'warn' > ,
49
48
) : ImportChain {
50
- let mod : ModuleInfo | null = getModuleInfo ( id ) ! ;
49
+ let mod : ModuleInfo | null = plugin . getModuleInfo ( id ) ! ;
51
50
if ( ! mod ) {
52
51
return [ ] ;
53
52
}
@@ -61,14 +60,14 @@ export function buildImportChain(
61
60
break ;
62
61
}
63
62
if ( chain . some ( ( [ id ] ) => id === lastImporterId ) ) {
64
- warn (
63
+ plugin . warn (
65
64
`Circular import detected. Can’t determine ideal import order of module.\n${ chain
66
65
. reverse ( )
67
66
. join ( '\n → ' ) } `,
68
67
) ;
69
68
break ;
70
69
}
71
- mod = getModuleInfo ( lastImporterId ) ;
70
+ mod = plugin . getModuleInfo ( lastImporterId ) ;
72
71
if ( ! mod ) {
73
72
break ;
74
73
}
0 commit comments