Procházet zdrojové kódy

Porting... porting... porting.

Piotrek Koszuliński před 10 roky
rodič
revize
2ed7f34c39

+ 39 - 11
dev/bender/plugins/ckeditor5/static/extensions.js

@@ -4,7 +4,7 @@
  */
 
 /* jshint node: false, browser: true, globalstrict: true */
-/* globals bender, require */
+/* globals bender, require, define */
 
 'use strict';
 
@@ -15,6 +15,40 @@
 	 * AMD tools related to CKEditor.
 	 */
 	bender.amd = {
+		getModulePath( name ) {
+			if ( name != 'ckeditor' ) {
+				// Resolve shortened feature names to `featureName/featureName`.
+				if ( name.indexOf( '/' ) < 0 ) {
+					name = name + '/' + name;
+				}
+
+				// Add the prefix to shortened paths like `core/editor` (will be `ckeditor5-core/editor`).
+				// Don't add the prefix to the main file and files frok ckeditor5/ module.
+				if ( !( /^ckeditor5\//.test( name ) ) ) {
+					name = 'ckeditor5-' + name;
+				}
+			}
+
+			return basePath + name + '.js';
+		},
+
+		define( name, deps, body ) {
+			if ( arguments.length == 2 ) {
+				body = deps;
+				deps = [];
+			}
+
+			const depsPaths = deps.map( bender.amd.getModulePath );
+
+			define( bender.amd.getModulePath( name ), depsPaths, function() {
+				const loadedDeps = Array.from( arguments ).map( ( module ) => module.default );
+
+				return {
+					default: body.apply( this, loadedDeps )
+				};
+			} );
+		},
+
 		/**
 		 * Gets an object which holds the CKEditor modules guaranteed to be loaded before tests start.
 		 *
@@ -26,15 +60,7 @@
 			const done = bender.defer();
 
 			const names = Array.from( arguments );
-			const modulePaths = names.map( ( name ) => {
-				// Add the prefix to shortened paths like `core/editor` (will be `ckeditor5-core/editor`).
-				// Don't add the prefix to the main file and files frok ckeditor5/ module.
-				if ( name != 'ckeditor' && !( /^ckeditor5\//.test( name ) ) ) {
-					name = 'ckeditor5-' + name;
-				}
-
-				return basePath + name + '.js';
-			} );
+			const modulePaths = names.map( bender.amd.getModulePath );
 
 			require( modulePaths, function() {
 				for ( let i = 0; i < names.length; i++ ) {
@@ -43,7 +69,9 @@
 
 				// Finally give green light for tests to start.
 				done();
-			} );
+			}/*, ( err ) => {
+				debugger;
+			}*/ );
 
 			return modules;
 		}

+ 19 - 17
dev/tasks/gulp/utils.js

@@ -10,7 +10,6 @@ const gulpWatch = require( 'gulp-watch' );
 const gulpPlumber = require( 'gulp-plumber' );
 const gutil = require( 'gulp-util' );
 const multipipe = require( 'multipipe' );
-const stream = require( 'stream' );
 
 const sep = path.sep;
 
@@ -67,17 +66,14 @@ const utils = {
 			throw new Error( `Incorrect format: ${ format }` );
 		}
 
-		return new stream.PassThrough( { objectMode: true } )
-			// Not used so far, but we'll need it.
-			// .pipe( utils.pickVersionedFile( format ) )
-			.pipe( babel( {
-				plugins: [ `transform-es2015-modules-${ babelModuleTranspiler }` ],
-				// Ensure that all paths ends with '.js' because Require.JS (unlike Common.JS/System.JS)
-				// will not add it to module names which look like paths.
-				resolveModuleSource: ( source ) => {
-					return utils.appendModuleExtension( source );
-				}
-			} ) );
+		return babel( {
+			plugins: [ `transform-es2015-modules-${ babelModuleTranspiler }` ],
+			// Ensure that all paths ends with '.js' because Require.JS (unlike Common.JS/System.JS)
+			// will not add it to module names which look like paths.
+			resolveModuleSource: ( source ) => {
+				return utils.appendModuleExtension( source );
+			}
+		} );
 	},
 
 	/**
@@ -92,6 +88,8 @@ const utils = {
 		return ( pipes, format ) => {
 			const conversionPipes = [];
 
+			conversionPipes.push( utils.pickVersionedFile( format ) );
+
 			if ( format != 'esnext' ) {
 				conversionPipes.push( utils.transpile( format ) );
 			}
@@ -109,11 +107,15 @@ const utils = {
 		};
 	},
 
-	// Not used so far, but will allow us to pick one of files suffixed with the format (`__esnext`, `__amd`, or `__cjs`).
-	// This will be needed when we'll want to have different piece of code for specific formats.
-	//
-	// For example: we'll have `loader__esnext.js`, `loader__amd.js` and `loader__cjs.js`. After applying this
-	// transformation when compiling code for a specific format the proper file will be renamed to `loader.js`.
+	/**
+	 * Allows us to pick one of files suffixed with the format (`__esnext`, `__amd`, or `__cjs`).
+	 *
+	 * For example: we have `load__esnext.js`, `load__amd.js` and `load__cjs.js`. After applying this
+	 * transformation when compiling code for a specific format the proper file will be renamed to `load.js`.
+	 *
+	 * @param {String} format
+	 * @returns {Stream}
+	 */
 	pickVersionedFile( format ) {
 		return rename( ( path ) => {
 			const regexp = new RegExp( `__${ format }$` );

+ 25 - 0
src/load__amd.js

@@ -0,0 +1,25 @@
+/**
+ * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+'use strict';
+
+// We import the 'require' module, so Require.JS gives us a localized version of require().
+// Otherwise we would use the global one which resolves paths relatively to the base dir.
+import require from 'require';
+
+// NOTE: modulePath MUST BE RELATIVE to this module.
+export default function load( modulePath ) {
+	return new Promise( ( resolve, reject ) => {
+		require(
+			[ modulePath ],
+			( module ) => {
+				resolve( module );
+			},
+			( err ) => {
+				reject( err );
+			}
+		);
+	} );
+}

+ 14 - 0
src/load__cjs.js

@@ -0,0 +1,14 @@
+/**
+ * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+'use strict';
+
+/* global require */
+
+export default function load( modulePath ) {
+	return new Promise( ( resolve ) => {
+		resolve( require( modulePath ) );
+	} );
+}

+ 16 - 0
src/load__esnext.js

@@ -0,0 +1,16 @@
+/**
+ * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+'use strict';
+
+/* global System */
+
+export default function load( modulePath ) {
+	return System
+		.import( modulePath )
+		.then( ( module ) => {
+			return module;
+		} );
+}