8
0

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