1
0

importTypescript.js 7.0 KB

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