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 960845c commit 7ee9efd
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 4 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 @@ -36,6 +36,7 @@ export interface Config {
source: string
namespace?: string
specifiers?: string[]
defaultSpecifier?: string
}

// CLI only
Expand Down
8 changes: 5 additions & 3 deletions website/pages/docs/options.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,11 @@ 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

Expand Down

0 comments on commit 7ee9efd

Please sign in to comment.