importTypescript.js 6.8 KB

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