|
1 | | -(function (ts, env, libs) { |
2 | | - var src = '', version = 0; |
3 | | - var lib = libs.concat([ |
4 | | - 'declare function exit(): never;', |
5 | | - 'declare function go(): any;', |
6 | | - 'declare function getTsDeclarations(): string[];' |
7 | | - ]).join(''); |
8 | | - var libSnapshot = ts.ScriptSnapshot.fromString(lib); |
9 | | - var environments = {}; |
10 | | - var declSnapshots = []; |
11 | | - |
12 | | - var settings = { |
13 | | - outDir: "/out", |
14 | | - declarationDir: "/out", |
15 | | - target: ts.ScriptTarget.ES5, |
16 | | - lib: [ ], |
17 | | - module: ts.ModuleKind.None, |
18 | | - declaration: true, |
19 | | - stripInternal: true, |
20 | | - downlevelIteration: true, |
21 | | - forceConsistentCasingInFileNames: true, |
22 | | - experimentalDecorators: true, |
23 | | - strict: true, |
24 | | - sourceMap: true, |
25 | | - }; |
26 | | - |
27 | | - var reg = ts.createDocumentRegistry(); |
28 | | - var service = ts.createLanguageService({ |
29 | | - getCurrentDirectory: function() { return "/"; }, |
30 | | - getDefaultLibFileName: function() { return "/lib.d.ts"; }, |
31 | | - getScriptFileNames: function() { |
32 | | - var res = [ "/src.ts", "/lib.d.ts" ]; |
33 | | - for (var i = 0; i < declSnapshots.length; i++) res.push("/glob." + (i + 1) + ".d.ts"); |
34 | | - return res; |
35 | | - }, |
36 | | - getCompilationSettings: function () { return settings; }, |
37 | | - fileExists: function(filename) { return filename === "/lib.d.ts" || filename === "/src.ts" || filename === "/glob.d.ts"; }, |
38 | | - |
39 | | - getScriptSnapshot: function(filename) { |
40 | | - if (filename === "/lib.d.ts") return libSnapshot; |
41 | | - if (filename === "/src.ts") return ts.ScriptSnapshot.fromString(src); |
42 | | - |
43 | | - var index = /\/glob\.(\d+)\.d\.ts/g.exec(filename); |
44 | | - if (index && index[1] && (index = Number(index[1])) && index > 0 && index <= declSnapshots.length) { |
45 | | - return declSnapshots[index - 1]; |
46 | | - } |
47 | | - |
48 | | - throw new Error("File '" + filename + "' doesn't exist."); |
49 | | - }, |
50 | | - getScriptVersion: function (filename) { |
51 | | - if (filename === "/lib.d.ts" || filename.startsWith("/glob.")) return 0; |
52 | | - else return version; |
53 | | - }, |
54 | | - }, reg); |
55 | | - |
56 | | - service.getEmitOutput("/lib.d.ts"); |
57 | | - log("Loaded libraries!"); |
58 | | - |
59 | | - var oldCompile = env.compile; |
60 | | - |
61 | | - function compile(code, filename, env) { |
62 | | - src = code; |
63 | | - version++; |
64 | | - |
65 | | - if (!environments[env.id]) environments[env.id] = [] |
66 | | - declSnapshots = environments[env.id]; |
67 | | - var emit = service.getEmitOutput("/src.ts"); |
68 | | - |
69 | | - var diagnostics = [] |
70 | | - .concat(service.getCompilerOptionsDiagnostics()) |
71 | | - .concat(service.getSyntacticDiagnostics("/src.ts")) |
72 | | - .concat(service.getSemanticDiagnostics("/src.ts")) |
73 | | - .map(function (diagnostic) { |
74 | | - var message = ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n"); |
75 | | - if (diagnostic.file) { |
76 | | - var pos = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start); |
77 | | - var file = diagnostic.file.fileName.substring(1); |
78 | | - if (file === "src.ts") file = filename; |
79 | | - return file + ":" + (pos.line + 1) + ":" + (pos.character + 1) + ": " + message; |
80 | | - } |
81 | | - else return message; |
82 | | - }); |
83 | | - |
84 | | - if (diagnostics.length > 0) { |
85 | | - throw new SyntaxError(diagnostics.join("\n")); |
86 | | - } |
87 | | - |
88 | | - var map = JSON.parse(emit.outputFiles[0].text); |
89 | | - var result = emit.outputFiles[1].text; |
90 | | - var declaration = emit.outputFiles[2].text; |
91 | | - |
92 | | - var compiled = oldCompile(result, filename, env); |
93 | | - |
94 | | - return { |
95 | | - function: function () { |
96 | | - var val = compiled.function.apply(this, arguments); |
97 | | - if (declaration !== '') declSnapshots.push(ts.ScriptSnapshot.fromString(declaration)); |
98 | | - return val; |
99 | | - }, |
100 | | - breakpoints: compiled.breakpoints, |
101 | | - mapChain: compiled.mapChain.concat(map.mappings), |
102 | | - }; |
103 | | - } |
104 | | - |
105 | | - function apply(env) { |
106 | | - env.compile = compile; |
107 | | - env.global.getTsDeclarations = function() { |
108 | | - return environments[env.id]; |
109 | | - } |
110 | | - } |
111 | | - |
112 | | - apply(env); |
113 | | -})(arguments[0], arguments[1], arguments[2]); |
| 1 | +(function (ts, env, libs) { |
| 2 | + var src = '', version = 0; |
| 3 | + var lib = libs.concat([ |
| 4 | + 'declare function exit(): never;', |
| 5 | + 'declare function go(): any;', |
| 6 | + 'declare function getTsDeclarations(): string[];' |
| 7 | + ]).join(''); |
| 8 | + var libSnapshot = ts.ScriptSnapshot.fromString(lib); |
| 9 | + var environments = {}; |
| 10 | + var declSnapshots = []; |
| 11 | + |
| 12 | + var settings = { |
| 13 | + outDir: "/out", |
| 14 | + declarationDir: "/out", |
| 15 | + target: ts.ScriptTarget.ES5, |
| 16 | + lib: [ ], |
| 17 | + module: ts.ModuleKind.None, |
| 18 | + declaration: true, |
| 19 | + stripInternal: true, |
| 20 | + downlevelIteration: true, |
| 21 | + forceConsistentCasingInFileNames: true, |
| 22 | + experimentalDecorators: true, |
| 23 | + strict: true, |
| 24 | + sourceMap: true, |
| 25 | + }; |
| 26 | + |
| 27 | + var reg = ts.createDocumentRegistry(); |
| 28 | + var service = ts.createLanguageService({ |
| 29 | + getCurrentDirectory: function() { return "/"; }, |
| 30 | + getDefaultLibFileName: function() { return "/lib.d.ts"; }, |
| 31 | + getScriptFileNames: function() { |
| 32 | + var res = [ "/src.ts", "/lib.d.ts" ]; |
| 33 | + for (var i = 0; i < declSnapshots.length; i++) res.push("/glob." + (i + 1) + ".d.ts"); |
| 34 | + return res; |
| 35 | + }, |
| 36 | + getCompilationSettings: function () { return settings; }, |
| 37 | + fileExists: function(filename) { return filename === "/lib.d.ts" || filename === "/src.ts" || filename === "/glob.d.ts"; }, |
| 38 | + |
| 39 | + getScriptSnapshot: function(filename) { |
| 40 | + if (filename === "/lib.d.ts") return libSnapshot; |
| 41 | + if (filename === "/src.ts") return ts.ScriptSnapshot.fromString(src); |
| 42 | + |
| 43 | + var index = /\/glob\.(\d+)\.d\.ts/g.exec(filename); |
| 44 | + if (index && index[1] && (index = Number(index[1])) && index > 0 && index <= declSnapshots.length) { |
| 45 | + return declSnapshots[index - 1]; |
| 46 | + } |
| 47 | + |
| 48 | + throw new Error("File '" + filename + "' doesn't exist."); |
| 49 | + }, |
| 50 | + getScriptVersion: function (filename) { |
| 51 | + if (filename === "/lib.d.ts" || filename.startsWith("/glob.")) return 0; |
| 52 | + else return version; |
| 53 | + }, |
| 54 | + }, reg); |
| 55 | + |
| 56 | + service.getEmitOutput("/lib.d.ts"); |
| 57 | + log("Loaded libraries!"); |
| 58 | + |
| 59 | + var oldCompile = env.compile; |
| 60 | + |
| 61 | + function compile(code, filename, env) { |
| 62 | + src = code; |
| 63 | + version++; |
| 64 | + |
| 65 | + if (!environments[env.id]) environments[env.id] = [] |
| 66 | + var decls = declSnapshots = environments[env.id]; |
| 67 | + var emit = service.getEmitOutput("/src.ts"); |
| 68 | + |
| 69 | + var diagnostics = [] |
| 70 | + .concat(service.getCompilerOptionsDiagnostics()) |
| 71 | + .concat(service.getSyntacticDiagnostics("/src.ts")) |
| 72 | + .concat(service.getSemanticDiagnostics("/src.ts")) |
| 73 | + .map(function (diagnostic) { |
| 74 | + var message = ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n"); |
| 75 | + if (diagnostic.file) { |
| 76 | + var pos = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start); |
| 77 | + var file = diagnostic.file.fileName.substring(1); |
| 78 | + if (file === "src.ts") file = filename; |
| 79 | + return file + ":" + (pos.line + 1) + ":" + (pos.character + 1) + ": " + message; |
| 80 | + } |
| 81 | + else return message; |
| 82 | + }); |
| 83 | + |
| 84 | + if (diagnostics.length > 0) { |
| 85 | + throw new SyntaxError(diagnostics.join("\n")); |
| 86 | + } |
| 87 | + |
| 88 | + var map = JSON.parse(emit.outputFiles[0].text); |
| 89 | + var result = emit.outputFiles[1].text; |
| 90 | + var declaration = emit.outputFiles[2].text; |
| 91 | + |
| 92 | + var compiled = oldCompile(result, filename, env); |
| 93 | + |
| 94 | + return { |
| 95 | + function: function () { |
| 96 | + var val = compiled.function.apply(this, arguments); |
| 97 | + if (declaration !== '') decls.push(ts.ScriptSnapshot.fromString(declaration)); |
| 98 | + return val; |
| 99 | + }, |
| 100 | + breakpoints: compiled.breakpoints, |
| 101 | + mapChain: compiled.mapChain.concat(map.mappings), |
| 102 | + }; |
| 103 | + } |
| 104 | + |
| 105 | + function apply(env) { |
| 106 | + env.compile = compile; |
| 107 | + env.global.getTsDeclarations = function() { |
| 108 | + return environments[env.id]; |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + apply(env); |
| 113 | +})(arguments[0], arguments[1], arguments[2]); |
0 commit comments