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 09a537c
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`plugin javascript #jsxRuntime allows to specify a custom "classic" jsxRuntime using "defaultSpecifier" 1`] = `
"import h from "hyperapp-jsx-pragma";
const SvgComponent = () => <svg><g /></svg>;
export default SvgComponent;"
`;

exports[`plugin javascript #jsxRuntime allows to specify a custom "classic" jsxRuntime using "namespace" 1`] = `
"import * as Preact from "preact";
const SvgComponent = () => <svg><g /></svg>;
Expand Down Expand Up @@ -226,6 +232,12 @@ const Memo = memo(ForwardRef);
export default Memo;"
`;
exports[`plugin typescript #jsxRuntime allows to specify a custom "classic" jsxRuntime using "defaultSpecifier" 1`] = `
"import h from "hyperapp-jsx-pragma";
const SvgComponent = () => <svg><g /></svg>;
export default SvgComponent;"
`;
exports[`plugin typescript #jsxRuntime allows to specify a custom "classic" jsxRuntime using "namespace" 1`] = `
"import * as Preact from "preact";
const SvgComponent = () => <svg><g /></svg>;
Expand Down
10 changes: 9 additions & 1 deletion packages/babel-plugin-transform-svg-component/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -338,14 +338,22 @@ describe('plugin', () => {
expect(code).toMatchSnapshot()
})

it('allows to specify a custom "classic" jsxRuntime using "defaultSpecifier"', () => {
const { code } = testPlugin(language)('<svg><g /></svg>', {
jsxRuntime: 'classic',
jsxRuntimeImport: { defaultSpecifier: 'h', source: 'hyperapp-jsx-pragma' },
})
expect(code).toMatchSnapshot()
})

it('throws with invalid configuration', () => {
expect(() => {
testPlugin(language)('<svg><g /></svg>', {
jsxRuntime: 'classic',
jsxRuntimeImport: { source: 'preact' },
})
}).toThrow(
'Specify either "namespace" or "specifiers" in "jsxRuntimeImport" option',
'Specify "namespace", "defaultSpecifier", or "specifiers" in "jsxRuntimeImport" option',
)
})
})
Expand Down
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 09a537c

Please sign in to comment.