https://github.com/AbePlays/react-webpack-mastery/commit/4883bb1d61acb1f2745ab2734622c1f13b1f40fc
Begin by copying the provided starter files located in the src
directory to get started with the project.
This application aims to provide a straightforward solution for users to input two numbers and calculate their sum. A simple drop-in css is added to make the content look good on most displays. The JavaScript code handles the addition of the two numbers. Finally, the CSS and JavaScript files are linked to the HTML document by including the appropriate file references within the head section. Open the HTML file, perform the addition operation, and voila! The application should function successfully.
The current setup described above has a few limitations and challenges. Let's discuss them and see how Webpack can improve the overall development process:
Webpack addresses these problems and provides several benefits:
Let's utilize webpack to bundle our files, eliminating the need to manually handle the import of files into our HTML. By doing so, we can focus more on developing our project.
To get started, we need to remove the script and CSS link tags from our HTML file since webpack will handle their inclusion for us. We achieve this by configuring webpack to generate the HTML and JavaScript files and establish the necessary connections between them automatically. This task is accomplished using webpack plugins since the default webpack configuration lacks this capability.
<aside> 📦 webpack plugins are powerful tools that extend the functionality of webpack by performing specific tasks during the bundling process. They enhance the build pipeline, optimize assets, provide additional features, and automate various tasks. Here are some key points about webpack plugins:
</aside>
The HtmlWebpackPlugin
is a powerful plugin for webpack that simplifies the process of generating HTML files and automatically injecting bundled JavaScript and CSS files into them. It eliminates the need to manually manage HTML file configurations and script tags. To use the HtmlWebpackPlugin, follow these steps:
Open the terminal or command prompt and navigate to the project directory. Run the following command to install the html-webpack-plugin
as a development dependency:
pnpm add -D html-webpack-plugin