sprockets icon indicating copy to clipboard operation
sprockets copied to clipboard

single //= directive at the beginnig of the file breaks sourcemaps

Open rottame opened this issue 5 years ago • 1 comments

source maps are not aligned to the compiled javascripts when //= directives are used on js files

Expected behavior

Source maps should be aligned with actual compiled javascripts

Actual behavior

when js source files begins with a single //= directive, the header is replaced with a blank string (lib/sprockets/directive_processor.rb:157-159)

with multiple //= directives, the header is replaced with the correct number of blank lines

when 100s of files are concatenated, source maps are off by several lines, pointing to to wrong line

this is more evident on minified js where sourcemaps are useless, pointing to the wrong source file

System configuration

  • 4.0.0 beta8, beta10 and master
  • 2.2.x and 2.5.x
  • rails from 4.1.x to 5.2.x

Example App

https://github.com/rottame/sprockets-sourcemap-bug.git

no compression, //=link application.debug.js in manifest

all 5 source files are similar --- cut --- //= require test1.js let application_js = null; --- cut ---

the generated application.js is: ---- cut here --- let test4_js = null; let test3_js = null; let test2_js = null; let test1_js = null; let application_js = null; --- cut here ---

the source map: offset for application.js is 7 lines the offset between test4.js.erb and test3.js is 2 lines the offset between test3.js and test2.js, and between test2.js and test1.js.erb is 1 line the offset between test1.js.erb and application.js is 2 lines

the mappings for application.js, test2.js and test3.js are "AAAA" the mappings for test1.js.eb and test4.js.erb are "AAAA,AACA"

mappings for all files should be 'AAAA,AACA' with an offset of 2 between files the concatenated javascript should maintain the empty lines between files

i'm working with a forked copy of sprockets the commit https://github.com/rottame/sprockets/commit/b3f3fc543098cbe944c30d378bf406da83baf3f8 fixes the problem for me, but breaks almost half tests and I'm not sure if is the correct way of fixing the problem

rottame avatar Sep 06 '19 22:09 rottame

I confirm that since version 4.0.0 of sprockets, a single line directive is breaking the source maps and the javascript debugging system by consequence.

I've been using some rails-assets gems in my project, and every gem has a single JS file that contains one line directive with a line break afterwards but this is breaking everything in my debugging session.

I've been able to find three alternatives ahead of the bugfix :

  • requiring directly the final JS file from the application.js file (although not following the standard setup from rails-assets)
  • putting a line break before the directive in the Gem (not suitable because I don't own the gem)
    1.  
    2.  //= directive
    
  • putting 2 line breaks after the directive in the Gem (not suitable because I don't own the gem)
    1.  //= directive
    2.  
    3.  
    

I would be grateful if you could fix this issue.

rgisiger avatar Oct 12 '20 15:10 rgisiger