foyer

archived

rawr

factually inaccurate line art drawing of a centrosaurus
rawr [source]

I currently work with Deno a lot across a few projects and one of the things that I’ve been annoyed with is reusing code.

Here’s an example. Let’s say I have a project (Triceratops) wherein I’ve written a module that rendered horns or something. Then I start another project (Chasmosaurus) that would also benefit from using the Triceratops horns module. I could do any of the following:

Import (relatively) from the other directory. This isn’t good as changes in folder structure could break things.

import horns from "../triceratops/lib/horns.js"

Create a copy of the module. This wouldn’t be ideal if I need to patch or update something in the module.

lazy solution

Fortunately, Deno has import maps, so I ended up creating one with links to my local modules:

{
  "imports": {
    "horns": "/home/j/dev/core/horns.js"
  }
}

Now I can just import "horns" from any project and if a module’s file location is changed at some point, I would only have to update this import map.

I do have to add another flag when running though, which calls for another lazy solution — an alias:

alias rawr="deno run --allow-all --import-map=$HOME/.deno/core.json $1"

Rawr! 🦖