Notes

Notes on transpiling and compile targets

Edit on GitHub

Web Development
2 minutes
  • Transpiling to older compile targets results in bulkier code with lots of polyfills. 6x more code in some cases. Supporting IE11 will give you bulky oversized code that runs really slow.
  • ES2017 has 95% browser support. Transpiling to ES2017 is recommended by Google experts (because it is cost effective)
  • ES2021 has 70% browser support
  • Node 12.8 supports ES2019+ natively. The exports field implies ES2019 code because it is only available in Node 12.8. (main can still be used as an ES5 fallback)
  • You can check serve different JS files based on type="module" and type="nomodule" attributes added to your <script> tags

There are two ways of supporting both modern and older browsers:

  • Compile application twice for different targets (one bundle for modern, one bundle for legacy)
  • Compile application once for modern ES2017 (all code incl. deps will be ES2017 at this point) and then transpile that down for legacy targets

Generating multiple bundles

Generating bundles for different targets is usually simple with bundlers. For example, rollup supports bundling from several unrelated inputs to several outputs.

Rollup

 1// rollup.config.js (building more than one bundle)
 2
 3export default [
 4  {
 5    input: 'main-a.js',
 6    output: {
 7      file: 'dist/bundle-a.js',
 8      format: 'cjs',
 9    },
10  },
11  {
12    input: 'main-b.js',
13    output: [
14      {
15        file: 'dist/bundle-b1.js',
16        format: 'cjs',
17      },
18      {
19        file: 'dist/bundle-b2.js',
20        format: 'es',
21      },
22    ],
23  },
24]

Typescript

1tsc -p /path/to/first/tsconfig.json --outFile controllers.js
2tsc -p /path/to/second/tsconfig.json --outFile plugins.js

or if outFile or outDir is mentioned inside the tsconfig.json

1tsc -p /path/to/first/tsconfig.json
2tsc -p /path/to/second/tsconfig.json

Typescript and Rollup

Use tsc to transpile the Typescript into an intermediate build folder, then I use rollup to bundle that transpiled code and output it to the desired destination.