It sounds like you're encountering an issue where the production build process is not handling your React dependencies correctly. Here's a step-by-step approach to troubleshoot and potentially solve the problem:
-
Check for Multiple Instances of React: Ensure that there are no multiple instances of React being loaded. This can happen if your package inside
vendoris bundling its own version of React. Since you've mentioned that React and ReactDOM are listed as peer dependencies, make sure that they are not being bundled with your package. -
Verify Vite's External Configuration: In your
vite.config.js, you've mentioned usingrollupOptions.excludeto exclude React from the build. However, the correct property to use with Vite and Rollup should beexternal. Here's how you can specify external dependencies in your Vite config:// vite.config.js export default { // ... other config options build: { rollupOptions: { external: ['react', 'react-dom'], }, }, }; -
Check the Library's Build Process: If the library you're using from the
vendordirectory has its own build process, ensure that it's correctly set up to treat React and ReactDOM as external dependencies. This will prevent them from being included in the library's bundle. -
Inspect the Error Message: The error message "TypeError: fe.current is null (useState)" suggests that there might be an issue with the way hooks are being used. This could be a symptom of having multiple React instances. Make sure that the library's components are using hooks correctly and that they are not relying on any global state that might not be initialized in a production build.
-
Clean Install Dependencies: Sometimes, issues can arise from corrupted or outdated node modules. Try deleting your
node_modulesfolder and thepackage-lock.jsonfile (oryarn.lockif you're using Yarn), and then runnpm install(oryarn install) to do a clean install of your dependencies. -
Check for Environment-Specific Code: Since the issue only appears in the production build, check if there's any code that behaves differently based on the environment (e.g., using
process.env.NODE_ENV). Ensure that such code is correctly configured for production. -
Review the Library's Package.json: Make sure that the library's
package.jsonis correctly set up withpeerDependenciesand that it doesn't includereactandreact-domindependenciesordevDependencieswhich might cause them to be bundled. -
Test with a Minimal Setup: Create a minimal setup that only includes the library and tries to build it with Vite. This can help isolate the issue and determine if the problem is with the library or the way it's integrated into your project.
Here's a minimal example of how your vite.config.js might look:
// vite.config.js
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
export default defineConfig({
plugins: [react()],
resolve: {
alias: {
// your aliases
},
},
build: {
rollupOptions: {
external: ['react', 'react-dom'],
},
},
});
If none of these steps resolve the issue, consider providing more specific error logs or a reproduction repository, as this can greatly help in diagnosing the problem.