importTypescript.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 srcPath = path.join(TYPESCRIPT_LIB_SOURCE, name);
  62. var contents = fs.readFileSync(srcPath).toString();
  63. var dstPath = path.join(TYPESCRIPT_LIB_DESTINATION, dstName + '.ts');
  64. fs.writeFileSync(dstPath,
  65. `/*---------------------------------------------------------------------------------------------
  66. * Copyright (c) Microsoft Corporation. All rights reserved.
  67. * Licensed under the MIT License. See License.txt in the project root for license information.
  68. *--------------------------------------------------------------------------------------------*/
  69. export const contents = "${escapeText(contents)}";
  70. `);
  71. }
  72. /**
  73. * Escape text such that it can be used in a javascript string enclosed by double quotes (")
  74. */
  75. function escapeText(text) {
  76. // See http://www.javascriptkit.com/jsref/escapesequence.shtml
  77. var _backspace = '\b'.charCodeAt(0);
  78. var _formFeed = '\f'.charCodeAt(0);
  79. var _newLine = '\n'.charCodeAt(0);
  80. var _nullChar = 0;
  81. var _carriageReturn = '\r'.charCodeAt(0);
  82. var _tab = '\t'.charCodeAt(0);
  83. var _verticalTab = '\v'.charCodeAt(0);
  84. var _backslash = '\\'.charCodeAt(0);
  85. var _doubleQuote = '"'.charCodeAt(0);
  86. var startPos = 0, chrCode, replaceWith = null, resultPieces = [];
  87. for (var i = 0, len = text.length; i < len; i++) {
  88. chrCode = text.charCodeAt(i);
  89. switch (chrCode) {
  90. case _backspace:
  91. replaceWith = '\\b';
  92. break;
  93. case _formFeed:
  94. replaceWith = '\\f';
  95. break;
  96. case _newLine:
  97. replaceWith = '\\n';
  98. break;
  99. case _nullChar:
  100. replaceWith = '\\0';
  101. break;
  102. case _carriageReturn:
  103. replaceWith = '\\r';
  104. break;
  105. case _tab:
  106. replaceWith = '\\t';
  107. break;
  108. case _verticalTab:
  109. replaceWith = '\\v';
  110. break;
  111. case _backslash:
  112. replaceWith = '\\\\';
  113. break;
  114. case _doubleQuote:
  115. replaceWith = '\\"';
  116. break;
  117. }
  118. if (replaceWith !== null) {
  119. resultPieces.push(text.substring(startPos, i));
  120. resultPieces.push(replaceWith);
  121. startPos = i + 1;
  122. replaceWith = null;
  123. }
  124. }
  125. resultPieces.push(text.substring(startPos, len));
  126. return resultPieces.join('');
  127. }