@@ -96,76 +96,30 @@ export interface StylesheetLanguage {
96
96
const postcssProcessors = new Map < string , WeakRef < PostcssProcessor > > ( ) ;
97
97
98
98
export class StylesheetPluginFactory {
99
- private postcssProcessor ?: PostcssProcessor ;
100
-
101
99
constructor (
102
100
private readonly options : StylesheetPluginOptions ,
103
101
private readonly cache ?: LoadResultCache ,
104
102
) { }
105
103
106
104
create ( language : Readonly < StylesheetLanguage > ) : Plugin {
105
+ const { cache, options, setupPostcss } = this ;
106
+
107
107
// Return a noop plugin if no load actions are required
108
- if (
109
- ! language . process &&
110
- ! this . options . postcssConfiguration &&
111
- ! this . options . tailwindConfiguration
112
- ) {
108
+ if ( ! language . process && ! options . postcssConfiguration && ! options . tailwindConfiguration ) {
113
109
return {
114
110
name : 'angular-' + language . name ,
115
111
setup ( ) { } ,
116
112
} ;
117
113
}
118
114
119
- const { cache, options } = this ;
120
- const setupPostcss = async ( ) => {
121
- // Return already created processor if present
122
- if ( this . postcssProcessor ) {
123
- return this . postcssProcessor ;
124
- }
125
-
126
- if ( options . postcssConfiguration ) {
127
- const postCssInstanceKey = JSON . stringify ( options . postcssConfiguration ) ;
128
-
129
- this . postcssProcessor = postcssProcessors . get ( postCssInstanceKey ) ?. deref ( ) ;
130
-
131
- if ( ! this . postcssProcessor ) {
132
- postcss ??= ( await import ( 'postcss' ) ) . default ;
133
- this . postcssProcessor = postcss ( ) ;
134
-
135
- for ( const [ pluginName , pluginOptions ] of options . postcssConfiguration . plugins ) {
136
- const { default : plugin } = await import ( pluginName ) ;
137
- if ( typeof plugin !== 'function' || plugin . postcss !== true ) {
138
- throw new Error ( `Attempted to load invalid Postcss plugin: "${ pluginName } "` ) ;
139
- }
140
- this . postcssProcessor . use ( plugin ( pluginOptions ) ) ;
141
- }
142
-
143
- postcssProcessors . set ( postCssInstanceKey , new WeakRef ( this . postcssProcessor ) ) ;
144
- }
145
- } else if ( options . tailwindConfiguration ) {
146
- const { package : tailwindPackage , file : config } = options . tailwindConfiguration ;
147
- const postCssInstanceKey = tailwindPackage + ':' + config ;
148
- this . postcssProcessor = postcssProcessors . get ( postCssInstanceKey ) ?. deref ( ) ;
149
-
150
- if ( ! this . postcssProcessor ) {
151
- postcss ??= ( await import ( 'postcss' ) ) . default ;
152
- const tailwind = await import ( tailwindPackage ) ;
153
- this . postcssProcessor = postcss ( ) . use ( tailwind . default ( { config } ) ) ;
154
- postcssProcessors . set ( postCssInstanceKey , new WeakRef ( this . postcssProcessor ) ) ;
155
- }
156
- }
157
-
158
- return this . postcssProcessor ;
159
- } ;
160
-
161
115
return {
162
116
name : 'angular-' + language . name ,
163
117
async setup ( build ) {
164
118
// Setup postcss if needed
165
119
let postcssProcessor : PostcssProcessor | undefined ;
166
120
build . onStart ( async ( ) => {
167
121
try {
168
- postcssProcessor = await setupPostcss ( ) ;
122
+ postcssProcessor = await setupPostcss ;
169
123
} catch {
170
124
return {
171
125
errors : [
@@ -229,6 +183,49 @@ export class StylesheetPluginFactory {
229
183
} ,
230
184
} ;
231
185
}
186
+
187
+ private _setupPostcss : Promise < PostcssProcessor | undefined > | undefined ;
188
+ private get setupPostcss ( ) : Promise < PostcssProcessor | undefined > {
189
+ return ( this . _setupPostcss ??= this . initPostcss ( ) ) ;
190
+ }
191
+
192
+ private async initPostcss ( ) : Promise < PostcssProcessor | undefined > {
193
+ const { options } = this ;
194
+ if ( options . postcssConfiguration ) {
195
+ const postCssInstanceKey = JSON . stringify ( options . postcssConfiguration ) ;
196
+ let postcssProcessor = postcssProcessors . get ( postCssInstanceKey ) ?. deref ( ) ;
197
+
198
+ if ( ! postcssProcessor ) {
199
+ postcss ??= ( await import ( 'postcss' ) ) . default ;
200
+ postcssProcessor = postcss ( ) ;
201
+ for ( const [ pluginName , pluginOptions ] of options . postcssConfiguration . plugins ) {
202
+ const { default : plugin } = await import ( pluginName ) ;
203
+ if ( typeof plugin !== 'function' || plugin . postcss !== true ) {
204
+ throw new Error ( `Attempted to load invalid Postcss plugin: "${ pluginName } "` ) ;
205
+ }
206
+
207
+ postcssProcessor . use ( plugin ( pluginOptions ) ) ;
208
+ }
209
+
210
+ postcssProcessors . set ( postCssInstanceKey , new WeakRef ( postcssProcessor ) ) ;
211
+ }
212
+
213
+ return postcssProcessor ;
214
+ } else if ( options . tailwindConfiguration ) {
215
+ const { package : tailwindPackage , file : config } = options . tailwindConfiguration ;
216
+ const postCssInstanceKey = tailwindPackage + ':' + config ;
217
+ let postcssProcessor = postcssProcessors . get ( postCssInstanceKey ) ?. deref ( ) ;
218
+
219
+ if ( ! postcssProcessor ) {
220
+ postcss ??= ( await import ( 'postcss' ) ) . default ;
221
+ const tailwind = await import ( tailwindPackage ) ;
222
+ postcssProcessor = postcss ( ) . use ( tailwind . default ( { config } ) ) ;
223
+ postcssProcessors . set ( postCssInstanceKey , new WeakRef ( postcssProcessor ) ) ;
224
+ }
225
+
226
+ return postcssProcessor ;
227
+ }
228
+ }
232
229
}
233
230
234
231
async function processStylesheet (
0 commit comments