Skip to content

Commit

Permalink
Allow specifying a single specifier for custom imports, see gregberge…
Browse files Browse the repository at this point in the history
…#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"`
  • Loading branch information
shish committed Nov 29, 2022
1 parent 1118a6e commit c46a256
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 8 deletions.
1 change: 1 addition & 0 deletions packages/babel-plugin-transform-svg-component/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ interface State {
export interface JSXRuntimeImport {
source: string
namespace?: string
defaultSpecifier?: string
specifiers?: string[]
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,17 @@ const getJsxRuntimeImport = (cfg: JSXRuntimeImport) => {
const specifiers = (() => {
if (cfg.namespace)
return [t.importNamespaceSpecifier(t.identifier(cfg.namespace))]
if (cfg.defaultSpecifier) {
const identifier = t.identifier(cfg.defaultSpecifier)
return [t.importDefaultSpecifier(identifier)]
}
if (cfg.specifiers)
return cfg.specifiers.map((specifier) => {
const identifier = t.identifier(specifier)
return t.importSpecifier(identifier, identifier)
})
throw new Error(
`Specify either "namespace" or "specifiers" in "jsxRuntimeImport" option`,
`Specify "namespace", "defaultSpecifier", or "specifiers" in "jsxRuntimeImport" option`,
)
})()
return t.importDeclaration(specifiers, t.stringLiteral(cfg.source))
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export interface Config {
jsxRuntimeImport?: {
source: string,
namespace?: string,
defaultSpecifier?: string,
specifiers?: string[]
}

Expand Down
16 changes: 9 additions & 7 deletions website/pages/docs/options.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ Specify a JSX runtime to use.
- "automatic": do not add anything
- "classic-preact": adds `import { h } from 'preact'` on the top of file

| Default | CLI Override | API Override |
| --------- | --------------- | ---------------------- | ----------- | ----------------- |
| Default | CLI Override | API Override |
| --------- | --------------- | -------------------------------------------------------- |
| `classic` | `--jsx-runtime` | `jsxRuntime: 'classic' | 'automatic' | 'classic-preact'` |

## JSX runtime import source
Expand All @@ -46,16 +46,18 @@ Specify a custom JSX runtime source to use. Allows to customize the import added

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

| Default | CLI Override | API Override |
| ------- | ------------ | ------------------------------------------------------------ |
| `null` | - | `jsxRuntimeImport: { source: string, specifiers: string[] }` |
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";`

| Default | CLI Override | API Override |
| ------- | ------------ | -------------------------------------------------------------------------------------- |
| `null` | - | `jsxRuntimeImport: { source: string, specifiers: string[], defaultSpecifier: string }` |

## Icon

Replace SVG `width` and `height` by a custom value. If value is omitted, it uses `1em` in order to make SVG size inherits from text size.

| Default | CLI Override | API Override |
| ------- | ---------------- | -------------- | ------ | ------- |
| Default | CLI Override | API Override |
| ------- | ---------------- | --------------------------------- |
| `false` | `--icon [value]` | `icon: boolean | string | number` |

> If you use `--icon` without any argument, be careful of separating files from arguments using `--`, ex: `svgr --icon -- assets/svg`
Expand Down

0 comments on commit c46a256

Please sign in to comment.