-
Notifications
You must be signed in to change notification settings - Fork 54
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,9 +25,9 @@ | |
"vite": "^2.6.13" | ||
}, | ||
"peerDependencies": { | ||
"vite": "^2.0.0" | ||
"vite": "^2.6.0" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I bumped this because this is the version when |
||
}, | ||
"dependencies": { | ||
"@svgr/core": "^6.0.0-alpha.2" | ||
"@svgr/core": "^6.0.0-alpha.3" | ||
} | ||
} |
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') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
|
||
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 }` | ||
) | ||
}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I chained these together with a |
||
|
||
const res = await transformWithEsbuild( | ||
componentCode + '\n' + code, | ||
|
There was a problem hiding this comment.
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?