importTypescript.js 8.7 KB

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