Skip to content

Commit c799503

Browse files
authored
Load .cjs with require only (#1605)
Ref #1596 At the moment dynamic import may randomly fail with segfault. To workaround this for some users .cjs extension is loaded exclusively with require.
1 parent 4b4391f commit c799503

File tree

1 file changed

+21
-14
lines changed

1 file changed

+21
-14
lines changed

lib/svgo-node.js

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,28 @@ exports.createContentItem = createContentItem;
1515

1616
const importConfig = async (configFile) => {
1717
let config;
18-
try {
19-
// dynamic import expects file url instead of path and may fail
20-
// when windows path is provided
21-
const { default: imported } = await import(pathToFileURL(configFile));
22-
config = imported;
23-
} catch (importError) {
24-
// TODO remove require in v3
18+
// at the moment dynamic import may randomly fail with segfault
19+
// to workaround this for some users .cjs extension is loaded
20+
// exclusively with require
21+
if (configFile.endsWith('.cjs')) {
22+
config = require(configFile);
23+
} else {
2524
try {
26-
config = require(configFile);
27-
} catch (requireError) {
28-
// throw original error if es module is detected
29-
if (requireError.code === 'ERR_REQUIRE_ESM') {
30-
throw importError;
31-
} else {
32-
throw requireError;
25+
// dynamic import expects file url instead of path and may fail
26+
// when windows path is provided
27+
const { default: imported } = await import(pathToFileURL(configFile));
28+
config = imported;
29+
} catch (importError) {
30+
// TODO remove require in v3
31+
try {
32+
config = require(configFile);
33+
} catch (requireError) {
34+
// throw original error if es module is detected
35+
if (requireError.code === 'ERR_REQUIRE_ESM') {
36+
throw importError;
37+
} else {
38+
throw requireError;
39+
}
3340
}
3441
}
3542
}

0 commit comments

Comments
 (0)