0

I'm working on a monorepo and I'm trying to create a package that would be a React ui-kit library. For now, I've been trying to export a custom button component and building the ESM et CJS modules with Vite.

However when I'm building it with Vite, I get this error :

Module './components/button.js' was resolved to '[...]/libs/ui/src/components/button.tsx', but '--jsx' is not set.

Here is the component

// src/components/button.tsx
import React, { ComponentProps } from 'react'
import "../index.css";
 
export function Button(props: Omit<ComponentProps<'button'>, 'className'>) {
  return <button className="bg-blue-500 text-white px-4 py-2 rounded" {...props} />;
}
// src/main.ts
export { Button } from './components/button.js';

And here is the Vite config as well as the tsconfig:

// vite.config.ts
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
// export type definition files
import dts from 'vite-plugin-dts'

import path from 'path'

import packageJson from './package.json'

export default defineConfig({
  root: '/',
  plugins: [react(), dts({ include: 'src' })],
  build: {
    // build package as a library
    lib: {
      entry: path.resolve(__dirname, 'src/index.ts'),
      // es = ES module
      formats: ['es', 'cjs']
    },
    rollupOptions: {
      // avoid bundling react and react-dom
      // as they will be already required and bundled by the user app
      // using this application
      external: ['react', 'react-dom', 'react/jsx-runtime', 'tailwindcss']
    },
    sourcemap: true
  }
})
// tsconfig.json
{
  "extends": "../../tsconfig.json",
  "include": ["src/**/*.ts", "src/**/*.tsx"],
  "compilerOptions": {
    "module": "NodeNext",
    "moduleResolution": "NodeNext",
    "jsx": "react-jsx",
    "resolveJsonModule": true
  },
  "exclude": ["node_modules/"]
}

If I do not set react/jsx-runtime as external dependency, I get another error:

error during build:
[vite]: Rollup failed to resolve import "react/jsx-runtime" from "[...]/libs/ui/src/components/button.tsx".

I do not know what's wrong in my config ?

1 Answer 1

0

Apparently this is because of the root option in the vite.config.ts. This is not necessary anyway, so I removed it and it works now.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.