1
0

importTypescript.js 7.5 KB

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