dev-setup.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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 =
  10. window.location.protocol +
  11. '//' +
  12. window.location.hostname +
  13. port +
  14. window.location.pathname.substr(0, window.location.pathname.lastIndexOf('/'));
  15. let bases = document.getElementsByTagName('base');
  16. if (bases.length > 0) {
  17. DIRNAME = DIRNAME + '/' + bases[0].getAttribute('href');
  18. }
  19. }
  20. let LOADER_OPTS = (function () {
  21. function parseQueryString() {
  22. let str = window.location.search;
  23. str = str.replace(/^\?/, '');
  24. let pieces = str.split(/&/);
  25. let result = {};
  26. pieces.forEach(function (piece) {
  27. let config = piece.split(/=/);
  28. result[config[0]] = config[1];
  29. });
  30. return result;
  31. }
  32. let overwrites = parseQueryString();
  33. let result = {};
  34. result['editor'] = overwrites['editor'] || 'npm/dev';
  35. METADATA.PLUGINS.map(function (plugin) {
  36. result[plugin.name] = overwrites[plugin.name] || 'dev';
  37. });
  38. return result;
  39. })();
  40. function toHREF(search) {
  41. let port = window.location.port;
  42. if (port.length > 0) {
  43. port = ':' + port;
  44. }
  45. return (
  46. window.location.protocol +
  47. '//' +
  48. window.location.hostname +
  49. port +
  50. window.location.pathname +
  51. search +
  52. window.location.hash
  53. );
  54. }
  55. function Component(name, modulePrefix, paths, rootPath, contrib) {
  56. this.name = name;
  57. this.modulePrefix = modulePrefix;
  58. this.paths = paths;
  59. this.rootPath = rootPath;
  60. this.contrib = contrib;
  61. this.selectedPath = LOADER_OPTS[name];
  62. }
  63. Component.prototype.isRelease = function () {
  64. return /release/.test(this.selectedPath);
  65. };
  66. Component.prototype.getResolvedPath = function (PATH_PREFIX) {
  67. let resolvedPath = this.paths[this.selectedPath];
  68. if (/\.\//.test(resolvedPath)) {
  69. // starts with ./ => treat as relative to the root path
  70. if (IS_FILE_PROTOCOL) {
  71. resolvedPath = DIRNAME + '/../../' + this.rootPath + '/' + resolvedPath;
  72. } else {
  73. resolvedPath = PATH_PREFIX + '/monaco-editor/' + this.rootPath + '/' + resolvedPath;
  74. }
  75. } else if (
  76. this.selectedPath === 'npm/dev' ||
  77. this.selectedPath === 'npm/min' ||
  78. this.isRelease()
  79. ) {
  80. if (IS_FILE_PROTOCOL) {
  81. resolvedPath = DIRNAME + '/../../' + resolvedPath;
  82. } else {
  83. resolvedPath = PATH_PREFIX + '/monaco-editor/' + resolvedPath;
  84. }
  85. } else {
  86. if (IS_FILE_PROTOCOL) {
  87. resolvedPath = DIRNAME + '/../../../' + resolvedPath;
  88. } else {
  89. resolvedPath = PATH_PREFIX + resolvedPath;
  90. }
  91. }
  92. return resolvedPath;
  93. };
  94. Component.prototype.generateLoaderConfig = function (dest, PATH_PREFIX) {
  95. dest[this.modulePrefix] = this.getResolvedPath(PATH_PREFIX);
  96. };
  97. Component.prototype.generateUrlForPath = function (pathName) {
  98. let NEW_LOADER_OPTS = {};
  99. Object.keys(LOADER_OPTS).forEach(function (key) {
  100. NEW_LOADER_OPTS[key] = LOADER_OPTS[key] === 'npm/dev' ? undefined : LOADER_OPTS[key];
  101. });
  102. NEW_LOADER_OPTS[this.name] = pathName === 'npm/dev' ? undefined : pathName;
  103. let search = Object.keys(NEW_LOADER_OPTS)
  104. .map(function (key) {
  105. let value = NEW_LOADER_OPTS[key];
  106. if (value) {
  107. return key + '=' + value;
  108. }
  109. return '';
  110. })
  111. .filter(function (assignment) {
  112. return !!assignment;
  113. })
  114. .join('&');
  115. if (search.length > 0) {
  116. search = '?' + search;
  117. }
  118. return toHREF(search);
  119. };
  120. Component.prototype.renderLoadingOptions = function () {
  121. return (
  122. '<strong style="width:130px;display:inline-block;">' +
  123. this.name +
  124. '</strong>:&#160;&#160;&#160;' +
  125. Object.keys(this.paths)
  126. .map(
  127. function (pathName) {
  128. if (pathName === this.selectedPath) {
  129. return '<strong>' + pathName + '</strong>';
  130. }
  131. return '<a href="' + this.generateUrlForPath(pathName) + '">' + pathName + '</a>';
  132. }.bind(this)
  133. )
  134. .join('&#160;&#160;&#160;')
  135. );
  136. };
  137. let RESOLVED_CORE = new Component('editor', 'vs', METADATA.CORE.paths);
  138. self.RESOLVED_CORE_PATH = RESOLVED_CORE.getResolvedPath('');
  139. let RESOLVED_PLUGINS = METADATA.PLUGINS.map(function (plugin) {
  140. return new Component(
  141. plugin.name,
  142. plugin.modulePrefix,
  143. plugin.paths,
  144. plugin.rootPath,
  145. plugin.contrib
  146. );
  147. });
  148. METADATA = null;
  149. function loadScript(path, callback) {
  150. let script = document.createElement('script');
  151. script.onload = callback;
  152. script.async = true;
  153. script.type = 'text/javascript';
  154. script.src = path;
  155. document.head.appendChild(script);
  156. }
  157. (function () {
  158. let allComponents = [RESOLVED_CORE];
  159. if (!RESOLVED_CORE.isRelease()) {
  160. allComponents = allComponents.concat(RESOLVED_PLUGINS);
  161. }
  162. let div = document.createElement('div');
  163. div.className = 'dev-setup-control';
  164. div.style.position = 'fixed';
  165. div.style.top = 0;
  166. div.style.right = 0;
  167. div.style.background = 'lightgray';
  168. div.style.padding = '5px 20px 5px 5px';
  169. div.style.zIndex = '1000';
  170. div.innerHTML =
  171. '<ul><li>' +
  172. allComponents
  173. .map(function (component) {
  174. return component.renderLoadingOptions();
  175. })
  176. .join('</li><li>') +
  177. '</li></ul>';
  178. document.body.appendChild(div);
  179. let aElements = document.getElementsByTagName('a');
  180. for (let i = 0; i < aElements.length; i++) {
  181. let aElement = aElements[i];
  182. if (aElement.className === 'loading-opts') {
  183. aElement.href += window.location.search;
  184. }
  185. }
  186. })();
  187. self.getCodiconPath = function (PATH_PREFIX) {
  188. PATH_PREFIX = PATH_PREFIX || '';
  189. const result = RESOLVED_CORE.getResolvedPath(PATH_PREFIX);
  190. return result + '/base/browser/ui/codicons/codicon/codicon.ttf';
  191. };
  192. self.loadEditor = function (callback, PATH_PREFIX) {
  193. PATH_PREFIX = PATH_PREFIX || '';
  194. loadScript(RESOLVED_CORE.getResolvedPath(PATH_PREFIX) + '/loader.js', function () {
  195. let loaderPathsConfig = {};
  196. window.AMD = true;
  197. if (IS_FILE_PROTOCOL) {
  198. loaderPathsConfig['vs/language/fillers/monaco-editor-core'] =
  199. DIRNAME + '/../.././out/amd/fillers/monaco-editor-core-amd';
  200. loaderPathsConfig['vs/fillers/monaco-editor-core'] =
  201. DIRNAME + '/../.././out/amd/fillers/monaco-editor-core-amd';
  202. } else {
  203. loaderPathsConfig['vs/language/fillers/monaco-editor-core'] =
  204. PATH_PREFIX + '/monaco-editor/./out/amd/fillers/monaco-editor-core-amd';
  205. loaderPathsConfig['vs/fillers/monaco-editor-core'] =
  206. PATH_PREFIX + '/monaco-editor/./out/amd/fillers/monaco-editor-core-amd';
  207. }
  208. if (!RESOLVED_CORE.isRelease()) {
  209. RESOLVED_PLUGINS.forEach(function (plugin) {
  210. plugin.generateLoaderConfig(loaderPathsConfig, PATH_PREFIX);
  211. });
  212. }
  213. RESOLVED_CORE.generateLoaderConfig(loaderPathsConfig, PATH_PREFIX);
  214. console.log('LOADER CONFIG: ');
  215. console.log(JSON.stringify(loaderPathsConfig, null, '\t'));
  216. require.config({
  217. paths: loaderPathsConfig
  218. // 'vs/nls' : {
  219. // availableLanguages: {
  220. // '*': 'de'
  221. // }
  222. // }
  223. });
  224. require(['vs/editor/editor.main'], function () {
  225. if (!RESOLVED_CORE.isRelease()) {
  226. // At this point we've loaded the monaco-editor-core
  227. require(RESOLVED_PLUGINS.map(function (plugin) {
  228. return plugin.contrib;
  229. }), function () {
  230. // At this point we've loaded all the plugins
  231. callback();
  232. });
  233. } else {
  234. callback();
  235. }
  236. });
  237. });
  238. };
  239. })();