Issue with React Setup in Figma Plugin
I’m working on creating a Figma plugin with React and TypeScript. When I launch the plugin in Figma, I get this error in the console:
Uncaught TypeError: Cannot read properties of undefined (reading 'createElement')
Here’s my current setup:
package.json dependencies:
{
"react": "^18.3.0",
"react-dom": "^18.3.0"
}
Main entry file (main.tsx):
import React from 'react';
import { createRoot } from 'react-dom/client';
import MainComponent from './ui/MainComponent';
import './assets/styles.css';
document.addEventListener('DOMContentLoaded', function () {
const rootElement = document.getElementById('app');
if (rootElement) {
const reactRoot = createRoot(rootElement);
reactRoot.render(<MainComponent />);
} else {
console.error("App container not found");
}
});
My build config:
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const InlineChunkHtmlPlugin = require('inline-chunk-html-plugin');
module.exports = {
entry: {
ui: './src/main.tsx',
plugin: './plugin.ts',
},
output: {
path: path.resolve(__dirname, 'build'),
filename: '[name].js',
},
resolve: {
extensions: ['.ts', '.tsx', '.js'],
},
module: {
rules: [
{
test: /\.tsx?$/,
use: 'ts-loader',
exclude: /node_modules/,
},
{
test: /\.css$/,
use: ['style-loader', 'css-loader'],
},
],
},
plugins: [
new HtmlWebpackPlugin({
template: './public/index.html',
filename: 'index.html',
chunks: ['ui'],
}),
new InlineChunkHtmlPlugin(HtmlWebpackPlugin, [/ui/]),
],
};
The build completes without errors, but React seems to not be available when the plugin runs. What could be causing this createElement issue?