release.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. "use strict";
  2. exports.__esModule = true;
  3. var fs = require("fs");
  4. var ts = require("typescript");
  5. var path = require("path");
  6. var REPO_ROOT = path.join(__dirname, '../');
  7. process({
  8. repoRoot: REPO_ROOT,
  9. esmSource: 'out',
  10. esmDestination: 'release/esm',
  11. entryPoints: [
  12. 'monaco.contribution.js',
  13. 'jsonMode.js',
  14. 'json.worker.js'
  15. ],
  16. resolveAlias: {
  17. 'vscode-nls': path.join(REPO_ROOT, "out/fillers/vscode-nls.js")
  18. },
  19. resolveSkip: [
  20. 'monaco-editor-core'
  21. ],
  22. destinationFolderSimplification: {
  23. 'node_modules': '_deps',
  24. 'jsonc-parser/lib/esm': 'jsonc-parser',
  25. 'vscode-languageserver-types/lib/esm': 'vscode-languageserver-types',
  26. 'vscode-uri/lib/esm': 'vscode-uri',
  27. 'vscode-json-languageservice/lib/esm': 'vscode-json-languageservice'
  28. }
  29. });
  30. function process(options) {
  31. options.repoRoot = path.normalize(options.repoRoot).replace(/(\/|\\)$/, '');
  32. var ESM_SRC = path.join(options.repoRoot, options.esmSource);
  33. var ESM_DEST = path.join(options.repoRoot, options.esmDestination);
  34. var in_queue = Object.create(null);
  35. var queue = [];
  36. var enqueue = function (filePath) {
  37. if (in_queue[filePath]) {
  38. return;
  39. }
  40. in_queue[filePath] = true;
  41. queue.push(filePath);
  42. };
  43. var seenDir = {};
  44. var createDirectoryRecursive = function (dir) {
  45. if (seenDir[dir]) {
  46. return;
  47. }
  48. var lastSlash = dir.lastIndexOf('/');
  49. if (lastSlash === -1) {
  50. lastSlash = dir.lastIndexOf('\\');
  51. }
  52. if (lastSlash !== -1) {
  53. createDirectoryRecursive(dir.substring(0, lastSlash));
  54. }
  55. seenDir[dir] = true;
  56. try {
  57. fs.mkdirSync(dir);
  58. }
  59. catch (err) { }
  60. };
  61. seenDir[options.repoRoot] = true;
  62. var applyDestinationFolderSimplifications = function (filePath) {
  63. filePath = filePath.replace(/\\/g, '/');
  64. for (var key in options.destinationFolderSimplification) {
  65. var test = key.replace(/\\/g, '/');
  66. while (filePath.indexOf(test) >= 0) {
  67. filePath = filePath.replace(test, options.destinationFolderSimplification[test]);
  68. }
  69. }
  70. return filePath;
  71. };
  72. var shouldSkipImport = function (importText) {
  73. for (var i = 0; i < options.resolveSkip.length; i++) {
  74. var skip = options.resolveSkip[i];
  75. if (importText.indexOf(skip) === 0) {
  76. return true;
  77. }
  78. }
  79. return false;
  80. };
  81. var computeDestinationFilePath = function (filePath) {
  82. if (filePath.indexOf(ESM_SRC) === 0) {
  83. // This file is from our sources
  84. return path.join(ESM_DEST, path.relative(ESM_SRC, filePath));
  85. }
  86. else {
  87. // This file is from node_modules
  88. return path.normalize(applyDestinationFolderSimplifications(path.join(ESM_DEST, path.relative(options.repoRoot, filePath))));
  89. }
  90. };
  91. var write = function (filePath, fileContents) {
  92. var finalFilePath = computeDestinationFilePath(filePath);
  93. createDirectoryRecursive(path.dirname(finalFilePath));
  94. fs.writeFileSync(finalFilePath, fileContents);
  95. };
  96. options.entryPoints.forEach(function (filePath) {
  97. enqueue(path.join(ESM_SRC, filePath));
  98. });
  99. while (queue.length > 0) {
  100. var filePath = queue.shift();
  101. var fileContents = fs.readFileSync(filePath).toString();
  102. var info = ts.preProcessFile(fileContents);
  103. for (var i = info.importedFiles.length - 1; i >= 0; i--) {
  104. var importText = info.importedFiles[i].fileName;
  105. if (shouldSkipImport(importText)) {
  106. continue;
  107. }
  108. var pos = info.importedFiles[i].pos;
  109. var end = info.importedFiles[i].end;
  110. if (/(^\.\/)|(^\.\.\/)/.test(importText)) {
  111. // Relative import
  112. var importedFilename = path.join(path.dirname(filePath), importText) + '.js';
  113. enqueue(importedFilename);
  114. }
  115. else {
  116. var importedFilename = void 0;
  117. if (options.resolveAlias[importText]) {
  118. importedFilename = options.resolveAlias[importText];
  119. }
  120. else {
  121. importedFilename = findNodeModuleImport(options.repoRoot, importText, filePath);
  122. }
  123. var myDestinationPath = computeDestinationFilePath(filePath);
  124. var importDestinationPath = computeDestinationFilePath(importedFilename);
  125. var relativePath = path.relative(path.dirname(myDestinationPath), importDestinationPath);
  126. if (!/(^\.\/)|(^\.\.\/)/.test(relativePath)) {
  127. relativePath = './' + relativePath;
  128. }
  129. relativePath = relativePath.replace(/\\/g, '/');
  130. relativePath = relativePath.replace(/\.js$/, '');
  131. fileContents = (fileContents.substring(0, pos + 1)
  132. + relativePath
  133. + fileContents.substring(end + 1));
  134. enqueue(importedFilename);
  135. }
  136. }
  137. write(filePath, fileContents);
  138. }
  139. }
  140. function findNodeModuleImport(repoRoot, module, sourceFilePath) {
  141. var modulePath = findNodeModule(repoRoot, module, sourceFilePath);
  142. var modulePackagePath = path.join(modulePath, 'package.json');
  143. if (!fs.existsSync(modulePackagePath)) {
  144. throw new Error("Missing " + modulePackagePath + " in node module " + modulePath);
  145. }
  146. var modulePackage = JSON.parse(fs.readFileSync(modulePackagePath).toString());
  147. if (typeof modulePackage.module !== 'string') {
  148. throw new Error("Missing property 'module' package.json at " + modulePackagePath);
  149. }
  150. var result = path.join(modulePath, modulePackage.module);
  151. if (!fs.existsSync(result)) {
  152. throw new Error("Missing file " + result);
  153. }
  154. return result;
  155. function findNodeModule(repoRoot, module, sourceFilePath) {
  156. var modulePaths = generatePaths(repoRoot, module, sourceFilePath);
  157. for (var i = 0; i < modulePaths.length; i++) {
  158. if (fs.existsSync(modulePaths[i])) {
  159. return modulePaths[i];
  160. }
  161. }
  162. throw new Error("Cannot find module " + module + " requested by " + sourceFilePath);
  163. }
  164. function generatePaths(repoRoot, module, sourceFilePath) {
  165. var sourceDir = path.dirname(sourceFilePath);
  166. var result = [];
  167. while (sourceDir.length >= repoRoot.length) {
  168. result.push(path.join(sourceDir, 'node_modules', module));
  169. sourceDir = path.dirname(sourceDir);
  170. }
  171. return result;
  172. }
  173. }