dev-setup.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. (function() {
  2. var LOADER_OPTS = (function() {
  3. function parseQueryString() {
  4. var str = window.location.search;
  5. str = str.replace(/^\?/, '');
  6. var pieces = str.split(/&/);
  7. var result = {};
  8. pieces.forEach(function(piece) {
  9. var config = piece.split(/=/);
  10. result[config[0]] = config[1];
  11. });
  12. return result;
  13. }
  14. var overwrites = parseQueryString();
  15. var result = {};
  16. result['editor'] = overwrites['editor'] || 'npm';
  17. METADATA.PLUGINS.map(function(plugin) {
  18. result[plugin.name] = overwrites[plugin.name] || 'npm';
  19. });
  20. return result;
  21. })();
  22. function Component(name, modulePrefix, paths, contrib) {
  23. this.name = name;
  24. this.modulePrefix = modulePrefix;
  25. this.paths = paths;
  26. this.contrib = contrib;
  27. this.selectedPath = LOADER_OPTS[name];
  28. }
  29. Component.prototype.getResolvedPath = function() {
  30. var resolvedPath = this.paths[this.selectedPath];
  31. if (this.selectedPath === 'npm') {
  32. resolvedPath = '/monaco-editor/' + resolvedPath;
  33. }
  34. return resolvedPath;
  35. };
  36. Component.prototype.generateLoaderConfig = function(dest) {
  37. dest[this.modulePrefix] = this.getResolvedPath();
  38. };
  39. var RESOLVED_CORE = new Component('editor', 'vs', METADATA.CORE.paths);
  40. self.RESOLVED_CORE_PATH = RESOLVED_CORE.getResolvedPath();
  41. var RESOLVED_PLUGINS = METADATA.PLUGINS.map(function(plugin) {
  42. return new Component(plugin.name, plugin.modulePrefix, plugin.paths, plugin.contrib);
  43. });
  44. METADATA = null;
  45. function loadScript(path, callback) {
  46. var script = document.createElement('script');
  47. script.onload = callback;
  48. script.async = true;
  49. script.type = 'text/javascript';
  50. script.src = path;
  51. document.head.appendChild(script);
  52. }
  53. self.loadEditor = function(callback, PATH_PREFIX) {
  54. PATH_PREFIX = PATH_PREFIX || '';
  55. loadScript(PATH_PREFIX + RESOLVED_CORE.getResolvedPath() + '/loader.js', function() {
  56. var loaderPathsConfig = {};
  57. RESOLVED_PLUGINS.forEach(function(plugin) {
  58. plugin.generateLoaderConfig(loaderPathsConfig);
  59. });
  60. RESOLVED_CORE.generateLoaderConfig(loaderPathsConfig);
  61. console.log('LOADER CONFIG: ');
  62. console.log(JSON.stringify(loaderPathsConfig, null, '\t'));
  63. require.config({
  64. paths: loaderPathsConfig
  65. });
  66. require(['vs/editor/editor.main'], function() {
  67. // At this point we've loaded the monaco-editor-core
  68. require(RESOLVED_PLUGINS.map(function(plugin) { return plugin.contrib; }), function() {
  69. // At this point we've loaded all the plugins
  70. callback();
  71. });
  72. });
  73. });
  74. }
  75. })();