question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItĀ collects links to all the places you might be looking at while hunting down a tough bug.

And, if youā€™re still stuck at the end, weā€™re happy to hop on a call to see how we can help out.

How to use a custom jsx pragma or get rid of import React?

See original GitHub issue

Question: How to use a custom jsx pragma? OR How to get rid of import * as React from 'react' at the output when i use a custom jsx processing library?

  1. I am using a custom library for jsx processing (so i donā€™t need React package to be installed).
  2. As a jsx transpiler, the only used plugin is: https://babeljs.io/docs/en/babel-plugin-transform-react-jsx/
  3. This babel plugin (2) works fine for regular .jsx files (auto-imports the ā€œCustomJsxLibraryā€ and so onā€¦)
  4. This babel plugin (2) works great with svgr , but there is an extra import * as React from 'react' remaining at the beginning of the svg component.

What iā€™ve got so farā€¦ Not a very convenient but working method is to use a custom template and delete ${imports} :

...

return typeScriptTpl.ast`
    ${interfaces}
    function ${componentName}(${props}) {
        return ${jsx};
    }
    ${exports}
`

...

Configuration from webpack.config.js:

...

test: /\.svg$/, 
use: [
    {
        loader: 'babel-loader',
        options: {
            presets: ['@babel/preset-env'],
            plugins: [
                [
                    '@babel/plugin-transform-react-jsx',
                    {
                        runtime: 'automatic',
                        importSource: 'CustomJsxLibrary'
                    }
                ],
            ]
        },
    },
    {
        loader: '@svgr/webpack',
        options: {
            babel: false,
            template: require(path.resolve(__dirname, './template.js')) // without the custom template doesn't work :'(
        },
    }
]

...

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:5
  • Comments:6

github_iconTop GitHub Comments

1reaction
vitharanagedonjani-i2ecommented, Feb 25, 2022

--jsx-runtime 'automatic' flag prevent import * as React from "react" being added šŸ˜ƒ

example: svgr --icon --typescript --ref --jsx-runtime 'automatic' -d src assets

1reaction
peter-moulandcommented, Mar 22, 2021

Iā€™ve done as you suggest šŸ¤¦


function defaultTemplate(
    { template },
     opts,
    { imports, interfaces, componentName, props, jsx, exports },
) {
    const plugins = ['jsx']
    if (opts.typescript) {
        plugins.push('typescript')
    }
    const typeScriptTpl = template.smart({ plugins })
    return typeScriptTpl.ast`
${interfaces}
function ${componentName}(${props}) {
  return ${jsx};
}
${exports}
`;
}

const jsData = await svgr(svgCode, { template:defaultTemplate }, { componentName })
Read more comments on GitHub >

github_iconTop Results From Across the Web

Custom elements in React using a custom JSX pragma
Example using a custom JSX pragma for enable better tooling for custom elements. This project was bootstrapped with Create React App.
Read more >
Introducing the New JSX Transform ā€“ React Blog
Remove all unused React imports as a result of upgrading to the new JSX transform. Change all default React imports (i.e. import React...
Read more >
Using a custom jsx function - ReScript Forum
It's imported from Theme UI like so: import { jsx } from 'theme-ui' . So basically, I need to figure out how to...
Read more >
JSX Pragma - Theme UI
JSX Pragma. Theme UI uses custom JSX functions and JSX import source comments to allow you to style elements with values from your...
Read more >
What is JSX pragma? - Gatsby
What is JSX pragma? Ā· Using a custom JSX pragma. There are two ways to specify a custom function (and therefore replace React.createElement...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found