Skip to content

Commit 86bb86f

Browse files
authoredMar 14, 2023
feat: allow specifying jsxRuntimeImport in config
* Allow specifying `jsxRuntimeImport` in config, see #801 I need to have a config which isn't currently supported * Allow specifying a single specifier for custom imports, see #801 `hyperapp-jsx-pragma` uses the default export so we need to `import h from "hyperapp-jsx-pragma"`, not `import { h } from "hyperapp-jsx-pragma"`
1 parent 331c92c commit 86bb86f

File tree

7 files changed

+44
-5
lines changed

7 files changed

+44
-5
lines changed
 

‎packages/babel-plugin-transform-svg-component/src/__snapshots__/index.test.ts.snap

+12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`plugin javascript #jsxRuntime allows to specify a custom "classic" jsxRuntime using "defaultSpecifier" 1`] = `
4+
"import h from "hyperapp-jsx-pragma";
5+
const SvgComponent = () => <svg><g /></svg>;
6+
export default SvgComponent;"
7+
`;
8+
39
exports[`plugin javascript #jsxRuntime allows to specify a custom "classic" jsxRuntime using "namespace" 1`] = `
410
"import * as Preact from "preact";
511
const SvgComponent = () => <svg><g /></svg>;
@@ -226,6 +232,12 @@ const Memo = memo(ForwardRef);
226232
export default Memo;"
227233
`;
228234
235+
exports[`plugin typescript #jsxRuntime allows to specify a custom "classic" jsxRuntime using "defaultSpecifier" 1`] = `
236+
"import h from "hyperapp-jsx-pragma";
237+
const SvgComponent = () => <svg><g /></svg>;
238+
export default SvgComponent;"
239+
`;
240+
229241
exports[`plugin typescript #jsxRuntime allows to specify a custom "classic" jsxRuntime using "namespace" 1`] = `
230242
"import * as Preact from "preact";
231243
const SvgComponent = () => <svg><g /></svg>;

‎packages/babel-plugin-transform-svg-component/src/index.test.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -338,14 +338,22 @@ describe('plugin', () => {
338338
expect(code).toMatchSnapshot()
339339
})
340340

341+
it('allows to specify a custom "classic" jsxRuntime using "defaultSpecifier"', () => {
342+
const { code } = testPlugin(language)('<svg><g /></svg>', {
343+
jsxRuntime: 'classic',
344+
jsxRuntimeImport: { defaultSpecifier: 'h', source: 'hyperapp-jsx-pragma' },
345+
})
346+
expect(code).toMatchSnapshot()
347+
})
348+
341349
it('throws with invalid configuration', () => {
342350
expect(() => {
343351
testPlugin(language)('<svg><g /></svg>', {
344352
jsxRuntime: 'classic',
345353
jsxRuntimeImport: { source: 'preact' },
346354
})
347355
}).toThrow(
348-
'Specify either "namespace" or "specifiers" in "jsxRuntimeImport" option',
356+
'Specify "namespace", "defaultSpecifier", or "specifiers" in "jsxRuntimeImport" option',
349357
)
350358
})
351359
})

‎packages/babel-plugin-transform-svg-component/src/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ interface State {
2929
export interface JSXRuntimeImport {
3030
source: string
3131
namespace?: string
32+
defaultSpecifier?: string
3233
specifiers?: string[]
3334
}
3435

‎packages/babel-plugin-transform-svg-component/src/variables.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,17 @@ const getJsxRuntimeImport = (cfg: JSXRuntimeImport) => {
6969
const specifiers = (() => {
7070
if (cfg.namespace)
7171
return [t.importNamespaceSpecifier(t.identifier(cfg.namespace))]
72+
if (cfg.defaultSpecifier) {
73+
const identifier = t.identifier(cfg.defaultSpecifier)
74+
return [t.importDefaultSpecifier(identifier)]
75+
}
7276
if (cfg.specifiers)
7377
return cfg.specifiers.map((specifier) => {
7478
const identifier = t.identifier(specifier)
7579
return t.importSpecifier(identifier, identifier)
7680
})
7781
throw new Error(
78-
`Specify either "namespace" or "specifiers" in "jsxRuntimeImport" option`,
82+
`Specify "namespace", "defaultSpecifier", or "specifiers" in "jsxRuntimeImport" option`,
7983
)
8084
})()
8185
return t.importDeclaration(specifiers, t.stringLiteral(cfg.source))

‎packages/core/src/config.ts

+6
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ export interface Config {
3232
exportType?: 'named' | 'default'
3333
namedExport?: string
3434
jsxRuntime?: 'classic' | 'classic-preact' | 'automatic'
35+
jsxRuntimeImport?: {
36+
source: string
37+
namespace?: string
38+
specifiers?: string[]
39+
defaultSpecifier?: string
40+
}
3541

3642
// CLI only
3743
index?: boolean

‎packages/plugin-jsx/src/index.ts

+6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ import svgrBabelPreset, {
77
import type { Plugin, Config } from '@svgr/core'
88

99
const getJsxRuntimeOptions = (config: Config): Partial<SvgrPresetOptions> => {
10+
if (config.jsxRuntimeImport) {
11+
return {
12+
importSource: config.jsxRuntimeImport.source,
13+
jsxRuntimeImport: config.jsxRuntimeImport,
14+
}
15+
}
1016
switch (config.jsxRuntime) {
1117
case null:
1218
case undefined:

‎website/pages/docs/options.mdx

+5-3
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,11 @@ Specify a custom JSX runtime source to use. Allows to customize the import added
4646

4747
Example: `jsxRuntimeImport: { source: 'preact', specifiers: ['h'] }` for "classic-preact" equivalent.
4848

49-
| Default | CLI Override | API Override |
50-
| ------- | ------------ | ------------------------------------------------------------ |
51-
| `null` | - | `jsxRuntimeImport: { source: string, specifiers: string[] }` |
49+
To use the default import instead of a list of names, you can use `defaultSpecifier`, for example to use svgr with `hyperapp-jsx-pragma`, you can specify `jsxRuntimeImport: { source: 'hyperapp-jsx-pragma', defaultSpecifier: 'h' }` to get an end result of `import h from "hyperapp-jsx-pragma";`
50+
51+
| Default | CLI Override | API Override |
52+
| ------- | ------------ | -------------------------------------------------------------------------------------- |
53+
| `null` | - | `jsxRuntimeImport: { source: string, specifiers: string[], defaultSpecifier: string }` |
5254

5355
## Icon
5456

1 commit comments

Comments
 (1)

vercel[bot] commented on Mar 14, 2023

@vercel[bot]

Successfully deployed to the following URLs:

svgr – ./

svgr-git-main-gregberge.vercel.app
api.react-svgr.com
svgr-gregberge.vercel.app

Please sign in to comment.