Oops! Something went wrong while submitting the form.
We use cookies to improve your browsing experience on our website, to show you personalised content and to analize our website traffic. By browsing our website, you consent to our use of cookies. Read privacy policy.
Hands up if you've ever built a React project with Create-React-App (CRA)—and that's all of us, isn't it? Now, how about we pull back the curtain and see what's actually going on behind the scenes? Buckle up, it's time to understand what CRA really is and explore the wild, untamed world of creating a React project without it. Sounds exciting, huh?
What is CRA?
CRA—Create React App (https://create-react-app.dev/)—is a command line utility provided by Facebook for creating react apps with preconfigured setup. CRA provides an abstraction layer over the nitty-gritty details of configuring tools like Babel and Webpack, allowing us to focus on writing code. Apart from this, it basically comes with everything preconfigured, and developers don’t need to worry about anything but code.
That's all well and good, but why do we need to learn about manual configuration? At some point in your career, you'll likely have to adjust webpack configurations. And if that’s not a convincing reason, how about satisfying your curiosity? 🙂
“At its core, webpack is a static module bundler for modern JavaScript applications.”
But what does that actually mean? Let’s break it down:
static:It refers to the static assets (HTML, CSS, JS, images) on our application.
module:It refers to a piece of code in one of our files. In a large application, it’s not usually possible to write everything in a single file, so we have multiple modules piled up together.
bundler:It is a tool (which is webpack in our case), that bundles up everything we have used in our project and converts it to native, browser understandable JS, CSS, HTML (static assets).
So, in essence, webpack takes our application's static assets (like JavaScript modules, CSS files, and more) and bundles them together, resolving dependencies and optimizing the final output.
Webpack is preconfigured in our Create-React-App (CRA), and for most use cases, we don't need to adjust it. You'll find that many tutorials begin a React project with CRA. However, to truly understand webpack and its functionalities, we need to configure it ourselves. In this guide, we'll attempt to do just that.
Let’s break this whole process into multiple steps:
In a nutshell, some of the reasons we use Babel are:
JavaScript ECMAScript Compatibility:
Babel allows developers to use the latest ECMAScript (ES) features in their code, even if the browser or Node.js environment doesn't yet support them. This is achieved through the process of transpiling, where Babel converts modern JavaScript code (ES6 and beyond) into a version that is compatible with a wider range of browsers and environments.
JSX Transformation:
JSX (JavaScript XML) is a syntax extension for JavaScript used with React. Babel is required to transform JSX syntax into plain JavaScript, as browsers do not understand JSX directly. This transformation is necessary for React components to be properly rendered in the browser.
Module System Transformation:
Babel helps in transforming the module system used in JavaScript. It can convert code written using the ES6 module syntax (import and export) into the CommonJS or AMD syntax that browsers and older environments understand.
Polyfilling:
Babel can include polyfills for features not present in the target environment. This ensures your application can use newer language features or APIs even if they are not supported natively.
Browser Compatibility:
Different browsers have varying levels of support for JavaScript features. Babel helps address these compatibility issues by allowing developers to write code using the latest features and then automatically transforming it to a version that works across different browsers.
The html-webpack-plugin is a popular webpack plugin that simplifies the process of creating an HTML file to serve your bundled JavaScript files. It automatically injects the bundled script(s) into the HTML file, saving you from having to manually update the script tags every time your bundle changes. To put it in perspective, if you don’t have this plugin, you won’t see your React index file injected into the HTML file.
Step 8: Configure Babel
Create a .babelrc file in the project root and add the following configuration:
Let's go through each section of the provided webpack.config.js file and explain what each keyword means:
const path = require('path');
This line imports the Node.js path module, which provides utilities for working with file and directory paths. Our webpack configuration ensures that file paths are specified correctly and consistently across different operating systems.
This line imports the HtmlWebpackPlugin module. This webpack plugin simplifies the process of creating an HTML file to include the bundled JavaScript files. It's a convenient way of automatically generating an HTML file that includes the correct script tags for our React application.
module.exports = { ... };
This line exports a JavaScript object, which contains the configuration for webpack. It specifies how webpack should bundle and process your code.
entry: './src/index.js',
This configuration tells webpack the entry point of your application, which is the main JavaScript file where the bundling process begins. In this case, it's ./src/index.js.
This configuration specifies where the bundled JavaScript file should be output: path is the directory, and filename is the name of the output file. In this case, it will be placed in the dist directory with the name bundle.js.
module: { rules: [ ... ], },
This section defines rules for how webpack should process different types of files. In this case, it specifies a rule for JavaScript and JSX files (those ending with .js or .jsx). The babel-loader is used to transpile these files using Babel, excluding files in the node_modules directory.
plugins: [ new HtmlWebpackPlugin({ template: 'public/index.html', }), ],
This section includes an array of webpack plugins. In particular, it adds the HtmlWebpackPlugin, configured to use the public/index.html file as a template. This plugin will automatically generate an HTML file with the correct script tags for the bundled JavaScript.
This configuration is for the webpack development server. It specifies the base directory for serving static files (public in this case) and the port number (3000) on which the development server will run. The development server provides features like hot-reloading during development.
And there you have it! We've just scratched the surface of the wild world of webpack. But don't worry, this is just the opening act. Grab your gear, because in the upcoming articles, we're going to plunge into the deep end, exploring the advanced terrains of webpack. Stay tuned!
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
Exploring Marvels of Webpack : Ep 1 - React Project without CRA
Hands up if you've ever built a React project with Create-React-App (CRA)—and that's all of us, isn't it? Now, how about we pull back the curtain and see what's actually going on behind the scenes? Buckle up, it's time to understand what CRA really is and explore the wild, untamed world of creating a React project without it. Sounds exciting, huh?
What is CRA?
CRA—Create React App (https://create-react-app.dev/)—is a command line utility provided by Facebook for creating react apps with preconfigured setup. CRA provides an abstraction layer over the nitty-gritty details of configuring tools like Babel and Webpack, allowing us to focus on writing code. Apart from this, it basically comes with everything preconfigured, and developers don’t need to worry about anything but code.
That's all well and good, but why do we need to learn about manual configuration? At some point in your career, you'll likely have to adjust webpack configurations. And if that’s not a convincing reason, how about satisfying your curiosity? 🙂
“At its core, webpack is a static module bundler for modern JavaScript applications.”
But what does that actually mean? Let’s break it down:
static:It refers to the static assets (HTML, CSS, JS, images) on our application.
module:It refers to a piece of code in one of our files. In a large application, it’s not usually possible to write everything in a single file, so we have multiple modules piled up together.
bundler:It is a tool (which is webpack in our case), that bundles up everything we have used in our project and converts it to native, browser understandable JS, CSS, HTML (static assets).
So, in essence, webpack takes our application's static assets (like JavaScript modules, CSS files, and more) and bundles them together, resolving dependencies and optimizing the final output.
Webpack is preconfigured in our Create-React-App (CRA), and for most use cases, we don't need to adjust it. You'll find that many tutorials begin a React project with CRA. However, to truly understand webpack and its functionalities, we need to configure it ourselves. In this guide, we'll attempt to do just that.
Let’s break this whole process into multiple steps:
In a nutshell, some of the reasons we use Babel are:
JavaScript ECMAScript Compatibility:
Babel allows developers to use the latest ECMAScript (ES) features in their code, even if the browser or Node.js environment doesn't yet support them. This is achieved through the process of transpiling, where Babel converts modern JavaScript code (ES6 and beyond) into a version that is compatible with a wider range of browsers and environments.
JSX Transformation:
JSX (JavaScript XML) is a syntax extension for JavaScript used with React. Babel is required to transform JSX syntax into plain JavaScript, as browsers do not understand JSX directly. This transformation is necessary for React components to be properly rendered in the browser.
Module System Transformation:
Babel helps in transforming the module system used in JavaScript. It can convert code written using the ES6 module syntax (import and export) into the CommonJS or AMD syntax that browsers and older environments understand.
Polyfilling:
Babel can include polyfills for features not present in the target environment. This ensures your application can use newer language features or APIs even if they are not supported natively.
Browser Compatibility:
Different browsers have varying levels of support for JavaScript features. Babel helps address these compatibility issues by allowing developers to write code using the latest features and then automatically transforming it to a version that works across different browsers.
The html-webpack-plugin is a popular webpack plugin that simplifies the process of creating an HTML file to serve your bundled JavaScript files. It automatically injects the bundled script(s) into the HTML file, saving you from having to manually update the script tags every time your bundle changes. To put it in perspective, if you don’t have this plugin, you won’t see your React index file injected into the HTML file.
Step 8: Configure Babel
Create a .babelrc file in the project root and add the following configuration:
Let's go through each section of the provided webpack.config.js file and explain what each keyword means:
const path = require('path');
This line imports the Node.js path module, which provides utilities for working with file and directory paths. Our webpack configuration ensures that file paths are specified correctly and consistently across different operating systems.
This line imports the HtmlWebpackPlugin module. This webpack plugin simplifies the process of creating an HTML file to include the bundled JavaScript files. It's a convenient way of automatically generating an HTML file that includes the correct script tags for our React application.
module.exports = { ... };
This line exports a JavaScript object, which contains the configuration for webpack. It specifies how webpack should bundle and process your code.
entry: './src/index.js',
This configuration tells webpack the entry point of your application, which is the main JavaScript file where the bundling process begins. In this case, it's ./src/index.js.
This configuration specifies where the bundled JavaScript file should be output: path is the directory, and filename is the name of the output file. In this case, it will be placed in the dist directory with the name bundle.js.
module: { rules: [ ... ], },
This section defines rules for how webpack should process different types of files. In this case, it specifies a rule for JavaScript and JSX files (those ending with .js or .jsx). The babel-loader is used to transpile these files using Babel, excluding files in the node_modules directory.
plugins: [ new HtmlWebpackPlugin({ template: 'public/index.html', }), ],
This section includes an array of webpack plugins. In particular, it adds the HtmlWebpackPlugin, configured to use the public/index.html file as a template. This plugin will automatically generate an HTML file with the correct script tags for the bundled JavaScript.
This configuration is for the webpack development server. It specifies the base directory for serving static files (public in this case) and the port number (3000) on which the development server will run. The development server provides features like hot-reloading during development.
And there you have it! We've just scratched the surface of the wild world of webpack. But don't worry, this is just the opening act. Grab your gear, because in the upcoming articles, we're going to plunge into the deep end, exploring the advanced terrains of webpack. Stay tuned!
Velotio Technologies is an outsourced software product development partner for top technology startups and enterprises. We partner with companies to design, develop, and scale their products. Our work has been featured on TechCrunch, Product Hunt and more.
We have partnered with our customers to built 90+ transformational products in areas of edge computing, customer data platforms, exascale storage, cloud-native platforms, chatbots, clinical trials, healthcare and investment banking.
Since our founding in 2016, our team has completed more than 90 projects with 220+ employees across the following areas:
Building web/mobile applications
Architecting Cloud infrastructure and Data analytics platforms