Skip to content

Commit

Permalink
📝 svgo impl
Browse files Browse the repository at this point in the history
  • Loading branch information
JiangWeixian committed May 22, 2024
1 parent 3737079 commit 7682a5e
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/fresh-vans-press.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@svgr-rs/svgrs-plugin": minor
---

support nodejs svgo
5 changes: 0 additions & 5 deletions .changeset/giant-items-hide.md

This file was deleted.

5 changes: 0 additions & 5 deletions .changeset/red-ways-relax.md

This file was deleted.

67 changes: 67 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,73 @@ Use [svgr-rs](https://github.com/svg-rust/svgr-rs) with vite and webpack.
pnpm i @svgr-rs/svgrs-plugin -D
```

## options

> [!NOTE]
Common options, both work with `vite` and `webpack`. Check more supported options from [svg-rust/svgr-rs](https://github.com/svg-rust/svgr-rs).

**Extra options for plugins:**

`svgoImplementation`

> [!NOTE]
Use different version `svgo` to optimize svg. Only work when `options.svgo` is enabled.

- type check below example code for details

```ts
import Svgo from 'svgo'

function getInfo(state: { filePath?: string }) {
return state.filePath
? {
input: 'file',
path: state.filePath,
}
: {
input: 'string',
}
}

export const svgo = () => {
let svgo: any
return {
async optimize(code: string, { path, ...config }: any) {
if (!svgo) {
svgo = new Svgo(config)
}
return svgo.optimize(code, getInfo({ filePath: path }))
},
}
}

// example for webpack config
{
loader: require.resolve('@svgr-rs/svgrs-plugin/webpack'),
options: {
ref: true,
exportType: 'default',
jsxRuntime: 'automatic',
icon: false,
svgo: true,
svgoImplementation: svgo(),
svgoConfig: {
plugins: [
{ prefixIds: true },
{ removeDimensions: false },
{ removeViewBox: true },
],
},
},
},
```


`svgoConfig`

- type check `svgo` config for more details


## usage

### `vite`
Expand Down
1 change: 0 additions & 1 deletion src/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@ export interface Config extends SvgrsConfig {
* @description Extended config options for @svgr-rs/core, load custom svgo implementation
*/
svgoImplementation?: any
// svgoConfig is hidden in @svgr-rs/core
svgoConfig?: any
}

0 comments on commit 7682a5e

Please sign in to comment.