UNPKG

1.91 kBJavaScriptView Raw
1
2const tempModule = window.module, tempExports = window.exports;
3window.module = {}, window.exports = {};
4await import('./_hyperscript.js');
5const _hyperscript = window.module.exports;
6
7export default _hyperscript;
8
9window.module = tempModule;
10window.exports = tempExports;
11
12import * as path from "https://deno.land/std@0.139.0/path/mod.ts"
13
14/**
15 * File extension for _hyperscript files
16 */
17const hsExt = '._hs';
18
19/**
20 *
21 * @param {String} modulePath
22 */
23export function run(modulePath) {
24 modulePath = path.resolve(modulePath);
25 const args = { module: { dir: path.dirname(modulePath), id: modulePath } }
26 return Deno.readTextFile(modulePath)
27 .then(code => _hyperscript.evaluate(code, {}, args))
28 .catch(e => console.error("Cannot execute file: ", e));
29}
30
31_hyperscript.addFeature('import', (parser, runtime, tokens) => {
32 if (!tokens.matchToken('import')) return;
33 /** @type {string} */
34 let id = parser.requireElement('nakedString', tokens)
35 // @ts-ignore
36 .evaluate({});
37
38 let name = id;
39 if (tokens.matchToken('as')) {
40 name = tokens.requireTokenType('IDENTIFIER').value;
41 } else {
42 name = path.basename(id)
43 .replace(/\.[^\.]*$/, '') // remove extension
44 }
45
46 return {
47 async install(target, source, args) {
48 if (id.startsWith('./') || id.startsWith('../')) {
49 id = path.join(args.module.dir, id);
50 }
51
52 let mod;
53 if (id.endsWith(hsExt)) mod = run(id);
54 if (await resolves(Deno.stat(id + hsExt))) mod = run(id + hsExt);
55 else mod = await import(id);
56 runtime.assignToNamespace(target, [], name, mod);
57 //console.log(id, name, mod.toString(), target.hyperscriptFeatures);
58 }
59 }
60})
61
62function resolves(promise) {
63 promise.then(() => true, () => false)
64}
65
66if (import.meta.main) run(Deno.args[0])