diff --git a/package-lock.json b/package-lock.json
index 345285ae..16fdd3d9 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -20265,7 +20265,7 @@
},
"packages/babel-plugin-transform-svg-component": {
"name": "@svgr/babel-plugin-transform-svg-component",
- "version": "6.0.0-alpha.2",
+ "version": "6.0.0-alpha.3",
"license": "MIT",
"engines": {
"node": ">=12"
@@ -20280,7 +20280,7 @@
},
"packages/babel-preset": {
"name": "@svgr/babel-preset",
- "version": "6.0.0-alpha.2",
+ "version": "6.0.0-alpha.3",
"license": "MIT",
"dependencies": {
"@svgr/babel-plugin-add-jsx-attribute": "^6.0.0-alpha.1",
@@ -20290,7 +20290,7 @@
"@svgr/babel-plugin-svg-dynamic-title": "^6.0.0-alpha.1",
"@svgr/babel-plugin-svg-em-dimensions": "^6.0.0-alpha.1",
"@svgr/babel-plugin-transform-react-native-svg": "^6.0.0-alpha.1",
- "@svgr/babel-plugin-transform-svg-component": "^6.0.0-alpha.2"
+ "@svgr/babel-plugin-transform-svg-component": "^6.0.0-alpha.3"
},
"engines": {
"node": ">=10"
@@ -20305,11 +20305,11 @@
},
"packages/cli": {
"name": "@svgr/cli",
- "version": "6.0.0-alpha.2",
+ "version": "6.0.0-alpha.3",
"license": "MIT",
"dependencies": {
- "@svgr/core": "^6.0.0-alpha.2",
- "@svgr/plugin-jsx": "^6.0.0-alpha.2",
+ "@svgr/core": "^6.0.0-alpha.3",
+ "@svgr/plugin-jsx": "^6.0.0-alpha.3",
"@svgr/plugin-prettier": "^6.0.0-alpha.1",
"@svgr/plugin-svgo": "^6.0.0-alpha.1",
"camelcase": "^6.2.0",
@@ -20399,10 +20399,10 @@
},
"packages/core": {
"name": "@svgr/core",
- "version": "6.0.0-alpha.2",
+ "version": "6.0.0-alpha.3",
"license": "MIT",
"dependencies": {
- "@svgr/plugin-jsx": "^6.0.0-alpha.2",
+ "@svgr/plugin-jsx": "^6.0.0-alpha.3",
"camelcase": "^6.2.0",
"cosmiconfig": "^7.0.1"
},
@@ -20419,7 +20419,7 @@
},
"packages/hast-util-to-babel-ast": {
"name": "@svgr/hast-util-to-babel-ast",
- "version": "6.0.0-alpha.1",
+ "version": "6.0.0-alpha.3",
"license": "MIT",
"dependencies": {
"@babel/types": "^7.15.6",
@@ -20438,12 +20438,12 @@
},
"packages/plugin-jsx": {
"name": "@svgr/plugin-jsx",
- "version": "6.0.0-alpha.2",
+ "version": "6.0.0-alpha.3",
"license": "MIT",
"dependencies": {
"@babel/core": "^7.15.5",
- "@svgr/babel-preset": "^6.0.0-alpha.2",
- "@svgr/hast-util-to-babel-ast": "^6.0.0-alpha.1",
+ "@svgr/babel-preset": "^6.0.0-alpha.3",
+ "@svgr/hast-util-to-babel-ast": "^6.0.0-alpha.3",
"svg-parser": "^2.0.2"
},
"engines": {
@@ -20498,15 +20498,15 @@
},
"packages/rollup": {
"name": "@svgr/rollup",
- "version": "6.0.0-alpha.2",
+ "version": "6.0.0-alpha.3",
"license": "MIT",
"dependencies": {
"@babel/core": "^7.15.5",
"@babel/plugin-transform-react-constant-elements": "^7.14.5",
"@babel/preset-env": "^7.15.6",
"@babel/preset-react": "^7.14.5",
- "@svgr/core": "^6.0.0-alpha.2",
- "@svgr/plugin-jsx": "^6.0.0-alpha.2",
+ "@svgr/core": "^6.0.0-alpha.3",
+ "@svgr/plugin-jsx": "^6.0.0-alpha.3",
"@svgr/plugin-svgo": "^6.0.0-alpha.1",
"rollup-pluginutils": "^2.8.2"
},
@@ -20525,7 +20525,7 @@
},
"packages/webpack": {
"name": "@svgr/webpack",
- "version": "6.0.0-alpha.2",
+ "version": "6.0.0-alpha.3",
"license": "MIT",
"dependencies": {
"@babel/core": "^7.15.5",
@@ -20533,8 +20533,8 @@
"@babel/preset-env": "^7.15.6",
"@babel/preset-react": "^7.14.5",
"@babel/preset-typescript": "^7.15.0",
- "@svgr/core": "^6.0.0-alpha.2",
- "@svgr/plugin-jsx": "^6.0.0-alpha.2",
+ "@svgr/core": "^6.0.0-alpha.3",
+ "@svgr/plugin-jsx": "^6.0.0-alpha.3",
"@svgr/plugin-svgo": "^6.0.0-alpha.1",
"loader-utils": "^2.0.0"
},
@@ -24034,14 +24034,14 @@
"@svgr/babel-plugin-svg-dynamic-title": "^6.0.0-alpha.1",
"@svgr/babel-plugin-svg-em-dimensions": "^6.0.0-alpha.1",
"@svgr/babel-plugin-transform-react-native-svg": "^6.0.0-alpha.1",
- "@svgr/babel-plugin-transform-svg-component": "^6.0.0-alpha.2"
+ "@svgr/babel-plugin-transform-svg-component": "^6.0.0-alpha.3"
}
},
"@svgr/cli": {
"version": "file:packages/cli",
"requires": {
- "@svgr/core": "^6.0.0-alpha.2",
- "@svgr/plugin-jsx": "^6.0.0-alpha.2",
+ "@svgr/core": "^6.0.0-alpha.3",
+ "@svgr/plugin-jsx": "^6.0.0-alpha.3",
"@svgr/plugin-prettier": "^6.0.0-alpha.1",
"@svgr/plugin-svgo": "^6.0.0-alpha.1",
"@types/glob": "^7.2.0",
@@ -24101,7 +24101,7 @@
"@svgr/core": {
"version": "file:packages/core",
"requires": {
- "@svgr/plugin-jsx": "^6.0.0-alpha.2",
+ "@svgr/plugin-jsx": "^6.0.0-alpha.3",
"@types/svgo": "^2.6.0",
"camelcase": "^6.2.0",
"cosmiconfig": "^7.0.1"
@@ -24119,8 +24119,8 @@
"version": "file:packages/plugin-jsx",
"requires": {
"@babel/core": "^7.15.5",
- "@svgr/babel-preset": "^6.0.0-alpha.2",
- "@svgr/hast-util-to-babel-ast": "^6.0.0-alpha.1",
+ "@svgr/babel-preset": "^6.0.0-alpha.3",
+ "@svgr/hast-util-to-babel-ast": "^6.0.0-alpha.3",
"svg-parser": "^2.0.2"
}
},
@@ -24146,8 +24146,8 @@
"@babel/plugin-transform-react-constant-elements": "^7.14.5",
"@babel/preset-env": "^7.15.6",
"@babel/preset-react": "^7.14.5",
- "@svgr/core": "^6.0.0-alpha.2",
- "@svgr/plugin-jsx": "^6.0.0-alpha.2",
+ "@svgr/core": "^6.0.0-alpha.3",
+ "@svgr/plugin-jsx": "^6.0.0-alpha.3",
"@svgr/plugin-svgo": "^6.0.0-alpha.1",
"rollup": "^2.56.3",
"rollup-plugin-image": "^1.0.2",
@@ -24163,8 +24163,8 @@
"@babel/preset-env": "^7.15.6",
"@babel/preset-react": "^7.14.5",
"@babel/preset-typescript": "^7.15.0",
- "@svgr/core": "^6.0.0-alpha.2",
- "@svgr/plugin-jsx": "^6.0.0-alpha.2",
+ "@svgr/core": "^6.0.0-alpha.3",
+ "@svgr/plugin-jsx": "^6.0.0-alpha.3",
"@svgr/plugin-svgo": "^6.0.0-alpha.1",
"@types/loader-utils": "^2.0.3",
"babel-loader": "^8.2.2",
diff --git a/packages/babel-plugin-svg-em-dimensions/README.md b/packages/babel-plugin-svg-em-dimensions/README.md
index 215c1d02..b7759641 100644
--- a/packages/babel-plugin-svg-em-dimensions/README.md
+++ b/packages/babel-plugin-svg-em-dimensions/README.md
@@ -12,7 +12,9 @@ npm install --save-dev @svgr/babel-plugin-svg-em-dimensions
```json
{
- "plugins": ["@svgr/babel-plugin-svg-em-dimensions"]
+ "plugins": [
+ ["@svgr/babel-plugin-svg-em-dimensions", { "width": 24, "height": 24 }]
+ ]
}
```
diff --git a/packages/babel-plugin-svg-em-dimensions/src/index.test.ts b/packages/babel-plugin-svg-em-dimensions/src/index.test.ts
index e687647d..5e9d96d6 100644
--- a/packages/babel-plugin-svg-em-dimensions/src/index.test.ts
+++ b/packages/babel-plugin-svg-em-dimensions/src/index.test.ts
@@ -1,9 +1,9 @@
import { transform } from '@babel/core'
-import plugin from '.'
+import plugin, { Options } from '.'
-const testPlugin = (code: string) => {
+const testPlugin = (code: string, options?: Options) => {
const result = transform(code, {
- plugins: ['@babel/plugin-syntax-jsx', plugin],
+ plugins: ['@babel/plugin-syntax-jsx', [plugin, options]],
configFile: false,
})
@@ -11,7 +11,7 @@ const testPlugin = (code: string) => {
}
describe('plugin', () => {
- it('should replace width / height attributes', () => {
+ it('replaces width / height attributes', () => {
expect(
testPlugin(''),
).toMatchInlineSnapshot(
@@ -19,9 +19,23 @@ describe('plugin', () => {
)
})
- it('should add theme if they are not present', () => {
+ it('adds theme if they are not present', () => {
expect(testPlugin('')).toMatchInlineSnapshot(
`";"`,
)
})
+
+ it('accepts numeric values', () => {
+ expect(
+ testPlugin('', { width: 24, height: 24 }),
+ ).toMatchInlineSnapshot(`";"`)
+ })
+
+ it('accepts string values', () => {
+ expect(
+ testPlugin('', { width: '2em', height: '2em' }),
+ ).toMatchInlineSnapshot(
+ `";"`,
+ )
+ })
})
diff --git a/packages/babel-plugin-svg-em-dimensions/src/index.ts b/packages/babel-plugin-svg-em-dimensions/src/index.ts
index 0b24accc..7ef835df 100644
--- a/packages/babel-plugin-svg-em-dimensions/src/index.ts
+++ b/packages/babel-plugin-svg-em-dimensions/src/index.ts
@@ -1,10 +1,26 @@
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
-import { types as t, NodePath } from '@babel/core'
+import { types as t, NodePath, ConfigAPI } from '@babel/core'
const elements = ['svg', 'Svg']
-const value = t.stringLiteral('1em')
-const plugin = () => ({
+export interface Options {
+ width: number | string
+ height: number | string
+}
+
+const getValue = (raw: undefined | number | string) => {
+ if (raw === undefined) return t.stringLiteral('1em')
+ switch (typeof raw) {
+ case 'number':
+ return t.jsxExpressionContainer(t.numericLiteral(raw))
+ case 'string':
+ return t.stringLiteral(raw)
+ default:
+ return t.stringLiteral('1em')
+ }
+}
+
+const plugin = (_: ConfigAPI, opts: Options) => ({
visitor: {
JSXOpeningElement(path: NodePath) {
if (
@@ -14,7 +30,11 @@ const plugin = () => ({
)
return
- const requiredAttributes = ['width', 'height']
+ const values = {
+ width: getValue(opts.width),
+ height: getValue(opts.height),
+ }
+ const requiredAttributes = Object.keys(values)
path.get('attributes').forEach((attributePath) => {
if (!attributePath.isJSXAttribute()) return
@@ -25,14 +45,17 @@ const plugin = () => ({
if (index === -1) return
const valuePath = attributePath.get('value')
- valuePath.replaceWith(value)
+ valuePath.replaceWith(values[namePath.node.name as 'width' | 'height'])
requiredAttributes.splice(index, 1)
})
path.pushContainer(
'attributes',
requiredAttributes.map((attr) =>
- t.jsxAttribute(t.jsxIdentifier(attr), value),
+ t.jsxAttribute(
+ t.jsxIdentifier(attr),
+ values[attr as 'width' | 'height'],
+ ),
),
)
},
diff --git a/packages/babel-preset/src/index.test.ts b/packages/babel-preset/src/index.test.ts
index cd703f29..90073f5a 100644
--- a/packages/babel-preset/src/index.test.ts
+++ b/packages/babel-preset/src/index.test.ts
@@ -17,7 +17,7 @@ const testPreset = (code: string, options: Partial) => {
}
describe('preset', () => {
- it('should handle svgProps', () => {
+ it('handles svgProps', () => {
expect(
testPreset('', {
svgProps: {
@@ -34,7 +34,7 @@ describe('preset', () => {
`)
})
- it('should handle titleProp', () => {
+ it('handles titleProp', () => {
expect(
testPreset('', {
titleProp: true,
@@ -50,7 +50,7 @@ describe('preset', () => {
export default SvgComponent;"
`)
})
- it('should handle titleProp and fallback on existing title', () => {
+ it('handles titleProp and fallback on existing title', () => {
// testing when existing title has string as chilren
expect(
testPreset(``, {
@@ -83,7 +83,7 @@ describe('preset', () => {
`)
})
- it('should handle replaceAttrValues', () => {
+ it('handles replaceAttrValues', () => {
expect(
testPreset('', {
replaceAttrValues: {
@@ -100,7 +100,7 @@ describe('preset', () => {
`)
})
- it('should handle expandProps & icon & dimensions', () => {
+ it('handles expandProps & icon & dimensions', () => {
expect(
testPreset('', {
expandProps: 'end',
@@ -115,4 +115,38 @@ describe('preset', () => {
export default SvgComponent;"
`)
})
+
+ it('handles custom icon size', () => {
+ expect(
+ testPreset('', {
+ expandProps: 'end',
+ icon: 24,
+ dimensions: true,
+ }),
+ ).toMatchInlineSnapshot(`
+ "import * as React from \\"react\\";
+
+ const SvgComponent = props => ;
+
+ export default SvgComponent;"
+ `)
+ })
+
+ it('defaults to 24 on native', () => {
+ expect(
+ testPreset('', {
+ expandProps: 'end',
+ icon: true,
+ native: true,
+ dimensions: true,
+ }),
+ ).toMatchInlineSnapshot(`
+ "import * as React from \\"react\\";
+ import Svg from \\"react-native-svg\\";
+
+ const SvgComponent = props => ;
+
+ export default SvgComponent;"
+ `)
+ })
})
diff --git a/packages/babel-preset/src/index.ts b/packages/babel-preset/src/index.ts
index 66501915..5405a032 100644
--- a/packages/babel-preset/src/index.ts
+++ b/packages/babel-preset/src/index.ts
@@ -20,7 +20,7 @@ export interface Options extends TransformOptions {
titleProp?: boolean
expandProps?: boolean | 'start' | 'end'
dimensions?: boolean
- icon?: boolean
+ icon?: boolean | string | number
native?: boolean
svgProps?: { [key: string]: string }
replaceAttrValues?: { [key: string]: string }
@@ -96,7 +96,18 @@ const plugin = (_: ConfigAPI, opts: Options) => {
const plugins: any[] = [
[transformSvgComponent, opts],
- ...(opts.icon && opts.dimensions ? [svgEmDimensions] : []),
+ ...(opts.icon !== false && opts.dimensions
+ ? [
+ [
+ svgEmDimensions,
+ opts.icon !== true
+ ? { width: opts.icon, height: opts.icon }
+ : opts.native
+ ? { width: 24, height: 24 }
+ : {},
+ ],
+ ]
+ : []),
[
removeJSXAttribute,
{ elements: ['svg', 'Svg'], attributes: toRemoveAttributes },
diff --git a/packages/cli/src/__snapshots__/index.test.ts.snap b/packages/cli/src/__snapshots__/index.test.ts.snap
index 7004f9df..dc21e590 100644
--- a/packages/cli/src/__snapshots__/index.test.ts.snap
+++ b/packages/cli/src/__snapshots__/index.test.ts.snap
@@ -203,6 +203,46 @@ export default SvgFile
"
`;
+exports[`cli should support various args: --icon 2em 1`] = `
+"import * as React from 'react'
+
+const SvgFile = (props) => (
+
+)
+
+export default SvgFile
+
+"
+`;
+
+exports[`cli should support various args: --icon 24 1`] = `
+"import * as React from 'react'
+
+const SvgFile = (props) => (
+
+)
+
+export default SvgFile
+
+"
+`;
+
exports[`cli should support various args: --jsx-runtime automatic 1`] = `
"const SvgFile = (props) => (