Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add specific bower components as IE8 conditional block in Gulp

I have several bower components, of which I need some components like json3, respondjs, es5shim to be added in an IE8 conditional block (in build as well as serve) like this :

<!--[if lt IE 9]> <script src="bower_components/json3/json3.js"></script> <![endif]-->

How should I proceed to write the task? I could not find any suitable examples with gulp-inject as well as wiredep for this issue. Please advise

like image 366
Koustuv Sinha Avatar asked Nov 01 '22 11:11

Koustuv Sinha


1 Answers

It is possible with gulp-filter and gulp-inject's starttag option.

Suppose you have the following injections defined in index.html:

<!--[if lt IE 9]>
<![endif]-->
<!-- inject:js -->
<!-- endinject -->

You can wire them in gulpfile:

var gulp = require('gulp')
var bowerFiles = require('main-bower-files');
var gulpFilter = require('gulp-filter');
var gulpInject = require('gulp-inject');

gulp.task('wiredep', function() {
    var ie8Files = ['**/json3.js', '**/es5shim.js'];
    // the same as: var restFiles = ['*', '!**/json3.js', '!**/es5shim.js'];
    var restFiles = ['*'].concat(ie8Files.map(function(e) { return '!' + e;}));
    return gulp.src('index.html')
    .pipe(gulpInject(gulp.src(bowerFiles(), {read: false}).pipe(gulpFilter(restFiles))))
    .pipe(gulpInject(gulp.src(bowerFiles(), {read: false}).pipe(gulpFilter(ie8Files)),
                    {starttag: '<!--[if lt IE 9]>', endtag: '<![endif]-->'}))
    .pipe(gulp.dest('dist'));
});

Which results in (for example):

<!--[if lt IE 9]>
<script src="bower_components/json3/json3.js"></script>
<script src="bower_components/shim/es5shim.js"></script>
<![endif]-->
<!-- inject:js -->
<script src="bower_components/jquery/jquery.js"></script>
<!-- endinject -->
like image 58
fracz Avatar answered Nov 09 '22 08:11

fracz