1 |
|
2 | const tempModule = window.module, tempExports = window.exports;
|
3 | window.module = {}, window.exports = {};
|
4 | await import('./_hyperscript.js');
|
5 | const _hyperscript = window.module.exports;
|
6 |
|
7 | export default _hyperscript;
|
8 |
|
9 | window.module = tempModule;
|
10 | window.exports = tempExports;
|
11 |
|
12 | import * as path from "https://deno.land/std@0.139.0/path/mod.ts"
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | const hsExt = '._hs';
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 | export 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 |
|
34 | let id = parser.requireElement('nakedString', tokens)
|
35 |
|
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(/\.[^\.]*$/, '')
|
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 |
|
58 | }
|
59 | }
|
60 | })
|
61 |
|
62 | function resolves(promise) {
|
63 | promise.then(() => true, () => false)
|
64 | }
|
65 |
|
66 | if (import.meta.main) run(Deno.args[0])
|