Skip to content

Commit f7a68f7

Browse files
refactor: code
1 parent 1e9505e commit f7a68f7

File tree

1 file changed

+42
-57
lines changed

1 file changed

+42
-57
lines changed

src/utils.js

Lines changed: 42 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -167,23 +167,32 @@ function pluginFactory() {
167167
};
168168
}
169169

170-
async function getImportEsm(module, requireError) {
171-
let importESM;
170+
async function load(module) {
171+
let exports;
172172

173173
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);
179176

180-
if (requireError.code === "ERR_REQUIRE_ESM" && importESM) {
181-
const exports = await importESM(module);
177+
return exports;
178+
} catch (requireError) {
179+
let importESM;
182180

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);
185190

186-
throw requireError;
191+
return exports.default;
192+
}
193+
194+
throw requireError;
195+
}
187196
}
188197

189198
async function getPostcssOptions(
@@ -272,61 +281,37 @@ async function getPostcssOptions(
272281

273282
if (typeof processOptions.parser === "string") {
274283
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+
);
290291
}
291292
}
292293

293294
if (typeof processOptions.stringifier === "string") {
294295
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+
);
310303
}
311304
}
312305

313306
if (typeof processOptions.syntax === "string") {
314307
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+
);
330315
}
331316
}
332317

0 commit comments

Comments
 (0)