<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Gary Chew</title>
    <description>The latest articles on DEV Community by Gary Chew (@garylchew).</description>
    <link>https://dev.to/garylchew</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F442483%2Fbb0d80bc-34ae-4ec6-9367-49c928a23123.png</url>
      <title>DEV Community: Gary Chew</title>
      <link>https://dev.to/garylchew</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/garylchew"/>
    <language>en</language>
    <item>
      <title>Bringing Modern JavaScript to Libraries</title>
      <dc:creator>Gary Chew</dc:creator>
      <pubDate>Fri, 31 Jul 2020 18:45:16 +0000</pubDate>
      <link>https://dev.to/garylchew/bringing-modern-javascript-to-libraries-432c</link>
      <guid>https://dev.to/garylchew/bringing-modern-javascript-to-libraries-432c</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;tl;dr:&lt;/strong&gt; To bring modern JavaScript to our libraries, we should adopt a new &lt;code&gt;"browser2017"&lt;/code&gt; &lt;a href="https://nodejs.org/api/esm.html#esm_conditional_exports"&gt;conditional export&lt;/a&gt; key. The &lt;code&gt;"browser2017"&lt;/code&gt; key points to modern code that targets modern browsers, without the polyfills that bloat our bundles. This change requires support from bundlers and adoption from package authors.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Background
&lt;/h2&gt;

&lt;p&gt;Although modern browsers represent &lt;a href="https://caniuse.com/#feat=es6-module"&gt;over 90% of web traffic&lt;/a&gt;, many websites still transpile JavaScript to ES5 to support the &amp;lt;10% still stuck on older browsers like IE 11. To do this, most websites transpile their code and deliver polyfills which reimplement functionality already included in modern browsers. This produces larger bundles, which mean longer load and parse times for everyone.&lt;/p&gt;

&lt;h4&gt;
  
  
  The module/nomodule pattern
&lt;/h4&gt;

&lt;p&gt;In 2017, the &lt;a href="https://philipwalton.com/articles/deploying-es2015-code-in-production-today/"&gt;module/no module pattern&lt;/a&gt; began being recommended as a solution to this problem. Leveraging the fact that newer browsers support &lt;code&gt;&amp;lt;script type="module"&amp;gt;&lt;/code&gt; and older browsers don’t, we can do the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"module"&lt;/span&gt; &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"bundle.modern.js"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;nomodule&lt;/span&gt; &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"bundle.legacy.js"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This technique serves newer browsers the ES2017 &lt;code&gt;index.modern.js&lt;/code&gt; bundle and older browsers the polyfilled ES5 &lt;code&gt;index.legacy.js&lt;/code&gt; bundle. Though &lt;a href="https://github.com/johnstew/differential-serving#tests"&gt;there's a bit more complexity involved&lt;/a&gt;, it provides a mechanism for the majority of users to take advantage of ES2017 syntax without needing to rely on user agent detection or dynamic hosting.&lt;/p&gt;

&lt;h2&gt;
  
  
  Problem
&lt;/h2&gt;

&lt;p&gt;Though the module/nomodule pattern has introduced a mechanism to serve modern bundles, there’s still one glaring problem: &lt;strong&gt;virtually all our third-party dependencies (&lt;a href="https://medium.com/npm-inc/this-year-in-javascript-2018-in-review-and-npms-predictions-for-2019-3a3d7e5298ef#:~:text=97%25%20of%20the%20code%20in%20a%20modern%20web%20application%20comes%20from%20npm"&gt;and thus the majority of our JavaScript code&lt;/a&gt;) are stuck in ES5&lt;/strong&gt;.  We’ve left transpilation to package authors, but have established &lt;strong&gt;no mechanism for them to publish a modern version of their code.&lt;/strong&gt; Until we develop a standard for doing so, applications cannot truly reap the benefits of modern JavaScript. Conditional exports can provide that standard.  &lt;/p&gt;




&lt;h2&gt;
  
  
  Proposal: &lt;code&gt;"browser2017"&lt;/code&gt; Conditional Export
&lt;/h2&gt;

&lt;p&gt;In January 2020, Node v13.7.0 &lt;a href="https://nodejs.org/en/blog/release/v13.7.0/"&gt;announced&lt;/a&gt; official support for &lt;a href="https://github.com/nodejs/node/pull/29978"&gt;conditional exports&lt;/a&gt;. Conditional exports allow packages to specify per-environment entry points via an &lt;code&gt;"exports"&lt;/code&gt; package.json field. For example, a library might do the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight json-doc"&gt;&lt;code&gt;&lt;span class="c1"&gt;// my-library's package.json&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"my-library"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"main"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"./index-node.js"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"module"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"./index.production.mjs"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"browser"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"./index.production.js"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"exports"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"node"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"./index-node.js"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// Node.js build&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"development"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"./index.development.mjs"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// browser development build&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"default"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"./index.production.js"&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// browser ES5 production build&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;From here, based on which conditions get matched, a bundler or runtime like Node.js can select the most appropriate entry point to use when resolving the module.&lt;/p&gt;

&lt;p&gt;With conditional exports introduced, we finally have an opportunity for packages to offer a modern version of their code. To that end, we propose standardizing a new conditional exports key, &lt;code&gt;"browser2017"&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight json-doc"&gt;&lt;code&gt;&lt;span class="c1"&gt;// my-library's package.json&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"my-library"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"main"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"./index-node.js"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"module"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"./index.production.mjs"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"browser"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"./index.production.js"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"exports"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"node"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"./index-node.js"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// Node.js build&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"development"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"./index.development.mjs"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// browser development build&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"browser2017"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"./index.browser2017.mjs"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// browser modern production build&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"default"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"./index.production.js"&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// browser ES5 production build&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;"browser2017"&lt;/code&gt; key specifies an ES module entry point that uses JavaScript features available in browsers that support &lt;code&gt;&amp;lt;script type="module"&amp;gt;&lt;/code&gt;. That translates to Chrome 61+, Edge 16+, Firefox 60+ and Safari 10.1+.&lt;/p&gt;

&lt;p&gt;These targets pair cleanly with the module/nomodule pattern, eliminating polyfills for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  All ES2015 features (classes, arrow functions, maps, sets) excluding tail-call optimization&lt;/li&gt;
&lt;li&gt;  All ES2016 features (array.includes(), exponentiation operator)&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Most ES2017 features (async/await, Object.entries())&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: The &lt;code&gt;"browser2017"&lt;/code&gt; key only approximates ECMAScript 2017. This is because browsers implement the ECMAScript spec independently and arbitrarily. For example, no browsers other than Safari have implemented ES2015’s tail-call optimization and both Firefox and Safari have broken/partial implementations of ES2017’s shared memory and atomics features.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Naming the key &lt;code&gt;"browser2017"&lt;/code&gt; might seem confusing, since its semantics don’t map exactly to ECMAScript 2017 but rather serve as an alias to the browsers that support &lt;code&gt;&amp;lt;script type="module"&amp;gt;&lt;/code&gt;. However, the name clearly communicates to developers that it represents a certain syntax level, and that syntax level most closely corresponds to ES2017.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
  &lt;thead&gt;&lt;tr&gt;
&lt;th&gt;Feature Supported&lt;/th&gt;
&lt;th&gt;Chrome&lt;/th&gt;
&lt;th&gt;Edge&lt;/th&gt;
&lt;th&gt;Firefox&lt;/th&gt;
&lt;th&gt;Safari&lt;/th&gt;
&lt;/tr&gt;&lt;/thead&gt;
  &lt;tr&gt;
&lt;td&gt;&amp;lt;script type="module"&amp;gt;&lt;/td&gt;
&lt;td&gt;61+&lt;/td&gt;
&lt;td&gt;16+&lt;/td&gt;
&lt;td&gt;60+&lt;/td&gt;
&lt;td&gt;10.1+&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;All ES2017 features (excluding atomics+shared memory)&lt;/td&gt;
&lt;td&gt;58+&lt;/td&gt;
&lt;td&gt;16+&lt;/td&gt;
&lt;td&gt;53+&lt;/td&gt;
&lt;td&gt;10.1+&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Packages can generate this entry point using either @babel/preset-env’s &lt;a href="https://babeljs.io/docs/en/babel-preset-env#targetsesmodules"&gt;targets.esmodules option&lt;/a&gt;, or the TypeScript compiler’s &lt;a href="https://webhint.io/docs/user-guide/hints/hint-typescript-config/target/"&gt;ES2017 target&lt;/a&gt;.&lt;/p&gt;

&lt;h5&gt;
  
  
  Library Size by Transpilation Target
&lt;/h5&gt;

&lt;p&gt;One of the benefits of publishing modern JavaScript is that newer syntax is generally much smaller than polyfilled ES5 syntax. The table below shows size differences for some popular libraries:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
  &lt;thead&gt;
   &lt;tr&gt;
   &lt;th&gt;Library
   &lt;/th&gt;
   &lt;th&gt;ES5
   &lt;/th&gt;
   &lt;th&gt;"browser2017"
   &lt;/th&gt;
  &lt;/tr&gt;
&lt;/thead&gt;
  &lt;tr&gt;
   &lt;td&gt;bowser
   &lt;/td&gt;
   &lt;td&gt;25.2 KB
   &lt;/td&gt;
   &lt;td&gt;23.3 KB &lt;b&gt;(-7.5%)&lt;/b&gt;
&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
   &lt;td&gt;swr
   &lt;/td&gt;
   &lt;td&gt;24.0 KB
   &lt;/td&gt;
   &lt;td&gt;14.4 KB &lt;b&gt;(-40.0%)&lt;/b&gt;
   &lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
   &lt;td&gt;reactstrap
   &lt;/td&gt;
   &lt;td&gt;225.0 KB
   &lt;/td&gt;
   &lt;td&gt;197.5 KB &lt;b&gt;(-12.1%)&lt;/b&gt;
   &lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
   &lt;td&gt;react-popper
   &lt;/td&gt;
   &lt;td&gt;11.3KB
   &lt;/td&gt;
   &lt;td&gt;9.75KB &lt;b&gt;(-13.7%)&lt;/b&gt;
   &lt;/td&gt;
  &lt;/tr&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;sup&gt;*Data gathered using unminified and uncompressed output&lt;/sup&gt;&lt;/p&gt;

&lt;p&gt;Furthermore, some library authors are forced to write in legacy syntax, as transpiled modern code can sometimes be significantly slower or larger than its legacy counterpart. Establishing a &lt;code&gt;"browser2017"&lt;/code&gt; entry point would enable these authors to instead write in modern syntax and optimize for modern browsers.&lt;/p&gt;

&lt;h4&gt;
  
  
  Adoption from Package Authors
&lt;/h4&gt;

&lt;p&gt;For many package authors who already write their source code in modern syntax, supporting this could be as simple as adding another target to their build process. For example, if Rollup is used:&lt;/p&gt;

&lt;h5&gt;
  
  
  Example rollup.config.js
&lt;/h5&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="c1"&gt;// existing config&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;input&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;src/main.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;output&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;file&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;pkg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;main&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;format&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;es&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="na"&gt;plugins&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="nx"&gt;babel&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="na"&gt;exclude&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;node_modules/**&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;

    &lt;span class="c1"&gt;// additional "browser2017" config&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;input&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;src/main.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;output&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;file&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;pkg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;browser&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;format&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;es&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="na"&gt;plugins&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
            &lt;span class="nx"&gt;babel&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
                &lt;span class="na"&gt;exclude&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;node_modules/**&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="na"&gt;presets&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@babel/preset-env&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                    &lt;span class="na"&gt;targets&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;esmodules&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
                &lt;span class="p"&gt;}]],&lt;/span&gt;
            &lt;span class="p"&gt;})&lt;/span&gt;
        &lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;];&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h4&gt;
  
  
  Support from Bundlers
&lt;/h4&gt;

&lt;p&gt;Before it can be consumed by applications, the &lt;code&gt;"browser2017"&lt;/code&gt; conditional export needs support from existing tooling. Currently however, most tools have yet to implement support for conditional exports at all. This is documented below:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
  &lt;tr&gt;
   &lt;th&gt;Bundler / Tool
   &lt;/th&gt;
   &lt;th&gt;Export Maps
   &lt;/th&gt;
   &lt;th&gt;Conditional Maps
   &lt;/th&gt;
  &lt;/tr&gt;
&lt;/thead&gt;
  &lt;tr&gt;
   &lt;td&gt;
&lt;a href="https://nodejs.org/"&gt;Node.js&lt;/a&gt;
   &lt;/td&gt;
   &lt;td&gt;
&lt;a href="https://nodejs.org/api/esm.html#esm_package_exports"&gt;shipped&lt;/a&gt;
   &lt;/td&gt;
   &lt;td&gt;
&lt;a href="https://nodejs.org/api/esm.html#esm_package_exports"&gt;shipped&lt;/a&gt;
   &lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
   &lt;td&gt;
&lt;a href="https://webpack.js.org/"&gt;Webpack&lt;/a&gt;
   &lt;/td&gt;
   &lt;td&gt;
&lt;a href="https://github.com/webpack/enhanced-resolve/blob/4eb55a849d335701b8396cc4f2726006d27baed7/lib/processExportsField.js"&gt;implemented&lt;/a&gt;
   &lt;/td&gt;
   &lt;td&gt;
&lt;a href="https://github.com/webpack/enhanced-resolve/blob/4eb55a849d335701b8396cc4f2726006d27baed7/lib/processExportsField.js#L53"&gt;implemented&lt;/a&gt;
   &lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
   &lt;td&gt;
&lt;a href="https://rollupjs.org/"&gt;Rollup&lt;/a&gt;
   &lt;/td&gt;
   &lt;td&gt;
&lt;a href="https://github.com/rollup/plugins/issues/362"&gt;not implemented&lt;/a&gt;
   &lt;/td&gt;
   &lt;td&gt;not implemented
   &lt;/td&gt;
  &lt;/tr&gt;
&lt;tr&gt;
   &lt;td&gt;
&lt;a href="http://browserify.org/"&gt;Browserify&lt;/a&gt;
   &lt;/td&gt;
   &lt;td&gt;
&lt;a href="https://github.com/browserify/resolve/pull/224"&gt;not implemented&lt;/a&gt;
   &lt;/td&gt;
   &lt;td&gt;not implemented
   &lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
   &lt;td&gt;
&lt;a href="https://parceljs.org/"&gt;Parcel&lt;/a&gt;
   &lt;/td&gt;
   &lt;td&gt;
&lt;span&gt;not implemented&lt;/span&gt;
   &lt;/td&gt;
   &lt;td&gt;not implemented
   &lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
   &lt;td&gt;
&lt;a href="https://www.npmjs.com/esm"&gt;esm&lt;/a&gt;
   &lt;/td&gt;
   &lt;td&gt;
&lt;a href="https://github.com/standard-things/esm/issues/863"&gt;not implemented&lt;/a&gt;
   &lt;/td&gt;
   &lt;td&gt;not implemented
   &lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
   &lt;td&gt;
&lt;a href="https://www.snowpack.dev/"&gt;Snowpack&lt;/a&gt;
   &lt;/td&gt;
   &lt;td&gt;
&lt;a href="https://github.com/pikapkg/snowpack/pull/539"&gt;implemented&lt;/a&gt;
   &lt;/td&gt;
   &lt;td&gt;
&lt;a href="https://twitter.com/gr2m/status/1235745781870555137"&gt;not implemented&lt;/a&gt;
   &lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
   &lt;td&gt;
&lt;a href="https://github.com/vuejs/vite"&gt;Vite&lt;/a&gt;
   &lt;/td&gt;
   &lt;td&gt;not implemented
   &lt;/td&gt;
   &lt;td&gt;not implemented
   &lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
   &lt;td&gt;
&lt;a href="https://www.npmjs.com/package/es-dev-server"&gt;es-dev-server&lt;/a&gt;
   &lt;/td&gt;
   &lt;td&gt;
&lt;a href="https://twitter.com/LarsdenBakker18/status/1258044686155108352"&gt;not implemented&lt;/a&gt;
   &lt;/td&gt;
   &lt;td&gt;not implemented
   &lt;/td&gt;
  &lt;/tr&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h4&gt;
  
  
  Drawbacks
&lt;/h4&gt;

&lt;p&gt;The &lt;code&gt;"browser2017"&lt;/code&gt; conditional export enables publishing ES2017 syntax, but what about ES2018+ features? We would still pay the cost of transpiling features like &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax#:~:text=Spread%20in%20object%20literals"&gt;object rest/spread&lt;/a&gt; and &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for-await...of"&gt;for await...of&lt;/a&gt;. Furthermore, the &lt;code&gt;"browser2017"&lt;/code&gt; key isn't futureproof. By the time ES2025 arrives, &lt;code&gt;"browser2017"&lt;/code&gt; may be considered legacy.&lt;/p&gt;

&lt;h2&gt;
  
  
  Alternative Solution: Multiple Entry Points by Year
&lt;/h2&gt;

&lt;p&gt;One solution is to add additional entry points each year:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight json-doc"&gt;&lt;code&gt;&lt;span class="c1"&gt;// my-library's package.json&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"my-library"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"main"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"./index-node.js"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"module"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"./index.production.mjs"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"browser"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"./index.production.js"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"exports"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"node"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"./index-node.js"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"development"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"./index.development.mjs"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"browser"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="nl"&gt;"2020"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"./index.2020.mjs"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="nl"&gt;"2019"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"./index.2019.mjs"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="nl"&gt;"2018"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"./index.2018.mjs"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="nl"&gt;"2017"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"./index.2017.mjs"&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"default"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"./index.production.js"&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Though the module/nomodule pattern cannot take advantage of &lt;code&gt;"browser2018"&lt;/code&gt;+ keys, other techniques can. For example, a website can serve ES2019 code by doing any of the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  Using user-agent sniffing&lt;/li&gt;
&lt;li&gt;  &lt;a href="https://jasonformat.com/modern-script-loading/#:~:text=Option%201"&gt;Dynamically loading bundles&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;  Choosing to entirely abandon support for older browsers.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Drawbacks
&lt;/h4&gt;

&lt;h5&gt;
  
  
  Drawbacks of ES2018+ Differential Loading Techniques
&lt;/h5&gt;

&lt;p&gt;However, each of the aforementioned mechanisms have their drawbacks and thus have not garnered much adoption. User-agent sniffing is complex and error-prone, and dynamic loading does not allow for preloading (&lt;a href="https://jasonformat.com/modern-script-loading/"&gt;source&lt;/a&gt;). A &lt;a href="https://docs.google.com/document/d/1kOLu53dYzwElJZ6JBgMR137-Rdj8cDc_rX2YkPFYUsY/edit#heading=h.kixnwvcm52lq"&gt;static solution was proposed&lt;/a&gt; in 2019, but was met with standardization challenges. At the earliest, &lt;a href="https://github.com/WICG/import-maps#general-url-like-specifier-remapping"&gt;import maps&lt;/a&gt; might give us a technique for a &lt;code&gt;"browser2021"&lt;/code&gt; key or some form of differential loading.&lt;/p&gt;

&lt;h5&gt;
  
  
  Diminishing Improvements in Size
&lt;/h5&gt;

&lt;p&gt;It’s also worth highlighting that ECMAScript versions after ES2017 contain fewer features with less adoption, so additional entry points might not have a significant impact on bundle size.&lt;/p&gt;

&lt;h6&gt;
  
  
  Features by ECMAScript Year
&lt;/h6&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
  &lt;tr&gt;
   &lt;th&gt;es2015
   &lt;/th&gt;
   &lt;th&gt;es2016
   &lt;/th&gt;
   &lt;th&gt;es2017
   &lt;/th&gt;
   &lt;th&gt;es2018
   &lt;/th&gt;
   &lt;th&gt;es2019
   &lt;/th&gt;
   &lt;th&gt;es2020
   &lt;/th&gt;
   &lt;th&gt;es2021+
   &lt;/th&gt;
  &lt;/tr&gt;
&lt;/thead&gt;
  &lt;tr&gt;
   &lt;td&gt;const, let
   &lt;/td&gt;
   &lt;td&gt;** operator
   &lt;/td&gt;
   &lt;td&gt;async/await
   &lt;/td&gt;
   &lt;td&gt;Object Spread/Rest
   &lt;/td&gt;
   &lt;td&gt;Array.flat, Array.flatMap
   &lt;/td&gt;
   &lt;td&gt;String.matchAll
   &lt;/td&gt;
   &lt;td&gt;String.replaceAll
   &lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
   &lt;td&gt;Template literals
   &lt;/td&gt;
   &lt;td&gt;Array.includes
   &lt;/td&gt;
   &lt;td&gt;String padding
   &lt;/td&gt;
   &lt;td&gt;Promise.finally
   &lt;/td&gt;
   &lt;td&gt;Object.fromEntries
   &lt;/td&gt;
   &lt;td&gt;BigInt
   &lt;/td&gt;
   &lt;td&gt;Promise.any
   &lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
   &lt;td&gt;Destructuring
   &lt;/td&gt;
   &lt;td&gt;
   &lt;/td&gt;
   &lt;td&gt;Object.{values, entries, …}
   &lt;/td&gt;
   &lt;td&gt;RegExp features
   &lt;/td&gt;
   &lt;td&gt;Optional catch binding
   &lt;/td&gt;
   &lt;td&gt;Promise.allSettled
   &lt;/td&gt;
   &lt;td&gt;Logical Assignment
   &lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
   &lt;td&gt;Arrow functions
   &lt;/td&gt;
   &lt;td&gt;
   &lt;/td&gt;
   &lt;td&gt;Atomics
   &lt;/td&gt;
   &lt;td&gt;for await...of
   &lt;/td&gt;
   &lt;td&gt;
   &lt;/td&gt;
   &lt;td&gt;globalThis
   &lt;/td&gt;
   &lt;td&gt;… to be decided
   &lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
   &lt;td&gt;Classes
   &lt;/td&gt;
   &lt;td&gt;
   &lt;/td&gt;
   &lt;td&gt;Shared Memory
   &lt;/td&gt;
   &lt;td&gt;
   &lt;/td&gt;
   &lt;td&gt;
   &lt;/td&gt;
   &lt;td&gt;Optional chaining
   &lt;/td&gt;
   &lt;td&gt;
   &lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
   &lt;td&gt;Promises
   &lt;/td&gt;
   &lt;td&gt;
   &lt;/td&gt;
   &lt;td&gt;
   &lt;/td&gt;
   &lt;td&gt;
   &lt;/td&gt;
   &lt;td&gt;
   &lt;/td&gt;
   &lt;td&gt;Nullish coalescing
   &lt;/td&gt;
   &lt;td&gt;
   &lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
   &lt;td&gt; &lt;strong&gt;&lt;a href="https://kangax.github.io/compat-table/es6/"&gt;... a lot more&lt;/a&gt;&lt;/strong&gt;
   &lt;/td&gt;
   &lt;td&gt;
   &lt;/td&gt;
   &lt;td&gt;
   &lt;/td&gt;
   &lt;td&gt;
   &lt;/td&gt;
   &lt;td&gt;
   &lt;/td&gt;
   &lt;td&gt;
   &lt;/td&gt;
   &lt;td&gt;
   &lt;/td&gt;
  &lt;/tr&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h6&gt;
  
  
  Library Size by Transpilation Target
&lt;/h6&gt;

&lt;p&gt;Compared to the &lt;code&gt;"browser2017"&lt;/code&gt; target, transpiling to a &lt;code&gt;"browser2019"&lt;/code&gt; target tends to result in only very small reductions in size.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
  &lt;tr&gt;
   &lt;th&gt;Library
   &lt;/th&gt;
   &lt;th&gt;ES5
   &lt;/th&gt;
   &lt;th&gt;"browser2017"
   &lt;/th&gt;
   &lt;th&gt;"browser2019"
   &lt;/th&gt;
  &lt;/tr&gt;
&lt;/thead&gt;
  &lt;tr&gt;
   &lt;td&gt;bowser
   &lt;/td&gt;
   &lt;td&gt;25.2 KB
   &lt;/td&gt;
   &lt;td&gt;23.3 KB &lt;b&gt;(-7.5%)&lt;/b&gt;
   &lt;/td&gt;
   &lt;td&gt;23.3 KB &lt;b&gt;(-0%)&lt;/b&gt;
   &lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
   &lt;td&gt;swr
   &lt;/td&gt;
   &lt;td&gt;24.0 KB
   &lt;/td&gt;
   &lt;td&gt;14.4 KB &lt;b&gt;(-40.0%)&lt;/b&gt;
   &lt;/td&gt;
   &lt;td&gt;13.8 KB &lt;b&gt;(-4.2%)&lt;/b&gt;
   &lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
   &lt;td&gt;reactstrap
   &lt;/td&gt;
   &lt;td&gt;225.0 KB
   &lt;/td&gt;
   &lt;td&gt;197.5 KB &lt;b&gt;(-12.1%)&lt;/b&gt;
   &lt;/td&gt;
   &lt;td&gt;197.5 KB &lt;b&gt;(-0%)&lt;/b&gt;
   &lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
   &lt;td&gt;react-popper
   &lt;/td&gt;
   &lt;td&gt;11.3KB
   &lt;/td&gt;
   &lt;td&gt;9.75KB &lt;b&gt;(-13.7%)&lt;/b&gt;
   &lt;/td&gt;
   &lt;td&gt;8.98 KB &lt;b&gt;(-7.9%)&lt;/b&gt;
   &lt;/td&gt;
  &lt;/tr&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;sup&gt;*Data gathered using unminified and uncompressed output&lt;/sup&gt;&lt;/p&gt;

&lt;h6&gt;
  
  
  Maximum Polyfill Size by Transpilation Target
&lt;/h6&gt;

&lt;p&gt;In practice, the size of polyfills depends on which features are actually used. However, we can estimate the maximum size of polyfills (the size assuming every unsupported feature is polyfilled) for each transpilation target. This data is useful for comparison, but it should be noted that the values for es2017 and es2019 include significant over-polyfilling as a result of technical constraints that can be addressed.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
  &lt;tr&gt;
   &lt;th&gt;Transpilation Target
   &lt;/th&gt;
&lt;th&gt;Browsers&lt;/th&gt;
   &lt;th&gt;Maximum Polyfill Size
   &lt;/th&gt;
  &lt;/tr&gt;
&lt;/thead&gt;
  &lt;tr&gt;
   &lt;td&gt;ES5
   &lt;/td&gt;
&lt;td&gt;IE11+&lt;/td&gt;
   &lt;td&gt;97.6 KB
   &lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
   &lt;td&gt;
&lt;code&gt;"browser2017"&lt;/code&gt;
   &lt;/td&gt;
&lt;td&gt;CH 61, Edge 16, FF 60, SF 10.1&lt;/td&gt;
   &lt;td&gt;59.5 KB
   &lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
   &lt;td&gt;
&lt;code&gt;"browser2019"&lt;/code&gt;
   &lt;/td&gt;
&lt;td&gt;CH 73, Edge 79, FF 64, SF 12.1&lt;/td&gt;
   &lt;td&gt;39.5 KB
   &lt;/td&gt;
  &lt;/tr&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;sup&gt;* Data gathered using minified and uncompressed output. Includes only &lt;a href="https://kangax.github.io/compat-table/es2016plus/"&gt;ECMAScript features polyfilled by babel+core-js&lt;/a&gt;.&lt;br&gt;
&lt;/sup&gt;&lt;/p&gt;
&lt;h5&gt;
  
  
  Complexity
&lt;/h5&gt;

&lt;p&gt;At least for now, yearly entry points might only further complicate the package authoring process. They would require year-to-year community-wide agreements upon what browser versions are considered part of a given year, and for package authors to correctly follow those definitions. Given the decentralized nature of the JavaScript ecosystem, it’s important to take into account that simpler solutions are easier to adopt.&lt;/p&gt;

&lt;p&gt;In the future, it might make sense to add another entry point only once a substantial amount of new features have been released, or after &lt;a href="https://wicg.github.io/import-maps/"&gt;a new differential loading mechanism&lt;/a&gt; becomes available. At that point, we could extend the less granular &lt;code&gt;"browser2017"&lt;/code&gt;, &lt;code&gt;"browser2021"&lt;/code&gt;, and &lt;code&gt;"browser2027"&lt;/code&gt; entry points, with each year serving as an alias for a set of targeted browsers. Tools like &lt;a href="https://babeljs.io/docs/en/babel-preset-env"&gt;@babel/preset-env&lt;/a&gt; could potentially adopt these aliases and abstract their precise definitions.&lt;/p&gt;
&lt;h2&gt;
  
  
  Alternative Solution: &lt;code&gt;"esnext"&lt;/code&gt; entry point
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;Note: This is nearly identical to Webpack’s &lt;a href="https://gist.github.com/sokra/e032a0f17c1721c71cfced6f14516c62#:~:text=browser:%20Compatible%20with%20current%20Spec"&gt;proposed &lt;b&gt;“browser”&lt;/b&gt; entry point&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;We can see that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  Application developers are the only ones who can know their target browsers&lt;/li&gt;
&lt;li&gt;  Maintaining multiple package variations is a pain point for package authors&lt;/li&gt;
&lt;li&gt;  Application developers already have transpilation integrated into their build process for their own code&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Given the above, what if we shift the burden of transpilation away from package authors and onto application developers? A generic &lt;code&gt;"esnext"&lt;/code&gt; export map key could point to code containing any stable ECMAScript feature as of the package’s publish date. With this knowledge, application developers could transpile all packages to work with their target browsers.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight json-doc"&gt;&lt;code&gt;&lt;span class="c1"&gt;// my-library's package.json&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"my-library"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"main"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"./index-node.js"&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"module"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"./index.production.mjs"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"browser"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"./index.production.js"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"exports"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"node"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"./index-node.js"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"development"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"./index.development.mjs"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"esnext"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"./index.esnext.mjs"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"default"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"./index.production.js"&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Both package authors and application developers would no longer need to worry about what syntax level a package is published in. Ideally, this solution would enable JavaScript libraries to always provide the most modern output - even as the definition of “modern” changes.&lt;/p&gt;

&lt;h4&gt;
  
  
  Drawbacks
&lt;/h4&gt;

&lt;h5&gt;
  
  
  Migrating to Transpiling node_modules
&lt;/h5&gt;

&lt;p&gt;The JavaScript ecosystem has a long-ingrained belief that we shouldn’t have to transpile &lt;code&gt;node_modules&lt;/code&gt;, and our tooling reflects this. Since libraries are already transpiled prior to being published, most applications have configured Babel to exclude transpiling &lt;code&gt;node_modules&lt;/code&gt;. Moving to an &lt;code&gt;"esnext"&lt;/code&gt; entry point would require application developers to move away from pre-transpiled dependencies, instead adopting slower fully-transpiled builds. The build impact could be alleviated to some degree through caching and limiting transpiling to production builds. Some tools have already adopted this approach, including Parcel and Create React App. This change would also require tooling changes to selectively transpile only packages that expose an “esnext” entry point.&lt;/p&gt;

&lt;h5&gt;
  
  
  Silent Breakages
&lt;/h5&gt;

&lt;p&gt;A moving &lt;code&gt;"esnext"&lt;/code&gt; target has the potential to cause silent breakages in applications. For example, ES2021 could introduce &lt;a href="https://github.com/tc39/proposal-observable"&gt;Observable&lt;/a&gt; to the standard library. If an npm library starts to use Observable in its &lt;code&gt;"esnext"&lt;/code&gt; entry point, older versions of Babel &lt;a href="https://babeljs.io/repl#?browsers=chrome%2071&amp;amp;build=&amp;amp;builtIns=usage&amp;amp;spec=false&amp;amp;loose=false&amp;amp;code_lz=GYVwdgxgLglg9mABAGxgZygUzACk8zAW2ygBpFMA3EgOQENiBKRAbwChFPEAnTKEbkjCYA7ogDyAIzSZulOpII4402dW6IAvAD5WHLgYD0hxAGFedLIjpIqJRAAsbAEwIaRDmBAeIZYZ2iIzpZ0iFBwYQ6YvjBgANb6BpwEUI4ubloU1GCpOogqMnKyAHTCAB5QeNlQjADcbIlJxogAglBQdN6R0XY5af5ujQb4RCTFdM7OAKLVADLoWMLcVbQMmOROA7LkUNwgmHUNSVzNAEp8AkihEAQ2IAAOiKCQsAiIHl4-IjDIyIgQNgg-G6WXsGAshCGXF4_EEiBwzDy7GOx2aABE-J0fFAoqC-ptXLIntw4IQQSNiDkoccKWNeIQ4NQZiR5hhsLIVjl6MQNultmE9gd6ijEABfYVcUWHUVAA&amp;amp;debug=false&amp;amp;forceAllTransforms=true&amp;amp;shippedProposals=false&amp;amp;circleciRepo=&amp;amp;evaluate=false&amp;amp;fileSize=false&amp;amp;timeTravel=false&amp;amp;sourceType=module&amp;amp;lineWrap=true&amp;amp;presets=env%2Cenv&amp;amp;prettier=false&amp;amp;targets=&amp;amp;version=7.10.5&amp;amp;externalPlugins="&gt;would not polyfill Observable but output no errors or warnings&lt;/a&gt;. For application developers who don’t update their transpilation tooling, this error would go uncaught until reaching testing or even production. Adding &lt;a href="https://gist.github.com/sokra/e032a0f17c1721c71cfced6f14516c62#:~:text=I%20would%20consider%20this%20is"&gt;more metadata in our package.json&lt;/a&gt; could be one approach to solving this. Even with this information, it may still be difficult or impossible to reliably determine the publish date for an installed package: npm injects the publish date into local package.json files when installing, but other tools like Yarn do not.&lt;/p&gt;

&lt;h2&gt;
  
  
  Solutions Comparison
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
  &lt;tr&gt;
   &lt;th&gt;Solution
   &lt;/th&gt;
   &lt;th&gt;Pros
   &lt;/th&gt;
   &lt;th&gt;Cons
   &lt;/th&gt;
  &lt;/tr&gt;
&lt;/thead&gt;
  &lt;tr&gt;
   &lt;td&gt;browser2017
   &lt;/td&gt;
   &lt;td&gt;
&lt;ul&gt;

&lt;li&gt;Simplest solution

&lt;/li&gt;
&lt;li&gt;Precise definition tied to a set of browsers

&lt;/li&gt;
&lt;li&gt;Applications do not need to transpile dependencies

&lt;/li&gt;
&lt;li&gt;Requires minor changes in tooling/configuration

&lt;/li&gt;
&lt;li&gt;Package authors control how their package gets transpiled
&lt;/li&gt;
&lt;/ul&gt;
   &lt;/td&gt;
   &lt;td&gt;
&lt;ul&gt;

&lt;li&gt;Misses out on ES2018+ syntax

&lt;/li&gt;
&lt;li&gt;We might need to introduce a “browser2025” entry point in the future

&lt;/li&gt;
&lt;li&gt;Does not support all ES2017 syntax; can be misinterpreted
&lt;/li&gt;
&lt;/ul&gt;
   &lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
   &lt;td&gt;browser2017 browser2018 browser2019 ...
   &lt;/td&gt;
   &lt;td&gt;
&lt;ul&gt;

&lt;li&gt;Gives applications power to target any syntax level

&lt;/li&gt;
&lt;li&gt;Applications do not need to transpile dependencies

&lt;/li&gt;
&lt;li&gt;Package authors control how their package gets transpiled

&lt;/li&gt;
&lt;li&gt;Requires minor changes in tooling/configuration
&lt;/li&gt;
&lt;/ul&gt;
   &lt;/td&gt;
   &lt;td&gt;
&lt;ul&gt;

&lt;li&gt;There is currently no &lt;strong&gt;static&lt;/strong&gt; differential loading mechanism for serving ES2018+ syntax

&lt;/li&gt;
&lt;li&gt;ES2018+ entry points currently would not significantly reduce size

&lt;/li&gt;
&lt;li&gt;Complicates package authoring process
&lt;/li&gt;
&lt;/ul&gt;
   &lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
   &lt;td&gt;esnext
   &lt;/td&gt;
   &lt;td&gt;
&lt;ul&gt;

&lt;li&gt;Gives applications full power in determining their target browsers

&lt;/li&gt;
&lt;li&gt;Future-proof; libraries will always be using the latest syntax

&lt;/li&gt;
&lt;li&gt;Simplifies package authoring process
&lt;/li&gt;
&lt;/ul&gt;
   &lt;/td&gt;
   &lt;td&gt;
&lt;ul&gt;

&lt;li&gt;There is currently no &lt;strong&gt;static&lt;/strong&gt; differential loading mechanism for serving ES2018+ syntax

&lt;/li&gt;
&lt;li&gt;Slow production builds; can be alleviated with caching

&lt;/li&gt;
&lt;li&gt;Tooling must be built to selectively transpile node_modules

&lt;/li&gt;
&lt;li&gt;Can cause silent breakage for package users

&lt;/li&gt;
&lt;li&gt;Package authors have no control over how their packages get transpiled
&lt;/li&gt;
&lt;/ul&gt;
   &lt;/td&gt;
  &lt;/tr&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Looking Forward
&lt;/h2&gt;

&lt;p&gt;A pre-transpiled &lt;code&gt;"browser2017"&lt;/code&gt; conditional export unlocks most of the potential benefits of modern JavaScript. However, in the future we might need subsequent "browser2021" and "browser2027" fields.&lt;/p&gt;

&lt;p&gt;In contrast, &lt;code&gt;"esnext"&lt;/code&gt; is futureproof but requires a solution that addresses silent breakage and versioning consensus before it can be viable. It also requires many changes in existing tooling and configurations.&lt;/p&gt;

&lt;p&gt;Our applications stand to benefit from serving modern JavaScript. Whichever mechanism we choose, we need to consider how it affects each part of the ecosystem: bundlers, library authors, and application developers.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;I'd love to hear your thoughts&lt;/em&gt;&lt;/strong&gt; 😃&lt;strong&gt;&lt;em&gt;! Feel free to leave a comment or suggestion below&lt;/em&gt;&lt;/strong&gt; 👇&lt;strong&gt;&lt;em&gt;.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Other Resources
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;a href="https://gist.github.com/sokra/e032a0f17c1721c71cfced6f14516c62"&gt;@sokra Introduces conditional exports to Webpack&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;a href="https://babeljs.io/blog/2018/06/26/on-consuming-and-publishing-es2015+-packages"&gt;On Consuming and Publishing ES2015+ packages - Henry Zhu&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;a href="https://philipwalton.com/articles/deploying-es2015-code-in-production-today/"&gt;Deploying ES2015+ Code in Production - Philip Walton&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;a href="https://dev.to/jovidecroock/modern-bundling-4chm"&gt;Modern Bundling - Jovi De Croock&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;a href="https://github.com/facebook/create-react-app/issues/1125"&gt;Create React App Introduces Transpilation of node_modules&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;a href="https://podcasts.google.com/feed/aHR0cHM6Ly9mZWVkcy50cmFuc2lzdG9yLmZtL3RoZS1iYWJlbC1wb2RjYXN0/episode/OGM5ZmNlNTYtYTY0Yy00ODhkLWE3MTUtMDkyODVhNzM5ZTFk?ved=0CAkQ38oDahcKEwiQhp7Z6r_qAhUAAAAAHQAAAAAQAQ"&gt;The Babel Podcast: Compiling Your Dependencies - Henry Zhu and Jason Miller&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;a href="https://kangax.github.io/compat-table/"&gt;Kangax compat-table&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;a href="https://twitter.com/RReverser/status/657601803954057216"&gt;Twitter Discussion about jsnext:main&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>react</category>
      <category>performance</category>
    </item>
  </channel>
</rss>
