Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Concat coffeescript + vanilla javascript in Gulp file

This may be an obvious thing, but I'm having trouble with Gulp playing nicely when trying to concat together coffeescript and javascript file into one nice and tasty javascript file.

This is the section of my Gulp code. If anyone can help me out that would be super amazing, many thanks!!

gulp.task('scriptspre', function() {
  return gulp.src([
                    'project/assets/scripts/scriptspre.coffee',
                    'bower_components/modernizr/modernizr.js'
                  ])
    .pipe(coffee())
    .pipe(sourcemaps.init())
    .pipe(concat('pre.js'))
    .pipe(sourcemaps.write())
    .pipe(gulp.dest('build/assets/js/'));
});

Thank you!

2 Answers

Try to split it in 2 tasks.

First you transform the coffee files into javascript.

Then you process the javascript files declaring the coffee task as a dependency.

gulp.task('javascript', ['coffee'], function() {

});

Hey Roberto Alicata, yeah I just came back to post that :) Thanks

My code is now like this, not sure if there is a better way or not but this is working great at the moment:

gulp.task('coffee', function() {
    return gulp.src('project/assets/scripts/*.coffeescript')
        .pipe(coffee())
        .pipe(gulp.dest('project/assets/scripts/'));
});

gulp.task('scriptspre', function() {
  return gulp.src([
                    'bower_components/modernizr/modernizr.js',
                    'project/assets/scripts/scriptspre.js'
                  ])
    .pipe(sourcemaps.init())
    .pipe(concat('pre.js'))
    .pipe(sourcemaps.write())
    .pipe(gulp.dest('build/assets/js/'));
});

gulp.task('scriptspost', function() {
  return gulp.src([
                    'bower_components/jquery/dist/jquery.js',
                    'bower_components/wow.js/dist/wow.js',
                    'project/assets/scripts/scriptspost.js',
                  ])
    .pipe(sourcemaps.init())
    .pipe(concat('post.js'))
    .pipe(sourcemaps.write())
    .pipe(gulp.dest('build/assets/js/'));
});

gulp.task('scripts',['coffee','scriptspre','scriptspost']);