DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. JavaScript
  4. Write Browser Compatible JavaScript Using Babel

Write Browser Compatible JavaScript Using Babel

We take a look at how Babel helps web application developers transpile code and deal with data using concepts such as parsing.

Robin Jangu user avatar by
Robin Jangu
·
Nov. 12, 18 · Analysis
Like (5)
Save
Tweet
Share
8.25K Views

Join the DZone community and get the full member experience.

Join For Free

Cross-browser compatibility can simply be summed up as a war between testers and developers versus the world wide web. Sometimes I feel that to achieve browser compatibility, you may need to sell your soul while performing a sacrificial ritual. Even then some API plugins won’t work!

I apologize for the mental imagery, but incompatibility issues get me riled up. All this was before I wasn’t familiar with Babel.

The Rise of JavaScript Compatibility Issues

With more and more developers contributing to the community, more and more framework versions came into existence. On the other hand, all browsers evolved with different philosophies. Not all JavaScript innovations were picked up by the browsers for native support. This in-turn resulted in a huge gap between the JavaScript language and its blanket support by all browsers. The older versions, however, were left in shambles, as they lacked support for JavaScript. Even the different versions of frameworks, when put together, added to the developer’s anxiety.

Leveraging Babel for Browser Compatibility

Babel is a transpiler and a brilliant one indeed. Everyday, developers face challenges when integrating the functionalities of different libraries. Different versions of JavaScript often lead to messy botched up jobs, which is a big reason for compromised efficiency.

Transpilers are often mistaken for compilers, but they are a bit different. They work on the source code and return a more sturdy and compatible source code. Developers can even customize Babel to suit them better.

How Does Babel Work?

Babel’s function is pretty straightforward and easy. It converts incompatible source code or a library's functionality into browser supported code, which your browser may find compatible. Babel does that in three simple steps.

Parsing

The first thing it does is to find the unsupported or incompatible parts in your code that were passed. It does that by using AST (Abstract Syntax Tree).

Abstract Syntax Tree

As the name itself indicates, it is an abstract representation of code. This works in similar manner to JSON. It creates a data structure of the program code which is given as input. With the representation, the manipulation of code becomes easier. It's a simple yet effective way to solve coding transformation ossies.

Transforming

The highlighted part of the source code is transformed into the compatible one. This is done with the help of the AST so as not to handicap any functionality of the program. You can even customize your own Babel plug-in.

Generation

You have now the transformed code, now you need additional plugins that will help you effectively use the transpiled code. Source map helps you in this, it works well with the V8 (Chrome and Node.js). It allows you to take the transpiled code to the address website url for it to parse.

Additional Babel Plugins Used for Compatibility

Babel alone is not enough for your JavaScript to be compatible. You need more tools for efficient cross browser compatibility. Webpack is another tool that helps in reducing the bundle size. Sourcemap, which we mentioned earlier, is also useful when bundling or catenating. Babel too has a lot of presets that are used to transpile JavaScript code for old browsers or solve problems with ECMAScript versioning. Polyfills in Babel include a custom regenerator runtime and core.js. It emulates an ES2015+ custom environment which is used in applications, meaning you can even use new built-ins like promise and weakmap along with a couple of new array functionalities. It automatically loads with Babel.

Babel (protocol) JavaScript

Published at DZone with permission of Robin Jangu. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Mr. Over, the Engineer [Comic]
  • ChatGPT: The Unexpected API Test Automation Help
  • Real-Time Stream Processing With Hazelcast and StreamNative
  • AWS Cloud Migration: Best Practices and Pitfalls to Avoid

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: