Skip to content

Commit 169eb2f

Browse files
committed
feat: add synchronous API
You can now use `svgr.sync` to call svgr synchronously! Closes #185
1 parent bb95828 commit 169eb2f

File tree

12 files changed

+425
-199
lines changed

12 files changed

+425
-199
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ You can find all default templates in [templates folder](https://github.com/smoo
199199

200200
## Node API usage
201201

202-
SVGR can also be used programmatically:
202+
### `svgr(code, config, state)`
203203

204204
```js
205205
import svgr from '@svgr/core'
@@ -218,6 +218,8 @@ svgr(svgCode, { icon: true }, { componentName: 'MyComponent' }).then(jsCode => {
218218
})
219219
```
220220

221+
Use `svgr.sync(code, config, state)` if you would like to use sync version.
222+
221223
## [Webpack loader](https://github.com/smooth-code/svgr/blob/master/packages/webpack)
222224

223225
## [Rollup plugin](https://github.com/smooth-code/svgr/blob/master/packages/rollup)

packages/core/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ svgr(svgCode, { icon: true }, { componentName: 'MyComponent' }).then(jsCode => {
2828
})
2929
```
3030

31+
Use `svgr.sync(code, config, state)` if you would like to use sync version.
32+
3133
## License
3234

3335
MIT
Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`svgo async #loadConfig [async] should load config using filePath 1`] = `
4+
Object {
5+
"dimensions": true,
6+
"expandProps": "end",
7+
"h2xConfig": null,
8+
"icon": true,
9+
"native": false,
10+
"noSemi": true,
11+
"prettier": true,
12+
"prettierConfig": null,
13+
"ref": false,
14+
"replaceAttrValues": Array [
15+
Array [
16+
"#063855",
17+
"currentColor",
18+
],
19+
],
20+
"runtimeConfig": true,
21+
"svgProps": null,
22+
"svgo": true,
23+
"svgoConfig": null,
24+
"template": null,
25+
"titleProp": false,
26+
}
27+
`;
28+
29+
exports[`svgo async #loadConfig [async] should not load config with "runtimeConfig: false 1`] = `
30+
Object {
31+
"dimensions": true,
32+
"expandProps": "end",
33+
"h2xConfig": null,
34+
"icon": true,
35+
"native": false,
36+
"noSemi": true,
37+
"prettier": true,
38+
"prettierConfig": null,
39+
"ref": false,
40+
"replaceAttrValues": Array [
41+
Array [
42+
"#063855",
43+
"currentColor",
44+
],
45+
],
46+
"runtimeConfig": true,
47+
"svgProps": null,
48+
"svgo": true,
49+
"svgoConfig": null,
50+
"template": null,
51+
"titleProp": false,
52+
"useRuntimeConfig": false,
53+
}
54+
`;
55+
56+
exports[`svgo async #loadConfig [async] should use default config without state.filePath 1`] = `
57+
Object {
58+
"dimensions": false,
59+
"expandProps": "end",
60+
"h2xConfig": null,
61+
"icon": false,
62+
"native": false,
63+
"prettier": true,
64+
"prettierConfig": null,
65+
"ref": false,
66+
"replaceAttrValues": null,
67+
"runtimeConfig": true,
68+
"svgProps": null,
69+
"svgo": true,
70+
"svgoConfig": null,
71+
"template": null,
72+
"titleProp": false,
73+
}
74+
`;
75+
76+
exports[`svgo async #loadConfig [async] should work with custom config path 1`] = `
77+
Object {
78+
"dimensions": true,
79+
"expandProps": "end",
80+
"h2xConfig": null,
81+
"icon": true,
82+
"native": false,
83+
"noSemi": true,
84+
"prettier": true,
85+
"prettierConfig": null,
86+
"ref": false,
87+
"replaceAttrValues": Array [
88+
Array [
89+
"#063855",
90+
"currentColor",
91+
],
92+
],
93+
"runtimeConfig": true,
94+
"svgProps": null,
95+
"svgo": true,
96+
"svgoConfig": null,
97+
"template": null,
98+
"titleProp": false,
99+
}
100+
`;
101+
102+
exports[`svgo sync #loadConfig [sync] should load config using filePath 1`] = `
103+
Object {
104+
"dimensions": true,
105+
"expandProps": "end",
106+
"h2xConfig": null,
107+
"icon": true,
108+
"native": false,
109+
"noSemi": true,
110+
"prettier": true,
111+
"prettierConfig": null,
112+
"ref": false,
113+
"replaceAttrValues": Array [
114+
Array [
115+
"#063855",
116+
"currentColor",
117+
],
118+
],
119+
"runtimeConfig": true,
120+
"svgProps": null,
121+
"svgo": true,
122+
"svgoConfig": null,
123+
"template": null,
124+
"titleProp": false,
125+
}
126+
`;
127+
128+
exports[`svgo sync #loadConfig [sync] should not load config with "runtimeConfig: false 1`] = `
129+
Object {
130+
"dimensions": true,
131+
"expandProps": "end",
132+
"h2xConfig": null,
133+
"icon": true,
134+
"native": false,
135+
"noSemi": true,
136+
"prettier": true,
137+
"prettierConfig": null,
138+
"ref": false,
139+
"replaceAttrValues": Array [
140+
Array [
141+
"#063855",
142+
"currentColor",
143+
],
144+
],
145+
"runtimeConfig": true,
146+
"svgProps": null,
147+
"svgo": true,
148+
"svgoConfig": null,
149+
"template": null,
150+
"titleProp": false,
151+
"useRuntimeConfig": false,
152+
}
153+
`;
154+
155+
exports[`svgo sync #loadConfig [sync] should use default config without state.filePath 1`] = `
156+
Object {
157+
"dimensions": false,
158+
"expandProps": "end",
159+
"h2xConfig": null,
160+
"icon": false,
161+
"native": false,
162+
"prettier": true,
163+
"prettierConfig": null,
164+
"ref": false,
165+
"replaceAttrValues": null,
166+
"runtimeConfig": true,
167+
"svgProps": null,
168+
"svgo": true,
169+
"svgoConfig": null,
170+
"template": null,
171+
"titleProp": false,
172+
}
173+
`;
174+
175+
exports[`svgo sync #loadConfig [sync] should work with custom config path 1`] = `
176+
Object {
177+
"dimensions": true,
178+
"expandProps": "end",
179+
"h2xConfig": null,
180+
"icon": true,
181+
"native": false,
182+
"noSemi": true,
183+
"prettier": true,
184+
"prettierConfig": null,
185+
"ref": false,
186+
"replaceAttrValues": Array [
187+
Array [
188+
"#063855",
189+
"currentColor",
190+
],
191+
],
192+
"runtimeConfig": true,
193+
"svgProps": null,
194+
"svgo": true,
195+
"svgoConfig": null,
196+
"template": null,
197+
"titleProp": false,
198+
}
199+
`;

packages/core/src/config.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,37 @@ export async function resolveConfig(searchFrom, configFile) {
3333
return result ? result.config : null
3434
}
3535

36+
resolveConfig.sync = (searchFrom, configFile) => {
37+
if (configFile == null) {
38+
const result = explorer.searchSync(searchFrom)
39+
return result ? result.config : null
40+
}
41+
const result = explorer.loadSync(configFile)
42+
return result ? result.config : null
43+
}
44+
3645
export async function resolveConfigFile(filePath) {
3746
const result = await explorer.search(filePath)
3847
return result ? result.filepath : null
3948
}
4049

50+
resolveConfigFile.sync = filePath => {
51+
const result = explorer.searchSync(filePath)
52+
return result ? result.filepath : null
53+
}
54+
4155
export async function loadConfig({ configFile, ...baseConfig }, state = {}) {
4256
const rcConfig =
4357
state.filePath && baseConfig.runtimeConfig !== false
4458
? await resolveConfig(state.filePath, configFile)
4559
: {}
4660
return { ...DEFAULT_CONFIG, ...rcConfig, ...baseConfig }
4761
}
62+
63+
loadConfig.sync = ({ configFile, ...baseConfig }, state = {}) => {
64+
const rcConfig =
65+
state.filePath && baseConfig.runtimeConfig !== false
66+
? resolveConfig.sync(state.filePath, configFile)
67+
: {}
68+
return { ...DEFAULT_CONFIG, ...rcConfig, ...baseConfig }
69+
}

0 commit comments

Comments
 (0)