Skip to content

Commit 2590cd8

Browse files
ilteooodjbroma
andauthored
feat: extend signature to allow svgr options (#1175)
Co-authored-by: Jakub Romańczyk <lorczyslav@gmail.com>
1 parent de06fff commit 2590cd8

File tree

5 files changed

+65
-6
lines changed

5 files changed

+65
-6
lines changed

.changeset/three-cups-juggle.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@callstack/repack": minor
3+
---
4+
5+
feat: allow passing additional SVGR options through `getAssetTransformRules`

packages/repack/src/utils/__tests__/__snapshots__/getAssetTransformRules.test.ts.snap

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,31 @@ exports[`getAssetTransformRules should add XML rule when svg="xml" 1`] = `
6262
]
6363
`;
6464

65+
exports[`getAssetTransformRules should include additional options for SVGR 1`] = `
66+
[
67+
{
68+
"test": /\\\\\\.\\(bmp\\|gif\\|jpg\\|jpeg\\|png\\|psd\\|webp\\|tiff\\|m4v\\|mov\\|mp4\\|mpeg\\|mpg\\|webm\\|aac\\|aiff\\|caf\\|m4a\\|mp3\\|wav\\|html\\|pdf\\|yaml\\|yml\\|otf\\|ttf\\|zip\\|obj\\)\\$/,
69+
"use": {
70+
"loader": "@callstack/repack/assets-loader",
71+
"options": {
72+
"inline": undefined,
73+
"remote": undefined,
74+
},
75+
},
76+
},
77+
{
78+
"test": /\\\\\\.svg\\$/,
79+
"use": {
80+
"loader": "@svgr/webpack",
81+
"options": {
82+
"dimensions": false,
83+
"native": true,
84+
},
85+
},
86+
},
87+
]
88+
`;
89+
6590
exports[`getAssetTransformRules should return default asset transform rules when no options provided 1`] = `
6691
[
6792
{

packages/repack/src/utils/__tests__/getAssetTransformRules.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,14 @@ describe('getAssetTransformRules', () => {
5454
const ruleTest = rules[0]?.test;
5555
expect(ruleTest.test('test.svg')).toEqual(false);
5656
});
57+
58+
it('should include additional options for SVGR', () => {
59+
const rules = getAssetTransformRules({
60+
svg: { type: 'svgr', options: { dimensions: false } },
61+
});
62+
63+
expect(rules).toHaveLength(2);
64+
expect(rules[1]?.use?.options).toHaveProperty('dimensions', false);
65+
expect(rules).toMatchSnapshot();
66+
});
5767
});

packages/repack/src/utils/getAssetTransformRules.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,23 @@ import {
44
getAssetExtensionsRegExp,
55
} from './assetExtensions.js';
66

7-
function getSvgRule(type: 'svgr' | 'xml' | 'uri') {
8-
if (type === 'svgr') {
7+
type SvgType =
8+
| 'svgr'
9+
| 'xml'
10+
| 'uri'
11+
| { type: 'svgr'; options: Record<string, any> };
12+
13+
function getSvgRule(type: SvgType) {
14+
const isTypeObject = typeof type === 'object';
15+
16+
if (type === 'svgr' || (isTypeObject && type?.type === 'svgr')) {
17+
const additionalOptions = isTypeObject && type?.options;
918
return {
1019
test: /\.svg$/,
11-
use: { loader: '@svgr/webpack', options: { native: true } },
20+
use: {
21+
loader: '@svgr/webpack',
22+
options: { native: true, ...additionalOptions },
23+
},
1224
};
1325
}
1426

@@ -38,7 +50,7 @@ interface GetAssetTransformRulesOptions {
3850
* - 'xml': Loads SVGs as raw XML source to be used with SvgXml from react-native-svg
3951
* - 'uri': Loads SVGs as inline URIs to be used with SvgUri from react-native-svg
4052
*/
41-
svg?: 'svgr' | 'xml' | 'uri';
53+
svg?: SvgType;
4254
}
4355

4456
/**

website/src/latest/api/utils/get-asset-transform-rules.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ interface GetAssetTransformRulesOptions {
2020
resourceExtensionType: string;
2121
}) => string;
2222
};
23-
svg?: "svgr" | "xml" | "uri";
23+
svg?: "svgr" | "xml" | "uri" | { type: "svgr", options: Record<string, any> };
2424
}
2525
```
2626

@@ -67,14 +67,21 @@ See [`assetsLoader` documentation](/api/loaders/assets-loader#remoteassetpath) f
6767

6868
### options.svg
6969

70-
- Type: `'svgr' | 'xml' | 'uri'`
70+
- Type: `'svgr' | 'xml' | 'uri' | { type: "svgr", options: Record<string, any>}`
7171

7272
Determines how SVG files should be processed:
7373

7474
- `'svgr'`: Uses `@svgr/webpack` to transform SVGs into React Native components
75+
- `type: "svgr"`: Uses `@svgr/webpack` to transform SVGs into React Native components and allows to configure additional options to `@svgr/webpack`.
7576
- `'xml'`: Loads SVGs as raw XML source to be used with `SvgXml` from `react-native-svg`
7677
- `'uri'`: Loads SVGs as inline URIs to be used with `SvgUri` from `react-native-svg`
7778

79+
#### options.svg.options
80+
81+
- Type: `Record<string, any>`
82+
83+
Allows to configure additional options to `@svgr/webpack`. The full list of available options can be found in the [svgr doc](https://react-svgr.com/docs/options/).
84+
7885
:::tip
7986
Learn more about using SVG in the [SVG guide](/docs/guides/svg).
8087
:::

0 commit comments

Comments
 (0)