From 456a054f9eda8d5e3cddc9746acd308149e6d286 Mon Sep 17 00:00:00 2001 From: Shish Date: Mon, 28 Nov 2022 17:53:09 +0000 Subject: [PATCH] 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"` --- .../src/index.test.ts | 2 +- .../babel-plugin-transform-svg-component/src/types.ts | 1 + .../babel-plugin-transform-svg-component/src/variables.ts | 6 +++++- packages/core/src/config.ts | 1 + website/pages/docs/options.mdx | 8 +++++--- 5 files changed, 13 insertions(+), 5 deletions(-) diff --git a/packages/babel-plugin-transform-svg-component/src/index.test.ts b/packages/babel-plugin-transform-svg-component/src/index.test.ts index 7272ff0d..c68c455e 100644 --- a/packages/babel-plugin-transform-svg-component/src/index.test.ts +++ b/packages/babel-plugin-transform-svg-component/src/index.test.ts @@ -345,7 +345,7 @@ describe('plugin', () => { jsxRuntimeImport: { source: 'preact' }, }) }).toThrow( - 'Specify either "namespace" or "specifiers" in "jsxRuntimeImport" option', + 'Specify "namespace", "defaultSpecifier", or "specifiers" in "jsxRuntimeImport" option', ) }) }) diff --git a/packages/babel-plugin-transform-svg-component/src/types.ts b/packages/babel-plugin-transform-svg-component/src/types.ts index c171a5bb..c4caad7f 100644 --- a/packages/babel-plugin-transform-svg-component/src/types.ts +++ b/packages/babel-plugin-transform-svg-component/src/types.ts @@ -29,6 +29,7 @@ interface State { export interface JSXRuntimeImport { source: string namespace?: string + defaultSpecifier?: string specifiers?: string[] } diff --git a/packages/babel-plugin-transform-svg-component/src/variables.ts b/packages/babel-plugin-transform-svg-component/src/variables.ts index 64a1481b..e597b390 100644 --- a/packages/babel-plugin-transform-svg-component/src/variables.ts +++ b/packages/babel-plugin-transform-svg-component/src/variables.ts @@ -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)) diff --git a/packages/core/src/config.ts b/packages/core/src/config.ts index c2006fe9..98f21560 100644 --- a/packages/core/src/config.ts +++ b/packages/core/src/config.ts @@ -36,6 +36,7 @@ export interface Config { source: string namespace?: string specifiers?: string[] + defaultSpecifier?: string } // CLI only diff --git a/website/pages/docs/options.mdx b/website/pages/docs/options.mdx index bb7c2da7..13a10771 100644 --- a/website/pages/docs/options.mdx +++ b/website/pages/docs/options.mdx @@ -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