importTypescript.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 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_esm =
  43. generatedNote +
  44. tsServices +
  45. `
  46. // MONACOCHANGE
  47. export var createClassifier = ts.createClassifier;
  48. export var createLanguageService = ts.createLanguageService;
  49. export var displayPartsToString = ts.displayPartsToString;
  50. export var EndOfLineState = ts.EndOfLineState;
  51. export var flattenDiagnosticMessageText = ts.flattenDiagnosticMessageText;
  52. export var IndentStyle = ts.IndentStyle;
  53. export var ScriptKind = ts.ScriptKind;
  54. export var ScriptTarget = ts.ScriptTarget;
  55. export var TokenClass = ts.TokenClass;
  56. export var typescript = ts;
  57. // END MONACOCHANGE
  58. `;
  59. fs.writeFileSync(
  60. path.join(TYPESCRIPT_LIB_DESTINATION, 'typescriptServices.js'),
  61. stripSourceMaps(tsServices_esm)
  62. );
  63. let dtsServices = fs.readFileSync(path.join(TYPESCRIPT_LIB_SOURCE, 'typescript.d.ts')).toString();
  64. fs.writeFileSync(
  65. path.join(TYPESCRIPT_LIB_DESTINATION, 'typescriptServices.d.ts'),
  66. generatedNote + dtsServices
  67. );
  68. })();
  69. function importLibs() {
  70. function readLibFile(name) {
  71. const srcPath = path.join(TYPESCRIPT_LIB_SOURCE, name);
  72. return fs.readFileSync(srcPath).toString();
  73. }
  74. let strLibResult = `/*---------------------------------------------------------------------------------------------
  75. * Copyright (c) Microsoft Corporation. All rights reserved.
  76. * Licensed under the MIT License. See License.txt in the project root for license information.
  77. *--------------------------------------------------------------------------------------------*/
  78. ${generatedNote}
  79. /** Contains all the lib files */
  80. export const libFileMap: Record<string, string> = {}
  81. `;
  82. let strIndexResult = `/*---------------------------------------------------------------------------------------------
  83. * Copyright (c) Microsoft Corporation. All rights reserved.
  84. * Licensed under the MIT License. See License.txt in the project root for license information.
  85. *--------------------------------------------------------------------------------------------*/
  86. ${generatedNote}
  87. /** Contains all the lib files */
  88. export const libFileSet: Record<string, boolean> = {}
  89. `;
  90. const dtsFiles = fs.readdirSync(TYPESCRIPT_LIB_SOURCE).filter((f) => f.includes('lib.'));
  91. while (dtsFiles.length > 0) {
  92. const name = dtsFiles.shift();
  93. const output = readLibFile(name).replace(/\r\n/g, '\n');
  94. strLibResult += `libFileMap['${name}'] = "${escapeText(output)}";\n`;
  95. strIndexResult += `libFileSet['${name}'] = true;\n`;
  96. }
  97. fs.writeFileSync(path.join(TYPESCRIPT_LIB_DESTINATION, 'lib.ts'), strLibResult);
  98. fs.writeFileSync(path.join(TYPESCRIPT_LIB_DESTINATION, 'lib.index.ts'), strIndexResult);
  99. }
  100. /**
  101. * Escape text such that it can be used in a javascript string enclosed by double quotes (")
  102. */
  103. function escapeText(text) {
  104. // See http://www.javascriptkit.com/jsref/escapesequence.shtml
  105. const _backspace = '\b'.charCodeAt(0);
  106. const _formFeed = '\f'.charCodeAt(0);
  107. const _newLine = '\n'.charCodeAt(0);
  108. const _nullChar = 0;
  109. const _carriageReturn = '\r'.charCodeAt(0);
  110. const _tab = '\t'.charCodeAt(0);
  111. const _verticalTab = '\v'.charCodeAt(0);
  112. const _backslash = '\\'.charCodeAt(0);
  113. const _doubleQuote = '"'.charCodeAt(0);
  114. const len = text.length;
  115. let startPos = 0;
  116. let chrCode;
  117. let replaceWith = null;
  118. let resultPieces = [];
  119. for (let i = 0; i < len; i++) {
  120. chrCode = text.charCodeAt(i);
  121. switch (chrCode) {
  122. case _backspace:
  123. replaceWith = '\\b';
  124. break;
  125. case _formFeed:
  126. replaceWith = '\\f';
  127. break;
  128. case _newLine:
  129. replaceWith = '\\n';
  130. break;
  131. case _nullChar:
  132. replaceWith = '\\0';
  133. break;
  134. case _carriageReturn:
  135. replaceWith = '\\r';
  136. break;
  137. case _tab:
  138. replaceWith = '\\t';
  139. break;
  140. case _verticalTab:
  141. replaceWith = '\\v';
  142. break;
  143. case _backslash:
  144. replaceWith = '\\\\';
  145. break;
  146. case _doubleQuote:
  147. replaceWith = '\\"';
  148. break;
  149. }
  150. if (replaceWith !== null) {
  151. resultPieces.push(text.substring(startPos, i));
  152. resultPieces.push(replaceWith);
  153. startPos = i + 1;
  154. replaceWith = null;
  155. }
  156. }
  157. resultPieces.push(text.substring(startPos, len));
  158. return resultPieces.join('');
  159. }
  160. function stripSourceMaps(str) {
  161. return str.replace(/\/\/# sourceMappingURL[^\n]+/gm, '');
  162. }