8
0

module__cjs.js 3.5 KB

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