importTypescript.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. //@ts-check
  6. const path = require('path');
  7. const fs = require('fs');
  8. const child_process = require('child_process');
  9. const { REPO_ROOT } = require('./utils');
  10. const generatedNote = `//
  11. // **NOTE**: Do not edit directly! This file is generated using \`npm run import-typescript\`
  12. //
  13. `;
  14. const TYPESCRIPT_LIB_SOURCE = path.join(REPO_ROOT, 'node_modules/typescript/lib');
  15. const TYPESCRIPT_LIB_DESTINATION = path.join(REPO_ROOT, 'src/typescript/lib');
  16. (function () {
  17. try {
  18. fs.statSync(TYPESCRIPT_LIB_DESTINATION);
  19. } catch (err) {
  20. fs.mkdirSync(TYPESCRIPT_LIB_DESTINATION);
  21. }
  22. importLibs();
  23. const npmLsOutput = JSON.parse(
  24. child_process.execSync('npm ls typescript --depth=0 --json=true', { cwd: REPO_ROOT }).toString()
  25. );
  26. const typeScriptDependencyVersion = npmLsOutput.dependencies.typescript.version;
  27. fs.writeFileSync(
  28. path.join(TYPESCRIPT_LIB_DESTINATION, 'typescriptServicesMetadata.ts'),
  29. `${generatedNote}
  30. export const typescriptVersion = "${typeScriptDependencyVersion}";\n`
  31. );
  32. let tsServices = fs
  33. .readFileSync(path.join(TYPESCRIPT_LIB_SOURCE, 'typescriptServices.js'))
  34. .toString();
  35. // Ensure we never run into the node system...
  36. // (this also removes require calls that trick webpack into shimming those modules...)
  37. tsServices = tsServices.replace(
  38. /\n ts\.sys =([^]*)\n \}\)\(\);/m,
  39. `\n // MONACOCHANGE\n ts.sys = undefined;\n // END MONACOCHANGE`
  40. );
  41. // Eliminate more require() calls...
  42. tsServices = tsServices.replace(
  43. /^( +)etwModule = require\(.*$/m,
  44. '$1// MONACOCHANGE\n$1etwModule = undefined;\n$1// END MONACOCHANGE'
  45. );
  46. tsServices = tsServices.replace(
  47. /^( +)var result = ts\.sys\.require\(.*$/m,
  48. '$1// MONACOCHANGE\n$1var result = undefined;\n$1// END MONACOCHANGE'
  49. );
  50. tsServices = tsServices.replace(
  51. /^( +)fs = require\("fs"\);$/m,
  52. '$1// MONACOCHANGE\n$1fs = undefined;\n$1// END MONACOCHANGE'
  53. );
  54. tsServices = tsServices.replace(
  55. /^( +)debugger;$/m,
  56. '$1// MONACOCHANGE\n$1// debugger;\n$1// END MONACOCHANGE'
  57. );
  58. tsServices = tsServices.replace(
  59. /= require\("perf_hooks"\)/m,
  60. '/* MONACOCHANGE */= {}/* END MONACOCHANGE */'
  61. );
  62. // Flag any new require calls (outside comments) so they can be corrected preemptively.
  63. // To avoid missing cases (or using an even more complex regex), temporarily remove comments
  64. // about require() and then check for lines actually calling require().
  65. // \/[*/] matches the start of a comment (single or multi-line).
  66. // ^\s+\*[^/] matches (presumably) a later line of a multi-line comment.
  67. const tsServicesNoCommentedRequire = tsServices.replace(
  68. /(\/[*/]|^\s+\*[^/]).*\brequire\(.*/gm,
  69. ''
  70. );
  71. const linesWithRequire = tsServicesNoCommentedRequire.match(/^.*?\brequire\(.*$/gm);
  72. // Allow error messages to include references to require() in their strings
  73. const runtimeRequires =
  74. linesWithRequire &&
  75. linesWithRequire.filter((l) => !l.includes(': diag(') && !l.includes('ts.DiagnosticCategory'));
  76. if (runtimeRequires && runtimeRequires.length && linesWithRequire) {
  77. console.error(
  78. 'Found new require() calls on the following lines. These should be removed to avoid breaking webpack builds.\n'
  79. );
  80. console.error(
  81. runtimeRequires.map((r) => `${r} (${tsServicesNoCommentedRequire.indexOf(r)})`).join('\n')
  82. );
  83. process.exit(1);
  84. }
  85. const tsServices_amd =
  86. generatedNote +
  87. tsServices +
  88. `
  89. // MONACOCHANGE
  90. // Defining the entire module name because r.js has an issue and cannot bundle this file
  91. // correctly with an anonymous define call
  92. define("vs/language/typescript/lib/typescriptServices", [], function() { return ts; });
  93. // END MONACOCHANGE
  94. `;
  95. fs.writeFileSync(
  96. path.join(TYPESCRIPT_LIB_DESTINATION, 'typescriptServices-amd.js'),
  97. stripSourceMaps(tsServices_amd)
  98. );
  99. // Remove pattern that creates warnings with esbuild
  100. // e.g.
  101. // > /src/typescript/lib/typescriptServices.js:20:21: warning: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
  102. // 20 │ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
  103. // ╵ ~~~~
  104. //
  105. tsServices = tsServices.replace(/\nvar ([^ ]+) = \(this && this\.([^)]+)\) \|\|/gm, '\nvar $1 =');
  106. const tsServices_esm =
  107. generatedNote +
  108. tsServices +
  109. `
  110. // MONACOCHANGE
  111. export var createClassifier = ts.createClassifier;
  112. export var createLanguageService = ts.createLanguageService;
  113. export var displayPartsToString = ts.displayPartsToString;
  114. export var EndOfLineState = ts.EndOfLineState;
  115. export var flattenDiagnosticMessageText = ts.flattenDiagnosticMessageText;
  116. export var IndentStyle = ts.IndentStyle;
  117. export var ScriptKind = ts.ScriptKind;
  118. export var ScriptTarget = ts.ScriptTarget;
  119. export var TokenClass = ts.TokenClass;
  120. // END MONACOCHANGE
  121. `;
  122. fs.writeFileSync(
  123. path.join(TYPESCRIPT_LIB_DESTINATION, 'typescriptServices.js'),
  124. stripSourceMaps(tsServices_esm)
  125. );
  126. let dtsServices = fs
  127. .readFileSync(path.join(TYPESCRIPT_LIB_SOURCE, 'typescriptServices.d.ts'))
  128. .toString();
  129. dtsServices += `
  130. // MONACOCHANGE
  131. export = ts;
  132. // END MONACOCHANGE
  133. `;
  134. fs.writeFileSync(
  135. path.join(TYPESCRIPT_LIB_DESTINATION, 'typescriptServices.d.ts'),
  136. generatedNote + dtsServices
  137. );
  138. })();
  139. function importLibs() {
  140. function readLibFile(name) {
  141. const srcPath = path.join(TYPESCRIPT_LIB_SOURCE, name);
  142. return fs.readFileSync(srcPath).toString();
  143. }
  144. let strLibResult = `/*---------------------------------------------------------------------------------------------
  145. * Copyright (c) Microsoft Corporation. All rights reserved.
  146. * Licensed under the MIT License. See License.txt in the project root for license information.
  147. *--------------------------------------------------------------------------------------------*/
  148. ${generatedNote}
  149. /** Contains all the lib files */
  150. export const libFileMap: Record<string, string> = {}
  151. `;
  152. let strIndexResult = `/*---------------------------------------------------------------------------------------------
  153. * Copyright (c) Microsoft Corporation. All rights reserved.
  154. * Licensed under the MIT License. See License.txt in the project root for license information.
  155. *--------------------------------------------------------------------------------------------*/
  156. ${generatedNote}
  157. /** Contains all the lib files */
  158. export const libFileSet: Record<string, boolean> = {}
  159. `;
  160. const dtsFiles = fs.readdirSync(TYPESCRIPT_LIB_SOURCE).filter((f) => f.includes('lib.'));
  161. while (dtsFiles.length > 0) {
  162. const name = dtsFiles.shift();
  163. const output = readLibFile(name).replace(/\r\n/g, '\n');
  164. strLibResult += `libFileMap['${name}'] = "${escapeText(output)}";\n`;
  165. strIndexResult += `libFileSet['${name}'] = true;\n`;
  166. }
  167. fs.writeFileSync(path.join(TYPESCRIPT_LIB_DESTINATION, 'lib.ts'), strLibResult);
  168. fs.writeFileSync(path.join(TYPESCRIPT_LIB_DESTINATION, 'lib.index.ts'), strIndexResult);
  169. }
  170. /**
  171. * Escape text such that it can be used in a javascript string enclosed by double quotes (")
  172. */
  173. function escapeText(text) {
  174. // See http://www.javascriptkit.com/jsref/escapesequence.shtml
  175. const _backspace = '\b'.charCodeAt(0);
  176. const _formFeed = '\f'.charCodeAt(0);
  177. const _newLine = '\n'.charCodeAt(0);
  178. const _nullChar = 0;
  179. const _carriageReturn = '\r'.charCodeAt(0);
  180. const _tab = '\t'.charCodeAt(0);
  181. const _verticalTab = '\v'.charCodeAt(0);
  182. const _backslash = '\\'.charCodeAt(0);
  183. const _doubleQuote = '"'.charCodeAt(0);
  184. const len = text.length;
  185. let startPos = 0;
  186. let chrCode;
  187. let replaceWith = null;
  188. let resultPieces = [];
  189. for (let i = 0; i < len; i++) {
  190. chrCode = text.charCodeAt(i);
  191. switch (chrCode) {
  192. case _backspace:
  193. replaceWith = '\\b';
  194. break;
  195. case _formFeed:
  196. replaceWith = '\\f';
  197. break;
  198. case _newLine:
  199. replaceWith = '\\n';
  200. break;
  201. case _nullChar:
  202. replaceWith = '\\0';
  203. break;
  204. case _carriageReturn:
  205. replaceWith = '\\r';
  206. break;
  207. case _tab:
  208. replaceWith = '\\t';
  209. break;
  210. case _verticalTab:
  211. replaceWith = '\\v';
  212. break;
  213. case _backslash:
  214. replaceWith = '\\\\';
  215. break;
  216. case _doubleQuote:
  217. replaceWith = '\\"';
  218. break;
  219. }
  220. if (replaceWith !== null) {
  221. resultPieces.push(text.substring(startPos, i));
  222. resultPieces.push(replaceWith);
  223. startPos = i + 1;
  224. replaceWith = null;
  225. }
  226. }
  227. resultPieces.push(text.substring(startPos, len));
  228. return resultPieces.join('');
  229. }
  230. function stripSourceMaps(str) {
  231. return str.replace(/\/\/# sourceMappingURL[^\n]+/gm, '');
  232. }