This package has been deprecated

Author message:

Please use 'dimport' instead

dynamic-import-ponyfill

0.1.0 • Public • Published

dynamic-import-ponyfill Build Status

A tiny (141B) ponyfill for dynamic imports — import()


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 dynamic-import-ponyfill

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="//unpkg.com/dynamic-import-ponyfill/dist/import.umd.js"></script>

This is 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

Package Sidebar

Install

npm i dynamic-import-ponyfill

Weekly Downloads

0

Version

0.1.0

License

MIT

Unpacked Size

7.49 kB

Total Files

9

Last publish

Collaborators

  • lukeed