node.jsgulpgulp-inject

Why gulp-inject does not add my javascript files to html


I have code to inject into my webpage by gulp-inject, and I have setup my static folder to be served like this

app.use(express.static(__dirname + '/../public/'));

Then in gulpfile.js I do my magic, but my spell is broken...

var inject = require('gulp-inject');
var jsFiles = ['*.js', 'backend/**/*.js'];
gulp.task('inject', function () {
    var wiredep = require('wiredep').stream;
    var options = {
        bowerJson: require('./bower.json')
        , directory: './public/lib'
    };
    var injectSrc = gulp.src(['css/**/*.css', 'js/**/*.js'], {
        read: false
    });
    var injectOptions = {
        ignorePath: './public'
    };
    return gulp.src('./public/index.html').pipe(inject(injectSrc, injectOptions)).pipe(gulp.dest('./public/'));

How can I inject static java script and css to my html templates?


Solution

  • First in your html

    <!--    inject:css-->
    <!--    endinject-->
    <!--    inject:js-->
    <!--    endinject-->
    

    Then in your gulpfile.js the way I found around this is removing ./ from ignorePath and adding full path to resources, like gulp.src(./public/some/folder/**/*.css)

    And you should be on the good way to go :)