wearefractal/gulp-coffee — pull request #24 by superstructor (2014-04-15 07:34:17+00:00)
Title: fixed sourceRoot option for sourcemaps

Pull request #21 is actually seriously broken for all but the extremely trivial case of not using any sub-directories in your CoffeeScript source directory.

As [described here](https://github.com/jashkenas/coffee-script/issues/3075#issue-16981090) `sourceRoot` discards any path information after the root directory.

In other words without this fix consider if `sourceRoot` is `../coffee/`, `gulp.dest` is `js/` and the file you are compiling happens to be `coffee/lib/util.coffee`. The result will be:

```
"sourceRoot": "../coffee/",
"sources": [
"utils.coffee"
]
```

Instead we need to compute the relative path from the **real** destination path back to source on a **per-file basis**. It must be on a per-file basis because different files may reside at different locations or nesting levels within the folder structure.

Once you realize this the issue is then that you do not have the **real** destination at the point the coffee plugin is called in the gulp stream; i.e. The argument passed to `gulp.dest()` is not available at that point. So I have created a new option that has exactly the same semantics of `gulp.dest()` called `sourceDest`. `sourceDest` is then used to correctly calculate the relative path to `sourceRoot` on a per-file basis.

This fix results in the previous example being correct:

```
"sourceRoot": "../coffee/lib",
"sources": [
"utils.coffee"
]
```

In practice this has been tested with a non-trivial CoffeeScript source directory with many different levels and directories. It means that source maps can actually work simply by giving advance notice to the gulp-coffee plugin of what you will pass to `gulp.dest()`.

While this is a breaking change for any existing user of the recently added `sourceRoot` option I hope this explanation illustrates that the current semantics of the `sourceRoot` option are completely broken and cannot be fixed without this breaking change.

Thanks!