In Gulp, how can I run a task in only one file if any of several files is newer?

I'm probably trying to make gulp something that is not idiomatic, but that goes here. I want my build task to be performed only if the source files are newer than the output file.

In gulp, the standard practice is to create a build task that always runs, and then set up a view task only to complete this build task when certain files change. This is good, but it means that you are always building your first launch.

So, is it possible to do what I want? Here is what I have so far (newer than gulp -newer):

gulp.task('build_lib', function() { return gulp.src(["app/**/*.ts"]) .pipe(newer("out/outputLib.js")) //are any of these files newer than the output? ** NEED SOMETHING HERE ** how do I say, "If I got _any_ files from the step before, replace all of them with a single hardcoded file "app/scripts/LibSource.ts" "? .pipe(typescript({ declaration: true, sourcemap: true, emitError: false, safe: true, target: "ES5", out: "outputLib.js" })) .pipe(gulp.dest('out/')) 

});

I tried using gulpif, but it does not work if there are no files in it.

  .pipe(gulpif(are_there_any_files_at_all, gulp.src(["app/scripts/LibSource.ts"]))) 

However, my state function is not even called because there are no files for which it can be called. gulpif calls the true thread in this case, so LibSource is added to my thread, which I don't want.

Perhaps all this in one thread is really not the right call, because the only reason I transfer these files through the gulp -newer filter is to see if any of them are newer. Then I drop them and replace them with another file. My question is still standing.

+6
source share
4 answers

You can write your own through / transform stream to handle the condition as follows:

 // Additional core libs needed below var path = require('path'); var fs = require('fs'); // Additional npm libs var newer = require('gulp-newer'); var through = require('through'); var File = require('vinyl'); gulp.task('build_lib', function() { return gulp.src(["app/**/*.ts"]) .pipe(newer("out/outputLib.js")) .pipe(through(function(file) { // If any files get through newer, just return the one entry var libsrcpath = path.resolve('app', 'scripts', 'LibSource.ts'); // Pass libsrc through the stream this.queue(new File({ base: path.dirname(libsrcpath), path: libsrcpath, contents: new Buffer(fs.readFileSync(libsrcpath)) })); // Then end this stream by passing null to queue // this will ignore any other additional files this.queue(null); })) .pipe(typescript({ declaration: true, sourcemap: true, emitError: true, safe: true, target: "ES5", out: "outputLib.js" })) .pipe(gulp.dest('out/')); }); 
+6
source

You do not need to build first. You can run a viewing task in your โ€œfirst runโ€, from which you start all the others.

Example:

 // Create your 'watch' task gulp.task( 'watch', function() { gulp.watch( 'scripts/*.js', [ 'lint', 'test', 'scripts' ] ); gulp.watch( 'styles/sass/*.scss', [ 'sass_dev' ] ); } ); // On your first run you will only call the watch task gulp.task( 'default', [ 'watch' ] ); 

This will avoid starting any task at startup. Hope this helps you.

0
source

May I suggest gulp-newy in which you can manipulate the path and file name in your own function. Then just use the function as a callback to newy() . This gives you full control over the files you would like to compare.

This will allow you to compare 1: 1 or many to 1.

 newy(function(projectDir, srcFile, absSrcFile) { // do whatever you want to here. // construct your absolute path, change filename suffix, etc. // then return /foo/bar/filename.suffix as the file to compare against } 

enter image description here

0
source

I know how, this question was published more than 4 years ago, however; I am sure that this problem crosses the path of everyone, and although I think I understand the question being asked, I feel that there is an easier way to complete this task, due to which I recently posted a similar question about stackoverflow in New in GULP - Do I need to copy all the files from the src directory to the dist directory for the project? It uses gulp-change, and it worked like a charm for me, so for others who can look at this post for similar reasons, take a look at my post and see if this is really what you are looking for. Yours faithfully

0
source

Source: https://habr.com/ru/post/970619/


All Articles