npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@import-maps/resolve

v2.0.0

Published

Parse and resolve imports via an import map

Downloads

498,290

Readme

Resolve import-maps

Library for parsing and resolving import maps.

Usage

npm i --save-dev @import-maps/resolve

Base URL

Parsing and resolving import maps requires a base URL. This is an instance of the URL constructor.

This can be a browser URL:

const myUrl = new URL('https://www.example.com/');

Or a file URL when working with a file system. The pathToFileURL function is useful for converting a file path to a URL object:

import path from 'path';
import { pathToFileURL } from 'url';

const fileUrl1 = new URL('file:///foo/bar');
const fileUrl2 = pathToFileURL(path.join(process.cwd(), 'foo', 'bar'));

Parsing an import map from a string

The parseFromString function parses an import map from a JSON string. It returns the parsed import map object to be used when resolving import specifiers.

import { parseFromString } from '@import-maps/resolve';

// get the import map from somewhere, for example read it from a string
const importMapString = '{ "imports": { "foo": "./bar.js" } }';
// create a base URL to resolve imports relatively to
const baseURL = new URL('https://www.example.com/');

const importMap = parseFromString(importMapString, baseURL);

Parsing an import map from an object

If you already have an object which represents the import map, it still needs to be parsed to validate it and to prepare it for resolving. You can use the parse function for this.

import { parse } from '@import-maps/resolve';

// get the import map from somewhere, for example read it from a string
const rawImportMap = { imports: { foo: './bar.js' } };
// create a base URL to resolve imports relatively to
const baseURL = new URL('https://www.example.com/');

const importMap = parse(rawImportMap, baseURL);

Resolving imports

Once you've created a parsed import map, you can start resolving specifiers. The resolve function returns an object with the resolved URL as well as a boolean whether the import was matched. When a bare import is not found in the import map, resolve returns null. When a relative import isn't found, the resolved URL is returned, and matched will be set to false.

import { resolve } from '@import-maps/resolve';

const importMapString = '{ "imports": { "foo": "./bar.js" } }';
const baseURL = new URL('https://www.example.com/');
const importMap = parseFromString(importMapString, baseURL);

const scriptUrl = new URL('https://www.example.com/my-app.js');

// resolvedImport: https://www.example.com/bar.js, matched: true
const { resolvedImport, matched } = resolve('foo', baseURL, scriptUrl);

// resolvedImport: https://www.example.com/x.js, matched: false
const { resolvedImport, matched } = resolve('./x.js', baseURL, scriptUrl);

// resolvedImport: null, matched: false
const { resolvedImport, matched } = resolve('bar', baseURL, scriptUrl);

If you need to use the resolved path on the file system, you can use the fileURLToPath utility:

import { fileURLToPath } from 'url';
import { resolve } from '@import-maps/resolve';

const { resolvedImport } = resolve(importMapString, baseURL, scriptUrl);

// the fully resolved file path
console.log(fileURLToPath(resolvedImport));

Acknowledgments

This implementation is heavily based on the import-maps reference implementation.