Did you know that import maps can be rendered client-side? (And links)
Import maps have been billed as a way to get npm-style import labels (import {thing} from "dependency";
) in the browser without bundling or transpiling your code.
They do that, but they also do much more than what you’re used to with npm
and a package.json
file.
When you’re using Node and bundling your code, usually the “labels” are defined in the package.json
file that’s modified by the npm
command line tool. You don’t mess with it unless you’re asking for trouble. The map between the package name and the import name is set in stone and then committed into version control.
You can make this more dynamic with experimental loaders and various other shenanigans, but those aren’t easy or accessible to most developers.
But did you know that the only limitation on import maps in the browser is that they have to be “baked” and in the DOM
before the first JavaScript module?
This might not sound too useful until you remember that, y’know, browsers still support non-module JavaScript.
This means it’s possible to modify the import map in the browser using client-side JavaScript, in response to the features you detect, settings you retrieve (using localStorage
, for example), as long as you do it before any module
-based JavaScript is loaded.
You can remap your entire module tree based on what you discover, dynamically, on load.
If this script
element is the first one in your HTML…
<script>
const importMap = {
imports: {
// If the browser supports native compression streams we
// map the `compress` label to `compress-native.js`
// otherwise we map to a larger script that uses fflate
compress:
"CompressionStream" in window
? "./compress-native.js"
: "./compress-fflate.js",
},
};
const importmap = document.createElement("script");
importmap.type = "importmap";
importmap.textContent = JSON.stringify(importMap);
document.currentScript.after(importmap);
</script>
… then every module in your project that imports the module label compress
will either import compress-native.js
or compress-fflate.js
based on what the browser supports.
(I first saw this done in a blog post by Ayooluwa Isaiah.)
Import maps have the potential to change everything about how we structure our web development projects, which is why I’ve been very excited to put together a course that should help introduce you to Import Maps and test-driven development, because that’s an area where I think they can add value – right now – before you wait for the technology to be more broadly supported.
The course will be launched on Black Friday and will be available until the end oc Cyber Monday as a part of a massively discounted bundle that includes the course, both of my existing ebooks, and some bonuses.
You can sign up to be notified when the bundle is available.
Links #
- "Fauxtomation | A Working Library". “But perhaps a more instructive question is, how will the work be degraded? What do those jobs become?”
- "Inside New Orleans’ struggle with facial-recognition policing - POLITICO".
- "The product manager role is a mistake - Software Product Development Pills".
- "CSS { In Real Life } | Stop Using AI-Generated Images". “By choosing AI-generated images over those made by a human, editors are taking away one of the only sources of income available to those entering the field. This means that fewer people will choose to train in the profession.”
- "Crisis point in AI? - by Gary Marcus - Marcus on AI"
- "How git cherry-pick and revert use 3-way merge"
- "Your Small Imprecise Ask Is a Big Waste of Their Time | Stay SaaSy". In all the years I’ve been making websites, I don’t think I’ve ever worked with a manager who was capable of learning this lesson.
- ’Thoughts on “generative AI Art”'. ‘Your reaction of “this is exactly what I imagined” says more about the long road ahead that that idea still needed to go than about how good these systems are.’
- "Is the Internet really broken? – Elizabeth Tai". “The Internet hasn’t been a pleasant place for people like me, a writer for a long time. The fact that we are the prime generator of content for the Internet is ironic. There won’t be the Internet without us.”
- "Adactio: Journal—HTML web components"
- "The Commit that Updated a Thousand Demos—zachleat.com". This feels like an elegant way of solving a lot of problems without tying yourself to a proprietary API.
- "The Anti-Workforce - Ed Zitron’s Where’s Your Ed At". “Those in control do not understand or participate in the process of what makes them rich, and the result is a deeply broken and inefficient workforce driven by top-level incompetence and greed.”
- "Google Should Fire Sundar Pichai". “Fire him today and replace him with someone else. If you make $280 million a year and you have to let high performers go because you were so bad at business, you should be fired because you are bad at your job.” An old post, but still relevant.
- "Businesses don’t hire designers for their good work | Koos Looijesteijn"
- "Monaspace". The good thing about bad companies releasing interesting stuff with an open license, like fonts, is you can use it without directly supporting them.
- "Messin’ around with web components. Also—JavaScript, generally - Lee Reamsnyder"
- "The Bluffer’s Guide to The Mythical Man-Month – Codemanship’s Blog". Still one of the best books on software development.
- "Weekend Reading — AI cycling — Labnotes (by Assaf Arkin)"
- "Smart drugs aren’t so smart after all | BPS"
- "htmx ~ Why I Tend Not To Use Content Negotiation"
- "Getting started with CSS container queries | MDN Blog"
- "An Attempted Taxonomy of Web Components—zachleat.com"
- "I Saw ‘Coyote Vs. Acme’ And It’s As Wonderful As Everyone Says It Is". “Dave Green’s Coyote vs. Acme isn’t merely good or above-average – it is SUPERB!”
- "dprint - Code Formatter". I hadn’t heard about this Rust-based code formatter before. I’d heard about Biome (née Rome) but not this one.
- "Deprecation of formatting rules - ESLint - Pluggable JavaScript Linter". This makes sense.
- ’No, “the Church” did not kill Joan of Arc, you credulous dullards – Going Medieval’. “IDK who you think the Church is, but if your answer is “whichever English guys happen to be in the area” then I am afraid you are just not correct.”
- "Is Your Team Caught in the Solution Fixation Trap?". “The solution fixation trap emerges when a team rushes into discussing possible solutions before fully understanding the problem at hand.” Solution Fixation Trap. A new synonym for “software development” just dropped.
- "Five Things: November 16, 2023 — As in guillotine…"
- "Sweden’s Tesla Blockade Is Spreading | WIRED UK"
- "Byte by byte | Corporate Europe Observatory". “The concerted lobbying attempts of Big Tech to avoid and reduce regulation appear to have been successful.” Well, fuck.
- "Adactio: Articles—Of Time And The Web"
- "A New Technique for Image Optimization: SVG Short Circuiting—zachleat.com"
- "Web components for (mathematical) writing, a first problem". Turns out counters are tricky.
- "Exclusive accordions exclude - Eric Eggert". “It’s unacceptable that we introduce native barriers in HTML and the web platform in 2023”