8
0

module__cjs.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals require, process */
  6. 'use strict';
  7. const mockery = require( 'mockery' );
  8. mockery.enable( {
  9. warnOnReplace: false,
  10. warnOnUnregistered: false
  11. } );
  12. const mocked = [];
  13. const path = require( 'path' );
  14. /**
  15. * CommonJS tools related to CKEditor.
  16. */
  17. const utils = {
  18. /**
  19. * Helper for generating an absolute module path from a simplified name.
  20. *
  21. * Transforms:
  22. *
  23. * * `foo` -> `/path/dist/cjs/ckeditor5/foo/foo.js`
  24. * * `foo/bar` -> `/path/dist/cjs/ckeditor5/foo/bar.js`
  25. * * `/ckeditor5/foo.js` -> `/path/dist/cjs/ckeditor5/foo.js`
  26. *
  27. * @param {String} modulePath The simplified path.
  28. * @returns {String} The real path.
  29. */
  30. getModulePath( modulePath ) {
  31. // Do nothing – path is already absolute.
  32. if ( modulePath.startsWith( '/' ) ) {
  33. return path.join( process.cwd(), 'build', 'cjs', modulePath );
  34. }
  35. if ( modulePath.indexOf( '/' ) < 0 ) {
  36. modulePath = modulePath + '/' + modulePath;
  37. }
  38. return path.join( process.cwd(), 'build', 'cjs', 'ckeditor5', modulePath + '.js' );
  39. },
  40. /**
  41. * Defines module in AMD style using CommonJS modules.
  42. *
  43. * This method uses {@link #getModulePath} to process module and dependency paths so you need to use
  44. * the simplified notation.
  45. *
  46. * For simplicity the dependencies passed to the `body` will be unwrapped
  47. * from the ES6 module object (so only the default export will be available). Also the returned value
  48. * will be automatically handled as a default export.
  49. *
  50. * See also module__amd.js file description.
  51. *
  52. * @param {String} path Path to the module.
  53. * @param {String[]} deps Dependencies of the module.
  54. * @param {Function} body Module body.
  55. */
  56. define( path, deps, body ) {
  57. if ( arguments.length == 2 ) {
  58. body = deps;
  59. deps = [];
  60. }
  61. deps = deps
  62. .map( ( dependency ) => utils.getModulePath( dependency ) )
  63. .map( ( dependency ) => {
  64. // Checking if module is already mocked - if module was not mocked it might be a real module.
  65. // Using require.resolve to check if module really exists without loading it ( it throws if module is
  66. // not present). When module is not mocked and does not exist it will be undefined in module body.
  67. try {
  68. if ( mocked.indexOf( dependency ) < 0 ) {
  69. // Test if required module exists without loading it.
  70. require.resolve( dependency );
  71. }
  72. } catch ( e ) {
  73. return;
  74. }
  75. // Return only default export.
  76. return require( dependency ).default;
  77. } );
  78. mocked.push( utils.getModulePath( path ) );
  79. mockery.registerMock( utils.getModulePath( path ), {
  80. default: body.apply( this, deps )
  81. } );
  82. },
  83. /**
  84. * Gets an object which holds the CKEditor modules. This function uses synchronous CommonJS `require()`
  85. * method, which means that after executing this method all modules are already loaded.
  86. *
  87. * This method uses {@link #getModulePath} to process module and dependency paths so you need to use
  88. * the simplified notation.
  89. *
  90. * const modules = amdTestUtils.require( { editor: 'core/Editor' } );
  91. *
  92. * // Later on, inside tests:
  93. * const Editor = modules.editor;
  94. *
  95. * @params {Object} modules The object (`ref => modulePath`) with modules to be loaded.
  96. * @returns {Object} The object that will hold the loaded modules.
  97. */
  98. require( modules ) {
  99. const resolved = {};
  100. for ( let name in modules ) {
  101. resolved[ name ] = require( utils.getModulePath( modules[ name ] ) ).default;
  102. }
  103. return resolved;
  104. }
  105. };
  106. export default utils;