dev-setup.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. (function() {
  2. let IS_FILE_PROTOCOL = (window.location.protocol === 'file:');
  3. let DIRNAME = null;
  4. if (IS_FILE_PROTOCOL) {
  5. let port = window.location.port;
  6. if (port.length > 0) {
  7. port = ':' + port;
  8. }
  9. DIRNAME = window.location.protocol + '//' + window.location.hostname + port + window.location.pathname.substr(0, window.location.pathname.lastIndexOf('/'));
  10. let bases = document.getElementsByTagName('base');
  11. if (bases.length > 0) {
  12. DIRNAME = DIRNAME + '/' + bases[0].getAttribute('href');
  13. }
  14. }
  15. let LOADER_OPTS = (function() {
  16. function parseQueryString() {
  17. let str = window.location.search;
  18. str = str.replace(/^\?/, '');
  19. let pieces = str.split(/&/);
  20. let result = {};
  21. pieces.forEach(function(piece) {
  22. let config = piece.split(/=/);
  23. result[config[0]] = config[1];
  24. });
  25. return result;
  26. }
  27. let overwrites = parseQueryString();
  28. let result = {};
  29. result['editor'] = overwrites['editor'] || 'npm/dev';
  30. METADATA.PLUGINS.map(function(plugin) {
  31. result[plugin.name] = overwrites[plugin.name] || 'npm/dev';
  32. });
  33. return result;
  34. })();
  35. function toHREF(search) {
  36. let port = window.location.port;
  37. if (port.length > 0) {
  38. port = ':' + port;
  39. }
  40. return window.location.protocol + '//' + window.location.hostname + port + window.location.pathname + search + window.location.hash;
  41. }
  42. function Component(name, modulePrefix, paths, contrib) {
  43. this.name = name;
  44. this.modulePrefix = modulePrefix;
  45. this.paths = paths;
  46. this.contrib = contrib;
  47. this.selectedPath = LOADER_OPTS[name];
  48. }
  49. Component.prototype.isRelease = function() {
  50. return /release/.test(this.selectedPath);
  51. };
  52. Component.prototype.getResolvedPath = function(PATH_PREFIX) {
  53. let resolvedPath = this.paths[this.selectedPath];
  54. if (this.selectedPath === 'npm/dev' || this.selectedPath === 'npm/min' || this.isRelease()) {
  55. if (IS_FILE_PROTOCOL) {
  56. resolvedPath = DIRNAME + '/../' + resolvedPath;
  57. } else {
  58. resolvedPath = PATH_PREFIX + '/monaco-editor/' + resolvedPath;
  59. }
  60. } else {
  61. if (IS_FILE_PROTOCOL) {
  62. resolvedPath = DIRNAME + '/../..' + resolvedPath;
  63. } else {
  64. resolvedPath = PATH_PREFIX + resolvedPath;
  65. }
  66. }
  67. return resolvedPath;
  68. };
  69. Component.prototype.generateLoaderConfig = function(dest, PATH_PREFIX) {
  70. dest[this.modulePrefix] = this.getResolvedPath(PATH_PREFIX);
  71. };
  72. Component.prototype.generateUrlForPath = function(pathName) {
  73. let NEW_LOADER_OPTS = {};
  74. Object.keys(LOADER_OPTS).forEach(function(key) {
  75. NEW_LOADER_OPTS[key] = (LOADER_OPTS[key] === 'npm/dev' ? undefined : LOADER_OPTS[key]);
  76. });
  77. NEW_LOADER_OPTS[this.name] = (pathName === 'npm/dev' ? undefined : pathName);
  78. let search = Object.keys(NEW_LOADER_OPTS).map(function(key) {
  79. let value = NEW_LOADER_OPTS[key];
  80. if (value) {
  81. return key + '=' + value;
  82. }
  83. return '';
  84. }).filter(function(assignment) { return !!assignment; }).join('&');
  85. if (search.length > 0) {
  86. search = '?' + search;
  87. }
  88. return toHREF(search);
  89. };
  90. Component.prototype.renderLoadingOptions = function() {
  91. return '<strong style="width:130px;display:inline-block;">' + this.name + '</strong>:&nbsp;&nbsp;&nbsp;' + Object.keys(this.paths).map(function(pathName) {
  92. if (pathName === this.selectedPath) {
  93. return '<strong>' + pathName + '</strong>';
  94. }
  95. return '<a href="' + this.generateUrlForPath(pathName) + '">' + pathName + '</a>';
  96. }.bind(this)).join('&nbsp;&nbsp;&nbsp;');
  97. };
  98. let RESOLVED_CORE = new Component('editor', 'vs', METADATA.CORE.paths);
  99. self.RESOLVED_CORE_PATH = RESOLVED_CORE.getResolvedPath('');
  100. let RESOLVED_PLUGINS = METADATA.PLUGINS.map(function(plugin) {
  101. return new Component(plugin.name, plugin.modulePrefix, plugin.paths, plugin.contrib);
  102. });
  103. METADATA = null;
  104. function loadScript(path, callback) {
  105. let script = document.createElement('script');
  106. script.onload = callback;
  107. script.async = true;
  108. script.type = 'text/javascript';
  109. script.src = path;
  110. document.head.appendChild(script);
  111. }
  112. (function() {
  113. let allComponents = [RESOLVED_CORE];
  114. if (!RESOLVED_CORE.isRelease()) {
  115. allComponents = allComponents.concat(RESOLVED_PLUGINS);
  116. }
  117. let div = document.createElement('div');
  118. div.style.position = 'fixed';
  119. div.style.top = 0;
  120. div.style.right = 0;
  121. div.style.background = 'lightgray';
  122. div.style.padding = '5px 20px 5px 5px';
  123. div.style.zIndex = '1000';
  124. div.innerHTML = '<ul><li>' + allComponents.map(function(component) { return component.renderLoadingOptions(); }).join('</li><li>') + '</li></ul>';
  125. document.body.appendChild(div);
  126. let aElements = document.getElementsByTagName('a');
  127. for (let i = 0; i < aElements.length; i++) {
  128. let aElement = aElements[i];
  129. if (aElement.className === 'loading-opts') {
  130. aElement.href += window.location.search
  131. }
  132. }
  133. })();
  134. self.loadEditor = function(callback, PATH_PREFIX) {
  135. PATH_PREFIX = PATH_PREFIX || '';
  136. loadScript(RESOLVED_CORE.getResolvedPath(PATH_PREFIX) + '/loader.js', function() {
  137. let loaderPathsConfig = {};
  138. if (!RESOLVED_CORE.isRelease()) {
  139. RESOLVED_PLUGINS.forEach(function(plugin) {
  140. plugin.generateLoaderConfig(loaderPathsConfig, PATH_PREFIX);
  141. });
  142. }
  143. RESOLVED_CORE.generateLoaderConfig(loaderPathsConfig, PATH_PREFIX);
  144. console.log('LOADER CONFIG: ');
  145. console.log(JSON.stringify(loaderPathsConfig, null, '\t'));
  146. require.config({
  147. paths: loaderPathsConfig
  148. });
  149. require(['vs/editor/editor.main'], function() {
  150. if (!RESOLVED_CORE.isRelease()) {
  151. // At this point we've loaded the monaco-editor-core
  152. require(RESOLVED_PLUGINS.map(function(plugin) { return plugin.contrib; }), function() {
  153. // At this point we've loaded all the plugins
  154. callback();
  155. });
  156. } else {
  157. callback();
  158. }
  159. });
  160. });
  161. }
  162. })();