Giter Club home page Giter Club logo

importmaps-exploration's Introduction

importmaps exploration

This repository is a little exploration of using importmaps as an alternative to using JavaScript bundlers and build steps. It is loosely following this introduction blog post to importmaps.

Setup

There are two simple HTML files here: index.html and about.html. Their contents are both hardcoded to avoid unnecessary distraction from the main topic of this exploration.

Note that both files have largely the same content because this exploration is done by importmapping Turbo which is a library for intercepting HTTP requests and only updating the HTML content without a full page reload, essentially enabling a Single Page application feel for HTML.

The interesting pieces are the script tags:

<script type="importmap">
  {
    "imports": {
      "@hotwired/turbo": "https://cdn.skypack.dev/@hotwired/turbo"
    }
  }
</script>
<script type="module">
  import * as Turbo from "@hotwired/turbo";
</script>

It is important that they appear in the HTML in that order ensuring that the type="module" knows how to resolve the package.


We can verify that the importmap is working by starting a server, e.g. via npx http-server and typing Turbo into the browser console. If no errors show up, the importmap is properly resolving.

Pinning packages

If we want to explicitly pin which version we resolve, we can do so by pinning it like so:

<script type="importmap">
  {
    "imports": {
      "@hotwired/turbo": "https://cdn.skypack.dev/pin/@hotwired/[email protected]/mode=imports,min/optimized/@hotwired/turbo.js"
    }
  }
</script>

It seems that the only way to find out the URL for pinning on Skypack is via visiting https://cdn.skypack.dev/@hotwired/turbo where something like this should show up:

/*
 * Skypack CDN - @hotwired/[email protected]
 *
 * Learn more:
 *   ๐Ÿ“™ Package Documentation: https://www.skypack.dev/view/@hotwired/turbo
 *   ๐Ÿ“˜ Skypack Documentation: https://www.skypack.dev/docs
 *
 * Pinned URL: (Optimized for Production)
 *   โ–ถ๏ธ Normal: https://cdn.skypack.dev/pin/@hotwired/[email protected]/mode=imports/optimized/@hotwired/turbo.js
 *   โฉ Minified: https://cdn.skypack.dev/pin/@hotwired/[email protected]/mode=imports,min/optimized/@hotwired/turbo.js
 *
 */

// Browser-Optimized Imports (Don't directly import the URLs below in your application!)
export * from '/-/@hotwired/[email protected]/dist=es2019,mode=imports/optimized/@hotwired/turbo.js';
export {default} from '/-/@hotwired/[email protected]/dist=es2019,mode=imports/optimized/@hotwired/turbo.js';

Open questions

  • How to resolve packages from other CDN's such as UNPKG, JSPM, etc.?
  • How to import CSS files from packages that require CSS to function properly such as MapLibre GL?

importmaps-exploration's People

Contributors

dnsos avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.