This is a very simple
require() polyfill for browsers that loads packages from
It supports simple packages only and doesn't work for generic ones — the main intention was to be able to launch up-to-date library demo sites on GitHub Pages without having to re-build the bundle after every update.
How does it work?
First, it statically analyzes the code for
require presence and recursively
require-d files from https://unpkg.com/, asynchronously.
Then, it dynamically evaluates modules code, providing a synchronous
implementation using the pre-loaded files.
If some deps names are computed dynamically — those are loaded synchronously at the dynamic evaluation step. Dynamic evaluation does not support loading new packages, only files with full or relative paths.
- Most of Node.js API is not supported (some could be loaded from ecosystem).
require()arguments are supported only partially.
- Only latest versions of packages are loaded by default.
- This loads packages from a thirdparty CDN.
cssfiles are not loaded, you have to specify them manually.
Not recommended to use this in production — the main intention of this package is make building GitHub Pages demo sites easier, without having to bundle stuff.
How to use?
Most simple setup:
The libary registers a single global
require function, so, alternatively,
require.async('./main.js') could be used (returns a
require('./main.js') also works, but without preloading it would try to load
the deps synchronously (which doesn't support loading packages by their names).
You should avoid doing that from your top-level code.
The above affects only the entry point — inside
./main.js (and other
require()-d code), use synchronous
require() as usual — file loads will be
Also, you can specify a specific version or version range when loading a