forked from valerauko/cljs-deno-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deno_bootstrap.js
82 lines (65 loc) · 2.16 KB
/
deno_bootstrap.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
var global = globalThis;
var goog = global.goog;
var SHADOW_IMPORTED = global.SHADOW_IMPORTED = {};
import * as PATH from "https://deno.land/[email protected]/path/mod.ts";
import * as VM from "https://deno.land/[email protected]/node/vm.ts";
var SHADOW_PROVIDE = function(name) {
return goog.exportPath_(name, undefined);
};
var SHADOW_REQUIRE = function(name) {
if (goog.isInModuleLoader_()) {
return goog.module.getInternal_(name);
}
return true;
};
var SHADOW_WRAP = function(js) {
var code = "(function (require, module, __filename, __dirname) {\n";
// this is part of goog/base.js and for some reason the only global var not on goog or goog.global
code += "var COMPILED = false;\n"
code += js;
code += "\n});";
return code;
};
var SHADOW_IMPORT_PATH = "./public/js/cljs-runtime"
var SHADOW_IMPORT = global.SHADOW_IMPORT = async function(src) {
if (CLOSURE_DEFINES["shadow.debug"]) {
console.info("SHADOW load:", src);
}
SHADOW_IMPORTED[src] = true;
// SHADOW_IMPORT_PATH is an absolute path
var filePath = PATH.resolve(SHADOW_IMPORT_PATH, src);
var fn;
try {
// see: https://medium.com/deno-the-complete-reference/dynamic-imports-in-deno-5e9eb7f66238
fn = await import(filePath);
} catch(e) {
console.log('=== '+e.message);
}
return true;
};
// strip a leading comment as generated for (defn x "foo" [a] a)
// /**
// * foo
// */
// (function (){
function SHADOW_STRIP_COMMENT(js) {
if (!js.startsWith("/*")) {
return js;
} else {
return js.substring(js.indexOf("*/") + 2).trimLeft();
}
};
global.SHADOW_NODE_EVAL = function(js, smJson) {
// special case handling for require since it may otherwise not be available
// FIXME: source maps get destroyed by the strip
js = "(function cljsEval(require) {\n return " + SHADOW_STRIP_COMMENT(js) + "\n});";
if (smJson) {
js += "\n//# sourceMappingURL=data:application/json;charset=utf-8;base64,";
js += btoa(String.fromCharCode(...new TextEncoder().encode(JSON.stringify(smJson))));
}
var fn = VM.runInThisContext.call(global, js,
{filename: "<eval>",
lineOffset: -1, // wrapper adds one line on top
displayErrors: true});
return fn(null);
};