Browse Source

Added changes to builder for supporting Node.js environment when building for cjs format.

Szymon Kupś 9 years ago
parent
commit
aa29bd76bc
2 changed files with 114 additions and 8 deletions
  1. 40 4
      dev/tasks/build/utils.js
  2. 74 4
      dev/tests/build/utils.js

+ 40 - 4
dev/tasks/build/utils.js

@@ -116,7 +116,7 @@ const utils = {
 				conversionPipes.push(
 					filterTests,
 					transpileTests,
-					utils.appendBenderLauncher(),
+					format === 'amd' ? utils.appendBenderLauncher() : utils.noop(),
 					filterTests.restore
 				);
 			}
@@ -172,7 +172,7 @@ const utils = {
 			plugins: utils.getBabelPlugins( format ),
 			// 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: utils.appendModuleExtension
+			resolveModuleSource: format == 'cjs' ? utils.resolveModuleSource : utils.appendModuleExtension
 		};
 	},
 
@@ -185,7 +185,7 @@ const utils = {
 	getBabelOptionsForTests( format ) {
 		return {
 			plugins: utils.getBabelPlugins( format ),
-			resolveModuleSource: utils.appendModuleExtension,
+			resolveModuleSource: format == 'cjs' ? utils.resolveModuleSource : utils.appendModuleExtension,
 			moduleIds: true,
 			moduleId: 'tests'
 		};
@@ -208,7 +208,7 @@ const utils = {
 			throw new Error( `Incorrect format: ${ format }` );
 		}
 
-		return [
+		const plugins = [
 			// Note: When plugin is specified by its name, Babel loads it from a context of a
 			// currently transpiled file (in our case - e.g. from ckeditor5-core/src/foo.js).
 			// Obviously that fails, since we have all the plugins installed only in ckeditor5/
@@ -218,6 +218,13 @@ const utils = {
 			// but it works... so let's hope it will.
 			require( `babel-plugin-transform-es2015-modules-${ babelModuleTranspiler }` )
 		];
+
+		// Add additional plugins for Node.js environment.
+		if ( format == 'cjs' ) {
+			plugins.push( require( 'babel-plugin-transform-es2015-parameters' ) );
+		}
+
+		return plugins;
 	},
 
 	/**
@@ -599,6 +606,35 @@ const utils = {
 				} )
 			);
 		} );
+	},
+
+	/**
+	 * Resolves CommonJS module source path.
+	 *
+	 * @param {String} source Module path passed to require() method.
+	 * @param {String} file Path to a file where require() method is called.
+	 * @returns {String} Fixed module path.
+	 */
+	resolveModuleSource( source, file ) {
+		// If path is relative - leave it as is.
+		if ( !path.isAbsolute( source ) ) {
+			return source;
+		}
+
+		// Find relative path of test file from cwd directory.
+		let testFile = path.relative( process.cwd(), file );
+
+		// Append `/` as all files uses it as root inside transpiled versions.
+		testFile = path.join( path.sep, testFile );
+
+		// Find relative path from test file to source.
+		let relativePath = path.relative( path.dirname( testFile ), path.dirname( source ) );
+		relativePath = path.join( relativePath, path.basename( source ) );
+
+		// Convert windows path to posix.
+		relativePath = relativePath.replace( /\\/g, '/' );
+
+		return utils.appendModuleExtension( ( relativePath.startsWith( '../' ) ? '' : './' ) + relativePath );
 	}
 };
 

+ 74 - 4
dev/tests/build/utils.js

@@ -123,10 +123,22 @@ describe( 'build-utils', () => {
 			const plugins = [ 'foo' ];
 			sandbox.stub( utils, 'getBabelPlugins', () => plugins );
 
-			const options = utils.getBabelOptionsForSource( 'format' );
+			const options = utils.getBabelOptionsForSource( 'amd' );
 
 			expect( options ).to.have.property( 'plugins', plugins );
 			expect( options ).to.have.property( 'resolveModuleSource' );
+			expect( options.resolveModuleSource ).to.equal( utils.appendModuleExtension );
+		} );
+
+		it( 'should return plugins for cjs format', () => {
+			const plugins = [ 'foo' ];
+			sandbox.stub( utils, 'getBabelPlugins', () => plugins );
+
+			const options = utils.getBabelOptionsForSource( 'cjs' );
+
+			expect( options ).to.have.property( 'plugins', plugins );
+			expect( options ).to.have.property( 'resolveModuleSource' );
+			expect( options.resolveModuleSource ).to.equal( utils.resolveModuleSource );
 		} );
 	} );
 
@@ -135,12 +147,26 @@ describe( 'build-utils', () => {
 			const plugins = [ 'foo' ];
 			sandbox.stub( utils, 'getBabelPlugins', () => plugins );
 
-			const options = utils.getBabelOptionsForTests( 'format' );
+			const options = utils.getBabelOptionsForTests( 'amd' );
+
+			expect( options ).to.have.property( 'plugins', plugins );
+			expect( options ).to.have.property( 'resolveModuleSource' );
+			expect( options ).to.have.property( 'moduleIds', true );
+			expect( options ).to.have.property( 'moduleId', 'tests' );
+			expect( options.resolveModuleSource ).to.equal( utils.appendModuleExtension );
+		} );
+
+		it( 'should return plugins for cjs format', () => {
+			const plugins = [ 'foo' ];
+			sandbox.stub( utils, 'getBabelPlugins', () => plugins );
+
+			const options = utils.getBabelOptionsForTests( 'cjs' );
 
 			expect( options ).to.have.property( 'plugins', plugins );
 			expect( options ).to.have.property( 'resolveModuleSource' );
 			expect( options ).to.have.property( 'moduleIds', true );
 			expect( options ).to.have.property( 'moduleId', 'tests' );
+			expect( options.resolveModuleSource ).to.equal( utils.resolveModuleSource );
 		} );
 	} );
 
@@ -149,6 +175,10 @@ describe( 'build-utils', () => {
 			expect( utils.getBabelPlugins( 'amd' ) ).to.be.an( 'array' );
 		} );
 
+		it( 'should return plugins for cjs format', () => {
+			expect( utils.getBabelPlugins( 'cjs' ) ).to.be.an( 'array' );
+		} );
+
 		it( 'should throw an exception when incorrect format is provided', () => {
 			const format = 'incorrect-format';
 
@@ -236,7 +266,31 @@ describe( 'build-utils', () => {
 		} );
 
 		describe( 'created conversion stream', () => {
-			it( 'should process test file', ( done ) => {
+			it( 'should process test file in amd format', ( done ) => {
+				const buildDir = 'build/';
+				const formats = [ 'amd' ];
+				const fn = utils.getConversionStreamGenerator( buildDir );
+				const streams = formats.reduce( fn, [] );
+
+				expect( streams ).to.have.length( 1 );
+
+				const stream = streams[ 0 ];
+
+				stream.pipe(
+					utils.noop( ( file ) => {
+						expect( file.contents.toString() ).to.equal( 'foo();amd;tests;launcher' );
+						done();
+					} )
+				);
+
+				stream.write( new Vinyl( {
+					cwd: './',
+					path: 'tests/core/file.js',
+					contents: new Buffer( 'foo()' )
+				} ) );
+			} );
+			
+			it( 'should process test file in cjs format', ( done ) => {
 				const buildDir = 'build/';
 				const formats = [ 'cjs' ];
 				const fn = utils.getConversionStreamGenerator( buildDir );
@@ -248,7 +302,7 @@ describe( 'build-utils', () => {
 
 				stream.pipe(
 					utils.noop( ( file ) => {
-						expect( file.contents.toString() ).to.equal( 'foo();cjs;tests;launcher' );
+						expect( file.contents.toString() ).to.equal( 'foo();cjs;tests' );
 						done();
 					} )
 				);
@@ -728,4 +782,20 @@ describe( 'build-utils', () => {
 			expect( streams ).to.have.length( 2 );
 		} );
 	} );
+
+	describe( 'resolveModuleSource', () => {
+		it( 'does not modify relative source paths', () => {
+			const source = '../module';
+			const resolved = utils.resolveModuleSource( source, '' );
+			expect( resolved ).to.equal( source );
+		} );
+
+		it( 'resolves absolute source paths', () => {
+			const source = '/ckeditor5/path/to/module.js';
+			const file = path.join( process.cwd(), 'tests', 'module', 'module.js' );
+
+			const resolved = utils.resolveModuleSource( source, file );
+			expect( resolved ).to.equal( '../../ckeditor5/path/to/module.js' );
+		} );
+	} );
 } );