ts.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. var compilerTriggerTimeoutID;
  2. function triggerCompile(d, func) {
  3. if (compilerTriggerTimeoutID !== null) {
  4. window.clearTimeout(compilerTriggerTimeoutID);
  5. }
  6. compilerTriggerTimeoutID = window.setTimeout(function () {
  7. try {
  8. var output = transpileModule(d, {
  9. module: ts.ModuleKind.AMD,
  10. target: ts.ScriptTarget.ES5,
  11. noLib: true,
  12. noResolve: true,
  13. suppressOutputPathCheck: true
  14. });
  15. if (typeof output === "string") {
  16. func(output);
  17. }
  18. }
  19. catch (e) {
  20. showError(e.message, e);
  21. }
  22. }, 100);
  23. }
  24. function transpileModule(input, options) {
  25. var inputFileName = options.jsx ? "module.tsx" : "module.ts";
  26. var sourceFile = ts.createSourceFile(inputFileName, input, options.target || ts.ScriptTarget.ES5);
  27. // Output
  28. var outputText;
  29. var program = ts.createProgram([inputFileName], options, {
  30. getSourceFile: function (fileName) { return fileName.indexOf("module") === 0 ? sourceFile : undefined; },
  31. writeFile: function (_name, text) { outputText = text; },
  32. getDefaultLibFileName: function () { return "lib.d.ts"; },
  33. useCaseSensitiveFileNames: function () { return false; },
  34. getCanonicalFileName: function (fileName) { return fileName; },
  35. getCurrentDirectory: function () { return ""; },
  36. getNewLine: function () { return "\r\n"; },
  37. fileExists: function (fileName) { return fileName === inputFileName; },
  38. readFile: function () { return ""; },
  39. directoryExists: function () { return true; },
  40. getDirectories: function () { return []; }
  41. });
  42. // Emit
  43. program.emit();
  44. if (outputText === undefined) {
  45. throw new Error("Output generation failed");
  46. }
  47. return outputText;
  48. }
  49. function getRunCode(jsEditor, callBack) {
  50. triggerCompile(jsEditor.getValue(), function(result) {
  51. callBack(result + "var createScene = function() { return Playground.CreateScene(engine, engine.getRenderingCanvas()); }")
  52. });
  53. }
  54. var defaultScene = "scripts/basic scene.txt";
  55. var monacoMode = "typescript";