importTypescript.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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(child_process.execSync("npm ls typescript --depth=0 --json=true").toString());
  22. const typeScriptDependencyVersion = npmLsOutput.dependencies.typescript.version;
  23. fs.writeFileSync(
  24. path.join(TYPESCRIPT_LIB_DESTINATION, 'typescriptServicesMetadata.ts'),
  25. `${generatedNote}
  26. export const typescriptVersion = "${typeScriptDependencyVersion}";\n`
  27. );
  28. var tsServices = fs.readFileSync(path.join(TYPESCRIPT_LIB_SOURCE, 'typescriptServices.js')).toString();
  29. // Ensure we never run into the node system...
  30. // (this also removes require calls that trick webpack into shimming those modules...)
  31. tsServices = (
  32. tsServices.replace(/\n ts\.sys =([^]*)\n \}\)\(\);/m, `\n // MONACOCHANGE\n ts.sys = undefined;\n // END MONACOCHANGE`)
  33. );
  34. // Eliminate more require() calls...
  35. tsServices = tsServices.replace(/^( +)etwModule = require\(.*$/m, '$1// MONACOCHANGE\n$1etwModule = undefined;\n$1// END MONACOCHANGE');
  36. tsServices = tsServices.replace(/^( +)var result = ts\.sys\.require\(.*$/m, '$1// MONACOCHANGE\n$1var result = undefined;\n$1// END MONACOCHANGE');
  37. // Flag any new require calls (outside comments) so they can be corrected preemptively.
  38. // To avoid missing cases (or using an even more complex regex), temporarily remove comments
  39. // about require() and then check for lines actually calling require().
  40. // \/[*/] matches the start of a comment (single or multi-line).
  41. // ^\s+\*[^/] matches (presumably) a later line of a multi-line comment.
  42. const tsServicesNoCommentedRequire = tsServices.replace(/(\/[*/]|^\s+\*[^/]).*\brequire\(.*/gm, '');
  43. const linesWithRequire = tsServicesNoCommentedRequire.match(/^.*?\brequire\(.*$/gm)
  44. // Allow error messages to include references to require() in their strings
  45. const runtimeRequires = linesWithRequire && linesWithRequire.filter(l => !l.includes(": diag("))
  46. if (runtimeRequires && runtimeRequires.length && linesWithRequire) {
  47. console.error('Found new require() calls on the following lines. These should be removed to avoid breaking webpack builds.\n');
  48. console.error(linesWithRequire.join('\n'));
  49. process.exit(1);
  50. }
  51. // Make sure process.args don't get called in the browser, this
  52. // should only happen in TS 2.6.2
  53. const beforeProcess = `ts.perfLogger.logInfoEvent("Starting TypeScript v" + ts.versionMajorMinor + " with command line: " + JSON.stringify(process.argv));`
  54. const afterProcess = `// MONACOCHANGE\n ts.perfLogger.logInfoEvent("Starting TypeScript v" + ts.versionMajorMinor + " with command line: " + JSON.stringify([]));\n// END MONACOCHANGE`
  55. tsServices = tsServices.replace(beforeProcess, afterProcess);
  56. var tsServices_amd = generatedNote + tsServices +
  57. `
  58. // MONACOCHANGE
  59. // Defining the entire module name because r.js has an issue and cannot bundle this file
  60. // correctly with an anonymous define call
  61. define("vs/language/typescript/lib/typescriptServices", [], function() { return ts; });
  62. // END MONACOCHANGE
  63. `;
  64. fs.writeFileSync(path.join(TYPESCRIPT_LIB_DESTINATION, 'typescriptServices-amd.js'), stripSourceMaps(tsServices_amd));
  65. var tsServices_esm = generatedNote + tsServices +
  66. `
  67. // MONACOCHANGE
  68. export var createClassifier = ts.createClassifier;
  69. export var createLanguageService = ts.createLanguageService;
  70. export var displayPartsToString = ts.displayPartsToString;
  71. export var EndOfLineState = ts.EndOfLineState;
  72. export var flattenDiagnosticMessageText = ts.flattenDiagnosticMessageText;
  73. export var IndentStyle = ts.IndentStyle;
  74. export var ScriptKind = ts.ScriptKind;
  75. export var ScriptTarget = ts.ScriptTarget;
  76. export var TokenClass = ts.TokenClass;
  77. // END MONACOCHANGE
  78. `;
  79. fs.writeFileSync(path.join(TYPESCRIPT_LIB_DESTINATION, 'typescriptServices.js'), stripSourceMaps(tsServices_esm));
  80. var dtsServices = fs.readFileSync(path.join(TYPESCRIPT_LIB_SOURCE, 'typescriptServices.d.ts')).toString();
  81. dtsServices +=
  82. `
  83. // MONACOCHANGE
  84. export = ts;
  85. // END MONACOCHANGE
  86. `;
  87. fs.writeFileSync(path.join(TYPESCRIPT_LIB_DESTINATION, 'typescriptServices.d.ts'), generatedNote + dtsServices);
  88. })();
  89. function importLibs() {
  90. function getFileName(name) {
  91. return (name === '' ? 'lib.d.ts' : `lib.${name}.d.ts`);
  92. }
  93. function getVariableName(name) {
  94. return (name === '' ? 'lib_dts' : `lib_${name.replace(/\./g, '_')}_dts`);
  95. }
  96. function readLibFile(name) {
  97. var srcPath = path.join(TYPESCRIPT_LIB_SOURCE, getFileName(name));
  98. return fs.readFileSync(srcPath).toString();
  99. }
  100. var queue = [];
  101. var in_queue = {};
  102. var enqueue = function (name) {
  103. if (in_queue[name]) {
  104. return;
  105. }
  106. in_queue[name] = true;
  107. queue.push(name);
  108. };
  109. enqueue('');
  110. enqueue('es2015');
  111. var result = [];
  112. while (queue.length > 0) {
  113. var name = queue.shift();
  114. var contents = readLibFile(name);
  115. var lines = contents.split(/\r\n|\r|\n/);
  116. var output = '';
  117. var writeOutput = function (text) {
  118. if (output.length === 0) {
  119. output = text;
  120. } else {
  121. output += ` + ${text}`;
  122. }
  123. };
  124. var outputLines = [];
  125. var flushOutputLines = function () {
  126. writeOutput(`"${escapeText(outputLines.join('\n'))}"`);
  127. outputLines = [];
  128. };
  129. var deps = [];
  130. for (let i = 0; i < lines.length; i++) {
  131. let m = lines[i].match(/\/\/\/\s*<reference\s*lib="([^"]+)"/);
  132. if (m) {
  133. flushOutputLines();
  134. writeOutput(getVariableName(m[1]));
  135. deps.push(getVariableName(m[1]));
  136. enqueue(m[1]);
  137. continue;
  138. }
  139. outputLines.push(lines[i]);
  140. }
  141. flushOutputLines();
  142. result.push({
  143. name: getVariableName(name),
  144. deps: deps,
  145. output: output
  146. });
  147. }
  148. var strResult = `/*---------------------------------------------------------------------------------------------
  149. * Copyright (c) Microsoft Corporation. All rights reserved.
  150. * Licensed under the MIT License. See License.txt in the project root for license information.
  151. *--------------------------------------------------------------------------------------------*/
  152. ${generatedNote}`;
  153. // Do a topological sort
  154. while (result.length > 0) {
  155. for (let i = result.length - 1; i >= 0; i--) {
  156. if (result[i].deps.length === 0) {
  157. // emit this node
  158. strResult += `\nexport const ${result[i].name}: string = ${result[i].output};\n`;
  159. // mark dep as resolved
  160. for (let j = 0; j < result.length; j++) {
  161. for (let k = 0; k < result[j].deps.length; k++) {
  162. if (result[j].deps[k] === result[i].name) {
  163. result[j].deps.splice(k, 1);
  164. break;
  165. }
  166. }
  167. }
  168. // remove from result
  169. result.splice(i, 1);
  170. break;
  171. }
  172. }
  173. }
  174. strResult += `
  175. /** This is the DTS which is used when the target is ES6 or below */
  176. export const lib_es5_bundled_dts = lib_dts;
  177. /** This is the DTS which is used by default in monaco-typescript, and when the target is 2015 or above */
  178. export const lib_es2015_bundled_dts = lib_es2015_dts + "" + lib_dom_dts + "" + lib_webworker_importscripts_dts + "" + lib_scripthost_dts + "";
  179. `
  180. var dstPath = path.join(TYPESCRIPT_LIB_DESTINATION, 'lib.ts');
  181. fs.writeFileSync(dstPath, strResult);
  182. }
  183. /**
  184. * Escape text such that it can be used in a javascript string enclosed by double quotes (")
  185. */
  186. function escapeText(text) {
  187. // See http://www.javascriptkit.com/jsref/escapesequence.shtml
  188. var _backspace = '\b'.charCodeAt(0);
  189. var _formFeed = '\f'.charCodeAt(0);
  190. var _newLine = '\n'.charCodeAt(0);
  191. var _nullChar = 0;
  192. var _carriageReturn = '\r'.charCodeAt(0);
  193. var _tab = '\t'.charCodeAt(0);
  194. var _verticalTab = '\v'.charCodeAt(0);
  195. var _backslash = '\\'.charCodeAt(0);
  196. var _doubleQuote = '"'.charCodeAt(0);
  197. var startPos = 0, chrCode, replaceWith = null, resultPieces = [];
  198. for (var i = 0, len = text.length; i < len; i++) {
  199. chrCode = text.charCodeAt(i);
  200. switch (chrCode) {
  201. case _backspace:
  202. replaceWith = '\\b';
  203. break;
  204. case _formFeed:
  205. replaceWith = '\\f';
  206. break;
  207. case _newLine:
  208. replaceWith = '\\n';
  209. break;
  210. case _nullChar:
  211. replaceWith = '\\0';
  212. break;
  213. case _carriageReturn:
  214. replaceWith = '\\r';
  215. break;
  216. case _tab:
  217. replaceWith = '\\t';
  218. break;
  219. case _verticalTab:
  220. replaceWith = '\\v';
  221. break;
  222. case _backslash:
  223. replaceWith = '\\\\';
  224. break;
  225. case _doubleQuote:
  226. replaceWith = '\\"';
  227. break;
  228. }
  229. if (replaceWith !== null) {
  230. resultPieces.push(text.substring(startPos, i));
  231. resultPieces.push(replaceWith);
  232. startPos = i + 1;
  233. replaceWith = null;
  234. }
  235. }
  236. resultPieces.push(text.substring(startPos, len));
  237. return resultPieces.join('');
  238. }
  239. function stripSourceMaps(str) {
  240. return str.replace(/\/\/# sourceMappingURL[^\n]+/gm, '');
  241. }