8
0

amd.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* jshint node: false, browser: true, globalstrict: true */
  6. /* globals bender, require, define */
  7. 'use strict';
  8. ( () => {
  9. /**
  10. * AMD tools related to CKEditor.
  11. */
  12. bender.amd = {
  13. /**
  14. * Generates an absolute path to an AMD version of a CKEditor module. The function takes care of
  15. * generating a base path for that file, taking into account whether a Bender job is being run
  16. * or a simple test.
  17. *
  18. * The name should be given in a simplified features naming convention. See {@link ckeditor5/path#getModulePath}
  19. * for more details.
  20. *
  21. * @param {String} name The name of the module.
  22. * @returns {String} The absolute path to the module.
  23. */
  24. getModulePath( name ) {
  25. let appBasePath = bender.basePath;
  26. let ckeditorBasePath = bender.config.applications.ckeditor.basePath;
  27. let moduleBasePath;
  28. // Reported: https://github.com/benderjs/benderjs/issues/248
  29. // Ugh... make some paths cleanup, because we need to combine these fragments and we don't want to
  30. // duplicate '/'. BTW. if you want to touch this make sure you haven't broken Bender jobs which
  31. // have different bender.basePaths (no trailing '/', unnecessary 'tests/' fragment).
  32. moduleBasePath =
  33. appBasePath
  34. .split( '/' )
  35. .filter( nonEmpty )
  36. // When running a job we need to drop the last parth of the base path, which is "tests".
  37. .slice( 0, -1 )
  38. .concat(
  39. ckeditorBasePath.split( '/' ).filter( nonEmpty )
  40. )
  41. .join( '/' );
  42. // NOTE: This code is duplicated in CKEDITOR.getModulePath() because we're not able to use here
  43. // that function. It may be possible to resolve this once we start using ES6 modules and transpilation
  44. // also for tests.
  45. if ( name != 'ckeditor' ) {
  46. // Resolve shortened feature names to `featureName/featureName`.
  47. if ( name.indexOf( '/' ) < 0 ) {
  48. name = name + '/' + name;
  49. }
  50. // Add the prefix to shortened paths like `core/editor` (will be `ckeditor5-core/editor`).
  51. // Don't add the prefix to the main file and files frok ckeditor5/ module.
  52. if ( !( /^ckeditor5\//.test( name ) ) ) {
  53. name = 'ckeditor5-' + name;
  54. }
  55. }
  56. return '/' + moduleBasePath + '/' + name + '.js';
  57. },
  58. /**
  59. * Shorthand for defining an AMD module.
  60. *
  61. * Note that the module and dependency names must be passed in the simplified features naming convention
  62. * (see {@link #getModulePath}).
  63. *
  64. * For simplicity the dependencies passed to the `body` will be unwrapped
  65. * from the ES6 module object (so only the default export will be available). Also the returned value
  66. * will be automatically handled as a default export.
  67. *
  68. * If you need to define a module which has access to other exports or can export more values,
  69. * use the global `define()` function:
  70. *
  71. * define( bender.amd.getModulePath( name ), [ 'exports', 'foo', ... ].map( bender.amd.getModulePath ), ( FooModule, ... ) {
  72. * const FooClass = FooModule.default;
  73. * const FooOtherProp = FooModule.otherProp;
  74. *
  75. * exports.default = MyClass;
  76. * exports.otherProp = 1;
  77. * } );
  78. *
  79. * **Note:** Since this method automatically unwraps modules from the ES6 module object when passing them
  80. * to the `body` function, circular dependencies will not work. If you need them, either use the raw `define()`
  81. * as shown above, or keep all the definitions outside modules and only access the variables from the scope:
  82. *
  83. * PluginE = createPlugin( 'E' );
  84. * PluginF = createPlugin( 'F' );
  85. *
  86. * PluginF.requires = [ PluginE ];
  87. * PluginE.requires = [ PluginF ];
  88. *
  89. * bender.amd.define( 'E', [ 'core/plugin', 'F' ], () => {
  90. * return PluginE;
  91. * } );
  92. *
  93. * bender.amd.define( 'F', [ 'core/plugin', 'E' ], () => {
  94. * return PluginF;
  95. * } );
  96. *
  97. * @param {String} name Name of the module.
  98. * @param {String[]} deps Dependencies of the module.
  99. * @param {Function} body Module body.
  100. */
  101. define( name, deps, body ) {
  102. if ( arguments.length == 2 ) {
  103. body = deps;
  104. deps = [];
  105. }
  106. const depsPaths = deps.map( bender.amd.getModulePath );
  107. // Use the exports object instead of returning from modules in order to handle circular deps.
  108. // http://requirejs.org/docs/api.html#circular
  109. depsPaths.unshift( 'exports' );
  110. define( bender.amd.getModulePath( name ), depsPaths, function( exports ) {
  111. const loadedDeps = Array.from( arguments ).slice( 1 ).map( ( module ) => module.default );
  112. exports.default = body.apply( this, loadedDeps );
  113. } );
  114. },
  115. /**
  116. * Gets an object which holds the CKEditor modules guaranteed to be loaded before tests start.
  117. *
  118. * @params {...String} module The name of the module to load.
  119. * @returns {Object} The object that will hold the loaded modules.
  120. */
  121. require() {
  122. const modules = {};
  123. const done = bender.defer();
  124. const names = Array.from( arguments );
  125. const modulePaths = names.map( bender.amd.getModulePath );
  126. require( modulePaths, function() {
  127. for ( let i = 0; i < names.length; i++ ) {
  128. modules[ names[ i ] ] = arguments[ i ].default;
  129. }
  130. // Finally give green light for tests to start.
  131. done();
  132. } );
  133. return modules;
  134. }
  135. };
  136. function nonEmpty( str ) {
  137. return !!str.length;
  138. }
  139. } )();