importTypescript.ts 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. import path = require('path');
  6. import fs = require('fs');
  7. import child_process = require('child_process');
  8. import { REPO_ROOT } from './utils';
  9. const generatedNote = `//
  10. // **NOTE**: Do not edit directly! This file is generated using \`npm run import-typescript\`
  11. //
  12. `;
  13. const TYPESCRIPT_LIB_SOURCE = path.join(REPO_ROOT, 'node_modules/typescript/lib');
  14. const TYPESCRIPT_LIB_DESTINATION = path.join(REPO_ROOT, 'src/language/typescript/lib');
  15. (function () {
  16. try {
  17. fs.statSync(TYPESCRIPT_LIB_DESTINATION);
  18. } catch (err) {
  19. fs.mkdirSync(TYPESCRIPT_LIB_DESTINATION);
  20. }
  21. importLibs();
  22. const npmLsOutput = JSON.parse(
  23. child_process.execSync('npm ls typescript --depth=0 --json=true', { cwd: REPO_ROOT }).toString()
  24. );
  25. const typeScriptDependencyVersion = npmLsOutput.dependencies.typescript.version;
  26. fs.writeFileSync(
  27. path.join(TYPESCRIPT_LIB_DESTINATION, 'typescriptServicesMetadata.ts'),
  28. `${generatedNote}
  29. export const typescriptVersion = "${typeScriptDependencyVersion}";\n`
  30. );
  31. let tsServices = fs.readFileSync(path.join(TYPESCRIPT_LIB_SOURCE, 'typescript.js')).toString();
  32. // The output from this build will only be accessible via AMD or ESM; rather than removing
  33. // references to require/module, define them as dummy variables that bundlers will ignore.
  34. // The TS code can figure out that it's not running under Node even with these defined.
  35. tsServices =
  36. `
  37. /* MONACOCHANGE */
  38. var require = undefined;
  39. var module = { exports: {} };
  40. /* END MONACOCHANGE */
  41. ` + tsServices;
  42. const tsServices_amd =
  43. generatedNote +
  44. tsServices +
  45. `
  46. // MONACOCHANGE
  47. // Defining the entire module name because r.js has an issue and cannot bundle this file
  48. // correctly with an anonymous define call
  49. define("vs/language/typescript/lib/typescriptServices", [], function() { return ts; });
  50. // END MONACOCHANGE
  51. `;
  52. fs.writeFileSync(
  53. path.join(TYPESCRIPT_LIB_DESTINATION, 'typescriptServices-amd.js'),
  54. stripSourceMaps(tsServices_amd)
  55. );
  56. const tsServices_esm =
  57. generatedNote +
  58. tsServices +
  59. `
  60. // MONACOCHANGE
  61. export var createClassifier = ts.createClassifier;
  62. export var createLanguageService = ts.createLanguageService;
  63. export var displayPartsToString = ts.displayPartsToString;
  64. export var EndOfLineState = ts.EndOfLineState;
  65. export var flattenDiagnosticMessageText = ts.flattenDiagnosticMessageText;
  66. export var IndentStyle = ts.IndentStyle;
  67. export var ScriptKind = ts.ScriptKind;
  68. export var ScriptTarget = ts.ScriptTarget;
  69. export var TokenClass = ts.TokenClass;
  70. export var typescript = ts;
  71. // END MONACOCHANGE
  72. `;
  73. fs.writeFileSync(
  74. path.join(TYPESCRIPT_LIB_DESTINATION, 'typescriptServices.js'),
  75. stripSourceMaps(tsServices_esm)
  76. );
  77. let dtsServices = fs.readFileSync(path.join(TYPESCRIPT_LIB_SOURCE, 'typescript.d.ts')).toString();
  78. fs.writeFileSync(
  79. path.join(TYPESCRIPT_LIB_DESTINATION, 'typescriptServices.d.ts'),
  80. generatedNote + dtsServices
  81. );
  82. })();
  83. function importLibs() {
  84. function readLibFile(name) {
  85. const srcPath = path.join(TYPESCRIPT_LIB_SOURCE, name);
  86. return fs.readFileSync(srcPath).toString();
  87. }
  88. let strLibResult = `/*---------------------------------------------------------------------------------------------
  89. * Copyright (c) Microsoft Corporation. All rights reserved.
  90. * Licensed under the MIT License. See License.txt in the project root for license information.
  91. *--------------------------------------------------------------------------------------------*/
  92. ${generatedNote}
  93. /** Contains all the lib files */
  94. export const libFileMap: Record<string, string> = {}
  95. `;
  96. let strIndexResult = `/*---------------------------------------------------------------------------------------------
  97. * Copyright (c) Microsoft Corporation. All rights reserved.
  98. * Licensed under the MIT License. See License.txt in the project root for license information.
  99. *--------------------------------------------------------------------------------------------*/
  100. ${generatedNote}
  101. /** Contains all the lib files */
  102. export const libFileSet: Record<string, boolean> = {}
  103. `;
  104. const dtsFiles = fs.readdirSync(TYPESCRIPT_LIB_SOURCE).filter((f) => f.includes('lib.'));
  105. while (dtsFiles.length > 0) {
  106. const name = dtsFiles.shift();
  107. const output = readLibFile(name).replace(/\r\n/g, '\n');
  108. strLibResult += `libFileMap['${name}'] = "${escapeText(output)}";\n`;
  109. strIndexResult += `libFileSet['${name}'] = true;\n`;
  110. }
  111. fs.writeFileSync(path.join(TYPESCRIPT_LIB_DESTINATION, 'lib.ts'), strLibResult);
  112. fs.writeFileSync(path.join(TYPESCRIPT_LIB_DESTINATION, 'lib.index.ts'), strIndexResult);
  113. }
  114. /**
  115. * Escape text such that it can be used in a javascript string enclosed by double quotes (")
  116. */
  117. function escapeText(text) {
  118. // See http://www.javascriptkit.com/jsref/escapesequence.shtml
  119. const _backspace = '\b'.charCodeAt(0);
  120. const _formFeed = '\f'.charCodeAt(0);
  121. const _newLine = '\n'.charCodeAt(0);
  122. const _nullChar = 0;
  123. const _carriageReturn = '\r'.charCodeAt(0);
  124. const _tab = '\t'.charCodeAt(0);
  125. const _verticalTab = '\v'.charCodeAt(0);
  126. const _backslash = '\\'.charCodeAt(0);
  127. const _doubleQuote = '"'.charCodeAt(0);
  128. const len = text.length;
  129. let startPos = 0;
  130. let chrCode;
  131. let replaceWith = null;
  132. let resultPieces = [];
  133. for (let i = 0; i < len; i++) {
  134. chrCode = text.charCodeAt(i);
  135. switch (chrCode) {
  136. case _backspace:
  137. replaceWith = '\\b';
  138. break;
  139. case _formFeed:
  140. replaceWith = '\\f';
  141. break;
  142. case _newLine:
  143. replaceWith = '\\n';
  144. break;
  145. case _nullChar:
  146. replaceWith = '\\0';
  147. break;
  148. case _carriageReturn:
  149. replaceWith = '\\r';
  150. break;
  151. case _tab:
  152. replaceWith = '\\t';
  153. break;
  154. case _verticalTab:
  155. replaceWith = '\\v';
  156. break;
  157. case _backslash:
  158. replaceWith = '\\\\';
  159. break;
  160. case _doubleQuote:
  161. replaceWith = '\\"';
  162. break;
  163. }
  164. if (replaceWith !== null) {
  165. resultPieces.push(text.substring(startPos, i));
  166. resultPieces.push(replaceWith);
  167. startPos = i + 1;
  168. replaceWith = null;
  169. }
  170. }
  171. resultPieces.push(text.substring(startPos, len));
  172. return resultPieces.join('');
  173. }
  174. function stripSourceMaps(str) {
  175. return str.replace(/\/\/# sourceMappingURL[^\n]+/gm, '');
  176. }