importTypescript.js 7.8 KB

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