Skip to content

Commit

Permalink
feat: add synchronous API
Browse files Browse the repository at this point in the history
You can now use `svgr.sync` to call svgr synchronously!

Closes #185
  • Loading branch information
gregberge committed Sep 30, 2018
1 parent bb95828 commit 169eb2f
Show file tree
Hide file tree
Showing 12 changed files with 425 additions and 199 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ You can find all default templates in [templates folder](https://github.com/smoo

## Node API usage

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

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

Use `svgr.sync(code, config, state)` if you would like to use sync version.

## [Webpack loader](https://github.com/smooth-code/svgr/blob/master/packages/webpack)

## [Rollup plugin](https://github.com/smooth-code/svgr/blob/master/packages/rollup)
Expand Down
2 changes: 2 additions & 0 deletions packages/core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ svgr(svgCode, { icon: true }, { componentName: 'MyComponent' }).then(jsCode => {
})
```

Use `svgr.sync(code, config, state)` if you would like to use sync version.

## License

MIT
Expand Down
199 changes: 199 additions & 0 deletions packages/core/src/__snapshots__/config.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`svgo async #loadConfig [async] should load config using filePath 1`] = `
Object {
"dimensions": true,
"expandProps": "end",
"h2xConfig": null,
"icon": true,
"native": false,
"noSemi": true,
"prettier": true,
"prettierConfig": null,
"ref": false,
"replaceAttrValues": Array [
Array [
"#063855",
"currentColor",
],
],
"runtimeConfig": true,
"svgProps": null,
"svgo": true,
"svgoConfig": null,
"template": null,
"titleProp": false,
}
`;

exports[`svgo async #loadConfig [async] should not load config with "runtimeConfig: false 1`] = `
Object {
"dimensions": true,
"expandProps": "end",
"h2xConfig": null,
"icon": true,
"native": false,
"noSemi": true,
"prettier": true,
"prettierConfig": null,
"ref": false,
"replaceAttrValues": Array [
Array [
"#063855",
"currentColor",
],
],
"runtimeConfig": true,
"svgProps": null,
"svgo": true,
"svgoConfig": null,
"template": null,
"titleProp": false,
"useRuntimeConfig": false,
}
`;

exports[`svgo async #loadConfig [async] should use default config without state.filePath 1`] = `
Object {
"dimensions": false,
"expandProps": "end",
"h2xConfig": null,
"icon": false,
"native": false,
"prettier": true,
"prettierConfig": null,
"ref": false,
"replaceAttrValues": null,
"runtimeConfig": true,
"svgProps": null,
"svgo": true,
"svgoConfig": null,
"template": null,
"titleProp": false,
}
`;

exports[`svgo async #loadConfig [async] should work with custom config path 1`] = `
Object {
"dimensions": true,
"expandProps": "end",
"h2xConfig": null,
"icon": true,
"native": false,
"noSemi": true,
"prettier": true,
"prettierConfig": null,
"ref": false,
"replaceAttrValues": Array [
Array [
"#063855",
"currentColor",
],
],
"runtimeConfig": true,
"svgProps": null,
"svgo": true,
"svgoConfig": null,
"template": null,
"titleProp": false,
}
`;

exports[`svgo sync #loadConfig [sync] should load config using filePath 1`] = `
Object {
"dimensions": true,
"expandProps": "end",
"h2xConfig": null,
"icon": true,
"native": false,
"noSemi": true,
"prettier": true,
"prettierConfig": null,
"ref": false,
"replaceAttrValues": Array [
Array [
"#063855",
"currentColor",
],
],
"runtimeConfig": true,
"svgProps": null,
"svgo": true,
"svgoConfig": null,
"template": null,
"titleProp": false,
}
`;

exports[`svgo sync #loadConfig [sync] should not load config with "runtimeConfig: false 1`] = `
Object {
"dimensions": true,
"expandProps": "end",
"h2xConfig": null,
"icon": true,
"native": false,
"noSemi": true,
"prettier": true,
"prettierConfig": null,
"ref": false,
"replaceAttrValues": Array [
Array [
"#063855",
"currentColor",
],
],
"runtimeConfig": true,
"svgProps": null,
"svgo": true,
"svgoConfig": null,
"template": null,
"titleProp": false,
"useRuntimeConfig": false,
}
`;

exports[`svgo sync #loadConfig [sync] should use default config without state.filePath 1`] = `
Object {
"dimensions": false,
"expandProps": "end",
"h2xConfig": null,
"icon": false,
"native": false,
"prettier": true,
"prettierConfig": null,
"ref": false,
"replaceAttrValues": null,
"runtimeConfig": true,
"svgProps": null,
"svgo": true,
"svgoConfig": null,
"template": null,
"titleProp": false,
}
`;

exports[`svgo sync #loadConfig [sync] should work with custom config path 1`] = `
Object {
"dimensions": true,
"expandProps": "end",
"h2xConfig": null,
"icon": true,
"native": false,
"noSemi": true,
"prettier": true,
"prettierConfig": null,
"ref": false,
"replaceAttrValues": Array [
Array [
"#063855",
"currentColor",
],
],
"runtimeConfig": true,
"svgProps": null,
"svgo": true,
"svgoConfig": null,
"template": null,
"titleProp": false,
}
`;
22 changes: 22 additions & 0 deletions packages/core/src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,37 @@ export async function resolveConfig(searchFrom, configFile) {
return result ? result.config : null
}

resolveConfig.sync = (searchFrom, configFile) => {
if (configFile == null) {
const result = explorer.searchSync(searchFrom)
return result ? result.config : null
}
const result = explorer.loadSync(configFile)
return result ? result.config : null
}

export async function resolveConfigFile(filePath) {
const result = await explorer.search(filePath)
return result ? result.filepath : null
}

resolveConfigFile.sync = filePath => {
const result = explorer.searchSync(filePath)
return result ? result.filepath : null
}

export async function loadConfig({ configFile, ...baseConfig }, state = {}) {
const rcConfig =
state.filePath && baseConfig.runtimeConfig !== false
? await resolveConfig(state.filePath, configFile)
: {}
return { ...DEFAULT_CONFIG, ...rcConfig, ...baseConfig }
}

loadConfig.sync = ({ configFile, ...baseConfig }, state = {}) => {
const rcConfig =
state.filePath && baseConfig.runtimeConfig !== false
? resolveConfig.sync(state.filePath, configFile)
: {}
return { ...DEFAULT_CONFIG, ...rcConfig, ...baseConfig }
}
Loading

0 comments on commit 169eb2f

Please sign in to comment.