Skip to content

Instantly share code, notes, and snippets.

@thiagomata
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thiagomata/1f6a7c3b47bcb7c16399 to your computer and use it in GitHub Desktop.
Save thiagomata/1f6a7c3b47bcb7c16399 to your computer and use it in GitHub Desktop.
A workaround to be able to create the maps files into a separate file of the javascript files from the coffescript files what are into a tree structure.
project
src
coffee
foo.coffee
component
bar.coffee
animals
dog.coffee
cat.coffee
public
scripts
foo.js
maps
foo.js.map
component
bar.js
maps
bar.js.map
animals
dog.js
cat.js
maps
dog.js.map
cat.js.map
var gulp = require('gulp');
var $plugin = require('gulp-load-plugins')({ camelize: true});
var gutil = require('gutil');
var glob = require('glob');
var path = require('path');
/**
* Project Paths
*/
glob.paths = {
publicScript: './public/scripts/',
srcCoffee: './src/coffee/'
};
/**
* Create the javascript and map files from the coffeescripts
*/
gulp.task('coffee-map', function( callback ) {
// // this what we want to do, but doesn't work as excepted //
// gulp.src(glob.paths.srcCoffee + '**/*.coffee')
// .pipe($plugin.sourcemaps.init())
// .pipe($plugin.coffee({bare: true}).on('error', gutil.log))
// .pipe($plugin.sourcemaps.write( './maps/'))
// .pipe(gulp.dest(glob.paths.publicScript));
/**
* @workaround This is a not so easy or fast way to compile the coffee
* scripts and create the maps but it was necessary to the current version
* @todo use the tradicional way when fixed
* @link https://github.com/gulpjs/gulp/issues/356
*/
var coffeeFiles = glob.sync( glob.paths.srcCoffee + '**/*.coffee');
var intCounter = 0;
coffeeFiles.forEach(function(fullPathFileName){
var srcDir = path.dirname(fullPathFileName);
var srcRelativeDir = srcDir.substr(glob.paths.srcCoffee.length);
var destDir = glob.paths.publicScript + srcRelativeDir + "/";
var answer = gulp.src(fullPathFileName)
.pipe($plugin.sourcemaps.init())
.pipe($plugin.coffee({bare: true}).on('error', gutil.log))
.pipe($plugin.sourcemaps.write( './maps/'))
.pipe(gulp.dest(destDir));
answer.on('end',function(){
intCounter++;
if( intCounter >= coffeeFiles.length ) {
callback();
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment