release.js 7.0 KB

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