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

Allow specifying svgr options #11

Merged
merged 1 commit into from
Nov 13, 2021
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
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,14 @@ import svgrPlugin from 'vite-plugin-svgr'

export default {
// ...
plugins: [svgrPlugin()],
plugins: [
svgrPlugin({
svgrOptions: {
icon: true,
// ...svgr options (https://react-svgr.com/docs/options/)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shall we put the v6 alpha options link here?

},
}),
],
}
```

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
"vite": "^2.6.13"
},
"peerDependencies": {
"vite": "^2.0.0"
"vite": "^2.6.0"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I bumped this because this is the version when transformWithEsbuild was exposed: vitejs/vite#4882

},
"dependencies": {
"@svgr/core": "^6.0.0-alpha.2"
"@svgr/core": "^6.0.0-alpha.3"
}
}
34 changes: 20 additions & 14 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,31 @@
import fs from 'fs'
import { transformWithEsbuild, Plugin } from 'vite'
import type { Config } from '@svgr/core'
import { transformWithEsbuild } from 'vite'
import type { Plugin } from 'vite'

type Options = {
svgrOptions?: Config
}

export = function svgrPlugin(options: Options = {}): Plugin {
const { svgrOptions = {} } = options

export default function svgrPlugin(): Plugin {
// TODO: options
return {
name: 'vite:svgr',
async transform(code, id) {
if (id.endsWith('.svg')) {
// TODO: exclude node_modules as an option
const { default: convert } = await import('@svgr/core')
const { transform: convert } = await import('@svgr/core')
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Starting with svgr 6.0.0-alpha.3, there is no default export, instead there's a named transform export.


const svgCode = await fs.promises.readFile(id, 'utf8')
let componentCode = await convert(
svgCode,
{},
{ componentName: 'ReactComponent' }
)
componentCode = componentCode.replace(
'export default ReactComponent',
'export { ReactComponent }'
)

const componentCode = await convert(svgCode, svgrOptions, {
componentName: 'ReactComponent',
}).then((res) => {
return res.replace(
'export default ReactComponent',
`export { ReactComponent }`
)
})
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I chained these together with a .then() because it seemed cleaner, but I can revert this change if needed.


const res = await transformWithEsbuild(
componentCode + '\n' + code,
Expand Down