1
0
mirror of https://github.com/svg/svgo.git synced 2025-07-31 07:44:22 +03:00

Load .cjs with require only (#1605)

Ref https://github.com/svg/svgo/issues/1596

At the moment dynamic import may randomly fail with segfault.
To workaround this for some users .cjs extension is loaded
exclusively with require.
This commit is contained in:
Bogdan Chadkin
2021-10-30 01:31:36 +03:00
committed by GitHub
parent 4b4391fbe3
commit c7995035ef

View File

@ -15,21 +15,28 @@ exports.createContentItem = createContentItem;
const importConfig = async (configFile) => { const importConfig = async (configFile) => {
let config; let config;
try { // at the moment dynamic import may randomly fail with segfault
// dynamic import expects file url instead of path and may fail // to workaround this for some users .cjs extension is loaded
// when windows path is provided // exclusively with require
const { default: imported } = await import(pathToFileURL(configFile)); if (configFile.endsWith('.cjs')) {
config = imported; config = require(configFile);
} catch (importError) { } else {
// TODO remove require in v3
try { try {
config = require(configFile); // dynamic import expects file url instead of path and may fail
} catch (requireError) { // when windows path is provided
// throw original error if es module is detected const { default: imported } = await import(pathToFileURL(configFile));
if (requireError.code === 'ERR_REQUIRE_ESM') { config = imported;
throw importError; } catch (importError) {
} else { // TODO remove require in v3
throw requireError; try {
config = require(configFile);
} catch (requireError) {
// throw original error if es module is detected
if (requireError.code === 'ERR_REQUIRE_ESM') {
throw importError;
} else {
throw requireError;
}
} }
} }
} }