30

I'd like to add source map support on a rails 3.2 application I am working on. As far as I know, generating source maps is not supported by Sprockets and from its github page it looks like the feature is planned for 4.0. I am working with Sprockets 2.2 and I think monkey patching is the only way to go. The module Processing under the main Sprockets module gives access to the js_compressor function which can be patched to generate source map for a single file. But, I don't know how to add this when the JS files combine. I am using Uglifier 2.4 as the compressor.

The project has a mixture of CoffeeScript, JS and EJS files. So, I think this is how sprockets would be compiling them together. First, it would convert Coffeescript and EJS to JS, then use the js_compressor to compress individual files and later concatenate them in groups. Now, as the source map for multiple files combined to the same file is a single file. So, I will need to change the compilation process somewhat and make the js_compressor run over the files, once the concatenation is finished. So, can anyone help out with this? Even explaining the sprockets compilation process and the modules used and functions involved would be of great help. I don't care about making source map files for the CoffeeScript code at present. Even mapping to their converted JS files would do.

Also, would like to add if there is some gem which can help with this it would be most welcome.

HaveNoDisplayName
  • 8,291
  • 106
  • 37
  • 47
erosenin
  • 1,052
  • 10
  • 22
  • Here is one gem that can be of help : https://github.com/markbates/coffee-rails-source-maps – dbKooper Jan 17 '15 at 16:44
  • I looked at this gem a long time back, since then I have written a custom solution for my need. The issue with that gem at that time was it didn't work with minified assets which is how the code is in production and from the github repo it seems it hasn't been updated in a year. – erosenin Jan 17 '15 at 16:48
  • great, then add your answer with monkey-patch link . – dbKooper Jan 17 '15 at 17:06
  • 1
    Yeah. Have been planning to do that for a long time now. It works but it's in a quite ugly state at this point. Will refactor it and post it here. – erosenin Jan 17 '15 at 18:04
  • Did you get round to making it available? – stephenmurdoch Apr 01 '15 at 11:10
  • 1
    People I know that are doing this do it by not using the asset pipeline at all and do a home-rolled setup, using grunt.js, or whatever. I've wanted this for a while but ultimately have never been willing to take the dive into home-rolled asset pipeline stuff. Good luck! – Alex Sharp May 05 '15 at 00:23
  • If I was you I would put my time into getting the application onto Rails 4 instead. Rails 3.2 is due to be EOL any day now. – max Aug 26 '15 at 17:36
  • @max Already moved the app to Rails 4. – erosenin Aug 27 '15 at 11:01

1 Answers1

10

Rails 4 does not have source maps either.

As far as I know, and as of today, this will only be part of rails 5.

A really nice approach to solve this right now is implemented in discourse by @SamSaffron and explained here: https://github.com/discourse/discourse/blob/master/lib/tasks/assets.rake

The gist, add a "before" task to the sprockets precompile process, and hack into the compilation process to generate the sourcemapped files and directives.

The nice thing in this approach is that you don't lose stuff from files that are both js and erb (*.js.erb) which is something quite common in rails.

I think that patching the whole sprockets pipeline is a bit of an abuse and risky.

JAR.JAR.beans
  • 9,668
  • 4
  • 45
  • 57