课程: Micro Front-End Architecture with React
Configuring Webpack for micro-frontend applications - React.js教程
课程: Micro Front-End Architecture with React
Configuring Webpack for micro-frontend applications
- [Instructor] Webpack makes it possible to separate and share code across Micro Frontends, which is a good foundation for creating scalable, independently deployable applications. In the coming lessons, you will learn how to build a Micro Frontend application using Module Federation from Webpack to build a bookstore application from scratch. This lesson will place you ahead of the aha moment when you get to the ends on practice section. To set up Webpack configuration that defines essential properties like entry, output, and loaders. Here's an example of a simple configuration file for a Micro Frontend component. In this setup, there are different elements, such as the entry, which defines the main file for the Micro Frontend. The entry is pointing to an index.js file in the src file. The src file is importing a file which is bootstrap. And in the bootstrap.js, we have created a Reacts components that is setting up and is taking the app.js file. The app.js file just has a simple writeup to show that a remote application with a color of red. Also, this file also specifies the output file and the publicPath set to auto for flexibility in loading assets. We've also specifies the loaders to file transformation such as Babel for JavaScript and CSS loaders. Module Federation allows different applications to share modules at one time. You need to add the ModuleFederationPlugin to your Webpack configuration. In this sample code set up, the following are worth to take note of. The unique identifier for this Micro Frontend application is the name which is remote, and this can be called any name. The file name specifies the file that contains the metadata about shared modules, whereby the exposes is listing the modules available for every other applications to consume. While the shared is managing dependencies to avoid the duplication, ensuring only one instance of libraries is loaded. Once you've configured your Micro Frontend to expose components, you need to set up the host application, consume them. Here is an example of how you can configure the host webpack file to recognize a remote Micro Frontend. The setup allows the host application to assess modules from the remote micro application by specifying its URL, making the expose component available for use. After configuring module federation, the host application can load and use component from the remote micro frontend. Here is an example of how to import the expose component dynamically. In your app.js, using React lazy and suspense helps you to load the component on demand showing a fallback while waiting for the module to load. During development, host module replacement, which is HMR, is a feature that you can tap into from webpack. It allows Webpack to update modules in real-time without a full reload, making it easier to test changes in your Micro Frontend without restarting the development server. To prepare your Micro Frontend for production, you need to enable webpacks optimization features. This V is define a bundle size and improves performance. First thing to do is to change the mode from development to production. The first thing to do is to change your development to production, which enables production optimizations by default. Also, we need to introduce minimize, optimization, minimize equals true, oh, sorry, this is supposed to be colon. Awesome. Also, you can introduce split chunks. Chunks or. This split code into separate chunks improving the performance by loading what is needed. All right, we are still in production. We are still in development, so let's fall back to our default mode production. Let's try and run this app and see if it works. Remember what you have in the host app, it says, "Host application, which is running on port 3000." And the remote app, also ask what remote app, which is running on port 3001, so let's run. Firstly, my directory into the remote folder. Change directory into remote and run NPM run dev, or let's confirm. Start, so NPM run starts, rather NPM starts. Okay, this is successful. And then we can open in browser. Don't forget we need this URL in the remote app, in the host app, so as to ensure that it is working. So this is the remote app working standalone. Let's copy this and we'll be back to our code. In the host's configuration, update the URL of the remote to the new URL. This is okay, and, let's change our directory, CD and then, okay, let's create a new directory. CD hosts. Okay, NPM start. So open in browser. Now, this is our Micro Frontend working. Automatically, both the host application, and the remote application are communicating. Let's update our remote app. We've saved. Let's check the hosts. Automatically, it's been updated, and the remote also is updated. Configuration webpack for a Micro Frontend application involves several important steps. Set up basic webpack configuration with entry, output, and loaders. Configure Module Federation to expose and consume components across Micro Frontends. Use hot module replacement for a similar development experience. Optimize for production with code splitting and magnification. Webpack configuration is the key to building and managing Micro Frontend applications using Module Federation.