nezzz
nezzz

Reputation: 101

#Gulp# How to output files to the path which depends on the path of input files?

Say I have these folders:

./
|
folder1/
  |---file1.js
  |---file2.js
folder2/
  |---file1.js
  |---file2.js

I want to run a task with gulp.src('./*/.js'), and I want them to be output as the following:

./
|
folder1/
  |---file1.js
  |---file2.js
  |---file1.min.js
  |---file2.min.js
folder2/
  |---file1.js
  |---file2.js
  |---file1.min.js
  |---file2.min.js

Upvotes: 0

Views: 275

Answers (1)

OverZealous
OverZealous

Reputation: 39570

This really is something that you can solve by reading any one of a number of gulp articles already out there. Google for gulp tutorial and start reading articles.

By default, the relative source path for an input file is preserved through to the output file in gulp. You don't have to do anything special. If you want something similar to what you've got, you need to use a JS minifier, such as the gulp-uglify plugin, and the gulp-rename plugin.

var gulp = require('gulp'),
    uglify = require('gulp-uglify'),
    rename = require('gulp-rename');

gulp.task('scripts', function() {
    return gulp.src('src/**/*.js')
        .pipe(uglify())
        .pipe(rename({extname: '.min.js'}))
        .pipe(gulp.dest('dest/'));
});

The relative path is based on the first glob in the string, in the example above, it's the **, so the relative path would be any folders after src/. This means a file at src/foo/bar.js would be saved as dest/foo/bar.min.js, because the relative path is foo/.

Upvotes: 2

Related Questions