8
0
Просмотр исходного кода

Merge pull request #81 from ckeditor/t/50

Remove unused version of files when building.
Piotrek Koszuliński 10 лет назад
Родитель
Сommit
23696266ff
2 измененных файлов с 59 добавлено и 4 удалено
  1. 17 4
      dev/tasks/build/utils.js
  2. 42 0
      dev/tests/build/utils.js

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

@@ -28,6 +28,11 @@ require( [ 'tests' ], bender.defer(), function( err ) {
 } );
 `,
 
+	/**
+	 * Module formats supported by the builder.
+	 */
+	SUPPORTED_FORMATS: [ 'esnext', 'amd', 'cjs' ],
+
 	/**
 	 * Creates a simple duplex stream.
 	 *
@@ -200,20 +205,28 @@ require( [ 'tests' ], bender.defer(), function( err ) {
 	},
 
 	/**
-	 * Allows us to pick one of files suffixed with the format (`__esnext`, `__amd`, or `__cjs`).
+	 * Allows us to pick one of files suffixed with the format (`__esnext`, `__amd`, or `__cjs`) and removes
+	 * files with other suffixes from the stream.
 	 *
 	 * 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`.
+	 * Files not matching a specified format will be removed.
 	 *
 	 * @param {String} format
 	 * @returns {Stream}
 	 */
 	pickVersionedFile( format ) {
-		return rename( ( path ) => {
-			const regexp = new RegExp( `__${ format }$` );
+		const rejectedFormats = utils.SUPPORTED_FORMATS
+			.filter( ( item ) => item !== format );
+		const pickRegexp = new RegExp( `__${ format }$` );
+		const rejectRegexp = new RegExp( `__(${ rejectedFormats.join( '|' ) }).js$` );
 
-			path.basename = path.basename.replace( regexp, '' );
+		const pick = rename( ( path ) => {
+			path.basename = path.basename.replace( pickRegexp, '' );
 		} );
+		const remove = gulpFilter( ( file ) => !rejectRegexp.test( file.path ) );
+
+		return multipipe( pick, remove );
 	},
 
 	/**

+ 42 - 0
dev/tests/build/utils.js

@@ -282,6 +282,48 @@ describe( 'build-utils', () => {
 
 			rename.end();
 		} );
+
+		it( 'should remove files in other formats', ( done ) => {
+			const rename = utils.pickVersionedFile( 'amd' );
+			const spy = sandbox.spy( ( data ) => {
+				expect( data.basename ).to.equal( 'load.js' );
+			} );
+
+			rename.pipe(
+				utils.noop( spy )
+			);
+
+			rename.on( 'end', () => {
+				sinon.assert.calledOnce( spy );
+				done();
+			} );
+
+			const amd = new Vinyl( {
+				cwd: '/',
+				base: '/test/',
+				path: '/test/load__amd.js',
+				contents: new Buffer( '' )
+			} );
+
+			const cjs = new Vinyl( {
+				cwd: '/',
+				base: '/test/',
+				path: '/test/load__cjs.js',
+				contents: new Buffer( '' )
+			} );
+
+			const esnext = new Vinyl( {
+				cwd: '/',
+				base: '/test/',
+				path: '/test/load__esnext.js',
+				contents: new Buffer( '' )
+			} );
+
+			rename.write( cjs );
+			rename.write( amd );
+			rename.write( esnext );
+			rename.end();
+		} );
 	} );
 
 	describe( 'renamePackageFiles', () => {