plugin.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* global define */
  6. 'use strict';
  7. // Plugin for RequireJS to properly load CKEditor plugins through the "plugin!name" scheme:
  8. // "plugin!name" => "node_modules/ckeditor5-plugin-name/name"
  9. //
  10. // Due to name conflict with the "ckeditor5-core/plugin" module, a workaround was needed. In this case, we extend the
  11. // core Plugin class with the necessary RequireJS plugin methods. This should have no harm on the use of the Plugin
  12. // class.
  13. define( 'plugin', [ 'plugin-core' ], ( CorePlugin ) => {
  14. // Called when a "plugin!" module is to be loaded.
  15. // http://requirejs.org/docs/plugins.html#apiload
  16. CorePlugin.load = function( name, require, onload ) {
  17. // We may have a path to plugin modules (e.g. test/somemodule). Here we break the path on slashes.
  18. let path = name.split( '/' );
  19. // Inject the /src/ part right after the plugin name (e.g test/src).
  20. path.splice( 1, 0, 'src' );
  21. // If we didn't have any subpart in the path, inject the plugin name at the end (e.g. test/src/test).
  22. if ( path.length == 2 ) {
  23. path.push( path[ 0 ] );
  24. }
  25. // Finally point to the right place, relatively to the `ckeditor5-core/src` directory (in node_modules).
  26. path = '../../ckeditor5-plugin-' + path.join( '/' );
  27. // Now require the module again, using the fully resolved path.
  28. require( [ path ], onload, onload.error );
  29. };
  30. return CorePlugin;
  31. } );