amd.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 CKEDITOR#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 ), [ 'foo', ... ].map( bender.amd.getModulePath ), ( FooModule, ... ) {
  72. * const FooClass = FooModule.default;
  73. * const FooOtherProp = FooModule.otherProp;
  74. *
  75. * return {
  76. * default: MyClass,
  77. * otherProp: 1
  78. * };
  79. * } );
  80. *
  81. * @param {String} name Name of the module.
  82. * @param {String[]} deps Dependencies of the module.
  83. * @param {Function} body Module body.
  84. */
  85. define( name, deps, body ) {
  86. if ( arguments.length == 2 ) {
  87. body = deps;
  88. deps = [];
  89. }
  90. const depsPaths = deps.map( bender.amd.getModulePath );
  91. define( bender.amd.getModulePath( name ), depsPaths, function() {
  92. const loadedDeps = Array.from( arguments ).map( ( module ) => module.default );
  93. return {
  94. default: body.apply( this, loadedDeps )
  95. };
  96. } );
  97. },
  98. /**
  99. * Gets an object which holds the CKEditor modules guaranteed to be loaded before tests start.
  100. *
  101. * @params {...String} module The name of the module to load.
  102. * @returns {Object} The object that will hold the loaded modules.
  103. */
  104. require() {
  105. const modules = {};
  106. const done = bender.defer();
  107. const names = Array.from( arguments );
  108. const modulePaths = names.map( bender.amd.getModulePath );
  109. require( modulePaths, function() {
  110. for ( let i = 0; i < names.length; i++ ) {
  111. modules[ names[ i ] ] = arguments[ i ].default;
  112. }
  113. // Finally give green light for tests to start.
  114. done();
  115. } );
  116. return modules;
  117. }
  118. };
  119. function nonEmpty( str ) {
  120. return !!str.length;
  121. }
  122. } )();