importTypescript.js 7.3 KB

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