rails / sprockets

Rack-based asset packaging system
MIT License
947 stars 788 forks source link

Sourcemap doesn't work by default #783

Open viraptor opened 1 year ago

viraptor commented 1 year ago

I was hoping to use the sourcemaps after upgrading to sprockets v4. Unfortunately they're not generated. Originally I was using uglifier, but switched to the recommended :terser option.

I'm running sprockets through rails 6.1 with config:

config.assets.js_compressor = :terser
config.assets.gzip = false
config.assets.compile = false
config.assets.digest = true
config.assets.debug = false

and running the assets:precompile job to trigger sprockets.

System configuration

Example App (Reproduction) - THIS IS IMPORTANT YOUR ISSUE LIKELY WILL NOT BE RESOLVED WITHOUT THIS

https://github.com/viraptor/sprockets-no-map-example/commits/master

3 commits:

After running RAILS_ENV=production bundle exec rails assets:precompile I get only these files - no maps generated:

$ find public/assets
public/assets
public/assets/application-04024382391bb910584145d8113cf35ef376b55d125bb4516cebeb14ce788597.css
public/assets/application-04024382391bb910584145d8113cf35ef376b55d125bb4516cebeb14ce788597.css.gz
public/assets/index-cdc242f925b3a102342a9e8f026cb6617436f4f17eb368f77b5e28622e4fafa8.js.gz
public/assets/manifest-04024382391bb910584145d8113cf35ef376b55d125bb4516cebeb14ce788597.js
public/assets/manifest-04024382391bb910584145d8113cf35ef376b55d125bb4516cebeb14ce788597.js.gz
public/assets/index-cdc242f925b3a102342a9e8f026cb6617436f4f17eb368f77b5e28622e4fafa8.js
public/assets/.sprockets-manifest-57569ca90afe16dad20155602c4f4bb0.json
ahorek commented 1 year ago

in development + config.assets.debug = true source maps should work, but they won't be exported.

autogeneration for production use was never implemented in sprockets. You have to use static sourcemaps or generate them on your own with some hackery like https://github.com/rails/sprockets/issues/410#issuecomment-270450004 (untested and outdated)...

viraptor commented 1 year ago

Thanks. This is kinda surprising and I was totally expecting it to work given the current documentation. I know it suggests that it's for development workflow, but you may want to spell that one out :⁠-⁠)

I'll check out the custom uglifier and report back if that's still valid for rails 6+

masciugo commented 1 year ago

I can confirm this behaviour in development with 4.2.0. It works with 4.1.1

viraptor commented 1 year ago

Back later, but the custom uglifier does not work OOTB in rails 6. It's beyond trivial changes so didn't spend too much time on it.

It would be nice if there was a simple way to do it in production automatically.

javier-menendez commented 6 months ago

Back later, but the custom uglifier does not work OOTB in rails 6. It's beyond trivial changes so didn't spend too much time on it.

It would be nice if there was a simple way to do it in production automatically.

In case it is still needed here is a gem to do that using terser sprockets_terser_with_source_maps