importTypescript.js 6.5 KB

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