From c7995035efc2a5c11fabd1f53f990e954c64cb09 Mon Sep 17 00:00:00 2001 From: Bogdan Chadkin Date: Sat, 30 Oct 2021 01:31:36 +0300 Subject: [PATCH] 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. --- lib/svgo-node.js | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/lib/svgo-node.js b/lib/svgo-node.js index 9e401811e..04087d520 100644 --- a/lib/svgo-node.js +++ b/lib/svgo-node.js @@ -15,21 +15,28 @@ exports.createContentItem = createContentItem; const importConfig = async (configFile) => { let config; - try { - // dynamic import expects file url instead of path and may fail - // when windows path is provided - const { default: imported } = await import(pathToFileURL(configFile)); - config = imported; - } catch (importError) { - // TODO remove require in v3 + // at the moment dynamic import may randomly fail with segfault + // to workaround this for some users .cjs extension is loaded + // exclusively with require + if (configFile.endsWith('.cjs')) { + config = require(configFile); + } else { 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; + // dynamic import expects file url instead of path and may fail + // when windows path is provided + const { default: imported } = await import(pathToFileURL(configFile)); + config = imported; + } catch (importError) { + // TODO remove require in v3 + 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; + } } } }