vitejs / vite

Next generation frontend tooling. It's fast!
http://vite.dev
MIT License
68.09k stars 6.13k forks source link

When we do yarn link from local react component library getting (Uncaught TypeError: Cannot read properties of null (reading 'useContext') at Object.useContext) #18430

Open vishalmarneni opened 8 hours ago

vishalmarneni commented 8 hours ago

Describe the bug

When I publish a new version of my React component library and pull the changes into my application, everything works fine. However, when I link the library locally to the application, I encounter the following error: Uncaught TypeError: Cannot read properties of null (reading 'useContext') `import react from "@vitejs/plugin-react";

Both the library and the application are using the same version of React, and React is listed as a peer dependency in the library's [package.json]

Vite Configuration: Below is the Vite configuration used in the React component library: import { glob } from "glob"; import { fileURLToPath } from "node:url"; import { extname, relative, resolve } from "path"; import { defineConfig } from "vite"; import dts from "vite-plugin-dts"; import { libInjectCss } from "vite-plugin-lib-inject-css";

// https://vitejs.dev/config/ export default defineConfig({ plugins: [ react(), libInjectCss(), dts({ include: ["lib"], rollupTypes: true, }), ], build: { copyPublicDir: false, lib: { entry: resolve(__dirname, "lib/main.ts"), formats: ["es"], }, rollupOptions: { external: ["react", "react-dom", "react/jsx-runtime"], input: Object.fromEntries( // https://rollupjs.org/configuration-options/#input glob .sync("lib/*/.{ts,tsx}", { ignore: ["lib/*/.d.ts"], }) .map((file) => [ // 1. The name of the entry point // lib/nested/foo.js becomes nested/foo relative("lib", file.slice(0, file.length - extname(file).length)), // 2. The absolute path to the entry file // lib/nested/foo.ts becomes /project/lib/nested/foo.ts fileURLToPath(new URL(file, import.meta.url)), ]), ), output: { assetFileNames: "assets/[name][extname]", entryFileNames: "[name].js", globals: { react: "React", "react-dom": "ReactDOM", }, }, }, }, }); ` Please investigate why the error occurs when the library is linked locally and provide a solution to ensure that the library works correctly in both published and linked scenarios.

image

Reproduction

https://clients-ui-toolkit.netlify.app/

Steps to reproduce

yarn build cd dist yarn link

System Info

no errors in the system

Used Package Manager

npm

Logs

No response

Validations

hi-ogawa commented 4 hours ago

Please provide a full reproduction step. I'm not sure where yarn is supposed to run.

github-actions[bot] commented 4 hours ago

Hello @vishalmarneni. Please provide a minimal reproduction using a GitHub repository or StackBlitz. Issues marked with needs reproduction will be closed if they have no activity within 3 days.