importTypescript.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. var tsServices_amd = tsServices +
  19. `
  20. // MONACOCHANGE
  21. // Defining the entire module name because r.js has an issue and cannot bundle this file
  22. // correctly with an anonymous define call
  23. define("vs/language/typescript/lib/typescriptServices", [], function() { return ts; });
  24. // END MONACOCHANGE
  25. `;
  26. fs.writeFileSync(path.join(TYPESCRIPT_LIB_DESTINATION, 'typescriptServices-amd.js'), tsServices_amd);
  27. var tsServices_esm = tsServices +
  28. `
  29. // MONACOCHANGE
  30. export const createClassifier = ts.createClassifier;
  31. export const createLanguageService = ts.createLanguageService;
  32. export const displayPartsToString = ts.displayPartsToString;
  33. export const EndOfLineState = ts.EndOfLineState;
  34. export const flattenDiagnosticMessageText = ts.flattenDiagnosticMessageText;
  35. export const IndentStyle = ts.IndentStyle;
  36. export const ScriptKind = ts.ScriptKind;
  37. export const ScriptTarget = ts.ScriptTarget;
  38. export const TokenClass = ts.TokenClass;
  39. // END MONACOCHANGE
  40. `;
  41. fs.writeFileSync(path.join(TYPESCRIPT_LIB_DESTINATION, 'typescriptServices.js'), tsServices_esm);
  42. var dtsServices = fs.readFileSync(path.join(TYPESCRIPT_LIB_SOURCE, 'typescriptServices.d.ts')).toString();
  43. dtsServices +=
  44. `
  45. // MONACOCHANGE
  46. export = ts;
  47. // END MONACOCHANGE
  48. `;
  49. fs.writeFileSync(path.join(TYPESCRIPT_LIB_DESTINATION, 'typescriptServices.d.ts'), dtsServices);
  50. })();
  51. /**
  52. * Import a lib*.d.ts file from TypeScript's dist
  53. */
  54. function importLibDeclarationFile(name) {
  55. var dstName = name.replace(/\.d\.ts$/, '').replace(/\./g, '-') + '-ts';
  56. var srcPath = path.join(TYPESCRIPT_LIB_SOURCE, name);
  57. var contents = fs.readFileSync(srcPath).toString();
  58. var dstPath = path.join(TYPESCRIPT_LIB_DESTINATION, dstName + '.ts');
  59. fs.writeFileSync(dstPath,
  60. `/*---------------------------------------------------------------------------------------------
  61. * Copyright (c) Microsoft Corporation. All rights reserved.
  62. * Licensed under the MIT License. See License.txt in the project root for license information.
  63. *--------------------------------------------------------------------------------------------*/
  64. export const contents = "${escapeText(contents)}";
  65. `);
  66. }
  67. /**
  68. * Escape text such that it can be used in a javascript string enclosed by double quotes (")
  69. */
  70. function escapeText(text) {
  71. // See http://www.javascriptkit.com/jsref/escapesequence.shtml
  72. var _backspace = '\b'.charCodeAt(0);
  73. var _formFeed = '\f'.charCodeAt(0);
  74. var _newLine = '\n'.charCodeAt(0);
  75. var _nullChar = 0;
  76. var _carriageReturn = '\r'.charCodeAt(0);
  77. var _tab = '\t'.charCodeAt(0);
  78. var _verticalTab = '\v'.charCodeAt(0);
  79. var _backslash = '\\'.charCodeAt(0);
  80. var _doubleQuote = '"'.charCodeAt(0);
  81. var startPos = 0, chrCode, replaceWith = null, resultPieces = [];
  82. for (var i = 0, len = text.length; i < len; i++) {
  83. chrCode = text.charCodeAt(i);
  84. switch (chrCode) {
  85. case _backspace:
  86. replaceWith = '\\b';
  87. break;
  88. case _formFeed:
  89. replaceWith = '\\f';
  90. break;
  91. case _newLine:
  92. replaceWith = '\\n';
  93. break;
  94. case _nullChar:
  95. replaceWith = '\\0';
  96. break;
  97. case _carriageReturn:
  98. replaceWith = '\\r';
  99. break;
  100. case _tab:
  101. replaceWith = '\\t';
  102. break;
  103. case _verticalTab:
  104. replaceWith = '\\v';
  105. break;
  106. case _backslash:
  107. replaceWith = '\\\\';
  108. break;
  109. case _doubleQuote:
  110. replaceWith = '\\"';
  111. break;
  112. }
  113. if (replaceWith !== null) {
  114. resultPieces.push(text.substring(startPos, i));
  115. resultPieces.push(replaceWith);
  116. startPos = i + 1;
  117. replaceWith = null;
  118. }
  119. }
  120. resultPieces.push(text.substring(startPos, len));
  121. return resultPieces.join('');
  122. }