From 7682a5edb32eac95885929f3befaa49883bf49f6 Mon Sep 17 00:00:00 2001 From: JW Date: Wed, 22 May 2024 10:35:12 +0800 Subject: [PATCH] :pencil: svgo impl --- .changeset/fresh-vans-press.md | 5 +++ .changeset/giant-items-hide.md | 5 --- .changeset/red-ways-relax.md | 5 --- README.md | 67 ++++++++++++++++++++++++++++++++++ src/type.ts | 1 - 5 files changed, 72 insertions(+), 11 deletions(-) create mode 100644 .changeset/fresh-vans-press.md delete mode 100644 .changeset/giant-items-hide.md delete mode 100644 .changeset/red-ways-relax.md diff --git a/.changeset/fresh-vans-press.md b/.changeset/fresh-vans-press.md new file mode 100644 index 0000000..0e6332a --- /dev/null +++ b/.changeset/fresh-vans-press.md @@ -0,0 +1,5 @@ +--- +"@svgr-rs/svgrs-plugin": minor +--- + +support nodejs svgo diff --git a/.changeset/giant-items-hide.md b/.changeset/giant-items-hide.md deleted file mode 100644 index 6c7db3e..0000000 --- a/.changeset/giant-items-hide.md +++ /dev/null @@ -1,5 +0,0 @@ ---- -"@svgr-rs/svgrs-plugin": patch ---- - -await optimize diff --git a/.changeset/red-ways-relax.md b/.changeset/red-ways-relax.md deleted file mode 100644 index 5a9f63c..0000000 --- a/.changeset/red-ways-relax.md +++ /dev/null @@ -1,5 +0,0 @@ ---- -"@svgr-rs/svgrs-plugin": patch ---- - -support nodejs svgo diff --git a/README.md b/README.md index 9818df9..9eb0963 100644 --- a/README.md +++ b/README.md @@ -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` diff --git a/src/type.ts b/src/type.ts index 33154f2..137054b 100644 --- a/src/type.ts +++ b/src/type.ts @@ -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 }