I would like to run minifaction on javascript files that are not in the ./src/ponents directory. I tried this:
gulp.task('index', function () {
var assets = useref.assets();
return gulp.src('src/index.html')
.pipe(assets)
.pipe(gulpif('!./src/ponents/** && *.js', uglify()))
.pipe(gulpif('*.css', minifyCss()))
.pipe(assets.restore())
.pipe(useref())
.pipe(gulp.dest('dist'));
});
but it does not seem to be working (i.e. it preforms minification on js files in ./src/ponents anyway).
All help is appreciated. Thanks in advance.
I would like to run minifaction on javascript files that are not in the ./src/ponents directory. I tried this:
gulp.task('index', function () {
var assets = useref.assets();
return gulp.src('src/index.html')
.pipe(assets)
.pipe(gulpif('!./src/ponents/** && *.js', uglify()))
.pipe(gulpif('*.css', minifyCss()))
.pipe(assets.restore())
.pipe(useref())
.pipe(gulp.dest('dist'));
});
but it does not seem to be working (i.e. it preforms minification on js files in ./src/ponents anyway).
All help is appreciated. Thanks in advance.
Share Improve this question edited Apr 9, 2015 at 11:05 pQuestions123 asked Apr 8, 2015 at 13:28 pQuestions123pQuestions123 4,6116 gold badges30 silver badges60 bronze badges3 Answers
Reset to default 7Update facing the real problem
Okay, that is a point. gulp-if
uses gulp-match
to evaluate conditions. There you can use an array of globs:
.pipe(gulpif(['*.js', '!./src/ponents/**'], uglify())
Still for others:
gulp-if
might be the wrong approach. I'd suggest a stream queue.
var queue = require('streamqueue')
...
return queue.queue(
// all the JS files without ponents
gulp.src(['**/*.js', '!./src/ponents/**/'])
.pipe(uglify()),
// all JS files in ponents
gulp.src('./src/ponents/**/*.js')
)
.done()
.pipe(doSomething())
This way you can do separate tasks on all your non-ponents, and afterwards execute mon processes.
Code's not tested, but you get the idea looking at the docs.
The real problem here I believe is that var assets = useref.assets();
stores the concatenated files in assets
. Therefore, the paths of the individual files are lost and any gulp-if statement that uses the paths, useless.
There is an option for useref.assets(noconcat: true)
. I thought I could then use:
var assets = useref.assets(noconcat: true);
return gulp.src('src/index.html')
.pipe(assets)
.pipe(gulpif(['*.js', '!./src/ponents/**'], uglify())
.pipe...
but this didn't work either. I am not sure why (perhaps the full paths of the files are not stored?).
Regardless the solution I eventually went with was to use gulp-usemin. gulp-usemin can do eveything I am trying to acplish in my original post like so:
gulp.task('usemin', function() {
return gulp.src(paths.index)
.pipe(usemin({
js1: [uglify(), 'concat'],
js2: ['concat'],
css: [minifyCss(), 'concat']
}))
.pipe(gulp.dest('dist/'));
});
where js1 and js2 are pipeline ids (specified in the html).
It is correct that useref.assets()
stores the concatenated files in assets. But, there is now an option to pass the non-concatenated files down the stream. Not sure if this will fix the problem or not, but in the other example that was posted earlier, the options are not being passed in correctly. Options should be passed in as an object, so it should look like this:
var assets = useref.assets({ noconcat: true });
return gulp.src('src/index.html')
.pipe(assets)
.pipe(gulpif(['*.js', '!./src/ponents/**'], uglify())
.pipe...
I will update the docs for that because I just noticed there is no example with the options in it.