1
0
mirror of https://github.com/svg/svgo.git synced 2025-08-07 15:22:54 +03:00

Support es modules (#1583)

Ref https://github.com/svg/svgo/issues/1579

In config of course. Projects with type:module can now use modules to
export config

```js
export default {
  plugins: []
}
```

Also added support for resolving svgo.config.mjs and svgo.config.cjs.

Moved loadConfig tests to svgo-node tests.

mjs test is skipped for now in node 10, just don't use modules there
This commit is contained in:
Bogdan Chadkin
2021-09-23 21:44:55 +03:00
committed by GitHub
parent 4c6a091a90
commit 7111c52f96
14 changed files with 132 additions and 93 deletions

View File

@@ -1,7 +1,5 @@
'use strict';
const path = require('path');
const { loadConfig } = require('../../lib/svgo-node.js');
const {
resolvePluginConfig,
extendDefaultPlugins,
@@ -173,91 +171,6 @@ describe('config', function () {
);
});
});
describe('config', () => {
it('is loaded by absolute path', async () => {
const config = await loadConfig(
path.join(process.cwd(), './test/config/fixtures/one/two/config.js')
);
expect(config).toEqual({ plugins: [] });
});
it('is loaded by relative path to cwd', async () => {
const config = await loadConfig(
'one/two/config.js',
path.join(process.cwd(), './test/config/fixtures')
);
expect(config).toEqual({ plugins: [] });
});
it('is searched in cwd and up', async () => {
const config = await loadConfig(
null,
path.join(process.cwd(), './test/config/fixtures/one/two')
);
expect(config).toEqual({ plugins: [] });
});
it('gives null when config is not found', async () => {
const config = await loadConfig(
null,
path.join(process.cwd(), './test/config')
);
expect(config).toEqual(null);
});
it('is failed when specified config does not exist', async () => {
try {
await loadConfig('{}');
expect.fail('Config is loaded successfully');
} catch (error) {
expect(error.message).toMatch(/Cannot find module/);
}
});
it('is failed to load when module exports not an object', async () => {
try {
await loadConfig(
path.join(process.cwd(), './test/config/fixtures/invalid-null.js')
);
expect.fail('Config is loaded successfully');
} catch (error) {
expect(error.message).toMatch(/Invalid config file/);
}
try {
await loadConfig(
path.join(process.cwd(), './test/config/fixtures/invalid-array.js')
);
expect.fail('Config is loaded successfully');
} catch (error) {
expect(error.message).toMatch(/Invalid config file/);
}
try {
await loadConfig(
path.join(process.cwd(), './test/config/fixtures/invalid-string.js')
);
expect.fail('Config is loaded successfully');
} catch (error) {
expect(error.message).toMatch(/Invalid config file/);
}
});
it('handles config errors properly', async () => {
try {
await loadConfig(
null,
path.join(process.cwd(), './test/config/fixtures/invalid/config')
);
expect.fail('Config is loaded successfully');
} catch (error) {
expect(error.message).toMatch(/plugins is not defined/);
}
});
it('handles MODULE_NOT_FOUND properly', async () => {
try {
await loadConfig(
path.join(process.cwd(), './test/config/fixtures/module-not-found.js')
);
expect.fail('Config is loaded successfully');
} catch (error) {
expect(error.message).toMatch(/Cannot find module 'unknown-module'/);
}
});
});
});
function getPlugin(name, plugins) {