liveBook · Manning

Transpilation

As explained in Web Components in Action

Transpilation is the source-to-source transformation of code from one readable programming language or language version into another.

Your Web Component may work in modern browsers yet fail in IE11 before it even runs: IE11 does not understand newer JavaScript features such as classes and arrow functions. Transpilation turns newer JavaScript into older JavaScript that the browser can read. It can also turn a language such as TypeScript or CoffeeScript into JavaScript. Unlike compilation to a lower-level output, the result remains readable source code. section 12.5 section 12.4

Transpilation is not the whole compatibility build. Rollup bundles imported modules; Babel transforms the JavaScript. For a component that needs IE11 support, bundle it first, then run Babel on the bundle to produce a separate older-browser file. If you only target browsers that support your component natively, you may not need that extra build. section 12.5 section 12.2

A small Babel setup adds the transformation without making it part of the component shipped to users:

  • Install @babel/core, @babel/cli, and @babel/preset-env as development dependencies. section 12.5
  • Configure the @babel/preset-env preset, then give the Babel command a bundled input file and a separate output filename. section 12.5
  • Add npm scripts for each component and combine the Rollup and Babel scripts into one build command. section 12.5
Listing 12.12. A Babel transpile step for each component
"build-slider-ie": "./node_modules/.bin/babel
  chapter12and13/components/slider/slider.js --out-file
  chapter12and13/components/slider/slider.build.js",
"build-coordpicker-ie": "./node_modules/.bin/babel
  chapter12and13/components/coordpicker/coordpicker.js --out-file
  chapter12and13/components/coordpicker/coordpicker.build.js",
"build-colorpicker-ie": "./node_modules/.bin/babel
  chapter12and13/components/colorpicker/colorpicker.js --out-file
  chapter12and13/components/colorpicker/colorpicker.build.js",

These npm scripts show the repeatable step: each bundled component file becomes a separate .build.js file for the older-browser build. section 12.5

Do not expect Babel to supply every missing browser feature. IE11 still needs a Custom Elements polyfill, and the component example also needs a CSS custom-property ponyfill. Likewise, a separate Edge gradient fix uses RGBA colors rather than relying on transpilation. Check the browser behavior after building, not just whether the build finishes. section 2.7 section 12.5 section 12.2

How Web Components in Action covers it

How deep it goes
Defines source-to-source transformation and walks through Babel installation, configuration, and npm scripts; does not examine Babel’s transformation internals.
Examples
Uses npm scripts that transform bundled slider, coordinate-picker, and color-picker files into separate older-browser builds.
What it assumes
Assumes existing Web Component files, but introduces transpilation by comparing it with compilation and showing the required npm packages.
What you can do after
Apply it via Babel and npm scripts to produce older-browser JavaScript alongside Rollup-bundled component files.
How it connects
Distinguishes transpilation from compilation and module bundling, and pairs it with polyfills for browser compatibility.

Also explained in

Compare how 4 books explain transpilation →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Web Components in Action. The section links point to the parts of the book it draws on.