Skip to content

Commit

Permalink
✨ support svgo implemention
Browse files Browse the repository at this point in the history
  • Loading branch information
JiangWeixian committed Sep 3, 2023
1 parent dbe02e4 commit c4c4fbf
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
* Copyright 2017 Smooth Code
*/

import type { Config, State } from '@svgr-rs/core'
import type { State } from '@svgr-rs/core'
import type SvgoInstance from 'svgo'
import type { Config } from './type'

/**
* @todo Load svgo config from config file
*/
export const getSvgoConfig = (config: Config): any => {
// @ts-expect-error -- ignore
if (config.svgoConfig) {
// @ts-expect-error -- ignore
return config.svgoConfig
}
return {}
Expand All @@ -21,9 +21,14 @@ export const svgo = async (code: string, config: Config, state: State) => {
if (!config.svgo) {
return code
}
const { optimize } = await import('svgo')
let svgoInstance: typeof SvgoInstance
if (config.svgoImplementation) {
svgoInstance = config.svgoImplementation
} else {
svgoInstance = await import('svgo')
}
const svgoConfig = getSvgoConfig(config)
const result = optimize(code, { ...svgoConfig, path: state.filePath })
const result = svgoInstance.optimize(code, { ...svgoConfig, path: state.filePath })

// @ts-expect-error -- ignore
if (result.modernError) {
Expand Down
10 changes: 10 additions & 0 deletions src/type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import type { Config as SvgrsConfig } from '@svgr-rs/core'

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 c4c4fbf

Please sign in to comment.