-
-
Notifications
You must be signed in to change notification settings - Fork 631
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
[Question/Bug]: Unable to properly build React application as a consumable UMD library #9616
Comments
+1 |
This doesn't look like a bug to me. The way to correctly handle your requirements depends on whether you have control of the code of the application where you want to use your library. If you have control of the consumer application you can make it load your application by using Module Federation and then you don't need to build it as a UMD. You can simply put React and ReactDOM as shared dependencies in the Module Federation configuration. If, however, you don't have control of the consumer app or you want to be able to inject your app inside any web application, whether it uses React or doesn't, then you have two choices:
|
Are the options not recommended? |
I couldn't use it in my project tests either. There seems to be an issue with rspack generating UMD. My project itself is based on rspack+react. I packaged some of the components into an SDK using rspack, setting react and react-dom as externals. However, when I put this SDK into a newly created rspack project and import it, it also failed to run either dev or production mode. However, there are no issues when rspack outputs in the ESM format. I had tested it, and after outputting in the ESM format, my SDK works seamlessly in rspack+react, vite+react, and umi+react projects. my rspack config const config = {
// ...
output: {
asyncChunks: false,
chunkFormat: 'module',
filename: 'index.js',
library: {
type: 'module',
},
},
experiments: {
outputModule: true,
},
externalsType: 'module',
externalsPresets: {
web: true,
},
externals: [
{
"react": "react",
"react-dom": "react-dom",
},
],
} |
I'll try it later, thanks for your help. |
I think I've done it, check out the latest commit in the project and run it locally to understand and see if you agree with the final result. |
@jonasmarco this looks right and it worked when I tested it |
Hi guys
I'm trying to build a React application as a UMD library that can be consumed by other React applications. The goal is to generate a single JavaScript file that can be imported via script tag into any React project.
Expected behavior:
Questions:
Any guidance would be greatly appreciated!
Project here: https://github.com/jonasmarco/rspack-starter
The text was updated successfully, but these errors were encountered: