importTypescript.js 6.8 KB

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