Using MapLibre GL JS from a CDN in Typescript

When my recent project build came out at 2 megabytes for something that should have been dead simple, I realised bundling Maplibre, the webgl-based map library, was adding a lot of bulk.

Since we use maps in various projects and we don’t need to duplicate the library for each project/release I wanted to split it out onto our CDN and import it as required.

This guide is specific to MapLibre GL JS, but could apply to any large library, really.


Import the library

Maplibre isn’t an ES module, and I’m using a webpack config that won’t allow import(), so I wrote a small function to load a module as a global:

  const promises = {};
  function importModule(url) {
    if (promises[url]) {
      return promises[url];
    }
    promises[url] = new Promise((resolve, reject) => {
      const s = document.createElement('script');
      s.src = url;
      s.type = 'module';
      s.addEventListener('load', resolve);
      s.addEventListener('error', reject);
      document.head.appendChild(s);
    });
    return promises[url];
  }

From here we can load MapLibre from wherever we like. The docs suggest unpkg:

importModule('https://unpkg.com/maplibre-gl@^5.3.0/dist/maplibre-gl.js')
  .then(() => {
    console.log(window.maplibregl);
  });

Adding Typescript types to the CDN library

This works fine, but now Typescript has no idea what’s going on. We need to import the types.

Turns out you can plop a .d.ts file in the same directory as your code and Typescript will pick it up and provide types for the global variable.

Maplibre distributes .d.ts files in their releases, so I downloaded the release corresponding to my CDN version, unzipped the file, and placed it in the directory where I’m using maplibre. As if by magic, Typescript and JSDoc are now available:


Importing additional types

When you need specific types from the .d.ts file, you can import them as types. This seems obvious, but I got caught up with this.

import type { MapOptions } from './maplibre-gl';
const mapOptions = {
	container: mapRootEl,
	interactive: false
} as MapOptions;
map = new Map(mapOptions);

There’s two gotchas, don’t include .d.ts in the import name. Typescript will find it without, and complain with. Also make sure to import as type otherwise your build may fail.

And that’s about it. Everything TIL.

Isolating vlog speech using Krisp AI

On a steam train ride with my mum, she starts telling a story of the trains when she was young. So thinking quickly I whip out my phone, press record, and get her to hold it so I can actually record her voice over the background noise.

It comes out distorted to ever loving shit.

A shot of DaVinci Resolve 17, video editor, with a video and audio track

So this sucks. I have to go back to the original onboard camera mic but it’s SO loud with all the engine noise, cabin chatter, and clanking in the background. Even tweaking all the knobs, you can barely hear mum at all.

Are there any AI tools to isolate voice? I remembered I’ve been using Krisp at work to cut down on the construction noise from next door. Maybe if I run the audio through that…

So I set the sound output from my video editor to go through Krisp, plug in my recorder, and play it through. It’s tinny, it’s dropped some quieter bits, but it’s totally legible! Holy cow.

Now I’ve got an audio track of mum’s voice isolated from the carriage noise. I can mix it back together with the original to boost the voice portion and quieten down the rest. This is kinda a game changer for shitty vlog audio.

This is a pretty convoluted workflow, so it’s really only useful for emergencies like this. But I’m really happy that it managed to recover a happy little memory. And I hope one day Krisp (or someone else, I don’t mind) release either a standalone audio tool or a plugin for DaVinci Resolve.

As an aside, the Google Recorder app is officially off my christmas list. Any recommendations for a better one?

Why is it called pound sterling?

I was curious why the pound sterling was called such.

Turns out it’s from Latin, lībra pondō (“a pound by weight”), as in a pound of silver. Has the same roots as the lira.

The “£” symbol is a blackletter “L” for “libra”, similar to the imperial weight “lb”. 🤯

A google search converting 100 lbs to aud. 100 pound sterling is approx 180.15 Aussie Dollars at time of publication.

Occasionally when I’m converting currencies I’ll “convert 100 lbs to aud” for a laugh. Google understands I mean “gbp” and that’s always amused me. Now it feels more connected.

I wasn’t sure if this is one of those things Europeans know that was never taught in Australia, but it does seem pretty esoteric. And Shawn hadn’t heard it before so maybe not.

Source: couple of fascinating articles: