dynamic-import-ponyfill
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:
{ try return !!'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; // using CommonJS modulesconst dip = ;
The UMD build is also available on unpkg:
This is exposes the function as window.import
~!
CAUTION: Browsers that recognize
import
andimport()
will only respond towindow.import
explicitly!
Usage
//------ foo.js ------exportssqrt = Mathsqrt;exports x * x; //------ bar.js ------module { var z = x + y; return z * z * z;} //------ src/index.js ------; ;
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