-
Notifications
You must be signed in to change notification settings - Fork 35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
convert to JS #38
base: master
Are you sure you want to change the base?
convert to JS #38
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
//@ts-check | ||
import { transform } from './transform.js'; | ||
import { define, load } from './load.js'; | ||
|
||
if (typeof document !== 'undefined') { | ||
const scr = document.querySelector('[data-main]'); | ||
if (scr) { | ||
load(new URL(scr.getAttribute('data-main'), document.baseURI).toString()); | ||
} | ||
} | ||
|
||
const VERSION = "__VERSION__"; | ||
|
||
export { transform, define, load, VERSION }; | ||
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// @ts-check | ||
import { transform } from './transform.js'; | ||
|
||
/** @type {Record<string, Promise<any>>} */ | ||
const promises = {}; | ||
|
||
/** @typedef {(id: string) => Promise<any>} __Import */ | ||
/** @typedef {Record<string, any>} __Exports */ | ||
|
||
/** | ||
* @param {string} id | ||
* @param {string[]} deps | ||
* @param {(__import: __Import, __exports: __Exports, ...deps: any[]) => void} factory | ||
*/ | ||
export function define(id, deps, factory) { | ||
const __import = (dep) => load(new URL(dep, id).toString()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [comment] |
||
|
||
return Promise.all(deps.map(__import)).then(__deps => { | ||
const __exports = {}; | ||
|
||
factory(__import, __exports, ...__deps); | ||
return __exports; | ||
}); | ||
} | ||
|
||
/** | ||
* @param {string} url | ||
*/ | ||
export function load(url) { | ||
return promises[url] || ( | ||
promises[url] = fetch(url) | ||
.then(r => r.text()) | ||
.then(text => evaluate(transform(text, url))) | ||
); | ||
} | ||
|
||
let uid = 1; | ||
|
||
/** | ||
* @param {string} code | ||
*/ | ||
function evaluate(code) { | ||
if (typeof document !== 'undefined' && typeof URL !== 'undefined') { | ||
return new Promise(fulfil => { | ||
const id = `__shimport__${uid++}`; | ||
|
||
// creating a script tag gives us proper stack traces | ||
const blob = new Blob([`${id}=${code}`], { | ||
type: 'application/javascript' | ||
}); | ||
|
||
const script = document.createElement('script'); | ||
script.src = URL.createObjectURL(blob); | ||
|
||
script.onload = () => { | ||
fulfil(window[id]); | ||
delete window[id]; | ||
}; | ||
|
||
document.head.appendChild(script); | ||
}); | ||
|
||
} else { | ||
// for browsers without `URL` | ||
return (0,eval)(code); | ||
} | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"type": "module" | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[comment]
would be nice to see this diff as a
git mv
, but minimal changes so it looks fine