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

Removing versioned files from stream if not matching current format.

Szymon Kupś 10 лет назад
Родитель
Сommit
cc3528c756
2 измененных файлов с 61 добавлено и 2 удалено
  1. 19 2
      dev/tasks/build/utils.js
  2. 42 0
      dev/tests/build/utils.js

+ 19 - 2
dev/tasks/build/utils.js

@@ -200,20 +200,37 @@ 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 specific format will be removed.
 	 *
 	 * @param {String} format
 	 * @returns {Stream}
 	 */
 	pickVersionedFile( format ) {
-		return rename( ( path ) => {
+		const pick = rename( ( path ) => {
 			const regexp = new RegExp( `__${ format }$` );
 
 			path.basename = path.basename.replace( regexp, '' );
 		} );
+
+		const remove = gulpFilter( ( file ) => {
+			return [ 'esnext', 'amd', 'cjs' ]
+				.filter( ( item ) => item !== format )
+				.reduce( ( prev, item ) => {
+					// If file was already matched, skip next checking.
+					if ( !prev ) {
+						return prev;
+					}
+
+					return !( new RegExp( `__${ item }\.js$` ).test( file.path ) );
+				}, true );
+		} );
+
+		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', () => {