Skip to content

Instantly share code, notes, and snippets.

@Rich-Harris
Rich-Harris / ssr-this.md
Last active October 19, 2018 06:24
SSR and web components

Responding to this Twitter convo

Let's say I'm building a blog post template page using from kind of front-end framework — for the sake of argument, we'll use Svelte. On this page, we'll have a share button that lets people share the blog post via Twitter.

Twitter makes this straightforward using their tweet web intent — just create an <a> tag with the appropriate href, and Twitter will take it from there. This is great — no need to faff around with an API.

But constructing the URL gets a bit unwieldy, so we'd like to turn it into a component:

<TwitterShare
@Rich-Harris
Rich-Harris / import-json.md
Created July 25, 2017 19:54
Should `import data from './data.json'` be possible?

In Node, you can do this:

const data = require('./data.json');

You can even do this (and Node will try various paths — ./data.js, ./data/index.js, ./data.json, ./data/index.json — until one matches):

const data = require('./data');
@Rich-Harris
Rich-Harris / README.md
Last active July 28, 2017 13:24
Bare imports in manifest

A hastily-written strawman for how bare imports could be resolved in browsers — see this convo.

@Rich-Harris
Rich-Harris / namespaces.md
Created May 18, 2017 16:40
why deopting on `import *` is bad

Most people treat these is interchangeable:

// named import
import { bar } from './foo.js';
bar();

// namespace import
import * as foo from './foo.js';
foo.bar();
@Rich-Harris
Rich-Harris / prepack-svelte.md
Last active May 19, 2022 11:02
Is Prepack like Svelte?

Note: I'm not involved in Prepack in any way — please correct me if I say anything incorrect below!

A few people have asked me if Prepack and Svelte are similar projects with similar goals. The answer is 'no, they're not', but let's take a moment to explore why.

What is Prepack?

Prepack describes itself as a 'partial evaluator for JavaScript'. What that means is that it will run your code in a specialised interpreter that, rather than having some effect on the world (like printing a message to the console), will track the effects that would have happened and express them more directly.

So for example if you give it this code...

@Rich-Harris
Rich-Harris / transpile-your-things.md
Last active October 12, 2020 15:09
Don't ship untranspiled code

When Babel 6 came out, it was hard for a lot of packages to upgrade because it was essentially an entirely different category of thing than Babel 5. So what happened was that some packages upgraded, and some didn't — at least not straight away.

Some projects took the prima facie enlightened view that packages should expose untranspiled code, so that the consumers of that code could determine for themselves what needed to get transpiled based on the environments they supported.

That was a costly decision. If I was the author of an app that was using Babel 6, I couldn't import a library that was still using Babel 5 and shipping untranspiled code (because the configs were completely incompatible), and vice versa. Frankly, it was a bloody nuisance. We are bad at anticipating these sorts of issues. It will happen again at some point.

Adding a few extra bytes to pkg.main or pkg.module is a small price to pay for things just working. As well as avoiding the aforementioned headaches, it means that your

node_modules
@Rich-Harris
Rich-Harris / serverpush.md
Created April 2, 2017 15:26
Server-push for ES2015 modules on unpkg.com

Following this convo:

Your browser requests https://unpkg.com/foo:

<script type='module' src='https://unpkg.com/foo'></script>

unpkg responds with the contents of foo...

node_modules