Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't use tsup to generate .d.ts files with svgr #570

Open
laozhu opened this issue Feb 18, 2022 · 4 comments
Open

Can't use tsup to generate .d.ts files with svgr #570

laozhu opened this issue Feb 18, 2022 · 4 comments

Comments

@laozhu
Copy link

laozhu commented Feb 18, 2022

First I have a svg file

<svg
  xmlns="http://www.w3.org/2000/svg"
  viewBox="0 0 24 24"
>
  <path d="M17 11h-2V9h2m-4 2h-2V9h2m-4 2H7V9h2m11-7H4a2 2 0 0 0-2 2v18l4-4h14a2 2 0 0 0 2-2V4a2 2 0 0 0-2-2Z"/>
</svg>

Then I use svgr to import it as react component

export { ReactComponent as Sms } from './icons/sms.svg';

I use vite and vite-plugin-svgr to bundle the icon libraries (success)

> @easynm/[email protected] build-lib /Users/Ritchie/Developer/@easynm/cloud/components/icons
> cross-env LIBRARY=1 vite build

vite v2.8.3 building for production...
✓ 2 modules transformed.
dist/icons.es.js   1.08 KiB / gzip: 0.53 KiB
dist/icons.es.js.map 0.56 KiB
dist/icons.umd.js   1.25 KiB / gzip: 0.68 KiB
dist/icons.umd.js.map 0.51 KiB

Try to generate .d.ts files with below cli (failed)

> @easynm/[email protected] build-dts /Users/Ritchie/Developer/@easynm/cloud/components/icons
> tsup src/index.ts --dts-only -d dist

CLI Building entry: src/index.ts
CLI Using tsconfig: tsconfig.json
CLI tsup v5.11.13
DTS Build start
DTS Build error
Error: 'ReactComponent' is not exported by src/icons/sms.svg, imported by src/index.ts
    at error (/Users/Ritchie/Developer/@easynm/cloud/node_modules/.pnpm/[email protected]/node_modules/rollup/dist/shared/rollup.js:160:30)
    at Module.error (/Users/Ritchie/Developer/@easynm/cloud/node_modules/.pnpm/[email protected]/node_modules/rollup/dist/shared/rollup.js:12438:16)
    at Module.getVariableForExportName (/Users/Ritchie/Developer/@easynm/cloud/node_modules/.pnpm/[email protected]/node_modules/rollup/dist/shared/rollup.js:12603:29)
    at Module.includeAllExports (/Users/Ritchie/Developer/@easynm/cloud/node_modules/.pnpm/[email protected]/node_modules/rollup/dist/shared/rollup.js:12674:37)
    at Graph.includeStatements (/Users/Ritchie/Developer/@easynm/cloud/node_modules/.pnpm/[email protected]/node_modules/rollup/dist/shared/rollup.js:23022:36)
    at Graph.build (/Users/Ritchie/Developer/@easynm/cloud/node_modules/.pnpm/[email protected]/node_modules/rollup/dist/shared/rollup.js:22937:14)
    at async rollupInternal (/Users/Ritchie/Developer/@easynm/cloud/node_modules/.pnpm/[email protected]/node_modules/rollup/dist/shared/rollup.js:23552:9)
    at async runRollup (/Users/Ritchie/Developer/@easynm/cloud/node_modules/.pnpm/[email protected][email protected]/node_modules/tsup/dist/rollup.js:8795:20)
    at async startRollup (/Users/Ritchie/Developer/@easynm/cloud/node_modules/.pnpm/[email protected][email protected]/node_modules/tsup/dist/rollup.js:8833:5)
 ELIFECYCLE  Command failed with exit code 1.

If I want to use tsup for dts only, can I have a method to avoid this error?

Upvote & Fund

  • We're using Polar.sh so you can upvote and help fund this issue.
  • We receive the funding once the issue is completed & confirmed by you.
  • Thank you in advance for helping prioritize & fund our backlog.
Fund with Polar
@thedaviddias
Copy link

@laozhu Did you find a solution for this?

@laozhu
Copy link
Author

laozhu commented Sep 1, 2022

@thedaviddias I ended up writing the type definitions manually.

declare module '@scope-name/icons' {
  type ReactComponent = React.FC<
    React.SVGProps<SVGSVGElement> & {
      title?: string | undefined;
    }
  >;

  const Sms: ReactComponent;
}

@dhikatb
Copy link

dhikatb commented Mar 28, 2023

tsup.config.ts

import svgrPlugin from 'esbuild-plugin-svgr';
import { defineConfig } from 'tsup';

export default defineConfig({
    ...,
    esbuildPlugins: [svgrPlugin({ template })],
});

function template(variables, { tpl }) {
    return tpl`
      ${variables.imports};
      ${variables.interfaces};
      const ${variables.componentName} = (${variables.props}) => (
        ${variables.jsx}
      ); 
      ${variables.exports};
      export const ReactComponent = ${variables.componentName};
    `;
}

it works for me

@richenyadav001
Copy link

SVG was compiled successfully, by using

import svgrPlugin from 'esbuild-plugin-svgr';
import { defineConfig } from 'tsup';

export default defineConfig({
    esbuildPlugins: [svgrPlugin()],
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants