1
0
mirror of https://github.com/svg/svgo.git synced 2025-07-29 20:21:14 +03:00

Drop node 10 require fallback when load config

This commit is contained in:
Bogdan Chadkin
2022-10-02 12:14:49 +03:00
parent 516c6e1fc1
commit d5a8555781
2 changed files with 12 additions and 32 deletions

View File

@ -14,24 +14,10 @@ const importConfig = async (configFile) => {
if (configFile.endsWith('.cjs')) { if (configFile.endsWith('.cjs')) {
config = require(configFile); config = require(configFile);
} else { } else {
try { // dynamic import expects file url instead of path and may fail
// dynamic import expects file url instead of path and may fail // when windows path is provided
// when windows path is provided const { default: imported } = await import(pathToFileURL(configFile));
const { default: imported } = await import(pathToFileURL(configFile)); config = imported;
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;
}
}
}
} }
if (config == null || typeof config !== 'object' || Array.isArray(config)) { if (config == null || typeof config !== 'object' || Array.isArray(config)) {
throw Error(`Invalid config file "${configFile}"`); throw Error(`Invalid config file "${configFile}"`);

View File

@ -147,12 +147,9 @@ describe('loadConfig', () => {
expect( expect(
await loadConfig(null, path.join(cwd, './test/fixtures/missing')) await loadConfig(null, path.join(cwd, './test/fixtures/missing'))
).toEqual(null); ).toEqual(null);
// TODO remove check in v3 expect(await loadConfig(null, path.join(fixtures, 'mjs'))).toEqual({
if (process.version.startsWith('v10.') === false) { plugins: ['mjs'],
expect(await loadConfig(null, path.join(fixtures, 'mjs'))).toEqual({ });
plugins: ['mjs'],
});
}
expect(await loadConfig(null, path.join(fixtures, 'cjs'))).toEqual({ expect(await loadConfig(null, path.join(fixtures, 'cjs'))).toEqual({
plugins: ['cjs'], plugins: ['cjs'],
}); });
@ -195,14 +192,11 @@ describe('loadConfig', () => {
} catch (error) { } catch (error) {
expect(error.message).toMatch(/plugins is not defined/); expect(error.message).toMatch(/plugins is not defined/);
} }
// TODO remove check in v3 try {
if (process.version.startsWith('v10.') === false) { await loadConfig(path.join(fixtures, 'invalid-runtime.mjs'));
try { expect.fail('Config is loaded successfully');
await loadConfig(path.join(fixtures, 'invalid-runtime.mjs')); } catch (error) {
expect.fail('Config is loaded successfully'); expect(error.message).toMatch(/plugins is not defined/);
} catch (error) {
expect(error.message).toMatch(/plugins is not defined/);
}
} }
}); });