importTypescript.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*---------------------------------------------------------------------------------------------
  2. * Copyright (c) Microsoft Corporation. All rights reserved.
  3. * Licensed under the MIT License. See License.txt in the project root for license information.
  4. *--------------------------------------------------------------------------------------------*/
  5. const path = require('path');
  6. const fs = require('fs');
  7. const TYPESCRIPT_LIB_SOURCE = path.join(__dirname, '../node_modules/typescript/lib');
  8. const TYPESCRIPT_LIB_DESTINATION = path.join(__dirname, '../src/lib');
  9. (function () {
  10. try {
  11. fs.statSync(TYPESCRIPT_LIB_DESTINATION);
  12. } catch (err) {
  13. fs.mkdirSync(TYPESCRIPT_LIB_DESTINATION);
  14. }
  15. importLibs();
  16. var tsServices = fs.readFileSync(path.join(TYPESCRIPT_LIB_SOURCE, 'typescriptServices.js')).toString();
  17. // Ensure we never run into the node system...
  18. // (this also removes require calls that trick webpack into shimming those modules...)
  19. tsServices = (
  20. tsServices.replace(/\n ts\.sys =([^]*)\n \}\)\(\);/m, `\n // MONACOCHANGE\n ts.sys = undefined;\n // END MONACOCHANGE`)
  21. );
  22. var tsServices_amd = tsServices +
  23. `
  24. // MONACOCHANGE
  25. // Defining the entire module name because r.js has an issue and cannot bundle this file
  26. // correctly with an anonymous define call
  27. define("vs/language/typescript/lib/typescriptServices", [], function() { return ts; });
  28. // END MONACOCHANGE
  29. `;
  30. fs.writeFileSync(path.join(TYPESCRIPT_LIB_DESTINATION, 'typescriptServices-amd.js'), tsServices_amd);
  31. var tsServices_esm = tsServices +
  32. `
  33. // MONACOCHANGE
  34. export const createClassifier = ts.createClassifier;
  35. export const createLanguageService = ts.createLanguageService;
  36. export const displayPartsToString = ts.displayPartsToString;
  37. export const EndOfLineState = ts.EndOfLineState;
  38. export const flattenDiagnosticMessageText = ts.flattenDiagnosticMessageText;
  39. export const IndentStyle = ts.IndentStyle;
  40. export const ScriptKind = ts.ScriptKind;
  41. export const ScriptTarget = ts.ScriptTarget;
  42. export const TokenClass = ts.TokenClass;
  43. // END MONACOCHANGE
  44. `;
  45. fs.writeFileSync(path.join(TYPESCRIPT_LIB_DESTINATION, 'typescriptServices.js'), tsServices_esm);
  46. var dtsServices = fs.readFileSync(path.join(TYPESCRIPT_LIB_SOURCE, 'typescriptServices.d.ts')).toString();
  47. dtsServices +=
  48. `
  49. // MONACOCHANGE
  50. export = ts;
  51. // END MONACOCHANGE
  52. `;
  53. fs.writeFileSync(path.join(TYPESCRIPT_LIB_DESTINATION, 'typescriptServices.d.ts'), dtsServices);
  54. })();
  55. function importLibs() {
  56. function getFileName(name) {
  57. return (name === '' ? 'lib.d.ts' : `lib.${name}.d.ts`);
  58. }
  59. function getVariableName(name) {
  60. return (name === '' ? 'lib_dts' : `lib_${name.replace(/\./g, '_')}_dts`);
  61. }
  62. function readLibFile(name) {
  63. var srcPath = path.join(TYPESCRIPT_LIB_SOURCE, getFileName(name));
  64. return fs.readFileSync(srcPath).toString();
  65. }
  66. var queue = [];
  67. var in_queue = {};
  68. var enqueue = function (name) {
  69. if (in_queue[name]) {
  70. return;
  71. }
  72. in_queue[name] = true;
  73. queue.push(name);
  74. };
  75. enqueue('');
  76. enqueue('es6');
  77. var result = [];
  78. while (queue.length > 0) {
  79. var name = queue.shift();
  80. var contents = readLibFile(name);
  81. var lines = contents.split(/\r\n|\r|\n/);
  82. var output = '';
  83. var writeOutput = function (text) {
  84. if (output.length === 0) {
  85. output = text;
  86. } else {
  87. output += ` + ${text}`;
  88. }
  89. };
  90. var outputLines = [];
  91. var flushOutputLines = function () {
  92. writeOutput(`"${escapeText(outputLines.join('\n'))}"`);
  93. outputLines = [];
  94. };
  95. var deps = [];
  96. for (let i = 0; i < lines.length; i++) {
  97. let m = lines[i].match(/\/\/\/\s*<reference\s*lib="([^"]+)"/);
  98. if (m) {
  99. flushOutputLines();
  100. writeOutput(getVariableName(m[1]));
  101. deps.push(getVariableName(m[1]));
  102. enqueue(m[1]);
  103. continue;
  104. }
  105. outputLines.push(lines[i]);
  106. }
  107. flushOutputLines();
  108. result.push({
  109. name: getVariableName(name),
  110. deps: deps,
  111. output: output
  112. });
  113. }
  114. var strResult = `/*---------------------------------------------------------------------------------------------
  115. * Copyright (c) Microsoft Corporation. All rights reserved.
  116. * Licensed under the MIT License. See License.txt in the project root for license information.
  117. *--------------------------------------------------------------------------------------------*/
  118. `;
  119. // Do a topological sort
  120. while (result.length > 0) {
  121. for (let i = result.length - 1; i >= 0; i--) {
  122. if (result[i].deps.length === 0) {
  123. // emit this node
  124. strResult += `\nexport const ${result[i].name} = ${result[i].output};\n`;
  125. // mark dep as resolved
  126. for (let j = 0; j < result.length; j++) {
  127. for (let k = 0; k < result[j].deps.length; k++) {
  128. if (result[j].deps[k] === result[i].name) {
  129. result[j].deps.splice(k, 1);
  130. break;
  131. }
  132. }
  133. }
  134. // remove from result
  135. result.splice(i, 1);
  136. break;
  137. }
  138. }
  139. }
  140. var dstPath = path.join(TYPESCRIPT_LIB_DESTINATION, 'lib.ts');
  141. fs.writeFileSync(dstPath, strResult);
  142. }
  143. /**
  144. * Escape text such that it can be used in a javascript string enclosed by double quotes (")
  145. */
  146. function escapeText(text) {
  147. // See http://www.javascriptkit.com/jsref/escapesequence.shtml
  148. var _backspace = '\b'.charCodeAt(0);
  149. var _formFeed = '\f'.charCodeAt(0);
  150. var _newLine = '\n'.charCodeAt(0);
  151. var _nullChar = 0;
  152. var _carriageReturn = '\r'.charCodeAt(0);
  153. var _tab = '\t'.charCodeAt(0);
  154. var _verticalTab = '\v'.charCodeAt(0);
  155. var _backslash = '\\'.charCodeAt(0);
  156. var _doubleQuote = '"'.charCodeAt(0);
  157. var startPos = 0, chrCode, replaceWith = null, resultPieces = [];
  158. for (var i = 0, len = text.length; i < len; i++) {
  159. chrCode = text.charCodeAt(i);
  160. switch (chrCode) {
  161. case _backspace:
  162. replaceWith = '\\b';
  163. break;
  164. case _formFeed:
  165. replaceWith = '\\f';
  166. break;
  167. case _newLine:
  168. replaceWith = '\\n';
  169. break;
  170. case _nullChar:
  171. replaceWith = '\\0';
  172. break;
  173. case _carriageReturn:
  174. replaceWith = '\\r';
  175. break;
  176. case _tab:
  177. replaceWith = '\\t';
  178. break;
  179. case _verticalTab:
  180. replaceWith = '\\v';
  181. break;
  182. case _backslash:
  183. replaceWith = '\\\\';
  184. break;
  185. case _doubleQuote:
  186. replaceWith = '\\"';
  187. break;
  188. }
  189. if (replaceWith !== null) {
  190. resultPieces.push(text.substring(startPos, i));
  191. resultPieces.push(replaceWith);
  192. startPos = i + 1;
  193. replaceWith = null;
  194. }
  195. }
  196. resultPieces.push(text.substring(startPos, len));
  197. return resultPieces.join('');
  198. }