-
-
Notifications
You must be signed in to change notification settings - Fork 228
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
Improve React compatibility #579
Comments
I am using npm link with tsup. Specially, I am using Luckily, found a solution: tsup.config.ts import fs from "fs";
import path from "path";
import { readPackageSync, NormalizeOptions } from "read-pkg";
import { defineConfig } from "tsup";
export default defineConfig((options) => {
const source = getPkgSource();
const inject: string[] = [];
const injectReactImport = detectAndInjectReactImport(source);
if (injectReactImport) inject.push(injectReactImport);
return {
...options,
entry: [source],
// react external https://github.com/vercel/turborepo/issues/360#issuecomment-1013885148
external: ["react"],
inject,
format: ["esm", "cjs", "iife"],
silent: !options.watch,
minify: !options.watch,
incremental: !options.watch,
dts: true,
sourcemap: true,
clean: false,
splitting: false,
};
});
export function getPkgSource(options?: NormalizeOptions | undefined) {
const pkg = readPackageSync(options);
if (typeof pkg.source !== "string") {
throw new Error(
"Please define a source https://github.com/developit/microbundle"
);
} else {
return pkg.source;
}
}
export function detectAndInjectReactImport(source: string): string | void {
// ignore non-react packages
if (!source.endsWith(".tsx")) {
return;
}
const file = `// NOTE: This file should not be edited
// see @configs/tsup for implementation.
// - https://esbuild.github.io/content-types/#auto-import-for-jsx
// - https://github.com/egoist/tsup/issues/390#issuecomment-933488738
import * as React from "react";
export { React };
`;
const relativefilePath = "./inject-react-import.js";
const absoluteFilePath = path.join(process.cwd(), relativefilePath);
if (!fs.existsSync(absoluteFilePath)) {
fs.writeFileSync(absoluteFilePath, file);
}
return relativefilePath;
} next.config.js /** @type {import('next').NextConfig} */
const nextConfig = {
reactStrictMode: true,
webpack: (config, { buildId, dev, isServer, defaultLoaders, webpack }) => {
config.resolve.alias["react/jsx-dev-runtime"] = require.resolve(
"react/jsx-dev-runtime.js"
);
config.resolve.alias["react/jsx-runtime"] = require.resolve(
"react/jsx-runtime.js"
);
config.resolve.alias["react"] = require.resolve("react");
// Important: return the modified config
return config;
},
};
module.exports = nextConfig; P.S. |
regarding this, I'd like to see a minimal repo so I can understand what's going on |
@egoist sent you an email subject |
For react 18, please note facebook/create-react-app#11769 and instead use: config.resolve.alias["react/jsx-dev-runtime"] = require.resolve(
"react/jsx-dev-runtime",
);
config.resolve.alias["react/jsx-runtime"] =
require.resolve("react/jsx-runtime");
config.resolve.alias["react"] = require.resolve("react"); |
Here
Here is a repo with this problem, any insight? thanks! 🙏🏼 |
Related #715 (comment) |
@jlarmstrongiv When researching zero-config ts+react bundlers, this issue title initially turned me away from tsup. However, it sounds like the originally may be largely solved? I'm not totally sure. Could the issue title be made more specific? Maybe you could add a small summary to the top? Maybe this issue could be closed altogether? |
This is a workaround for egoist/tsup#579
@devinrhode2 many of the issues have been fixed! React support has improved a lot 🎉 For example, tsup now supports For future readers, other issues I have workarounds for are: |
I have seen multiple issues and blog posts on how to use tsup with react. Often, I see:
--inject
option for react imports--external react
optionUnfortunately, solving the
ReferenceError: React is not defined
error only leads to theUnhandled Runtime Error
andError: Invalid hook call.
.I can’t seem to figure out how to use tsup with react. I’d love a workaround, though ideally tsup would support react without any configuration necessary.
Upvote & Fund
The text was updated successfully, but these errors were encountered: