My initial query was similar to this one here, but it led me to delve deeper into research and explore a new approach.
Essentially, I am attempting to consolidate all my .js
and .coffee
files within a single gulp.src()
object and execute relevant tasks based on the file extension.
What started as an exploration with gulp-if has evolved into utilizing gulp-filter, which I find preferable. However, I am currently facing challenges in harmonizing this setup with gulp-sourcemaps. It appears that the current task is overriding others; nevertheless, my ultimate goal is to concatenate everything into one file, source-map it into another, and have each file extension trigger its respective tasks. The commented-out uglify task continues to cause issues without providing much guidance. Additionally, when the filters are functioning and a CoffeeScript error occurs, I observe that coffeescriptlint()
executes its duty, but then my watch command remains unresponsive.
It seems like I might be heading towards extracting each sourcemap using a tool like gulp-extract-sourcemap, although I am unsure if that is the correct approach.
Usually, I would separate the JS and Coffeescript tasks; however, due to numerous intertwined aspects between the two, combining them using a simple filter seemed rational—especially since I am grappling with sourcemaps for both.
I believe I am close to resolving this issue, so any helpful nudges in the right direction would be highly appreciated. I have included the current gulpfile.js and package.json if you wish to test it out. Thank you!
Gulpfile.js
// Load plugins
var gulp = require('gulp'),
jshint = require('gulp-jshint'),
coffee = require('gulp-coffee'),
changed = require('gulp-changed'),
coffeelint = require('gulp-coffeelint'),
uglify = require('gulp-uglify'),
sourcemaps = require('gulp-sourcemaps'),
notify = require('gulp-notify'),
concat = require('gulp-concat'),
filesize = require('gulp-size'),
livereload = require('gulp-livereload'),
duration = require('gulp-duration'),
gutil = require('gulp-util'),
gFilter = require('gulp-filter');
gulp.task('scripts', function() {
var jsBuildDir = 'assets/js/build/',
jsFilter = gFilter('**/*.js'),
coffeeFilter = gFilter('**/*.coffee');
return gulp.src([
'assets/js/src/_init.coffee',
'assets/js/src/_init.js'
])
.pipe(coffeeFilter)
.pipe(coffeelint().on('error', gutil.log))
.pipe(coffeelint.reporter())
.pipe(sourcemaps.init())
.pipe(coffee({bare: true}).on('error', gutil.log))
.pipe(sourcemaps.write('../../maps'))
.pipe(coffeeFilter.restore())
.pipe(jsFilter)
.pipe(jshint({
'boss': true,
'sub': true,
'evil': true,
'browser': true,
'globals': {
'module': false,
'require': true
}
}),
jshint.reporter('jshint-stylish'))
.pipe(jsFilter.restore())
.pipe(concat('scripts.min.js'))
//.pipe(uglify())
.pipe(filesize({
title: 'Scripts:'
}))
.pipe(gulp.dest(jsBuildDir))
.pipe(duration('building script files'))
.pipe(notify({ message: 'Coffeescript task complete' }));
});
// Default task
gulp.task('default', ['scripts']);
// Watch
gulp.task('watch', function() {
gulp.watch(['assets/js/src/**/*.js', 'assets/js/src/**/*.coffee'], ['scripts']);
// Create LiveReload server
var server = livereload();
// Watch files in patterns below, reload on change
gulp.watch(['assets/js/build/*']).on('change', function(file) {
server.changed(file.path);
});
});
Package.json
{
"devDependencies": {
"gulp": "^3.8.8",
"gulp-changed": "^1.0.0",
"gulp-coffee": "^2.2.0",
"gulp-coffeelint": "^0.4.0",
"gulp-concat": "^2.4.0",
"gulp-duration": "0.0.0",
"gulp-filter": "^1.0.2",
"gulp-jshint": "^1.8.4",
"gulp-livereload": "^2.1.1",
"gulp-notify": "^1.6.0",
"gulp-size": "^1.1.0",
"gulp-sourcemaps": "^1.2.2",
"gulp-uglify": "^1.0.1",
"gulp-util": "^3.0.1",
"jshint-stylish": "^0.4.0"
}
}