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:
@ -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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user