JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 27236
  • Score
    100M100P100Q146029F
  • License MIT

WIP

Package Exports

  • dimport

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (dimport) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

dimport Build Status

WIP


Only CommonJS and UMD scripts are supported by this package. Any attempts to dynamically import ES6 modules will throw an error! Most (if not all) browsers that can understand ESM exports already have native import() support!


Package relies on Promise and fetch support. If you need to polyfill these as well, I'd suggest zousan and unfetch respectively.

This module exposes three module definitions:

  • ESM: dist/import.es.js
  • CommonJS: dist/import.js
  • UMD: dist/import.min.js

There is no built-in check for whether or not the current browser supports import() natively.
If you'd like to check, you may do so with the following:

function supported() {
  try {
    return !!new Function('import("")');
  } catch (e) {
    return false;
  }
}

Install

$ npm install --save dimport

Then with a module bundler like rollup or webpack, use as you would anything else:

// using ES6 modules
import dip from 'dynamic-import-ponyfill';

// using CommonJS modules
const dip = require('dynamic-import-ponyfill');

The UMD build is also available on unpkg:

<script src="https://unpkg.com/dynamic-import-ponyfill/dist/import.min.js"></script>

This exposes the function as window.import!

CAUTION: Browsers that recognize import and import() will only respond to window.import explicitly!

Usage

//------ foo.js ------
exports.sqrt = Math.sqrt;
exports.square = x => x * x;

//------ bar.js ------
module.exports = (x, y) => {
  var z = x + y;
  return z * z * z;
}

//------ src/index.js ------
import dImport from 'dynamic-import-ponyfill';

dImport('/foo.js').then(mod1 => {
  let { sqrt, square } = mod1.default || mod1;
  square(12);  //=> 144
  sqrt(36); //=> 6

  dImport('/bar.js').then(mod2 => {
    let fn = mod2.default || mod2;
    fn(3, 4); //=> 343
  })
});

API

import(url)

Returns: Promise

Returns a Promise containing the the CommonJS module contents.

url

Type: String

The URL of the CommonJS script to import.

License

MIT © Luke Edwards