Introduction
Understanding how a JavaScript project truly functions has always intrigued me. While many of us are proficient in writing code, not everyone can clearly explain what happens under the hood once the code is written.
In this article, we’ll explore how everything works behind the scenes.
Over 98.7% of websites use JavaScript.
Source: Radixweb
Overview
When you build your app, a lot of tools are cooking to provide a beautiful and performent app :
⚙️ The transpiler : It job is to ensure that your code works across different environments! Think of it as making sure your grandma’s old PC can handle your app without a hitch!
🦸🏻♂️ The bundler : Like a superhero, it optimizes your code, making it more efficient to load and execute. It bundles all your assets into a single file, reducing the number of requests your app has to make.
🏭 Task Runners : These are the Dark Knights of your development process. They automate routine tasks such as file minification (compressing code), compilation (converting SCSS to CSS), and image optimization. They save us from having to handle these tedious tasks manually.
These tools work together to create a streamlined development process, allowing developers to focus on building features rather than dealing with compatibility or performance issues.
Deep dive : How they work
When you build a project, the task runners orchestrate the process, coordinating the transpiler and bundler to ensure a streamlined and efficient build.
A transpiler, such as Babel, takes source code in modern Javascript (ES6+) or Typescript and transform it to a compatible Javascript for older browsers (ES5).
The bundler like Webpack takes all the files (Javascript, CSS, Images, etc...) and compiles them into smaller files or even a single file.
During this process, Webpack also optimizes the code by minimizing and compressing it, which reduces load times and improves performance.
But my project can work without these tools ?
Yes and No, you project can work if there is not a lot of complexity.
If you start to add complexity, the absence of these tools can lead to significant challenges :
Code Compatibility: Without a transpiler, your JavaScript code might not run correctly on older browsers, limiting your audience.
Performance: Without a bundler, your application may suffer from slower load times due to unoptimized code delivery.
Efficiency: Without a task runner, you would need to handle repetitive tasks manually, leading to inefficiencies and potential errors in the build process.
Example Workflow
1 - Write Modern JavaScript Code:
You write ES6+ or TypeScript in your source files.
2 - Run Build Command:
You execute a build command (npm run build) that triggers the transpiler.
-- Starting build process --
3 - Transpile Code:
The transpiler (e.g., Babel) converts your ES6+ code into ES5.
4 - Bundle Files:
The bundler (e.g., Webpack) then takes the transpiled files and combines them into a single or few optimized files.
-- End of the build process --
5 - Deploy:
The final bundled files are ready to be deployed to the server.
Task runners are not part of the build process itself but orchestrate it. They automate and manage tasks like transpilation and bundling by integrating with tools such as Babel and Webpack. Essentially, task runners streamline and coordinate the entire build process, ensuring tasks are executed in the correct sequence.
Thank you for reading 🚀