jsbundling-rails: Sprockets breaks the sourcemap comment

I’m using esbuild for js bundling, command:

esbuild app/assets/javascripts/*.* --bundle --outdir=app/assets/builds --sourcemap

Then sourcemap comment is added to builds/application.js:

//# sourceMappingURL=application.js.map

But after process by assets pipeline, it becomes:

//# sourceMappingURL=application.js.map;

It add a ; at the end and break the sourcemap config.

I found it’s caused by https://github.com/rails/sprockets/blob/cddf9fb841eece80276a1ccaee1e018a356547a0/lib/sprockets/utils.rb#L100 , and there is a related fixed. https://github.com/rails/sprockets/pull/515

But looks like this fix not work for js bundle from external.

I don’t know the internal of asset pipeline very well. How can I make the sourcemap from esbuild work?

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 15 (5 by maintainers)

Most upvoted comments

A basic version of Sprockets that just gives us a load path, digesting, copying, and compression would be all that’s needed for this new approach.

@dhh We are facing this issue with Webpack as well. In your last comment, you mentioned that Webpack does make something possible but I’m not sure if you meant that there is a way to overcome this issue when using Webpack. If this is documented somewhere, could you please direct me to that?

In our setup, Webpack generates the following files to app/assets/builds/:

app.js
app.js-2900f7bc66bb9248004610a2a6aaad98.digested.map

After asset pipeline process, we having the following in public/assets/

app-c030ba35ee6a76754b09871773215453129630da92e84c4ffb304c4e22f54f6b.js
app.js-2900f7bc66bb9248004610a2a6aaad98.digested.map

When I inspect the file the final file: app-c030ba35ee6a76754b09871773215453129630da92e84c4ffb304c4e22f54f6b.js

...
//# sourceMappingURL=app.js-2900f7bc66bb9248004610a2a6aaad98.digested.map;

Sure, I create a demo here: https://github.com/chloerei/rails-esbuild-demo .

bin/setup to setup and bin/dev to start service.

It generate sourcemap for js and css, css map is working but js not.

I also found I need to set config.assets.debug = false in config/environments/development.rb, otherwise it add digest in sourcemap path, both css and js map file can not found:

//# sourceMappingURL=application.js.map;

//# sourceMappingURL=application.js-2ff448f42b4456f23c978070fce3c2639ccb5652106b9af890dfb697c39e2415.map

For anyone else struggling like me, looks like this PR holds the key: https://github.com/rails/sprockets-rails/pull/515

Working on a path to a dramatically simpler sprockets. With our new path, we don’t need 80% of what’s currently in Sprockets.