1
0

importTypescript.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 TYPESCRIPT_LIB_SOURCE = path.join(__dirname, '../node_modules/typescript/lib');
  8. const TYPESCRIPT_LIB_DESTINATION = path.join(__dirname, '../src/lib');
  9. (function() {
  10. try {
  11. fs.statSync(TYPESCRIPT_LIB_DESTINATION);
  12. } catch (err) {
  13. fs.mkdirSync(TYPESCRIPT_LIB_DESTINATION);
  14. }
  15. importLibDeclarationFile('lib.d.ts');
  16. importLibDeclarationFile('lib.es6.d.ts');
  17. var tsServices = fs.readFileSync(path.join(TYPESCRIPT_LIB_SOURCE, 'typescriptServices.js')).toString();
  18. // Ensure we never run into the node system...
  19. // (this also removes require calls that trick webpack into shimming those modules...)
  20. tsServices = (
  21. tsServices.replace(/\n ts\.sys =([^]*)\n \}\)\(\);/m, `\n // MONACOCHANGE\n ts.sys = undefined;\n // END MONACOCHANGE`)
  22. );
  23. var tsServices_amd = tsServices +
  24. `
  25. // MONACOCHANGE
  26. // Defining the entire module name because r.js has an issue and cannot bundle this file
  27. // correctly with an anonymous define call
  28. define("vs/language/typescript/lib/typescriptServices", [], function() { return ts; });
  29. // END MONACOCHANGE
  30. `;
  31. fs.writeFileSync(path.join(TYPESCRIPT_LIB_DESTINATION, 'typescriptServices-amd.js'), tsServices_amd);
  32. var tsServices_esm = tsServices +
  33. `
  34. // MONACOCHANGE
  35. export const createClassifier = ts.createClassifier;
  36. export const createLanguageService = ts.createLanguageService;
  37. export const displayPartsToString = ts.displayPartsToString;
  38. export const EndOfLineState = ts.EndOfLineState;
  39. export const flattenDiagnosticMessageText = ts.flattenDiagnosticMessageText;
  40. export const IndentStyle = ts.IndentStyle;
  41. export const ScriptKind = ts.ScriptKind;
  42. export const ScriptTarget = ts.ScriptTarget;
  43. export const TokenClass = ts.TokenClass;
  44. // END MONACOCHANGE
  45. `;
  46. fs.writeFileSync(path.join(TYPESCRIPT_LIB_DESTINATION, 'typescriptServices.js'), tsServices_esm);
  47. var dtsServices = fs.readFileSync(path.join(TYPESCRIPT_LIB_SOURCE, 'typescriptServices.d.ts')).toString();
  48. dtsServices +=
  49. `
  50. // MONACOCHANGE
  51. export = ts;
  52. // END MONACOCHANGE
  53. `;
  54. fs.writeFileSync(path.join(TYPESCRIPT_LIB_DESTINATION, 'typescriptServices.d.ts'), dtsServices);
  55. })();
  56. /**
  57. * Import a lib*.d.ts file from TypeScript's dist
  58. */
  59. function importLibDeclarationFile(name) {
  60. var dstName = name.replace(/\.d\.ts$/, '').replace(/\./g, '-') + '-ts';
  61. var contents = resolveLibFile(null, name);
  62. var dstPath = path.join(TYPESCRIPT_LIB_DESTINATION, dstName + '.ts');
  63. fs.writeFileSync(dstPath,
  64. `/*---------------------------------------------------------------------------------------------
  65. * Copyright (c) Microsoft Corporation. All rights reserved.
  66. * Licensed under the MIT License. See License.txt in the project root for license information.
  67. *--------------------------------------------------------------------------------------------*/
  68. export const contents = "${escapeText(contents)}";
  69. `);
  70. }
  71. function resolveLibFile(name, filename) {
  72. var srcPath;
  73. if (filename) {
  74. srcPath = path.join(TYPESCRIPT_LIB_SOURCE, filename);
  75. } else {
  76. srcPath = path.join(TYPESCRIPT_LIB_SOURCE, `lib.${name}.d.ts`);
  77. }
  78. var contents = fs.readFileSync(srcPath).toString();
  79. var lines = contents.split(/\r\n|\r|\n/);
  80. var result = [];
  81. for (let i = 0; i < lines.length; i++) {
  82. let m = lines[i].match(/\/\/\/\s*<reference\s*lib="([^"]+)"/);
  83. if (m) {
  84. result.push('\n' + resolveLibFile(m[1], null) + '\n');
  85. continue;
  86. }
  87. result.push(lines[i]);
  88. }
  89. return result.join('\n');
  90. }
  91. /**
  92. * Escape text such that it can be used in a javascript string enclosed by double quotes (")
  93. */
  94. function escapeText(text) {
  95. // See http://www.javascriptkit.com/jsref/escapesequence.shtml
  96. var _backspace = '\b'.charCodeAt(0);
  97. var _formFeed = '\f'.charCodeAt(0);
  98. var _newLine = '\n'.charCodeAt(0);
  99. var _nullChar = 0;
  100. var _carriageReturn = '\r'.charCodeAt(0);
  101. var _tab = '\t'.charCodeAt(0);
  102. var _verticalTab = '\v'.charCodeAt(0);
  103. var _backslash = '\\'.charCodeAt(0);
  104. var _doubleQuote = '"'.charCodeAt(0);
  105. var startPos = 0, chrCode, replaceWith = null, resultPieces = [];
  106. for (var i = 0, len = text.length; i < len; i++) {
  107. chrCode = text.charCodeAt(i);
  108. switch (chrCode) {
  109. case _backspace:
  110. replaceWith = '\\b';
  111. break;
  112. case _formFeed:
  113. replaceWith = '\\f';
  114. break;
  115. case _newLine:
  116. replaceWith = '\\n';
  117. break;
  118. case _nullChar:
  119. replaceWith = '\\0';
  120. break;
  121. case _carriageReturn:
  122. replaceWith = '\\r';
  123. break;
  124. case _tab:
  125. replaceWith = '\\t';
  126. break;
  127. case _verticalTab:
  128. replaceWith = '\\v';
  129. break;
  130. case _backslash:
  131. replaceWith = '\\\\';
  132. break;
  133. case _doubleQuote:
  134. replaceWith = '\\"';
  135. break;
  136. }
  137. if (replaceWith !== null) {
  138. resultPieces.push(text.substring(startPos, i));
  139. resultPieces.push(replaceWith);
  140. startPos = i + 1;
  141. replaceWith = null;
  142. }
  143. }
  144. resultPieces.push(text.substring(startPos, len));
  145. return resultPieces.join('');
  146. }