amd.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals bender, define, require */
  6. 'use strict';
  7. /**
  8. * AMD tools related to CKEditor.
  9. */
  10. const utils = {
  11. /**
  12. * Helper for generating a full module path from a simplified name (similar to simplified plugin naming convention).
  13. *
  14. * Transforms:
  15. *
  16. * * `foo` -> `/ckeditor5/foo/foo.js`
  17. * * `foo/bar` -> `/ckeditor5/foo/bar.js`
  18. *
  19. * @param {String} modulePath The simplified path.
  20. * @returns {String} The real path.
  21. */
  22. getModulePath( modulePath ) {
  23. if ( modulePath.indexOf( '/' ) < 0 ) {
  24. modulePath = modulePath + '/' + modulePath;
  25. }
  26. return '/ckeditor5/' + modulePath + '.js';
  27. },
  28. /**
  29. * Shorthand for defining an AMD module.
  30. *
  31. * This method uses {@link #getModulePath} to process module and dependency paths so you need to use
  32. * the simplified notation.
  33. *
  34. * For simplicity the dependencies passed to the `body` will be unwrapped
  35. * from the ES6 module object (so only the default export will be available). Also the returned value
  36. * will be automatically handled as a default export.
  37. *
  38. * If you need to define a module which has access to other exports or can export more values,
  39. * use the global `define()` function:
  40. *
  41. * define( 'my/module', [ 'exports', 'foo', ... ], ( FooModule, ... ) {
  42. * const FooClass = FooModule.default;
  43. * const FooOtherProp = FooModule.otherProp;
  44. *
  45. * exports.default = MyClass;
  46. * exports.otherProp = 1;
  47. * } );
  48. *
  49. * **Note:** Since this method automatically unwraps modules from the ES6 module object when passing them
  50. * to the `body` function, circular dependencies will not work. If you need them, either use the raw `define()`
  51. * as shown above, or keep all the definitions outside modules and only access the variables from the scope:
  52. *
  53. * PluginE = createPlugin( 'E' );
  54. * PluginF = createPlugin( 'F' );
  55. *
  56. * PluginF.requires = [ PluginE ];
  57. * PluginE.requires = [ PluginF ];
  58. *
  59. * amdTestUtils.define( 'E', [ 'core/plugin', 'F' ], () => {
  60. * return PluginE;
  61. * } );
  62. *
  63. * amdTestUtils.define( 'E', [ 'core/plugin', 'E' ], () => {
  64. * return PluginF;
  65. * } );
  66. *
  67. * @param {String} path Path to the module.
  68. * @param {String[]} deps Dependencies of the module.
  69. * @param {Function} body Module body.
  70. */
  71. define( path, deps, body ) {
  72. if ( arguments.length == 2 ) {
  73. body = deps;
  74. deps = [];
  75. }
  76. deps = deps.map( utils.getModulePath );
  77. // Use the exports object instead of returning from modules in order to handle circular deps.
  78. // http://requirejs.org/docs/api.html#circular
  79. deps.unshift( 'exports' );
  80. define( utils.getModulePath( path ), deps, function( exports ) {
  81. const loadedDeps = Array.from( arguments ).slice( 1 ).map( ( module ) => module.default );
  82. exports.default = body.apply( this, loadedDeps );
  83. } );
  84. },
  85. /**
  86. * Gets an object which holds the CKEditor modules guaranteed to be loaded before tests start.
  87. *
  88. * This method uses {@link #getModulePath} to process module and dependency paths so you need to use
  89. * the simplified notation.
  90. *
  91. * const modules = amdTestUtils.require( { editor: 'core/Editor' } );
  92. *
  93. * // Later on, inside tests:
  94. * const Editor = modules.editor;
  95. *
  96. * @params {Object} modules The object (`ref => modulePath`) with modules to be loaded.
  97. * @returns {Object} The object that will hold the loaded modules.
  98. */
  99. require( modules ) {
  100. const resolved = {};
  101. const paths = [];
  102. const names = [];
  103. const done = bender.defer();
  104. for ( let name in modules ) {
  105. names.push( name );
  106. paths.push( utils.getModulePath( modules[ name ] ) );
  107. }
  108. require( paths, function( ...loaded ) {
  109. for ( let i = 0; i < names.length; i++ ) {
  110. resolved[ names[ i ] ] = loaded[ i ].default;
  111. }
  112. // Finally give green light for tests to start.
  113. done();
  114. } );
  115. return resolved;
  116. }
  117. };
  118. export default utils;