Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for svgo.config.cjs files in esm environments #879

Merged
merged 1 commit into from
Aug 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions __fixtures__/withSvgoConfig/svgo.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module.exports = {
plugins: [
{
name: 'preset-default',
params: {
overrides: {
removeTitle: false,
},
},
},
]
}
13 changes: 13 additions & 0 deletions packages/cli/src/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,19 @@ export default SvgFile
"
`;

exports[`cli should support --svgo-config as file with .cjs extension 1`] = `
"import * as React from 'react'
const SvgFile = (props) => (
<svg xmlns="http://www.w3.org/2000/svg" width={48} height={1} {...props}>
<title>{'Rectangle 5'}</title>
<path fill="#063855" fillRule="evenodd" d="M0 0h48v1H0z" />
</svg>
)
export default SvgFile

"
`;

exports[`cli should support --svgo-config as json 1`] = `
"import * as React from 'react'
const SvgFile = (props) => (
Expand Down
7 changes: 7 additions & 0 deletions packages/cli/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,13 @@ describe('cli', () => {
expect(result).toMatchSnapshot()
})

it('should support --svgo-config as file with .cjs extension', async () => {
const result = await cli(
`--svgo-config __fixtures__/withSvgoConfig/svgo.config.cjs __fixtures__/simple/file.svg`,
)
expect(result).toMatchSnapshot()
})

it.each([
['--no-dimensions'],
['--jsx-runtime classic-preact'],
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const parseConfig = (name: string) => (arg: string) => {
}

const ext = path.extname(arg)
if (ext === '.js' || ext === '.json') {
if (ext === '.js' || ext === '.json' || ext === '.cjs') {
return require(path.join(process.cwd(), arg))
}

Expand Down
1 change: 1 addition & 0 deletions packages/plugin-svgo/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const explorer = cosmiconfigSync('svgo', {
'.svgorc.yaml',
'.svgorc.yml',
'svgo.config.js',
'svgo.config.cjs',
'.svgo.yml',
],
transform: (result) => result && result.config,
Expand Down
2 changes: 1 addition & 1 deletion website/pages/docs/configuration-files.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ expandProps: false

## SVGO

The recommended way to configure SVGO for SVGR is to use [`svgo.config.js`](https://github.com/svg/svgo/blob/main/README.md#configuration).
The recommended way to configure SVGO for SVGR is to use [`svgo.config.js or svgo.config.cjs`](https://github.com/svg/svgo/blob/main/README.md#configuration).

Even if it is not recommended, you can also use `svgoConfig` option to specify your SVGO configuration. `svgoConfig` has precedence on `svgo.config.js`.

Expand Down