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

Turns out the previous attempt wasn't that good. Forking streams require also cloning the src stream content.

Piotrek Koszuliński 10 лет назад
Родитель
Сommit
92b756556a
3 измененных файлов с 78 добавлено и 51 удалено
  1. 35 48
      dev/tasks/gulp/build.js
  2. 39 3
      dev/tasks/gulp/utils.js
  3. 4 0
      package.json

+ 35 - 48
dev/tasks/gulp/build.js

@@ -2,16 +2,35 @@
 
 'use strict';
 
+const KNOWN_OPTIONS = {
+	build: {
+		string: [
+			'formats'
+		],
+
+		boolean: [
+			'watch'
+		],
+
+		default: {
+			formats: 'amd',
+			watch: false
+		}
+	}
+};
+
 const path = require( 'path' );
 const gulp = require( 'gulp' );
 const del = require( 'del' );
 const merge = require( 'merge-stream' );
-// const runSequence = require( 'run-sequence' );
-const watch = require( 'gulp-watch' );
+const gulpMirror = require( 'gulp-mirror' );
+const minimist = require( 'minimist' );
 const utils = require( './utils' );
 
 const sep = path.sep;
 
+const options = minimist( process.argv.slice( 2 ), KNOWN_OPTIONS[ process.argv[ 2 ] ] );
+
 module.exports = ( config ) => {
 	const distDir = path.join( config.ROOT_DIR, config.DIST_DIR );
 
@@ -21,31 +40,23 @@ module.exports = ( config ) => {
 		},
 
 		src: {
-			all() {
-				return merge( tasks.src.main(), tasks.src.ckeditor5(), tasks.src.modules() );
+			all( watch ) {
+				return merge( tasks.src.main( watch ), tasks.src.ckeditor5( watch ), tasks.src.modules( watch ) );
 			},
 
-			main() {
-				const srcDir = path.join( config.ROOT_DIR, 'ckeditor.js' );
-
-				return gulp.src( srcDir )
-					.pipe( watch( srcDir ) );
+			main( watch ) {
+				return utils.src( config.ROOT_DIR, 'ckeditor.js', watch );
 			},
 
-			ckeditor5() {
-				const srcDir = path.join( config.ROOT_DIR, 'src/**/*.js' );
-
-				return gulp.src( srcDir )
-					.pipe( watch( srcDir ) )
+			ckeditor5( watch ) {
+				return utils.src( config.ROOT_DIR, 'src/**/*.js', watch )
 					.pipe( utils.wrapCKEditor5Module() );
 			},
 
-			modules() {
-				const srcDir = path.join( config.ROOT_DIR, 'node_modules/ckeditor5-*/src/**/*.js' );
+			modules( watch ) {
 				const modulePathPattern = new RegExp( `node_modules${ sep }(ckeditor5-[^${ sep }]+)${ sep }src` );
 
-				return gulp.src( srcDir )
-					.pipe( watch( srcDir ) )
+				return utils.src( config.ROOT_DIR, 'node_modules/ckeditor5-*/src/**/*.js', watch )
 					.pipe( utils.unpackModules( modulePathPattern ) );
 			}
 		}
@@ -53,40 +64,16 @@ module.exports = ( config ) => {
 
 	gulp.task( 'build:clean', tasks.clean );
 
-	// gulp.task( 'build:copy', ( callback ) => {
-	// 	runSequence(
-	// 		'build:clean',
-	// 		[ 'build:copy:main', 'build:copy:ckeditor5', 'build:copy:modules' ],
-	// 		callback
-	// 	);
-	// } );
-
-	// gulp.task( 'build:transpile', [ 'build:copy' ], tasks.transpile );
-
-	gulp.task( 'build:watch', () => {
-		const codeStream = tasks.src.all()
+	gulp.task( 'build', [ 'build:clean' ], () => {
+		const formats = options.formats.split( ',' );
+		const codeStream = tasks.src.all( options.watch )
 			.on( 'data', ( file ) => {
 				console.log( `Processing ${ file.path }...` );
 			} );
+		const formatPipes = formats.reduce( utils.addFormat( distDir ), [] );
 
-		const esNextStream = utils.fork( codeStream )
-			.pipe( utils.dist( distDir, 'esnext' ) );
-
-		const amdStream = utils.fork( codeStream )
-			.pipe( utils.transpile( 'amd' ) )
-			.pipe( utils.dist( distDir, 'amd' ) );
-
-		const cjsStream = utils.fork( codeStream )
-			.pipe( utils.transpile( 'cjs' ) )
-			.pipe( utils.dist( distDir, 'cjs' ) );
-
-		return merge( esNextStream, amdStream, cjsStream )
-			// Unfortunately it gets triggered 3x per each file (what makes sense), but with a single path (cjs).
-			// I guess that it will be better to listen on amd/cjs streams because while the watcher is on you either
-			// need one or the other.
-			.on( 'data', ( file ) => {
-				console.log( `Finished writing ${ file.path }...` );
-			} );
+		return codeStream
+			.pipe( gulpMirror.apply( null, formatPipes ) );
 	} );
 
 	return tasks;

+ 39 - 3
dev/tasks/gulp/utils.js

@@ -6,15 +6,26 @@ const path = require( 'path' );
 const gulp = require( 'gulp' );
 const rename = require( 'gulp-rename' );
 const babel = require( 'gulp-babel' );
+const gulpWatch = require( 'gulp-watch' );
+const gulpPlumber = require( 'gulp-plumber' );
+const multipipe = require( 'multipipe' );
 const stream = require( 'stream' );
 
 const sep = path.sep;
 
 const utils = {
-	fork( sourceStream ) {
-		const fork = new stream.PassThrough( { objectMode: true } );
+	src( root, pattern, watch ) {
+		const srcDir = path.join( root, pattern );
+		let stream = gulp.src( srcDir );
 
-		return sourceStream.pipe( fork );
+		if ( watch ) {
+			stream = stream
+				// Let's use plumber only when watching. In other cases we should fail quickly and loudly.
+				.pipe( gulpPlumber() )
+				.pipe( gulpWatch( srcDir ) );
+		}
+
+		return stream;
 	},
 
 	dist( distDir, format ) {
@@ -30,6 +41,10 @@ const utils = {
 		};
 		const babelModuleTranspiler = babelModuleTranspilers[ format ];
 
+		if ( !babelModuleTranspiler ) {
+			throw new Error( `Incorrect format: ${ format }` );
+		}
+
 		return new stream.PassThrough( { objectMode: true } )
 			.pipe( utils.pickVersionedFile( format ) )
 			.pipe( babel( {
@@ -37,6 +52,27 @@ const utils = {
 			} ) );
 	},
 
+	addFormat( distDir ) {
+		return ( pipes, format ) => {
+			const conversionPipes = [];
+
+			if ( format != 'esnext' ) {
+				conversionPipes.push( utils.transpile( format ) );
+			}
+
+			conversionPipes.push(
+				utils.dist( distDir, format )
+					.on( 'data', ( file ) => {
+						console.log( `Finished writing ${ file.path }...` );
+					} )
+			);
+
+			pipes.push( multipipe.apply( null, conversionPipes ) );
+
+			return pipes;
+		};
+	},
+
 	pickVersionedFile( format ) {
 		return rename( ( path ) => {
 			const regexp = new RegExp( `__${ format }$` );

+ 4 - 0
package.json

@@ -31,13 +31,17 @@
     "grunt-text-replace": "^0.4.0",
     "gulp": "^3.9.0",
     "gulp-babel": "^6.1.0",
+    "gulp-mirror": "^0.4.0",
+    "gulp-plumber": "^1.0.1",
     "gulp-rename": "^1.2.2",
     "gulp-sourcemaps": "^1.6.0",
     "gulp-watch": "^4.3.5",
     "inquirer": "^0.11.0",
     "istanbul": "^0.4.1",
     "merge-stream": "^1.0.0",
+    "minimist": "^1.2.0",
     "mocha": "^2.2.5",
+    "multipipe": "^0.2.1",
     "ncp": "^2.0.0",
     "replace": "^0.3.0",
     "run-sequence": "^1.1.5",