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

parcel-plugin-import-maps

v0.3.1

Published

A plugin for Parcel to use import maps for microfrontends.

Downloads

43

Readme

parcel-plugin-import-maps

Build Status npm GitHub tag GitHub issues

Parcel plugin for declaring / using import maps. These externals will not be bundled in directly, but only be loaded if not already present. :rocket:

Usage

Install the plugin via npm:

npm i parcel-plugin-import-maps --save-dev

Declaring Import Maps

You can now add a new key to your package.json: importmap. The key can either hold an importmap structure (see specification) or a reference to a valid JSON file holding the structure.

Example for the containing the structure in the package.json:

{
  "name": "my-app",
  "version": "1.0.0",
  "devDependencies": {
    "parcel-bundler": "1.x",
    "parcel-plugin-at-alias": "latest",
  },
  "importmap": {
    "imports": {
      "/app/helper": "node_modules/helper/index.mjs",
      "lodash": "./node_modules/lodash-es/lodash.js"
    }
  }
}

Alternative version:

{
  "name": "my-app",
  "version": "1.0.0",
  "devDependencies": {
    "parcel-bundler": "1.x",
    "parcel-plugin-at-alias": "latest",
  },
  "importmap": "./my-imports.json"
}

where the my-imports.json looks like

{
  "imports": {
    "/app/helper": "node_modules/helper/index.mjs",
    "lodash": "./node_modules/lodash-es/lodash.js"
  }
}

With this equipped the given modules are loaded asynchronously at the beginning of the application. If multiple applications with import maps are loaded then the dependencies are either shared or not shared depending on their individual hashes.

This ensures proper dependency isolation while still being able to share what makes sense to be shared.

Most importantly, the plugin allows you to place scripts from other locations easily without bundling them in:

{
  "imports": {
    "lodash": "https://cdn.jsdelivr.net/npm/[email protected]/lodash.min.js"
  }
}

For proper IDE (or even TypeScript) usage we still advise to install the respective package or at least its bindings locally.

Loading Import Maps

The required import maps are loaded at startup asynchronously. Therefore, you'd need to wait before using them.

Unfortunately, in the current version this cannot be done implicitly (reliably), even though its desired for the future.

Right now the only way is to change code like (assumes lodash is used from an import map like above)

//app.js
import * as _ from 'lodash';

const _ = require('lodash');

export const partitions = _.partition([1, 2, 3, 4], n => n % 2);
});

to be

//app.js
require('importmap').ready().then(() => {
  const _ = require('lodash');
  return {
    partitions: _.partition([1, 2, 3, 4], n => n % 2),
  };
});

or, alternatively (more generically),

//index.js
module.exports = require('importmap').ready().then(() => require('./app'));

//app.js
import * as _ from 'lodash';

const _ = require('lodash');

export const partitions = _.partition([1, 2, 3, 4], n => n % 2);
});

You could also trigger the loading already in the beginning, i.e.,

//app.js
require('importmap');

// ...
//other.js
require('importmap').ready('lodash').then(() => {
  // either load or do something with require('lodash')
});

where we use ready with a single argument to determine what package should have been loaded to proceed. This is the deferred loading approach. Alternatively, an array with multiple package identifiers can be passed in.

Changelog

This project adheres to semantic versioning.

You can find the changelog in the CHANGELOG.md file.

License

This plugin is released using the MIT license. For more information see the LICENSE file.