Skip to content

Instantly share code, notes, and snippets.

@tweinfeld
Last active April 9, 2018 07:39
Show Gist options
  • Save tweinfeld/477dd9e5d4cd7f94fd98f28aa82ae1b4 to your computer and use it in GitHub Desktop.
Save tweinfeld/477dd9e5d4cd7f94fd98f28aa82ae1b4 to your computer and use it in GitHub Desktop.
import { fetch } from 'wix-fetch';
const requireAMD = function (url) {
return fetch(url)
.then((res) => res.text())
.then((code) => {
return new Promise((resolve, reject) => {
(new Function("define", code))(
(function () {
let define = (...args) => {
let [requires, definition] = [[], ...args].slice(-2);
if (requires.length > 0 && !requires.every((req) => req === "exports")) {
reject(new Error('Can only process the "exports" dependency at this time'));
} else {
let deps = requires.map(() => ({})),
compilation = definition(...deps);
resolve(deps.length ? deps.reduce(Object.assign) : compilation);
}
};
define.amd = {};
return define;
})()
);
});
});
};
/*
--- USAGE SAMPLE ---
async function main(){
const _ = await requireAMD('https://unpkg.com/lodash@4.17.5/lodash.min.js');
const kefir = await requireAMD('https://unpkg.com/kefir@3.8.3/dist/kefir.js');
...[application code]...
}
---
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment