<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>browser on Barney Parker</title><link>https://barneyparker.com/tags/browser/</link><description>Recent content in browser on Barney Parker</description><generator>Hugo -- gohugo.io</generator><language>en-us</language><copyright>This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.</copyright><lastBuildDate>Tue, 12 Dec 2023 08:34:47 +0000</lastBuildDate><atom:link href="https://barneyparker.com/tags/browser/index.xml" rel="self" type="application/rss+xml"/><item><title>Import Maps to simplify Browser Based ESM</title><link>https://barneyparker.com/posts/import-maps-to-simplify-browser-based-esm/</link><pubDate>Tue, 12 Dec 2023 08:34:47 +0000</pubDate><guid>https://barneyparker.com/posts/import-maps-to-simplify-browser-based-esm/</guid><description>&lt;p>I﻿mport maps let you use short names when importing packages in the browser rather than using &lt;code>script&lt;/code> tags.&lt;/p>
&lt;p>I﻿n order to mark a script as an import map, the &lt;code>script&lt;/code> tag needs a &lt;code>type&lt;/code> attribute with a value of &lt;code>importmap&lt;/code>.&lt;/p>
&lt;p>T﻿he contents should be an object with a single attribute of &lt;code>imports&lt;/code> which is an object where the keys are the name you will use to refer to a package, and the value is the URL to the package. e.g:&lt;/p>
&lt;div class="highlight">&lt;pre class="chroma">&lt;code class="language-html" data-lang="html">&lt;span class="p">&amp;lt;&lt;/span>&lt;span class="nt">script&lt;/span> &lt;span class="na">type&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="s">&amp;#34;importmap&amp;#34;&lt;/span>&lt;span class="p">&amp;gt;&lt;/span>
&lt;span class="p">{&lt;/span>
&lt;span class="s2">&amp;#34;imports&amp;#34;&lt;/span>&lt;span class="o">:&lt;/span> &lt;span class="p">{&lt;/span>
&lt;span class="s2">&amp;#34;React&amp;#34;&lt;/span>&lt;span class="o">:&lt;/span> &lt;span class="s2">&amp;#34;https://esm.sh/react@18.2.0&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;span class="s2">&amp;#34;react-dom&amp;#34;&lt;/span>&lt;span class="o">:&lt;/span> &lt;span class="s2">&amp;#34;https://esm.sh/react-dom@18.2.0&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;span class="s2">&amp;#34;htm&amp;#34;&lt;/span>&lt;span class="o">:&lt;/span> &lt;span class="s2">&amp;#34;https://esm.sh/htm@3.1.1&amp;#34;&lt;/span>
&lt;span class="p">}&lt;/span>
&lt;span class="p">}&lt;/span>
&lt;span class="p">&amp;lt;/&lt;/span>&lt;span class="nt">script&lt;/span>&lt;span class="p">&amp;gt;&lt;/span>
&lt;/code>&lt;/pre>&lt;/div>&lt;p>T﻿his should be placed in the &lt;code>head&lt;/code> section. The parser reads the import map, but doesn&amp;rsquo;t preload the imports. instead it waits until a package gets imported, and then translates it to its &lt;em>real&lt;/em> url for loading:&lt;/p>
&lt;div class="highlight">&lt;pre class="chroma">&lt;code class="language-javascript" data-lang="javascript">&lt;span class="kr">import&lt;/span> &lt;span class="nx">htm&lt;/span> &lt;span class="nx">from&lt;/span> &lt;span class="s1">&amp;#39;htm&amp;#39;&lt;/span>&lt;span class="p">;&lt;/span>
&lt;/code>&lt;/pre>&lt;/div></description></item></channel></rss>