@@ -167,23 +167,32 @@ function pluginFactory() {
167
167
} ;
168
168
}
169
169
170
- async function getImportEsm ( module , requireError ) {
171
- let importESM ;
170
+ async function load ( module ) {
171
+ let exports ;
172
172
173
173
try {
174
- // eslint-disable-next-line no-new-func
175
- importESM = new Function ( "id" , "return import(id);" ) ;
176
- } catch ( e ) {
177
- importESM = null ;
178
- }
174
+ // eslint-disable-next-line import/no-dynamic-require, global-require
175
+ exports = require ( module ) ;
179
176
180
- if ( requireError . code === "ERR_REQUIRE_ESM" && importESM ) {
181
- const exports = await importESM ( module ) ;
177
+ return exports ;
178
+ } catch ( requireError ) {
179
+ let importESM ;
182
180
183
- return exports . default ;
184
- }
181
+ try {
182
+ // eslint-disable-next-line no-new-func
183
+ importESM = new Function ( "id" , "return import(id);" ) ;
184
+ } catch ( e ) {
185
+ importESM = null ;
186
+ }
187
+
188
+ if ( requireError . code === "ERR_REQUIRE_ESM" && importESM ) {
189
+ exports = await importESM ( module ) ;
185
190
186
- throw requireError ;
191
+ return exports . default ;
192
+ }
193
+
194
+ throw requireError ;
195
+ }
187
196
}
188
197
189
198
async function getPostcssOptions (
@@ -272,61 +281,37 @@ async function getPostcssOptions(
272
281
273
282
if ( typeof processOptions . parser === "string" ) {
274
283
try {
275
- // eslint-disable-next-line import/no-dynamic-require, global-require
276
- processOptions . parser = require ( processOptions . parser ) ;
277
- } catch ( requireError ) {
278
- try {
279
- processOptions . parser = await getImportEsm (
280
- processOptions . parser ,
281
- requireError
282
- ) ;
283
- } catch ( error ) {
284
- loaderContext . emitError (
285
- new Error (
286
- `Loading PostCSS "${ processOptions . parser } " parser failed: ${ error . message } \n\n(@${ file } )`
287
- )
288
- ) ;
289
- }
284
+ processOptions . parser = await load ( processOptions . parser ) ;
285
+ } catch ( error ) {
286
+ loaderContext . emitError (
287
+ new Error (
288
+ `Loading PostCSS "${ processOptions . parser } " parser failed: ${ error . message } \n\n(@${ file } )`
289
+ )
290
+ ) ;
290
291
}
291
292
}
292
293
293
294
if ( typeof processOptions . stringifier === "string" ) {
294
295
try {
295
- // eslint-disable-next-line import/no-dynamic-require, global-require
296
- processOptions . stringifier = require ( processOptions . stringifier ) ;
297
- } catch ( requireError ) {
298
- try {
299
- processOptions . stringifier = await getImportEsm (
300
- processOptions . stringifier ,
301
- requireError
302
- ) ;
303
- } catch ( error ) {
304
- loaderContext . emitError (
305
- new Error (
306
- `Loading PostCSS "${ processOptions . stringifier } " stringifier failed: ${ error . message } \n\n(@${ file } )`
307
- )
308
- ) ;
309
- }
296
+ processOptions . stringifier = await load ( processOptions . stringifier ) ;
297
+ } catch ( error ) {
298
+ loaderContext . emitError (
299
+ new Error (
300
+ `Loading PostCSS "${ processOptions . stringifier } " stringifier failed: ${ error . message } \n\n(@${ file } )`
301
+ )
302
+ ) ;
310
303
}
311
304
}
312
305
313
306
if ( typeof processOptions . syntax === "string" ) {
314
307
try {
315
- // eslint-disable-next-line import/no-dynamic-require, global-require
316
- processOptions . syntax = require ( processOptions . syntax ) ;
317
- } catch ( requireError ) {
318
- try {
319
- processOptions . syntax = await getImportEsm (
320
- processOptions . syntax ,
321
- requireError
322
- ) ;
323
- } catch ( error ) {
324
- loaderContext . emitError (
325
- new Error (
326
- `Loading PostCSS "${ processOptions . syntax } " syntax failed: ${ error . message } \n\n(@${ file } )`
327
- )
328
- ) ;
329
- }
308
+ processOptions . syntax = await load ( processOptions . syntax ) ;
309
+ } catch ( error ) {
310
+ loaderContext . emitError (
311
+ new Error (
312
+ `Loading PostCSS "${ processOptions . syntax } " syntax failed: ${ error . message } \n\n(@${ file } )`
313
+ )
314
+ ) ;
330
315
}
331
316
}
332
317
0 commit comments