What parcel , babel and polyfilling do

Thakrashot
4 min readFeb 24, 2024

--

Parcel, Babel, and polyfilling are tools and concepts used in web development to enhance and optimize the development and deployment of web applications.

Parcel:

Parcel is a web application bundler. It simplifies the process of bundling and building web applications by providing a zero-configuration setup. You can use Parcel to bundle your HTML, CSS, JavaScript, and other assets without needing extensive configuration files. Parcel automatically handles tasks like code splitting, hot module replacement, and optimization. It’s a developer-friendly tool that aims to make the build process more straightforward.

Babel:

Babel is a JavaScript compiler that allows developers to write code in the latest ECMAScript (JavaScript) syntax while ensuring compatibility with older browsers and environments. Babel transpiles modern JavaScript code into an older version that is widely supported. This process is essential because not all browsers or environments support the latest ECMAScript features. Babel plugins and presets can be configured to target specific environments or include/exclude certain features.

Polyfilling:

Polyfilling is the practice of adding code to fill in the gaps for features that are not natively supported in a specific browser. Polyfills provide a way to emulate functionality that is part of newer JavaScript or web standards in environments that lack native support. Browsers evolve, and new features are introduced, so Polyfills help ensure a consistent experience for users across different browsers.

For example, if a newer JavaScript method or API is not supported in an older browser, a Polyfills can be added to the code to provide the missing functionality. Polyfills are especially crucial when working with modern JavaScript features that might not be supported in all browsers, allowing developers to use the latest language features while maintaining broad compatibility.

In summary, Parcel simplifies the bundling and building process, Babel allows developers to write modern JavaScript code that can run in various environments, and polyfills fill in the gaps for features not supported natively by browsers, ensuring a more consistent user experience. These tools are commonly used together in modern web development workflows.

Using in a simple Project

1 - First make index.html and other files like style.css, script.js in a folder and link to index.html.

2 - Then type in terminal with same directory npm init and fill the below points

npm init

The npm init command is used to initialize a new npm package in your project directory.

it create a package.json file

  • This file acts as the central manifest for your project, storing essential information like:
  • Project name and description
  • Author information
  • Version number
  • License
  • Dependencies (libraries your project relies on)
  • Scripts (custom commands you can run using npm run)

Install Babel Packages:

3 —Then type in terminal

npm install - save-dev @babel/core @babel/cli @babel/preset-env

4- Create a Babel Configuration File (.babelrc) in root directory:

write this code in it

{ "presets": ["@babel/preset-env"] }

5-Run Babel:

In your terminal, run the following command to transpile your code using Babel:

npx babel index.js --out-file output.js

This will create a transpiled version of your JavaScript in the root directory.

6- Polyfilling

Install Babel Polyfill:

npm install --save @babel/polyfill

7-Import Babel Polyfill in Your JavaScript File (index.js):

import "@babel/polyfill";
const message = "Hello, Polyfill!";
console.log(message);
// Your other code here…

8-Update Babel Configuration (.babelrc):

Ensure your .babelrc includes the "@babel/preset-env" preset and "useBuiltIns": "entry" option:

{
"presets": [
[
"@babel/preset-env",
{
"useBuiltIns": "entry"
}
]
]
}

9-Run Babel with Polyfill:

Run the Babel command as before:

npx babel index.js --out-file output.js

This will include the necessary Polyfills in the transpiled code, making your modern JavaScript compatible with older browsers.

These examples provide a basic setup. In real-world scenarios, you may need additional configurations based on your project requirements. Also, consider using package managers like npm or yarn to manage dependencies and scripts more efficiently.

You’ll need to include the resulting output.js file in your HTML file.

10-Install Parcel:

npm install -g parcel-bundler

If you prefer not to install Parcel globally, you can install it locally in your project by running:

npm install --save-dev parcel-bundler

11-Then, you can use the local installation in your project scripts in json file:

"scripts": {
"start": "parcel index.html"
}

12 — And run it with in terminal:

npm start

it create a folder dist in your directory . just upload this folder to your host server . you're done.

Build and Serve with Parcel

When you run Parcel to bundle and serve your project, it will take care of the dependencies and include the transpiled JavaScript file automatically. You don’t need to manually include the transpiled file in your HTML when using Parcel

During development, you might use Parcel to serve your project and automatically handle the bundling and transpilation.

For production, you run the Babel command to transpile your JavaScript source files and output them to a dist folder.

Upload the entire contents of the dist folder (including the transpiled JavaScript file) to your server.

Ensure that your HTML file references the correct paths for the production files.

--

--

No responses yet