1
- ( function ( ) {
2
- function evalSources ( sources ) {
3
- var modules = { } ;
4
- function dirname ( str ) {
5
- var ix = str . lastIndexOf ( "/" ) ;
6
- return ix < 0 ? "" : str . slice ( 0 , ix ) ;
1
+ /*
2
+ This script executes the JS files returned by PS compilation.
3
+ */
4
+
5
+ // Get directory name of path
6
+ function dirname ( str ) {
7
+ let ix = str . lastIndexOf ( "/" ) ;
8
+ return ix < 0 ? "" : str . slice ( 0 , ix ) ;
9
+ } ;
10
+
11
+ // Concatenates paths together
12
+ function resolvePath ( a , b ) {
13
+ // `b` relative to current directory with `./`
14
+ if ( b [ 0 ] === "." && b [ 1 ] === "/" ) {
15
+ return dirname ( a ) + b . slice ( 1 ) ;
16
+ }
17
+ // `b` relative to `a` parent directory with `../`
18
+ if ( b [ 0 ] === "." && b [ 1 ] === "." && b [ 2 ] === "/" ) {
19
+ return dirname ( dirname ( a ) ) + b . slice ( 2 ) ;
20
+ }
21
+ // `b` is either shim or path from root
22
+ return b ;
23
+ } ;
24
+
25
+ // Executes JS source and all dependencies.
26
+ // Maintains cache of previously-executed sources.
27
+ function evalSources ( sources ) {
28
+ // Cache all modules
29
+ var modules = { } ;
30
+ // Executes module source, or returns cached exports.
31
+ return function load ( name ) {
32
+ // Check if module is already cached
33
+ if ( modules [ name ] ) {
34
+ return modules [ name ] . exports ;
7
35
}
8
- function resolvePath ( a , b ) {
9
- if ( b [ 0 ] === "." && b [ 1 ] === "/" ) {
10
- return dirname ( a ) + b . slice ( 1 ) ;
11
- }
12
- if ( b [ 0 ] === "." && b [ 1 ] === "." && b [ 2 ] === "/" ) {
13
- return dirname ( dirname ( a ) ) + b . slice ( 2 ) ;
14
- }
15
- return b ;
36
+ // Not cached, so execute contents.
37
+ // Provide custom `require`, `module`, and `exports`.
38
+ // Custom `require` which executes file contents, as well as any dependencies.
39
+ function require ( path ) {
40
+ return load ( resolvePath ( name , path ) ) ;
16
41
}
17
- return function load ( name ) {
18
- if ( modules [ name ] ) {
19
- return modules [ name ] . exports ;
20
- }
21
- function require ( path ) {
22
- return load ( resolvePath ( name , path ) ) ;
23
- }
24
- var module = modules [ name ] = { exports : { } } ;
25
- new Function ( "module" , "exports" , "require" , sources [ name ] ) ( module , module . exports , require ) ;
26
- return module . exports ;
27
- } ;
42
+ // Provide empty exports, which will be set, and then returned.
43
+ var module = modules [ name ] = { exports : { } } ;
44
+ // Create a function from the module's file contents,
45
+ // and execute this function with our substitutions.
46
+ new Function ( "module" , "exports" , "require" , sources [ name ] ) ( module , module . exports , require ) ;
47
+ return module . exports ;
48
+ } ;
49
+ } ;
50
+
51
+ function loadFrame ( str ) {
52
+ // Convert JSON string back to object.
53
+ // keys: file paths
54
+ // values: compressed JS source
55
+ obj = JSON . parse ( str ) ;
56
+
57
+ // Decompress values back to JS source
58
+ Object . keys ( obj ) . forEach ( function ( key ) {
59
+ obj [ key ] = LZString . decompressFromEncodedURIComponent ( obj [ key ] ) ;
60
+ } ) ;
61
+
62
+ // Execute all sources, and save returned `exports` from `<file>`.
63
+ // Expecting a `exports.main` entry point.
64
+ let file = evalSources ( obj ) ( "<file>" ) ;
65
+
66
+ // Check if `main` can be launched
67
+ if ( ! file . main ) {
68
+ console . log ( 'Missing "main"' ) ;
69
+ } else if ( typeof file . main !== "function" ) {
70
+ console . log ( '"main" is not a function' ) ;
71
+ } else {
72
+ // Launch entry point
73
+ file . main ( ) ;
28
74
}
75
+ } ;
29
76
30
- var parent ;
31
-
32
- document . addEventListener ( "DOMContentLoaded" , function ( ) {
33
- window . addEventListener ( "message" , function ( event ) {
34
- parent = event . source ;
35
- parent . postMessage ( "trypurescript" , "*" ) ;
36
- var file = evalSources ( event . data ) ( "<file>" ) ;
37
- if ( file . main && typeof file . main === "function" ) {
38
- file . main ( ) ;
39
- }
40
- } , { once : true } ) ;
41
- } , { once : true } ) ;
42
-
43
- document . addEventListener ( "click" , function ( event ) {
44
- if ( parent && event . target . nodeName === "A" && event . target . hostname === "gist.github.com" ) {
45
- event . preventDefault ( ) ;
46
- parent . postMessage ( {
47
- gistId : event . target . pathname . split ( "/" ) . slice ( - 1 ) [ 0 ]
48
- } , "*" ) ;
49
- }
50
- } , false ) ;
51
- } ) ( ) ;
77
+ // Call script tag contents when frame loads.
78
+ // Expects a call to loadFrame, passing JS sources.
79
+ window . onload = function ( ) {
80
+ // https://stackoverflow.com/a/8677590
81
+ //grab the last script tag in the DOM
82
+ //this will always be the one that is currently evaluating during load
83
+ let tags = document . getElementsByTagName ( 'script' ) ;
84
+ let tag = tags [ tags . length - 1 ] ;
85
+ //force evaluation of the contents
86
+ eval ( tag . innerHTML ) ;
87
+ } ;
0 commit comments