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

Added gzipped size to bundle summary.

Oskar Wrobel 9 лет назад
Родитель
Сommit
16c133bc81
4 измененных файлов с 108 добавлено и 48 удалено
  1. 5 3
      dev/tasks/bundle/tasks.js
  2. 52 25
      dev/tasks/bundle/utils.js
  3. 50 20
      dev/tests/bundle/utils.js
  4. 1 0
      package.json

+ 5 - 3
dev/tasks/bundle/tasks.js

@@ -104,9 +104,11 @@ module.exports = ( config ) => {
 
 	gulp.task( 'bundle', ( callback ) => {
 		runSequence( 'bundle:generate', [ 'bundle:minify:js', 'bundle:minify:css' ], () => {
-			// Print files size on console just before the end of the task.
-			const files = [ 'ckeditor.js', 'ckeditor.min.js', 'ckeditor.css', 'ckeditor.min.css' ];
-			utils.logFilesSize( files, bundleDir );
+			const files = [ 'ckeditor.js', 'ckeditor.css', 'ckeditor.min.js', 'ckeditor.min.css' ];
+			const filesStats = utils.getFilesSizeStats( files, bundleDir );
+
+			// Show bundle summary on console.
+			utils.showFilesSummary( 'Bundle summary', filesStats );
 
 			// Finish the task.
 			callback();

+ 52 - 25
dev/tasks/bundle/utils.js

@@ -11,6 +11,7 @@ const gulp = require( 'gulp' );
 const gulpRename = require( 'gulp-rename' );
 const gutil = require( 'gulp-util' );
 const prettyBytes = require( 'pretty-bytes' );
+const gzipSize = require( 'gzip-size' );
 const mainUtils = require( '../utils' );
 
 const utils = {
@@ -30,50 +31,76 @@ const utils = {
 	},
 
 	/**
-	 * Get human readable size of the file.
+	 * Copy specified file to specified destination.
+	 *
+	 * @param {String} from file path
+	 * @param {String} to copied file destination
+	 * @returns {Promise}
+	 */
+	copyFile( from, to ) {
+		return new Promise( ( resolve ) => {
+			gulp.src( from )
+				.pipe( gulp.dest( to ) )
+				.on( 'finish', resolve );
+		} );
+	},
+
+	/**
+	 * Get size of the file.
 	 *
 	 * @param {String} path path to the file
+	 * @returns {Number} size size in bytes
 	 */
 	getFileSize( path ) {
-		return prettyBytes( fs.statSync( path ).size );
+		return fs.statSync( path ).size;
 	},
 
 	/**
-	 * Log on console size of every passed file in specified directory.
+	 * Get human readable gzipped size of the file.
 	 *
-	 * 		utils.logFileSize( [ 'ckeditor.min.js', 'ckeditor.min.css' ], 'path/to/dir' );
-	 *
-	 * 		ckeditor.min.js: 192.43 KB
-	 * 		ckeditor.min.css: 5.38 KB
+	 * @param {String} path path to the file
+	 * @returns {Number} size size in bytes
+	 */
+	getGzippedFileSize( path ) {
+		return gzipSize.sync( fs.readFileSync( path ) );
+	},
+
+	/**
+	 * Get normal and gzipped size of every passed file in specified directory.
 	 *
 	 * @param {String} [rootDir='']
 	 * @param {Array<String>} files
+	 * @returns {Array<Object>} List with file size data
 	 */
-	logFilesSize( files, rootDir = '' ) {
-		files = files.map( ( file ) => {
-			let filePath = path.join( rootDir, file );
-			let name = path.basename( filePath );
-			let size = utils.getFileSize( filePath );
+	getFilesSizeStats( files, rootDir = '' ) {
+		return files.map( ( file ) => {
+			const filePath = path.join( rootDir, file );
 
-			return `${name}: ${size}`;
+			return {
+				name: path.basename( filePath ),
+				size: utils.getFileSize( filePath ),
+				gzippedSize: utils.getGzippedFileSize( filePath )
+			};
 		} );
-
-		gutil.log( gutil.colors.green( `\n${ files.join( '\n' ) }` ) );
 	},
 
 	/**
-	 * Copy specified file to specified destination.
+	 * Print on console list of files with their size stats.
 	 *
-	 * @param {String} from file path
-	 * @param {String} to copied file destination
-	 * @return {Promise}
+	 * 		Title:
+	 * 		file.js: 1 MB (gzipped: 400 kB)
+	 * 		file.css 500 kB (gzipped: 100 kB)
+	 *
+	 * @param {String} title
+	 * @param {Array<Object>} filesStats
 	 */
-	copyFile( from, to ) {
-		return new Promise( ( resolve ) => {
-			gulp.src( from )
-				.pipe( gulp.dest( to ) )
-				.on( 'finish', resolve );
-		} );
+	showFilesSummary( title, filesStats ) {
+		const label = gutil.colors.underline( title );
+		const filesSummary = filesStats.map( ( file ) => {
+			return `${ file.name }: ${ prettyBytes( file.size ) } (gzipped: ${ prettyBytes( file.gzippedSize ) })`;
+		} ).join( '\n' );
+
+		gutil.log( gutil.colors.green( `\n${ label }:\n${ filesSummary }` ) );
 	}
 };
 

+ 50 - 20
dev/tests/bundle/utils.js

@@ -10,10 +10,10 @@
 const chai = require( 'chai' );
 const expect = chai.expect;
 const sinon = require( 'sinon' );
-const gutil = require( 'gulp-util' );
 const path = require( 'path' );
 const fs = require( 'fs' );
 const mainUtils = require( '../../tasks/utils' );
+const gzipSize = require( 'gzip-size' );
 
 describe( 'bundle-utils', () => {
 	const utils = require( '../../tasks/bundle/utils' );
@@ -32,49 +32,79 @@ describe( 'bundle-utils', () => {
 	} );
 
 	describe( 'getFileSize', () => {
-		it( 'should return human readable file size', () => {
-			let filePath = 'path/to/file';
-			let statSyncMock = sandbox.stub( fs, 'statSync', () => {
-				return { size: 1337 };
+		it( 'should return file size in bytes', () => {
+			const filePath = 'path/to/file';
+			const size = 1337;
+			const statSyncMock = sandbox.stub( fs, 'statSync', () => {
+				return { size };
 			} );
 
-			expect( utils.getFileSize( filePath ) ).to.be.equal( '1.34 kB' );
+			expect( utils.getFileSize( filePath ) ).to.be.equal( size );
 			sinon.assert.calledWithExactly( statSyncMock, filePath );
 		} );
 	} );
 
-	describe( 'logFilesSize', () => {
-		let gutilLogSpy;
+	describe( 'getGzippedFileSize', () => {
+		it( 'should return file size in bytes', () => {
+			const filePath = 'path/to/file';
+			const size = 1337;
+			const fileContent = 'some string';
+			const readFileSyncMock = sandbox.stub( fs, 'readFileSync', () => fileContent );
+			const gzipSizeMock = sandbox.stub( gzipSize, 'sync', () => 1337 );
+
+			expect( utils.getGzippedFileSize( filePath ) ).to.be.equal( size );
+			sinon.assert.calledWithExactly( readFileSyncMock, filePath );
+			sinon.assert.calledWithExactly( gzipSizeMock, fileContent );
+		} );
+	} );
+
+	describe( 'getFilesSizeStats', () => {
+		let size, gzippedSize;
 
 		beforeEach( () => {
-			gutilLogSpy = sandbox.stub( gutil, 'log' );
-			sandbox.stub( utils, 'getFileSize', () => '1 MB' );
+			size = 1337;
+			gzippedSize = 543;
+
+			sandbox.stub( utils, 'getFileSize', () => size );
+			sandbox.stub( utils, 'getGzippedFileSize', () => gzippedSize );
 		} );
 
-		it( 'should log only files base name with file size separate by new line character', () => {
-			const expected = gutil.colors.green( `\nfile1.js: 1 MB\nfile2.js: 1 MB` );
+		it( 'should returns an array with two elements', () => {
+			const result = utils.getFilesSizeStats( [ 'sub/dir/file.js', 'other/sub/dir/file.css' ] , 'root/path' );
+
+			expect( result ).to.be.an( 'array' );
+			expect( result ).to.have.length( 2 );
+		} );
 
-			utils.logFilesSize( [ 'sub/dir/file1.js', 'other/sub/dir/file2.js' ] , 'root/path' );
+		it( 'should returns list of object with files stats', () => {
+			const result = utils.getFilesSizeStats( [ 'sub/dir/file.js', 'other/sub/dir/file.css' ] , 'root/path' );
 
-			sinon.assert.calledWithExactly( gutilLogSpy, expected );
+			expect( result ).to.be.deep.equal( [
+				{ name: 'file.js', size, gzippedSize },
+				{ name: 'file.css', size, gzippedSize }
+			] );
 		} );
 
 		it( 'should get files from root directory', () => {
 			let basenameSpy = sandbox.spy( path, 'basename' );
 
-			utils.logFilesSize( [ 'sub/dir/file1.js', 'other/sub/dir/file2.js' ] , 'root/path' );
+			const result = utils.getFilesSizeStats( [ 'sub/dir/file.js', 'other/sub/dir/file.css' ] , 'root/path' );
 
-			sinon.assert.calledWithExactly( basenameSpy.firstCall, 'root/path/sub/dir/file1.js' );
-			sinon.assert.calledWithExactly( basenameSpy.secondCall, 'root/path/other/sub/dir/file2.js' );
+			expect( result[0] ).to.have.property( 'name' ).equal( 'file.js' );
+			expect( result[1] ).to.have.property( 'name' ).equal( 'file.css' );
+			sinon.assert.calledWithExactly( basenameSpy.firstCall, 'root/path/sub/dir/file.js' );
+			sinon.assert.calledWithExactly( basenameSpy.secondCall, 'root/path/other/sub/dir/file.css' );
 		} );
 
 		it( 'should get files if root directory is not specified', () => {
 			let basenameSpy = sandbox.spy( path, 'basename' );
 
-			utils.logFilesSize( [ 'sub/dir/file1.js', 'file2.js' ] );
+			const result = utils.getFilesSizeStats( [ 'sub/dir/file.js', 'file.css' ] );
 
-			sinon.assert.calledWithExactly( basenameSpy.firstCall, 'sub/dir/file1.js' );
-			sinon.assert.calledWithExactly( basenameSpy.secondCall, 'file2.js' );
+			expect( result[0] ).to.have.property( 'name' ).equal( 'file.js' );
+			expect( result[1] ).to.have.property( 'name' ).equal( 'file.css' );
+			sinon.assert.calledWithExactly( basenameSpy.firstCall, 'sub/dir/file.js' );
+			sinon.assert.calledWithExactly( basenameSpy.secondCall, 'file.css' );
 		} );
 	} );
 } );

+ 1 - 0
package.json

@@ -60,6 +60,7 @@
     "gulp-util": "^3.0.7",
     "gulp-watch": "4.3.5",
     "guppy-pre-commit": "^0.3.0",
+    "gzip-size": "^3.0.0",
     "inquirer": "^0.11.0",
     "istanbul": "^0.4.1",
     "jsdoc": "^3.4.0",