Last active
April 9, 2018 07:39
-
-
Save tweinfeld/477dd9e5d4cd7f94fd98f28aa82ae1b4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/[email protected]/lodash.min.js'); | |
const kefir = await requireAMD('https://unpkg.com/[email protected]/dist/kefir.js'); | |
...[application code]... | |
} | |
--- | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment