Skip to content

Commit

Permalink
Load .cjs with require only (#1605)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
TrySound authored Oct 29, 2021
1 parent 4b4391f commit c799503
Showing 1 changed file with 21 additions and 14 deletions.
35 changes: 21 additions & 14 deletions lib/svgo-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
}
Expand Down

0 comments on commit c799503

Please sign in to comment.