GithubHelp home page GithubHelp logo

florianrappl / parcel-plugin-import-maps Goto Github PK

View Code? Open in Web Editor NEW
4.0 3.0 1.0 14 KB

Enables support for import-maps in Parcel. :world_map:

Home Page: https://piral.io

License: MIT License

JavaScript 100.00%
parcel parcel-bundler parcel-plugin bundle import-maps dependencies

parcel-plugin-import-maps's Introduction

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. ๐Ÿš€

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.

parcel-plugin-import-maps's People

Contributors

florianrappl avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

parcel-plugin-import-maps's Issues

Support for also bundling the importmaps dependencies?

Hi there,

I am using Parcel to bundle my entire website and would like to switch from npm dependencies to a plain importmap (inlined into the index.html) so that everything is immediately browser-ready.

I only found this plugin about parcel and import-maps, but apparently it does not bundle the importmap definitions. I am looking for the exakt opposite, that importmaps are also bundled, just like npm dependencies would be bundled.

Could this be added as an option to the package? Or is there alternative?

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.