<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Rich Harris on Medium]]></title>
        <description><![CDATA[Stories by Rich Harris on Medium]]></description>
        <link>https://medium.com/@Rich_Harris?source=rss-a11f7c00cbbe------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*A-EDNw973JUwqRgj.jpeg</url>
            <title>Stories by Rich Harris on Medium</title>
            <link>https://medium.com/@Rich_Harris?source=rss-a11f7c00cbbe------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 03:59:29 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@Rich_Harris/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com" rel="hub"/>
        <item>
            <title><![CDATA[Rollup now has code-splitting! And we need your help]]></title>
            <link>https://medium.com/rollup/rollup-now-has-code-splitting-and-we-need-your-help-46defd901c82?source=rss-a11f7c00cbbe------2</link>
            <guid isPermaLink="false">https://medium.com/p/46defd901c82</guid>
            <category><![CDATA[bundler]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[modules]]></category>
            <dc:creator><![CDATA[Rich Harris]]></dc:creator>
            <pubDate>Thu, 08 Feb 2018 16:16:22 GMT</pubDate>
            <atom:updated>2018-02-08T18:20:03.738Z</atom:updated>
            <content:encoded><![CDATA[<p>Gather round for some news that’s really going to shake your tree: as of version 0.55, &lt;blink&gt;<a href="https://rollupjs.org">Rollup</a>, the JavaScript module bundler, finally has code-splitting&lt;/blink&gt;! This has been the most popular feature request for a long time, since it lets complex apps load quickly in smaller increments while still leveraging Rollup’s obsessive focus on efficiency. Rollup has long been the tool of choice for library authors — it powers React, Vue, D3, Moment, Three.js, Ember, Angular, Babylon, Jest, Bootstrap, Leaflet, Prettier, and thousands of other tools — but code-splitting means Rollup can now become a great choice for applications of all sizes.</p><p>We’ve also joined the crowdfunding platform <a href="https://opencollective.com/rollup">OpenCollective</a> to ensure a sustainable future for Rollup.</p><h4><strong>What is code-splitting, and why should I care?</strong></h4><p>Loading application code can take a long time, especially on mobile. Code-splitting breaks your app into smaller chunks, so that the user only has to load enough JavaScript to get started, and the application can quietly fetch the rest whenever it becomes relevant. For example, you probably don’t need to serve the code for your “settings” page until the user actually goes there.</p><p>Rollup’s implementation of code-splitting, by JSPM creator <a href="https://twitter.com/guybedford">Guy Bedford</a>, approaches the conceptual ideal for JavaScript. For the first time, your code-split chunks are <em>themselves</em> just standard ES modules that use the browser’s built-in module loader without any additional overhead, while still getting the full benefit of Rollup’s treeshaking. For browsers that don’t yet support ES modules, you can also use SystemJS or any AMD loader. It is completely automated and results in zero code duplication.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OO_-5xekaaZJsOeaytPcGA.png" /><figcaption>A <a href="https://github.com/rollup/rollup-starter-code-splitting/blob/master/rollup.config.js">sample configuration</a> that supports both modern and legacy browsers</figcaption></figure><p>Check out the docs <a href="https://rollupjs.org/guide/en#experimental-code-splitting">here</a> or try a <a href="https://github.com/rollup/rollup-starter-code-splitting">sample project</a>.</p><h4><strong>Now we need your help</strong></h4><p>Up until now, Rollup has been developed by <a href="https://github.com/rollup/rollup/graphs/contributors">over one hundred volunteers</a> who have collectively donated thousands of hours of coding and other forms of expertise, but it’s increasingly apparent that Rollup needs a more sustainable footing.</p><blockquote>import $ from ‘opencollective’;</blockquote><p>That’s why we’ve joined OpenCollective, the crowdfunding platform for open source projects. Your support will mean that we can continue to improve and iterate on cutting-edge features while ensuring this work is sustainable for our core team members — in particular Guy, who has also implemented dynamic import() support (the fudge sauce to the code-splitting sundae) and Lukas Taegert, who has overhauled Rollup’s treeshaking and laid the foundations for future optimisations.</p><p>We’re hugely grateful for the early backing we’ve received from <a href="https://mixmax.com">Mixmax</a>, <a href="https://twitter.com/kentcdodds">Kent C. Dodds</a>, <a href="https://twitter.com/LittleDotMatt">Matt Pilott</a> and <a href="https://twitter.com/yowainwright">Jeff Wainwright</a>. Because we’re using OpenCollective, our accounting is fully transparent — you’ll be able to keep track of our funding status and how the money is being used.</p><h4><strong>How to support Rollup</strong></h4><p>You can <a href="https://opencollective.com/rollup">donate here</a>. If Rollup (or a library that’s built with it) is an important part of your project, please consider donating or sharing this with your employer.</p><p>If you represent a company and you would like to donate more than the current top ‘sponsor’ tier ($100 per month) then please reach out to us on <a href="https://twitter.com/RollupJS">Twitter</a>! Our DMs are open.</p><p><em>Co-authored by </em><a href="https://twitter.com/vijithassar"><em>Vijith Assar</em></a><em>, </em><a href="https://twitter.com/guybedford"><em>Guy Bedford</em></a><em> and </em><a href="https://twitter.com/lukastaegert"><em>Lukas Taegert</em></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=46defd901c82" width="1" height="1" alt=""><hr><p><a href="https://medium.com/rollup/rollup-now-has-code-splitting-and-we-need-your-help-46defd901c82">Rollup now has code-splitting! And we need your help</a> was originally published in <a href="https://medium.com/rollup">Rollup</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Webpack and Rollup: the same but different]]></title>
            <link>https://medium.com/webpack/webpack-and-rollup-the-same-but-different-a41ad427058c?source=rss-a11f7c00cbbe------2</link>
            <guid isPermaLink="false">https://medium.com/p/a41ad427058c</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[rollup]]></category>
            <category><![CDATA[es2015]]></category>
            <category><![CDATA[webpack]]></category>
            <dc:creator><![CDATA[Rich Harris]]></dc:creator>
            <pubDate>Thu, 06 Apr 2017 16:10:19 GMT</pubDate>
            <atom:updated>2017-04-06T16:10:19.245Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rtjClMZ8sq3cLFT9Aq8Xyg.png" /></figure><p>This week, Facebook merged a <a href="https://github.com/facebook/react/pull/9327">monster pull request</a> into React that replaced its existing build process with one based on <a href="https://rollupjs.org/">Rollup</a>, <a href="https://twitter.com/stanlemon/status/849366789825994752">prompting</a> <a href="https://twitter.com/MrMohtas/status/849362334988595201">several</a> <a href="https://twitter.com/kyleholzinger/status/849683292760797184">people</a> to ask ‘why did you choose Rollup over webpack’?</p><p>Which is a completely reasonable question. <a href="https://webpack.js.org/">Webpack</a> is one of the modern JavaScript community’s greatest success stories, with millions of downloads every month powering tens of thousands of websites and applications. It has a large ecosystem, dozens of contributors, and — unusually for a community open source project — <a href="https://opencollective.com/webpack">meaningful financial support</a>.</p><p>By comparison, Rollup is a minnow. But React isn’t alone — Vue, Ember, Preact, D3, Three.js, Moment, and dozens of other well-known libraries also use Rollup. So what’s going on? Why can’t we have just one JavaScript module bundler that everyone agrees on?</p><h3>A tale of two bundlers</h3><p>webpack was started in 2012 by <a href="https://medium.com/u/cccc522e775a">Tobias Koppers</a> to solve a hard problem that existing tools didn’t address: building complex single-page applications (SPAs). Two features in particular changed everything:</p><ol><li><strong>Code-splitting</strong> makes it possible to break your app apart into manageable chunks that can be loaded on-demand, meaning your users get an interactive site much faster than if they had to wait for the whole application to download and parse. You <em>can</em> do this manually, but, well… good luck.</li><li><strong>Static assets</strong> such as images and CSS can be imported into your app and treated as just another node in the dependency graph. No more carefully placing your files in the right folders and hacked-together scripts for adding hashes to file URLs — webpack can take care of it for you.</li></ol><p>Rollup was created for a different reason: to build flat distributables of JavaScript libraries as efficiently as possible, taking advantage of the ingenious design of ES2015 modules. Other module bundlers — webpack included — work by wrapping each module in a function, putting them in a bundle with a browser-friendly implementation of require, and evaluating them one-by-one. That’s great if you need things like on-demand loading, but otherwise it’s a bit of a waste, and it <a href="https://nolanlawson.com/2016/08/15/the-cost-of-small-modules/">gets worse if you have lots of modules</a>.</p><p>ES2015 modules enable a different approach, which Rollup uses. All your code is put in the same place and evaluates in one go, resulting in leaner, simpler code that starts up faster. You can <a href="https://rollupjs.org/repl">see it for yourself with the Rollup REPL</a>.</p><p>But there’s a trade-off: code-splitting is a much hairier problem, and at the time of writing Rollup doesn’t support it. Similarly, Rollup doesn’t do hot module replacement (HMR). And perhaps the biggest pain point for people coming to Rollup — while it handles most CommonJS files (via a <a href="https://github.com/rollup/rollup-plugin-commonjs">plugin</a>), some things just don’t translate to ES2015, whereas webpack handles everything you throw at it with aplomb.</p><h3>So which should I use?</h3><p>By now, hopefully it’s clear why both tools coexist and support each other — they serve different purposes. The tl;dr is this:</p><blockquote>Use webpack for apps, and Rollup for libraries</blockquote><p>That’s not a hard and fast rule — lots of sites and apps are built with Rollup, and lots of libraries are built with webpack. But it’s a good rule of thumb.</p><p>If you need code-splitting, or you have lots of static assets, or you’re building something with lots of CommonJS dependencies, Webpack is a better choice. If your codebase is ES2015 modules and you’re making something to be used by other people, you probably want Rollup.</p><h3>Package authors: use pkg.module!</h3><p>For a long time, using JavaScript libraries was a bit of a crapshoot, because you and the library author effectively had to agree on a module system. If you were using Browserify but she preferred AMD, you would have to duct tape things together before you could actually build anything. The <a href="https://github.com/umdjs/umd">Universal Module Definition</a> (UMD) format <em>sort of</em> fixed that, but because it wasn’t enforced anywhere you never knew quite what you were going to get.</p><p>ES2015 changes all that, because import and export are part of the language. In the future, there’ll be no ambiguity, and things will work a lot more seamlessly. Unfortunately, because browsers (mostly) and Node don’t yet support import and export, we still need to ship UMD files (or CommonJS, if you’re building something Node-only).</p><p>By adding a &quot;module&quot;: &quot;dist/my-library.es.js&quot; entry to your library’s package.json file (aka pkg.module), it’s possible to serve UMD and ES2015 at the same time, right now. <strong>That’s important because Webpack and Rollup can both use </strong><strong>pkg.module to generate the most efficient code possible</strong> — in some cases, they can even both <a href="https://webpack.js.org/guides/tree-shaking/">tree-shake</a> unused parts of your library away.</p><p><em>Learn more about </em><em>pkg.module on the </em><a href="https://github.com/rollup/rollup/wiki/pkg.module"><em>Rollup wiki</em></a><em>.</em></p><p>Hopefully this article makes the relationship between the two projects a bit clearer. If you still have questions, find us on Twitter at <a href="https://twitter.com/rich_harris">rich_harris</a>/<a href="https://twitter.com/rollupjs">rollupjs</a> and <a href="https://twitter.com/thelarkinn">thelarkinn</a>. Happy bundling!</p><p><em>Our thanks to Rich Harris for writing this article. We believe that collaboration in open source is incredibly vital to ensure we push technology and the web forward together.</em></p><p><em>No time to help contribute? Want to give back in other ways? Become a Backer or Sponsor to webpack by </em><a href="https://opencollective.com/webpack"><em>donating to our open collective</em></a><em>. Open Collective not only helps support the Core Team, but also supports contributors who have spent significant time improving our organization on their free time! ❤</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a41ad427058c" width="1" height="1" alt=""><hr><p><a href="https://medium.com/webpack/webpack-and-rollup-the-same-but-different-a41ad427058c">Webpack and Rollup: the same but different</a> was originally published in <a href="https://medium.com/webpack">webpack</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Phonograph.js: Tolerable mobile web audio]]></title>
            <link>https://medium.com/@Rich_Harris/phonograph-js-tolerable-mobile-web-audio-55286bd5e567?source=rss-a11f7c00cbbe------2</link>
            <guid isPermaLink="false">https://medium.com/p/55286bd5e567</guid>
            <category><![CDATA[audio]]></category>
            <category><![CDATA[html5]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[web-audio-api]]></category>
            <category><![CDATA[mobile]]></category>
            <dc:creator><![CDATA[Rich Harris]]></dc:creator>
            <pubDate>Thu, 11 Aug 2016 20:26:33 GMT</pubDate>
            <atom:updated>2016-08-11T20:26:33.561Z</atom:updated>
            <content:encoded><![CDATA[<p>Audio on the mobile web is a mess. The easy way to play sound — creating an <em>&lt;audio&gt;</em> element and calling the <em>audio.play()</em> method — doesn’t work unless playback starts in response to a ‘user gesture’, and will only let you play one clip at a time.</p><p>The hard way — loading the audio, decoding it using the web audio API’s <a href="https://developer.mozilla.org/en-US/docs/Web/API/AudioContext/decodeAudioData">context.decodeAudioData(…)</a>, creating an <a href="https://developer.mozilla.org/en-US/docs/Web/API/AudioBufferSourceNode">AudioBufferSourceNode</a> and playing <em>that</em> — gives you a lot more flexibility, but comes with a rather important caveat: it will crash your phone.</p><p>There’s a simple reason for that. The browser needs to store the entire audio clip, decoded, in memory, which — since a 5Mb mp3 file typically equates to a 55Mb wav file — you can quickly find yourself running out of if you’re using large audio files. When that happens, the way you find out about it is the whole tab going kaput.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.facebook.com%2Fvideo%2Fembed%3Fvideo_id%3D1423961437630919&amp;url=https%3A%2F%2Fwww.facebook.com%2FGuardianUs%2Fvideos%2Fvb.456740217686384%2F1423961437630919%2F%3Ftype%3D2%26theater&amp;image=https%3A%2F%2Fscontent.xx.fbcdn.net%2Fv%2Fl%2Ft15.0-10%2Fp128x128%2F13936203_1423965634297166_732463135_n.jpg%3Foh%3D6afafb36302d3707e7a3e7fbaf8e0e9c%26oe%3D5822DC6B&amp;key=d04bfffea46d4aeda930ec88cc64b87c&amp;type=text%2Fhtml&amp;schema=facebook" width="560" height="315" frameborder="0" scrolling="no"><a href="https://medium.com/media/17f0a2390d2c81e4cc7919610eef9680/href">https://medium.com/media/17f0a2390d2c81e4cc7919610eef9680/href</a></iframe><p>For <a href="https://riorun.theguardian.com">RioRun</a>, an interactive podcast we (the Guardian US interactive team) recently built, this was a major problem. At any one time we might have as many as three separate layered audio clips playing at once, and each of those clips might be several minutes in length. And even if we didn’t have to worry about bursting the memory banks, the web audio API approach has another major drawback in that you have to download the <em>entire</em> clip before you can start playing <em>any</em> of it.</p><p>Since playback is controlled by the distance you’ve covered, as measured by your phone’s GPS, using <em>&lt;audio&gt;</em> is a non-starter — we can’t rely on the user tapping their screen.</p><h3>Break it down</h3><p>We created <a href="https://gitlab.com/Rich-Harris/phonograph">Phonograph</a>, an open source JavaScript library, to tackle this problem. It exploits a useful fact about mp3 files: like the <a href="https://en.wikipedia.org/wiki/Planarian">planarian flatworm</a>, you can slice them into smaller chunks and they won’t die — each chunk becomes a block of audio that can be played independently.</p><p>By reading in the raw binary data and breaking it into <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array">Uint8Arrays</a> of a few kilobytes each, we can decode just enough audio to get us through the next few seconds. As we reach the end of each chunk, the next one is decoded and starts playing.</p><p>Better still, if we’re in a browser that supports the <a href="https://jakearchibald.com/2015/thats-so-fetch/">fetch API</a> and implements <a href="https://jakearchibald.com/2016/streams-ftw/">streaming</a>, we can start playback before download is complete, by estimating the duration of the clip and how long it will take to arrive at the current rate. (That is, of course, something you get for free with traditional HTML5 <em>&lt;audio&gt;</em> — just not with the web audio API.)</p><h3>Here be dragons</h3><p>That’s the theory, at least. It turns out to be somewhat more challenging in practice. For one thing, you can’t just slice the mp3 file anywhere — you have to do it on a <em>frame boundary</em>, on which more later, otherwise you’ll lose data — and even then you’ll get audible seams between chunks because of something called the <em>byte reservoir</em>. This is one of the tricks that mp3 encoders use to cram more data into a smaller space. By filling unused space in less-complex-to-encode parts of the clip (such as silence) with extra bytes from upcoming more-complex-to-encode parts, encoders can achieve better quality with the same filesize. (This isn’t variable bitrate encoding or VBR, by the way — that’s a whole other can of planarian flatworms that we’ll open later.) The upshot is that any one frame may depend on as many as 9 preceding frames — and since a frame represents about 1/40th of a second, your ears notice it.</p><p>Phonograph solves this problem by linking chunks together: each chunk appends the first few kilobytes of the next chunk’s raw data to its own. Rather than stopping playback at the end of the audio that ‘belongs’ to the chunk, it continues for a fraction of a second while the next chunk starts playing silently. Once it’s safe to do so, Phonograph silences the first clip and unsilences the second.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*v0Z_cxqnkQehLZHwY814Tg.png" /></figure><p>Synchronising the volume changes is easy since the web audio API lets us schedule things like volume changes to a quadrillionth of a second. (Really! That’s a millionth of a billionth. Of course your hardware can’t possibly match that precision, but it means that two events scheduled for the same moment in time will definitely happen together.)</p><p>But our problems have only just begun.</p><h3>How long is a piece of audio?</h3><p>When you decode a chunk of audio with context.decodeAudioData(…), the resulting <a href="https://developer.mozilla.org/en-US/docs/Web/API/AudioBuffer">AudioBuffer</a> has a <em>duration</em> property representing the length of the clip in seconds. We need to know that in order to schedule playback of the subsequent chunk.</p><p>The problem is that <em>duration</em> is quite likely to be a complete fiction.</p><p>Safari will read the <em>file header</em> rather than the <em>frame header</em> for the first chunk, basing the duration property on how long the mp3 file claims to be, rather than what has actually been decoded. After that, it does a much better job, but that doesn’t help us if the second chunk starts playing several minutes too late.</p><p>Chrome, meanwhile, essentially spits out random numbers. It’s fine for CBR files — those encoded at a constant bitrate, like 128kbps — but for variable bitrate (VBR) files it’s likely to underestimate wildly.</p><p>When browser APIs fail us, we only have one choice: implement the logic the hard way in JavaScript.</p><h3>The anatomy of an mp3 file</h3><p>In order to do that, we need to understand the data we’re dealing with at the level of ones and zeroes. Helpfully, while the mp3 specification is copyrighted (you can get the paper if you want to pay for it; I don’t), kind and smart people have reverse engineered it and <a href="http://mpgedit.org/mpgedit/mpeg_format/mpeghdr.htm">made their findings freely available</a>. (If you’re interested in this stuff, <a href="http://blog.bjrn.se/2008/10/lets-build-mp3-decoder.html">Let’s build an MP3-decoder</a> by Björn Edström is also essential reading, though it doesn’t strictly pertain to what we’re doing with Phonograph.)</p><p>For our current purposes, we need to understand a few things:</p><ul><li>An MP3 file is composed of a series of frames. Each frame contains 1152 <em>samples</em>. Typically, the sample rate is 44.1kHz or 48kHz, making a frame last around 1/40th of a second.</li><li>Each frame has a frame header followed by the ‘main data’.</li><li>Frame headers are four bytes (32 bits) long and contain information about encoding format, sample rate, bitrate, number of channels, and various other things. Each header starts with 11 ones (or 12, if you don’t support MPEG Version 2.5, which Phonograph currently doesn’t) — this is called <em>frame sync </em>and is easy to identify in a sea of binary data, though you will occasionally get false positives.</li><li>Some of these things change from one frame to the next, meaning you can’t just look out for the same four byte sequence to identify frame headers. But some things don’t, making it possible to eliminate false positives from the frame sync check.</li></ul><p>Knowing all that, we can scan through the data one byte at a time until we find a frame sync header, eliminate false positives by checking that the frame’s metadata matches our reference header (the first one encountered) vis-à-vis sample rate and so on, and increment a frame counter.</p><p>Once we know how many frames there are, we multiply that number by 1152 and divide it by the sample rate (which we know from the reference header) to determine the exact duration of the clip.</p><p>Surprisingly, all this happens in fractions of a millisecond, meaning the page continues to work smoothly and the user is none the wiser.</p><h3>But seriously</h3><p>These are quite mad lengths to go to for something as straightforward as playing sound. Insisting on a user gesture for <em>&lt;audio&gt;</em> playback doesn’t accomplish anything except artificially restricting the capabilities of the web relative to native apps. It’s not in any spec, doesn’t achieve its stated goals, and simply results in a worse experience for both developers and users.</p><p>Browser makers should accept that it was a dumb mistake, and change the behaviour of mobile browsers to match desktop browsers.</p><h3>Phonograph is a work in progress</h3><p>We’ve found it to be pretty robust on Safari and Chrome, which dominate mobile browser usage. Firefox and Opera are a different story, since both apparently refuse to decode mp3 data.</p><p>Since we plan to use it in future projects, we’ll keep hammering away at these issues. But in the meantime, we’d love additional feedback and bug reports, so if you end up using Phonograph in a project please let us know how you get on.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=55286bd5e567" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to not break the internet with this one weird trick]]></title>
            <link>https://medium.com/@Rich_Harris/how-to-not-break-the-internet-with-this-one-weird-trick-e3e2d57fee28?source=rss-a11f7c00cbbe------2</link>
            <guid isPermaLink="false">https://medium.com/p/e3e2d57fee28</guid>
            <category><![CDATA[npm]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[modules]]></category>
            <dc:creator><![CDATA[Rich Harris]]></dc:creator>
            <pubDate>Wed, 23 Mar 2016 03:16:16 GMT</pubDate>
            <atom:updated>2016-03-23T03:16:16.962Z</atom:updated>
            <content:encoded><![CDATA[<h4>If you write JavaScript tools or libraries, you should bundle your code before publishing.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gqH7whxvsiu0G6vLqqgn_A.jpeg" /><figcaption>The internet</figcaption></figure><p>A few hours ago, <a href="https://medium.com/u/fb615fdcd90b">Azer Koçulu</a> <a href="https://medium.com/@azerbike/i-ve-just-liberated-my-modules-9045c06be67c#.z9m5qny4f">‘liberated’ his collection of modules from npm</a> following a trademark dispute. One of them — an <a href="https://github.com/azer/left-pad/blob/master/index.js">11-line utility</a> for putting zeroes in front of strings — was heavily depended on by other modules, including Babel, which is heavily depended on by the entire internet.</p><p>And so the internet broke.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hOUoUMycA47wcV0ZSMAp1A.png" /><figcaption><a href="https://github.com/azer/left-pad/issues/4#issuecomment-200051244">Source</a></figcaption></figure><p>People confirmed their biases:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MCe3LxfDHwv3osmH_-rEoQ.png" /><figcaption><a href="https://github.com/azer/left-pad/issues/4#issuecomment-200056459">Source</a>. (My thoughts on tiny modules are <a href="https://medium.com/@Rich_Harris/small-modules-it-s-not-quite-that-simple-3ca532d65de4#.x7ck8knoa">here</a>.)</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*s6YsTrpvi8zz4yg3duTEBQ.png" /><figcaption><a href="https://github.com/azer/left-pad/issues/4#issuecomment-200060983">Source</a>. (Some people would take issue with the ‘fixed in minutes’ part.)</figcaption></figure><p>People panicked:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-5Us75WuxccR2swXWFn5Bg.png" /><figcaption><a href="https://twitter.com/seldo/status/712414588281552900">Source</a></figcaption></figure><p>And people got angry:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*L0_7aNJIqOZMU9nJ_344kA.png" /><figcaption><a href="https://github.com/azer/left-pad/issues/4#issuecomment-200064149">Source</a></figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VMOT-3vWCb8y95iEBtEgUw.png" /><figcaption><a href="https://github.com/azer/left-pad/issues/4#issuecomment-200066563">Source</a></figcaption></figure><p>Everyone involved here has my sympathy. The situation sucks for everyone, not least Azer (who owes none of you ingrates a damn thing!). But reading the <a href="https://github.com/azer/left-pad/issues/4">GitHub thread</a> should leave you thoroughly exasperated, because this problem is <em>very easily solved</em>.</p><h3>Bundle your code, even if it’s not for the browser</h3><p>Just to recap:</p><ol><li>left-pad was unpublished</li><li>Babel uses fixed versions of its dependencies, one of which (transitively) was left-pad</li><li>When you install Babel, you also install all its dependencies (and their dependencies)</li><li>Therefore all old versions of Babel were hosed (until left-pad was un-unpublished)</li><li>People blame Azer</li></ol><p>The key item here is number 3. Suppose that instead of listing all those dependencies in package.json, Babel was distributed in bundled form instead, with all its dependencies inlined. (I’m picking on Babel because it’s the most high-profile casualty of the left-pad debacle, but Babel is just adhering to community norms — norms that I’m about to argue don’t really make sense.)</p><p>Had that been the case, old versions of Babel would continue to function perfectly well, and the maintainers would have the opportunity to find an alternative solution before the next version.</p><p>Full disclosure: as the creator of <a href="http://rollupjs.org">Rollup</a>, <em>of course</em> I think you should bundle your code. But for the purposes of this article, it doesn’t matter whether you use Rollup, Webpack, Browserify or something else entirely — it’s the idea that matters, not the specifics.</p><p>It turns out that as well as avoiding the chaos we all just experienced, this comes with an array of fairly significant benefits.</p><h3>Installing takes a tiny fraction of the time</h3><p>Have you tried installing a package with many dependencies through npm3 on a bad connection? It’s a hilariously terrible experience, largely because there’s a combinatorial explosion of network requests for dependencies and dependencies’ dependencies. Depending on the authors of those dependencies, you may be downloading READMEs, tests and other assorted gubbins that you don’t need. If a package has no dependencies, npm can download a single tar file and be done with it.</p><p>For a completely non-scientific demonstration, let’s take three packages that do <em>roughly</em> similar things — Rollup, Webpack and Browserify. How long does a fresh install take of each?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FGdKev32clVnhDG_UFjrDg.png" /></figure><p>Browserify, which has 47 immediate dependencies (many of which have their own dependencies, of course), took 26.4 seconds to install. Webpack, which has 15, took almost 30 seconds. Rollup, which has 3 (all for the built-in CLI — we could arguably remove even those), took less than 5. That’s a much better developer experience.</p><p>Rollup <em>has</em> dependencies, we just bundle them before publishing instead of forcing you to download them separately. (Yes, Rollup rolls up Rollup.)</p><h3>You waste less disk space</h3><p>As a corollary to the above, if you don’t download hundreds of transitive dependencies (plus aforementioned gubbins), it unsurprisingly takes less space on disk. Using npm3, Browserify takes up 12.2Mb for 2,459 items. Webpack takes 21Mb for 3,093 items! Rollup takes up 2.5Mb for 209 items.</p><p>(Of course I’m aware that they’re not directly comparable. But you get my point.)</p><h3>Startup is quicker</h3><p>In case you haven’t heard, <a href="https://kev.inburke.com/kevin/node-require-is-dog-slow/">Node’s `require` is dog slow</a>. And I’m going to pick on Babel again — when Babel 6 came out, having been rearchitected into a gazillion tiny modules, I found it to be completely unusable because it took about 3 seconds to start up. That’s a real bummer when you’re used to nice quick builds. Upgrading to npm3 drastically improved the situation because of the flat node_modules structure, but that’s a shame, because npm2 is much quicker.</p><h3>Your library is more stable</h3><p>If you use semver ranges for your dependencies, changes to those dependencies will filter through to the users of your library. In theory that’s a good thing — your users get bug fixes without you doing anything! — but in practice it’s just as likely to result in breakage.</p><p>(If you think semver protects you from that sort of breakage, you probably haven’t been doing this very long.)</p><h3>You make it harder for villains to be villainous</h3><p>Similarly, if your users are downloading your dependencies when they download your library, they’re not protected against malicious actors taking control of those dependencies on npm. And that’s a real risk:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3KwiXWLVGNFb6oTLPyGMjg.png" /><figcaption><a href="https://github.com/azer/left-pad/issues/4#issuecomment-200091562">Source</a></figcaption></figure><p>I have it on good authority that many financial institutions refuse to use Node and npm for this exact reason — it’s much too easy for someone to use npm to spread malicious code. Shipping bundled code reduces the surface area for these sorts of attacks.</p><h3>You’re not forcing your users to bundle it for you</h3><p>Particularly if your library is suitable for use in the browser, it’s likely that someone <em>else</em> will need to bundle it at some point. And then things get tricky, because they have to have the right combination of tooling and configuration to avoid any surprises. The <a href="https://pouchdb.com/">PouchDB</a> team found that their users were having difficulty using Webpack with their library, because it was built with Browserify in mind. Eventually they had a come-to-Jesus moment and <a href="https://pouchdb.com/2016/01/13/pouchdb-5.2.0-a-better-build-system-with-rollup.html">started shipping a single giant index.js file</a>.</p><p>Expecting users to bundle your source code is no different from expecting them to compile your ES6/TypeScript/CoffeeScript/whatever. And that’s just rude. It’s the difference between giving someone raw ingredients and a cooked meal — if you went to a restaurant and ordered a burger, you’d be pretty mad if they gave you half a pound of minced beef and a frying pan instead.</p><h3>Okay I take your point but you’re obviously wrong</h3><p>By now some of you are thinking I’ve clearly missed the point. You probably object on one of the following grounds:</p><ol><li>Bundling prevents de-duplication. If two of my dependencies, foo and bar, share a third dependency, baz, I don’t want baz to appear twice in my code.</li><li>Waaaaaahhhhh, I don’t like build steps.</li></ol><p>You’ll probably detect that I don’t have much sympathy for the second objection. If this is you, I get where you’re coming from, but it’s 2016 and this is how we do things now (for a reason). Read <a href="http://blog.keithcirkel.co.uk/how-to-use-npm-as-a-build-tool/">Keith Cirkel’s article</a> on how to use npm run scripts, and see if you still think build steps are unduly burdensome.</p><p>The first objection is more convincing. It’s certainly <em>not</em> an objection against the weaker claim that you should definitely bundle your <em>own</em> code (i.e. if you have many files in lib/ or src/, you should bundle those), but it does have some merit against the claim that you should bundle your own code <em>plus</em> your dependencies.</p><p>But it only applies to browsers, because in a Node environment I don’t care if some stuff is duplicated, and nor should you — disk space is cheap, sanity is expensive. And in my experience, we overestimate the actual savings to be had by bundling at the last possible moment instead of at a per-package level. It’s something you should consider on a case-by-case basis, weighing it against the voluminous benefits outlined above.</p><h3>I’m sold! What now?</h3><p>Learn how to use a module bundler (preferably <a href="http://rollupjs.org">Rollup</a> or <a href="http://jspm.io/">JSPM</a>! But also <a href="https://webpack.github.io/">Webpack</a> or <a href="http://browserify.org/">Browserify</a>) and use npm’s prepublish hook to bundle your code. Tell your friends to do the same.</p><p>And stop blaming people like Azer when your stuff breaks.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e3e2d57fee28" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Tree-shaking versus dead code elimination]]></title>
            <link>https://medium.com/@Rich_Harris/tree-shaking-versus-dead-code-elimination-d3765df85c80?source=rss-a11f7c00cbbe------2</link>
            <guid isPermaLink="false">https://medium.com/p/d3765df85c80</guid>
            <category><![CDATA[tree-shaking]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[rollup]]></category>
            <dc:creator><![CDATA[Rich Harris]]></dc:creator>
            <pubDate>Mon, 21 Dec 2015 23:14:04 GMT</pubDate>
            <atom:updated>2019-01-15T02:13:42.924Z</atom:updated>
            <content:encoded><![CDATA[<p>I’ve been working (albeit sporadically of late, admittedly) on a tool called <a href="http://rollupjs.org">Rollup</a>, which bundles together JavaScript modules. One of its features is <em>tree-shaking</em>, by which I mean that it only includes the bits of code your bundle actually needs to run.</p><p>Axel Rauschmayer asked where the term came from…</p><h3>Axel Rauschmayer on Twitter: &quot;@Rich_Harris Do you know where the term &quot;tree-shaking&quot; comes from? Haven&#39;t seen it before Rollup. / Twitter&quot;</h3><p>@Rich_Harris Do you know where the term &quot;tree-shaking&quot; comes from? Haven&#39;t seen it before Rollup.</p><p>…and Amjad Masad said that it’s really just a different name for <em>dead code elimination</em>…</p><h3>Amjad Masad on Twitter</h3><p>@addyosmani @rauschma @Rich_Harris @sethladd FWIW I think it&#39;s just dead code elimination. I don&#39;t see how it&#39;s different.</p><p>…which Sebastian McKenzie thinks is silly:</p><h3>Sebastian McKenzie on Twitter</h3><p>@amasad @addyosmani @rauschma @rich_harris @sethladd the term tree shaking is silly. it&#39;s the same as using transpiler instead of compiler</p><p>But they are in fact different things, even if they have the same goal (less code).</p><h4>Dead code elimination is silly</h4><p>Bad analogy time: imagine that you made cakes by throwing whole eggs into the mixing bowl and smashing them up, instead of cracking them open and pouring the contents out. Once the cake comes out of the oven, you remove the fragments of eggshell, except that’s quite tricky so most of the eggshell gets left in there.</p><p>You’d probably eat less cake, for one thing.</p><p>That’s what dead code elimination consists of — taking the finished product, and imperfectly removing bits you don’t want. Tree-shaking, on the other hand, asks the opposite question: given that I want to make a cake, which bits of what ingredients do I need to include in the mixing bowl?</p><p>Rather than <em>excluding dead code</em>, we’re <em>including live code</em>. Ideally the end result would be the same, but because of the limitations of static analysis in JavaScript that’s not the case. <a href="http://rollupjs.org/#%7B%22options%22%3A%7B%22format%22%3A%22cjs%22%2C%22moduleName%22%3A%22myBundle%22%2C%22globals%22%3A%7B%7D%7D%2C%22modules%22%3A%5B%7B%22name%22%3A%22main.js%22%2C%22code%22%3A%22import%20%7B%20foo%2C%20bar%20%7D%20from%20&#39;.%2Futils.js&#39;%3B%5Cn%5Cnconsole.log(%20foo.a%20)%3B%5Cn%5Cn%2F*%20uncomment%20this%20line%2C%20see%20what%20happens%20*%2F%5Cn%2F%2Fconsole.log(%20bar.b%20)%3B%22%7D%2C%7B%22name%22%3A%22utils.js%22%2C%22code%22%3A%22var%20foo%20%3D%20%7B%7D%3B%5Cnfoo.a%20%3D%201%3B%5Cn%5Cn%2F%2F%20A%20dead%20code%20eliminator%20would%20struggle%20to%20get%20rid%20of%5Cn%2F%2F%20the%20next%20two%20lines.%20For%20a%20tree%20shaker%2C%20it&#39;s%20easy%5Cnvar%20bar%20%3D%20%7B%7D%3B%5Cnbar.b%20%3D%202%3B%5Cn%5Cnexport%20%7B%20foo%2C%20bar%20%7D%3B%22%7D%5D%7D">Live code inclusion gets better results</a>, and is <em>prima facie</em> a more logical approach to the problem of preventing our users from downloading unused code.</p><p>It’s an idea that only really makes sense if you’re thinking in terms of modules and bundles though (since the contents of the ‘entry file’ can be thought of as roughly equivalent to the contents of the main method in other languages).</p><p>(As it happens, Rollup isn’t perfect — more on this below — so the best results are to be had by doing both steps, i.e. Rollup then UglifyJS or Webpack 2 with the Uglify plugin.)</p><h4>Yeah, it’s probably not a great name</h4><p>For one thing, it implies that you are indeed shaking off dead branches. But it has been used by people other than me to describe this ‘start with what you need, and work outwards’ technique (as opposed to ‘start with everything, and work backwards), mostly in the Dart community (though <a href="https://twitter.com/rauschma/status/678835068165595136">people have found other references</a>). I can’t remember exactly where I picked up the term.</p><p>I thought about using the ‘live code inclusion’ phrase with Rollup, but it seemed that I’d just be adding even more confusion seeing as tree-shaking is an existing concept. Maybe that was the wrong decision?</p><h4>Limits of Rollup’s tree-shaking</h4><p>It doesn’t currently remove things like unused methods from objects that <em>are</em> used, and there are times when it is forced to assume the worst in order to ensure the resulting program is correct. That’s because static analysis is difficult in a dynamic language like JavaScript. Look out for improvements in 2016 as we add type tracking and other techniques.</p><h4>Rollup is about more than tree-shaking</h4><p>The goal of Rollup is to produce <em>maximally efficient bundles</em> that look like a human wrote them. Tree-shaking is part of that, but there are other things Rollup does (or more correctly, doesn’t do) — it doesn’t wrap modules in functions, it doesn’t put a module loader at the top of your bundle, and it doesn’t generate the resulting code from an intermediate AST, but rather preserves your original code as far as possible. Because of that it’s a particularly good choice for writing libraries (it <em>can</em> be used for apps, though Webpack for example has a ton of features Rollup doesn’t, so YMMV), especially ones with few non-ES6 dependencies (though it <a href="https://github.com/rollup/rollup-plugin-commonjs">can use CommonJS modules</a>).</p><p>Anyway, that’s enough sales pitch. Hope this post sheds some light on the whole deal.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d3765df85c80" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Don’t crAMP our style]]></title>
            <link>https://medium.com/@Rich_Harris/don-t-cramp-our-style-9bcef09e638f?source=rss-a11f7c00cbbe------2</link>
            <guid isPermaLink="false">https://medium.com/p/9bcef09e638f</guid>
            <category><![CDATA[journalism]]></category>
            <category><![CDATA[google]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Rich Harris]]></dc:creator>
            <pubDate>Fri, 18 Dec 2015 17:52:47 GMT</pubDate>
            <atom:updated>2015-12-18T17:52:47.161Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kXvSu25alNAxqGiAbAVlOw.jpeg" /><figcaption><a href="https://www.flickr.com/photos/affers/2206560659/in/photolist-4mZcBi-2av6nP-vbLaQ-b6QYHx-aks8T9-874JmH-86sXrN-9k3p9Q-iw3Y8L-jbFKK-xqHWbH-7TB6qh-9HcvuZ-aGwfC4-2JvdUC-nwbdD8-jNQTEa-7vfUqj-jNSzSx-gN9zq-sEbrX-M7PY4-jNUaBq-54knXv-8aHgBA-jNUUAm-e3TkJY-eZHTcz-4JJkjx-e3T2hE-5qYPtH-ewxj3-eQLSK-5vZEvF-nifivw-9yxVus-6DQQj-7ZpSiq-bSwuf-oaGC9V-9tfcek-6r8sKi-6rcCo7-fE2BmQ-4GheUa-8nLHTh-eMCAn1-7qctPE-BR7iP-53jYQn"><em>Long Road Photography on Flickr</em></a><em>, used under Creative Commons license</em></figcaption></figure><p>Once again, the turkeys have voted for Christmas. The ailing news industry has rushed headlong into an alliance with Google, which is trying to stave off its own existential threats. It’s an alliance in which we are clearly the junior partner, and one that will hobble the diversity and development of digital journalism. And it could all have been avoided.</p><p>Google’s <a href="https://www.ampproject.org/">Accelerated Mobile Pages</a> project, or AMP, is one of those ideas that’s hard to argue with at first: an open source endeavour to create a subset of HTML that allows pages to be aggressively cached, and that limits the ability of careless web developers to create bad experiences on the mobile web by stuffing pages to the gills with badly made ads, overweight images, and six different analytics providers. Apps like Twitter (<a href="https://blog.twitter.com/2015/introducing-accelerated-mobile-pages-0">an AMP launch partner</a>) will be able to show these pages instantly, and users will be spared a screen full of invitations to download-our-app or fill-in-our-survey before they can scroll past the ads to get to the article (which hasn’t actually rendered yet because the web fonts are still downloading).</p><p>It sounds great. But there are some problems.</p><h4>Caught in the crossfire</h4><p>The background to AMP’s development is important. The mobile web — open, but slow, crufty and unreliable — is (at least perceived to be) losing ground to native apps — closed, but fast and functional. For a company that makes most of its money selling ads on the web, this is a problem. Meanwhile, Apple’s decision to allow ad blockers on iOS is a gift to users on one level, but on another is a a strategic move designed to weaken Google. The two companies are at war, and we’re stuck in the middle.</p><p>Of the two, we should prefer to side with Google. Apple has demonstrated its contempt for editorial freedom by banning apps like <a href="http://www.theguardian.com/technology/appsblog/2011/sep/14/apple-phone-story-rejection">Phone Story</a> and <a href="http://gawker.com/apple-kills-drone-strike-news-app-for-being-too-crude-1733402994">Dronestream</a> — actions that should prevent any news organisation from doing business with them, except here in reality, where companies as powerful as Apple get to behave as they please. By comparison, Google is warm and fuzzy.</p><p>And we should want the open web to win. But we’re setting the stage for a Pyrrhic victory, in which the web that wins is a crippled web.</p><h4>Full disclosure</h4><p>I should declare an interest: AMP could put me and my tribe out of work. I’m a JavaScript journalist at the Guardian (my business card says ‘interactive editor’, but interactivity isn’t always the defining feature of our work), and AMP disallows arbitrary JavaScript on the page.</p><p>Some people regard this as an inevitability, or even something to be welcomed — see, for example, Heather Billings’ Nieman Lab prediction for 2016 entitled ‘<a href="http://www.niemanlab.org/2015/12/static-is-the-new-interactive/">Static is the new interactive</a>’, or Thomas Wilburn’s comments on ‘<a href="https://source.opennews.org/en-US/articles/what-amp-maybe-means-news-developers/">What AMP (maybe) means for news developers</a>’.</p><h3>Rich Harris on Twitter</h3><p>Argh! &quot;We should stop innovating, and capitulate to Google&#39;s agenda.&quot; No, we shouldn&#39;t. We&#39;re witnessing the birth https://twitter.com/NiemanLab/status/676773833567772672 ...</p><h3>Rich Harris on Twitter</h3><p>of a fundamentally new form of media, and there are some growing pains. We SHOULD speed up our sites, and we SHOULD avoid gratuitous</p><h3>Rich Harris on Twitter</h3><p>interactivity that harms usability, but arguing that we should abandon JavaScript journalism and revert to print-but-on-the-web is like</p><h3>Rich Harris on Twitter</h3><p>arguing that we shouldn&#39;t cook food because fire is difficult to control. My colleagues here and in other newsrooms produce journalism that</p><h3>Rich Harris on Twitter</h3><p>simply couldn&#39;t have existed even two years ago - journalism that has the power to effect social change. We need more of it, not less.</p><p>I have a different perspective. The team I’m a part of produces journalism that couldn’t exist on a completely static page — that’s the whole point of it. Earlier this week we produced an <a href="http://www.theguardian.com/us-news/ng-interactive/2015/dec/14/gun-control-laws-congress-shooting-deaths-nra-lobby-campaign-donations">interactive map</a> that makes it easier for you to hold your elected representatives to account for their inaction on gun violence. A few weeks ago we made a scrubbable video player that allows you to <a href="http://www.theguardian.com/us-news/2015/sep/01/moving-targets-police-shootings-vehicles-the-counted">examine footage of police shootings</a> — footage that tells a different story when you’re able to watch it closely frame-by-frame, over and over. And earlier in the year we helped launch <a href="http://theguardian.com/thecounted">The Counted</a>, an interactive database of US police killings, which the FBI are now <a href="http://www.theguardian.com/us-news/2015/dec/09/fbi-launch-new-system-count-people-killed-police-officers-the-counted">going to copy</a>.</p><p>And that’s before we talk about beautiful <a href="http://www.sbs.com.au/theboat/">multimedia graphic novels</a>, or simulator journalism that places you inside an otherwise <a href="http://ncase.me/polygons/">hard-to-understand system</a>, or <a href="http://www.nytimes.com/interactive/2015/05/03/upshot/the-best-and-worst-places-to-grow-up-how-your-area-compares.html?_r=0">articles that rewrite themselves</a> to be more relevant to the reader. Often these are standalone set-piece interactives, but the trend in recent years has been towards greater integration between interactive teams and the newsrooms they’re a part of. The big experimental pieces prove out ideas and forms that eventually become part of the fabric of digital storytelling.</p><p>JavaScript journalism is expensive, because it’s time-consuming, and because there aren’t enough journalist-coders (most of us doing it professionally are self-taught obsessives), and because creating reusable code on newsroom deadlines is incredibly tough. We’re still figuring out the ‘grammar’ of interactivity, and we still get a lot of things wrong on a regular basis.</p><p>But the birth of JavaScript journalism is a pivotal moment in the history of news — journalism that has the potential to one day be richer, deeper and more personal than anything that came before. And there’s no place for it in Google’s vision of the future.</p><h4>With friends like these…</h4><p>The Guardian, an <a href="http://www.theguardian.com/help/insideguardian/2015/oct/07/todays-developer-release-of-accelerated-mobile-pages-amp">AMP launch partner</a>, has a very close relationship with Google (literally — Google are currently building their UK headquarters a stone’s throw from Guardian Towers in Kings Cross), and the same is true of many news organisations. We <em>like</em> Googlers, they’re nice people.</p><p>Because of that, it’s easy to forget that our partnerships are based on mutual self-interest rather than genuine shared values. In its bones, Google doesn’t care about journalism — it cares about #content.</p><p>So we should be concerned when Google’s Richard Ingras <a href="http://www.poynter.org/news/mediawire/390124/inside-googles-plan-to-speed-up-the-mobile-web/">confirms</a> that AMP pages will get special treatment in search results:</p><blockquote>we’ll actually show and feature articles from publishers using AMP in search results … there will be a carousel that will trigger on the page of results when there is sufficient coverage related to that query, so users can have the full benefit of getting the answers that they’re looking for and experiencing them in an instantaneous fashion.</blockquote><p>Somehow we decided that it was okay that an advertising company gets to control what journalism sees the light of day.</p><p>Google have said that AMP won’t affect search rankings (except indirectly, because page speed is a ranking factor), and that there will be ‘escape hatches’ for all the unsanctioned stuff (‘click to learn more’, which experience tells us will generate as much engagement as a banner saying ‘To view this web content you need to install Java Runtime Environment’). None of it matters. If news organisations see AMP pages replacing a significant portion of their traffic, that’s what they’ll optimise for — bland, conformist #content. The incentive to experiment with new forms of storytelling will be vastly reduced when journalists have to explain to their editors that they want to create something most of their readers won’t be able to use. ‘Can’t you just do a GIF or something?’</p><p>Take a snapshot of what digital journalism looks like today. I hope you like it.</p><h4>Frequently asked questions</h4><p><strong>Aren’t you over-reacting?</strong> Of course I am. I’m an endangered species! But I want the level-headed decision makers at organisations like mine to truly understand what AMP takes away, as well as what it gives us.</p><p><strong>It’s better than Facebook Instant Articles though, right?</strong> Ugh. Yes. Anything is better than Facebook Instant Articles.</p><p><strong>What’s the alternative?</strong> I’m glad you asked. It’s quite simple really: we get better. We build faster sites. We band together with other news organisations to enforce some basic standards around the quality of advertising code. We consolidate our analytics and stop tracking our users so much. (I’m proud to say<a href="http://www.nytimes.com/interactive/2015/10/01/business/cost-of-mobile-ads.html?_r=0"> the Guardian is an industry leader</a> when it comes to all this stuff). In other words, we do the things the AMP project wants us to do, but we do it on our terms.</p><p>So thank you, Google, for being on the side of the open web, and for shaming us into doing something about our bloaty websites. But we’ll take it from here.</p><p><em>Thanks to Kenton Powell, Nadja Popovich and Kenan Davis for reading drafts of this article, and to Michael Keller for helping to refine these ideas. Thanks also to Caty Enders and Anna Codrea-Rado for convincing me that my wild-eyed rant would work better as a blog post, though they might have just been trying to get me to shut up.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9bcef09e638f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Small modules: it’s not quite that simple]]></title>
            <link>https://medium.com/@Rich_Harris/small-modules-it-s-not-quite-that-simple-3ca532d65de4?source=rss-a11f7c00cbbe------2</link>
            <guid isPermaLink="false">https://medium.com/p/3ca532d65de4</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[modules]]></category>
            <category><![CDATA[npm]]></category>
            <dc:creator><![CDATA[Rich Harris]]></dc:creator>
            <pubDate>Fri, 03 Jul 2015 14:15:36 GMT</pubDate>
            <atom:updated>2016-10-01T05:39:12.231Z</atom:updated>
            <content:encoded><![CDATA[<p>There’s a <a href="http://dailyjs.com/2015/07/02/small-modules-complexity-over-size/">post currently doing the rounds</a> that articulates the ‘small modules’ philosophy. It’s written by <a href="https://twitter.com/sindresorhus">Sindre Sorhus</a>, whose <a href="https://www.npmjs.com/~sindresorhus">npm profile</a> is enough to make all but the most prolific developer feel wholly inadequate, and so carries with it a degree of authority.</p><p>Like a lot of ideas that seem to be self-evident and inarguable, ‘small modules’ contains a lot of truth but does not, I believe, bear close scrutiny. But because it is espoused by so many leading members of the JavaScript community, scrutiny is all too rarely applied.</p><p>I think I know why: <strong>it’s because the small modules philosophy favours library authors</strong> (like Sindre) <strong>at the ultimate expense of library users</strong>. Because library authors typically have larger megaphones (more Twitter followers, more credibility on GitHub and Hacker News, etc), their voices are heard more often.</p><p>But library users are more numerous, and so their needs are more important.</p><h3>Before I begin</h3><p>I’m someone who has benefitted a great deal from npm, and from the wealth of code available therein. In particular, I’m a frequent user of some of Sindre’s modules. The web is a better place because of him, and — even though node modules have certain design flaws from a client-side perspective — node and npm did more to introduce the concept of modularity to JavaScript developers than a thousand blog posts could have. Thank you Sindre, and thank you everyone involved in making node and npm.</p><p>What follows is not an absolutist claim that small modules are ‘bad’, or that we should explicitly strive to make ‘big modules’, but rather an attempt to bring out some of the nuance that is often glossed over by proponents of the small modules philosophy.</p><h3>Why npm is the most popular package manager</h3><p>You’ll often see presentations showing that <a href="http://www.modulecounts.com/">npm has more modules</a> than any other package manager, and is experiencing faster growth. The natural assumption is that this is because a) it is the best package manager, and b) the JavaScript community is the most energetic and productive.</p><p>I offer an additional explanation: that we in the JavaScript world have a higher tolerance for nonsense and dreck. Perhaps my favourite npm package is <a href="https://www.npmjs.com/package/peter">peter</a>, which, when required, prints the following to the console:</p><blockquote>Once upon a day there was a person, named Peter DeMartini<br>He is awesome…<br>Have a nice day…</blockquote><p>It’s an extreme case, but npm is riddled with packages of similar value — packages with no documentation or README, that never advanced beyond version 0.0.1, and which have a dozen or so downloaders per month of whom the majority are most likely bots. I should know, I’ve written a few.</p><p>This happens because npm makes it ridiculously easy for people to release their half-baked experiments into the wild. The only barrier to entry is the difficulty of finding an unused package name. I’m all in favour of enabling creators, but npm lowers the barriers right to the floor, with predictable results. Bear this in mind when encountering npm stats.</p><h3>The ensuing discoverability problem</h3><p>Discoverability is often cited as npm’s biggest flaw. Many blog posts — scratch that, entire websites — have been created to try and mitigate the difficulty of finding what you need on npm. Everyone has an idea about how to make it easier to find needles in the haystack, but no-one bothers to ask what all this hay is doing here in the first place.</p><p>Eventually, you find a package that seems to suit your needs, more or less. But your problems have only just begun. It’s up to you to evaluate the library: does it have tests? Can you understand the source code? Is it actively maintained? Is the documentation easy to find and consult?</p><p>The most vocal small modules proponents don’t have this problem, because they practically <em>live</em> on npm and already know which packages they like — possibly making it harder for them to appreciate how severe the discoverability issues are for the rest of us.</p><h3>Nostalgia</h3><p>Back when JavaScript development was almost synonymous with jQuery development, our job was so much easier. jQuery has hundreds of functions, but once you’ve learned a few, you can predict with a high degree of accuracy how most of the others will work: the library is <em>learnable</em>. If you do get stuck, you can head to <a href="http://api.jquery.com/">api.jquery.com</a> and find the answers you need in a few seconds. Hipster millenial front-enders (including yours truly) may prefer other tools nowadays, but jQuery really is a masterpiece of design.</p><p>It’s also large enough that a community exists around it — a community that keeps it up-to-date, writes tutorials and blog posts, organises meetups and conferences, and answers your questions on Stack Overflow. You can go a long way as a developer with the jQuery Swiss Army knife in your pocket.</p><p>The ‘small modules’ adherent, by contrast, is able to use best-of-breed tools for her niche requirements. But she must learn where they are to be found, and develop an encyclopaedic knowledge of them, and if she gets stuck the chances are she’s on her own.</p><h3>But what about…?</h3><p>Yes, small modules are easier to write. Yes, they’re easier to test. Yes, it’s easier to adhere to <a href="https://gist.github.com/jashkenas/cbd2b088e20279ae2c8e">semver</a>. <strong>These are all things that make your life as a library author easier</strong>. As we’ve seen, they come at a cost for others.</p><p>This pattern exerts itself in another form: small modules allow library authors to become lazy. Why include that six-line helper function when you can do a one-line `require`? Never mind that the required module has its own dependencies, with their own dependencies, until your users suddenly find that `npm install` involves downloading 70Mb of gubbins split into 15,000 files. I exaggerate, but barely.</p><h3>Back to the future</h3><p>Recently I’ve been wondering what it would take for the pendulum to swing back the other way, so that we can once again use libraries like jQuery (broad, learnable, battle-tested, well-documented, actively-developed) without being subjected to <a href="https://gist.github.com/substack/68f8d502be42d5cd4942#gistcomment-1365101">modularity shaming</a>.</p><p>I believe the answer lies in ES6 modules. I’ve been working on a tool called <a href="https://github.com/rollup/rollup">Rollup</a> which presents one possible way that we could enjoy the benefits of a large utility library like jQuery (or D3, or Underscore/Lodash, or even Three.js) without adding unnecessary bulk to our projects, by only including the code you actually use. (It does this by statically analysing your code and its dependencies — no configuration required, and it’s extremely fast. The effects are <a href="https://twitter.com/mbostock/status/604339652506472449">drastic</a>.)</p><p>But I don’t want this to be a ‘you should use Rollup’ post — I’ll write that post when it becomes true (right now it’s a promising experiment, but an experiment nonetheless). Instead, I want us to consider more broadly the trade-offs involved when we talk about small modules, and to remember that the needs of the many outweigh the needs of the few.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3ca532d65de4" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[This doesn’t have to be the (front) end]]></title>
            <link>https://medium.com/@Rich_Harris/this-doesn-t-have-to-be-the-front-end-cf187e558a14?source=rss-a11f7c00cbbe------2</link>
            <guid isPermaLink="false">https://medium.com/p/cf187e558a14</guid>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Rich Harris]]></dc:creator>
            <pubDate>Tue, 31 Mar 2015 18:21:48 GMT</pubDate>
            <atom:updated>2015-04-01T16:16:50.012Z</atom:updated>
            <content:encoded><![CDATA[<p>If you haven’t read Trek Glowacki’s post <a href="https://medium.com/@trek/requiem-for-the-front-ender-2f27c41bedb5">Requiem for the “Front Ender”</a>, you should. It’s essentially a follow-up to <a href="http://discuss.emberjs.com/t/will-there-be-continued-support-for-people-that-do-not-want-to-use-ember-cli/7672/2">this answer</a> to the question of whether Ember will continue to support users who don’t wish to use <a href="http://www.ember-cli.com/">ember-cli</a>. Both are beautifully written, and contain a lot of truth.</p><p>Stripped to its core, the argument is that web development is no longer a field for amateurs, but for Engineers with a capital E. (Often, when this subject is discussed online, there’s an ugly hint of triumphalism involved — of developers who have already climbed the ladder delighting in pulling it up behind them — but that’s absolutely not the case with Trek, who clearly feels a lot of sympathy for those web developers who are exhausted by the constantly shifting goalposts of our industry.)</p><p>The danger is that this becomes a self-fulfilling prophecy. If we start to believe that it’s true, or <em>right</em>, that web development should belong to a minority of elites, then we’ll stop creating tools that non-experts can use. The specific issue at hand is whether your JavaScript app needs a build step, and the Ember community argues that it does. And they’re right, if you’re building an Ember app, especially since ember-cli is so darn good. But applied to the larger question, I’d argue something different:</p><p><strong>Build tools are a hack.</strong></p><p>Let me briefly establish some credentials before defending that claim. I wrote and co-maintain a build tool (<a href="https://github.com/gobblejs/gobble">Gobble</a> — a work-in-progress, but used in a few places), I wrote and co-maintain the ES6 module transpiler used in ember-cli (<a href="http://esperantojs.org/">Esperanto</a>), and long before that I was an early adopter of Grunt, cajoling my colleagues at the <a href="http://www.theguardian.com/">Guardian</a> into using our custom-designed build process (with automated versioning and deployment and all the bells and whistles). I love automation.</p><p>But build tools are a hack nonetheless. It’s not me saying that, it’s the <a href="https://http2.github.io/faq/#why-revise-http">IETF HTTP Working Group</a>:</p><blockquote>This has led the industry to a place where it’s considered Best Practice to do things like spriting, data: inlining, domain sharding and concatenation. These hacks are indications of underlying problems in the protocol itself, and cause a number of problems on their own when used.</blockquote><p>In other words, one of the main reasons we use build tools — to cut down the number of HTTP requests in production — will be largely irrelevant when HTTP/2 gains traction. Of course build tools do other things, such as compiling templates and minifying, but for a large number of applications the build process cost-to-benefit equation will look very different in the future. So in a sense, <em>build tools are a polyfill for HTTP/2 and JavaScript modules</em>.</p><p>(Yes, you’ll need to fetch and parse JavaScript modules before requesting their dependencies, but it’s not hard to imagine browser preloading mechanisms being extended to modules — as soon the browser sees the `import foo from ‘foo’` bit of the response stream, it could start fetching `foo.js`.)</p><p>Web development used to welcome the amateur with open arms. That’s no longer the case, and build tools (and the pressure we put on people who aren’t ready to use them) are exhibit A.</p><h3>Why this matters to me (and hopefully you)</h3><p>I don’t have a background in programming. As a young child, I tinkered with BASIC V, and for a time it was fun to try and induce epileptic fits by drawing as many circles and rectangles per second as our Acorn Archimedes 440 (yes, we were one of those families) could manage. But it was soon abandoned.</p><p>Later, after a philosophy degree and a journalism diploma, I tried to learn a bit of PHP so I could make a website, because that was what you did if you wanted a job in digital journalism. XAMPP. FTP. phpMyAdmin. cPanel. GUIs all the way, because I’m a normal human being, and I found the command line terrifying. Then I started working as a reporter, and forgot everything I’d just learned because I didn’t need it any more.</p><p>A year or so later, I became fascinated by the nascent field of ‘interactive journalism’ — people were building JavaScript apps to tell the news in new and exciting ways. I wanted in! So I started viewing source, and figuring out people’s secrets (huh, what does this <a href="http://raphaeljs.com/">Raphaël</a> thing do?), and discovering tricks to let me put JavaScript in article pages via our CMS, which definitely wasn’t designed for such things.</p><p>The upshot is that I was building things and learning how to be a programmer long before I thought of myself as one, and long before I became comfortable using the terminal. Hell, long before I even <em>had</em> a usable terminal, being a Windows user at the time.</p><p>Now, I see people in the same situation I was in a few years ago — people (journalists, artists, educators…) who want to use web technologies to build fun interactive stuff, but who definitely don’t self-identify as capital E Engineers — being told they need to use node and npm and grunt and gulp and uglifyjs and sass and git and MEAN and isomorphic this and ES6 that and eslint the other thing and don’t forget to add a test suite, otherwise what’s the point?</p><p>I’m not sure I would have made it.</p><h3>The terminal is harder than you think</h3><p>It’s incredibly hard to remember what it was like to not understand something that you understand now. But try. Try to remember how <em>lost</em> you felt the first time you opened Terminal.app. GUIs make the things we’re working with — folders and files and drives and servers — into almost physical, tangible things that our brains are well-evolved to understand, whereas the command lines forces you to build a complex mental model.</p><p>Build tools take that problem and exacerbate it, by adding additional layers of abstraction and indirection. The link between the code you wrote with your hands, and the code that ends up getting served, is gossamer-thin, and only in your head.</p><h3>We have to build the future we want</h3><p>At jQuery UK a few weeks ago, I gave a talk entitled <a href="http://jqueryuk.com/2015/videos.php?s=dismantling-the-barriers-to-entry">Dismantling the barriers to entry</a>, in which I argued that creating tools that are accessible to newcomers, without sacrificing power and flexibility, is both possible and urgent.</p><p>Personally, I want a future in which as many people as possible can participate in the web as creators and not just consumers. I believe most people feel the same way. But it won’t happen naturally: it has to be a conscious effort.</p><p>Don’t pull the ladder up behind you.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cf187e558a14" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>