Skip to content

Commit e52cdce

Browse files
committed
feat(config): improve runtime config
- Add "runtimeConfig" option to enable or disable runtime configuration BREAKING CHANGE: - Runtime configuration is always loaded (even with Node API `convert`) - In CLI, "--config" is now "--config-file"; this new option can be used everywhere Closes #192
1 parent b687c9e commit e52cdce

16 files changed

Lines changed: 242 additions & 30 deletions

File tree

README.md

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ npx @svgr/cli
2424
## Supporting SVGR
2525

2626
SVGR is an MIT-licensed open source project. It's an independent project with ongoing development made possible thanks to the support of these awesome [backers](/BACKERS.md). If you'd like to join them, please consider:
27+
2728
- [Become a backer or sponsor on OpenCollective](https://opencollective.com/svgr).
2829

2930
### Gold Sponsors
@@ -93,12 +94,13 @@ powerful and configurable HTML transpiler. It uses AST (like
9394
## Command line usage
9495

9596
```
96-
Usage: npx @svgr/cli [options] <file|directory>
97+
Usage: svgr [options] <file|directory>
9798
9899
Options:
99100
100101
-V, --version output the version number
101-
--config <file> specify the path of the svgr config
102+
--config-file <file> specify the path of the svgr config
103+
--no-runtime-config disable runtime config (.svgrrc, .svgo.yml, .prettierrc)
102104
-d, --out-dir <dirname> output files into a directory
103105
--ext <ext> specify a custom file extension (default: "js")
104106
--filename-case <case> specify filename case (pascal, kebab, camel) (default: "pascal")
@@ -265,6 +267,22 @@ Even if it is not recommended, you can also use `svgoConfig` option to specify y
265267
SVGR ships with a handful of customizable options, usable in both the CLI and
266268
API.
267269

270+
### Config file
271+
272+
Specify a custom config file.
273+
274+
| Default | CLI Override | API Override |
275+
| ------- | --------------- | ---------------------- |
276+
| `null` | `--config-file` | `configFile: <string>` |
277+
278+
### Runtime config
279+
280+
Disable runtime config (`.svgrrc`, `.svgo.yml`, `.prettierrc`).
281+
282+
| Default | CLI Override | API Override |
283+
| ------- | --------------- | ---------------------- |
284+
| `null` | `--config-file` | `configFile: <string>` |
285+
268286
### File extension
269287

270288
Specify a custom extension for generated files.

packages/cli/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ Usage: svgr [options] <file|directory>
1818
Options:
1919
2020
-V, --version output the version number
21-
--config <file> specify the path of the svgr config
21+
--config-file <file> specify the path of the svgr config
22+
--no-runtime-config disable runtime config (.svgrrc, .svgo.yml, .prettierrc)
2223
-d, --out-dir <dirname> output files into a directory
2324
--ext <ext> specify a custom file extension (default: "js")
2425
--filename-case <case> specify filename case (pascal, kebab, camel) (default: "pascal")

packages/cli/src/dirCommand.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ async function dirCommand(
5151
relative = rename(relative, ext, filenameCase)
5252

5353
const dest = path.join(program.outDir, relative)
54-
const code = await convertFile(src, options, { filePath: dest })
54+
const code = await convertFile(src, options)
5555

5656
outputFileSync(dest, code)
5757
console.info(`${src} -> ${dest}`)

packages/cli/src/index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,11 @@ const parseConfig = name => arg => {
3838
program
3939
.version(pkg.version)
4040
.usage('[options] <file|directory>')
41-
.option('--config <file>', 'specify the path of the svgr config')
41+
.option('--config-file <file>', 'specify the path of the svgr config')
42+
.option(
43+
'--no-runtime-config',
44+
'disable runtime config (.svgrrc, .svgo.yml, .prettierrc)',
45+
)
4246
.option('-d, --out-dir <dirname>', 'output files into a directory')
4347
.option('--ext <ext>', 'specify a custom file extension (default: "js")')
4448
.option(

packages/cli/src/index.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const babelNode = path.join(__dirname, '../../../node_modules/.bin/babel-node')
1212

1313
describe('cli', () => {
1414
const cli = async args => {
15-
const { stdout } = await exec(`${babelNode} ${svgr} ${args}`)
15+
const { stdout } = await exec(`${babelNode} -- ${svgr} ${args}`)
1616
return stdout
1717
}
1818

@@ -173,7 +173,7 @@ describe('cli', () => {
173173
'should not override config with cli defaults',
174174
async () => {
175175
const result = await cli(
176-
'__fixtures__/simple/file.svg --config=__fixtures__/overrides.config.js',
176+
'__fixtures__/simple/file.svg --config-file=__fixtures__/overrides.config.js',
177177
)
178178
expect(result).toMatchSnapshot()
179179
},

packages/cli/src/util.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@
22
import fs from 'fs'
33
import chalk from 'chalk'
44
import util from 'util'
5-
import convert, { resolveConfig } from '@svgr/core'
5+
import convert from '@svgr/core'
66

77
export const readFile = util.promisify(fs.readFile)
88
export const stat = util.promisify(fs.stat)
99

10-
export async function convertFile(filePath, { config, ...options } = {}) {
10+
export async function convertFile(filePath, config = {}) {
1111
const code = await readFile(filePath, 'utf-8')
12-
const rcConfig = await resolveConfig(filePath, config)
13-
return convert(code, { ...rcConfig, ...options }, { filePath })
12+
return convert(code, config, { filePath })
1413
}
1514

1615
export function exitError(error) {

packages/cli/src/util.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ describe('util', () => {
1414
it('should support a custom config path', async () => {
1515
const file = path.join(FIXTURES, 'simple/file.svg')
1616
const result = await convertFile(file, {
17-
config: '__fixtures__/withSvgrRc/.svgrrc',
17+
configFile: '__fixtures__/withSvgrRc/.svgrrc',
1818
})
1919
expect(result).toMatchSnapshot()
2020
})
Lines changed: 13 additions & 0 deletions
Loading

packages/core/src/config.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export const DEFAULT_CONFIG = {
1616
svgoConfig: null,
1717
template: null,
1818
titleProp: false,
19+
runtimeConfig: true,
1920
}
2021

2122
const explorer = cosmiconfig('svgr', {
@@ -37,3 +38,11 @@ export async function resolveConfigFile(filePath) {
3738
const result = await explorer.search(filePath)
3839
return result ? result.filepath : null
3940
}
41+
42+
export async function loadConfig({ configFile, ...baseConfig }, state = {}) {
43+
const rcConfig =
44+
state.filePath && baseConfig.runtimeConfig !== false
45+
? await resolveConfig(state.filePath, configFile)
46+
: {}
47+
return { ...DEFAULT_CONFIG, ...rcConfig, ...baseConfig }
48+
}

packages/core/src/config.test.js

Lines changed: 127 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import path from 'path'
2-
import { resolveConfig, resolveConfigFile } from './config'
2+
import { resolveConfig, resolveConfigFile, loadConfig } from './config'
33

44
describe('config', () => {
55
describe('#resolveConfig', () => {
@@ -33,4 +33,130 @@ describe('config', () => {
3333
expect(config).toMatch(/__fixtures__(\/|\\)svgr(\/|\\)\.svgrrc$/)
3434
})
3535
})
36+
37+
describe('#loadConfig', () => {
38+
it('should use default config without state.filePath', async () => {
39+
const config = await loadConfig({ dimensions: false })
40+
expect(config).toMatchInlineSnapshot(`
41+
Object {
42+
"dimensions": false,
43+
"expandProps": true,
44+
"h2xConfig": null,
45+
"icon": false,
46+
"native": false,
47+
"prettier": true,
48+
"prettierConfig": null,
49+
"ref": false,
50+
"replaceAttrValues": null,
51+
"runtimeConfig": true,
52+
"svgAttributes": null,
53+
"svgProps": null,
54+
"svgo": true,
55+
"svgoConfig": null,
56+
"template": null,
57+
"titleProp": false,
58+
}
59+
`)
60+
})
61+
62+
it('should load config using filePath', async () => {
63+
const config = await loadConfig(
64+
{},
65+
{ filePath: path.join(__dirname, '__fixtures__/svgr/icon.svg') },
66+
)
67+
expect(config).toMatchInlineSnapshot(`
68+
Object {
69+
"dimensions": true,
70+
"expandProps": true,
71+
"h2xConfig": null,
72+
"icon": true,
73+
"native": false,
74+
"noSemi": true,
75+
"prettier": true,
76+
"prettierConfig": null,
77+
"ref": false,
78+
"replaceAttrValues": Array [
79+
Array [
80+
"#063855",
81+
"currentColor",
82+
],
83+
],
84+
"runtimeConfig": true,
85+
"svgAttributes": null,
86+
"svgProps": null,
87+
"svgo": true,
88+
"svgoConfig": null,
89+
"template": null,
90+
"titleProp": false,
91+
}
92+
`)
93+
})
94+
95+
it('should not load config with "runtimeConfig: false', async () => {
96+
const config = await loadConfig(
97+
{ useRuntimeConfig: false },
98+
{ filePath: path.join(__dirname, '__fixtures__/svgr/icon.svg') },
99+
)
100+
expect(config).toMatchInlineSnapshot(`
101+
Object {
102+
"dimensions": true,
103+
"expandProps": true,
104+
"h2xConfig": null,
105+
"icon": true,
106+
"native": false,
107+
"noSemi": true,
108+
"prettier": true,
109+
"prettierConfig": null,
110+
"ref": false,
111+
"replaceAttrValues": Array [
112+
Array [
113+
"#063855",
114+
"currentColor",
115+
],
116+
],
117+
"runtimeConfig": true,
118+
"svgAttributes": null,
119+
"svgProps": null,
120+
"svgo": true,
121+
"svgoConfig": null,
122+
"template": null,
123+
"titleProp": false,
124+
"useRuntimeConfig": false,
125+
}
126+
`)
127+
})
128+
129+
it('should work with custom config path', async () => {
130+
const config = await loadConfig(
131+
{ configFile: path.join(__dirname, '__fixtures__/svgr/.svgrrc') },
132+
{ filePath: __dirname },
133+
)
134+
expect(config).toMatchInlineSnapshot(`
135+
Object {
136+
"dimensions": true,
137+
"expandProps": true,
138+
"h2xConfig": null,
139+
"icon": true,
140+
"native": false,
141+
"noSemi": true,
142+
"prettier": true,
143+
"prettierConfig": null,
144+
"ref": false,
145+
"replaceAttrValues": Array [
146+
Array [
147+
"#063855",
148+
"currentColor",
149+
],
150+
],
151+
"runtimeConfig": true,
152+
"svgAttributes": null,
153+
"svgProps": null,
154+
"svgo": true,
155+
"svgoConfig": null,
156+
"template": null,
157+
"titleProp": false,
158+
}
159+
`)
160+
})
161+
})
36162
})

0 commit comments

Comments
 (0)