sprockets-rails icon indicating copy to clipboard operation
sprockets-rails copied to clipboard

Fix broken sourcemaps when using `esbuild --public-path`

Open richardkmiller opened this issue 2 years ago • 14 comments
trafficstars

When the --public-path option was added to esbuild it broke Sprockets's ability to resolve sourcemaps, even though they're still present.

Esbuild's sourcemap output with --public-path=assets: //# sourceMappingURL=assets/application.js.map

Esbuild's sourcemap output without --public-path: //# sourceMappingURL=application.js.map

It's desirable to keep --public-path=assets for properly resolving static assets in JavaScript files, but the leading directory in sourceMappingURL is undesirable, or at least it does not work with Sprockets.

This PR allows sourceMappingURL= to contain a leading, optional directory which is ignored as the path is resolved.

This is an alternative to #501. (Sorry, I didn't see it until after I found this solution.)

richardkmiller avatar Jan 18 '23 00:01 richardkmiller

Are we still planning on merging this? Should we fix errors from tests?

capripot avatar May 12 '23 23:05 capripot

@dhh could you take a look please?

Thanks

guillaumebriday-pa avatar Jun 13 '23 16:06 guillaumebriday-pa

Thanks for looking into this. Needs testing to verify behavior.

dhh avatar Jun 18 '23 12:06 dhh

I am also running into this as well with esbuild, I have to remove the --public-path option from esbuild to get sourcemaps to work, otherwise sprockets-rails seems to remove the sourcemap comment that esbuild creates.

joevandyk avatar Jan 24 '24 02:01 joevandyk

would the propshaft fix at https://github.com/rails/propshaft/pull/170 be applicable here?

joevandyk avatar Jan 24 '24 02:01 joevandyk

@richardkmiller would you like me to write test cases and maybe we can get this merged?

nicklozon avatar Feb 23 '24 17:02 nicklozon

@nicklozon Yes, that'd be great. Thanks for asking!

richardkmiller avatar Feb 24 '24 01:02 richardkmiller

@richardkmiller I wrote a test and opened a PR against your repo: https://github.com/richardkmiller/sprockets-rails/pull/1

I think the change makes sense and works - sprockets-rails assumes the map file will exists in the same directory as the source file so it stands to reason that any prefixed path should be removed from the sourceMappingUrl.

nicklozon avatar Mar 05 '24 16:03 nicklozon

@nicklozon Thank you for these tests! I've merged them just now.

richardkmiller avatar Mar 15 '24 21:03 richardkmiller

Would love to see this merged! Can confirm this is resolving some issues on my end.

jroes avatar Apr 11 '24 03:04 jroes

Any ideas on when this would possibly get merged [and released] ?

zacheryph avatar May 02 '24 20:05 zacheryph

Clearly not a great option but I managed to hack something together with a esbuild plugin that I use only in dev mode for now:

const pathSourceMapsPlugin = {
  name: "pathSourceMaps",
  setup({ onStart, onEnd, initialOptions }) {
    initialOptions.write = false

    onEnd(async (result) => {
      const tasks = []
      for (let { path, contents, text } of result.outputFiles) {
        if (path.match(/application.js/)) {
          const newValue = text.replace(
            /\/\/# sourceMappingURL=\/assets\/application.js.map/g,
            "//# sourceMappingURL=application.js.map",
          )
          contents = new TextEncoder().encode(newValue)
        }
        tasks.push(promises.writeFile(path, contents))
      }

      await Promise.all(tasks)
    })
  },
}

It's basically hardcoding the sourcemap path, but at least I get the sourcemaps 😅

qmenoret avatar Aug 19 '24 08:08 qmenoret

cc @rafaelfranca, do you or anyone on the team have some bandwidth to verify this? Sprockets is a bit of an unruly beast at the moment, and the criticality is high. We don't use Sprockets in active development any more (moved to Propshaft), so I'd feel better if someone who does signs off on this.

dhh avatar Aug 19 '24 16:08 dhh

Appreciate the work everyone is doing on rails, just wanted to chime in with another vote on this one. Have spent the last couple days trying to work out why I couldn't upload sourcemaps generated with esbuild and then processed by the rails asset pipeline to datadog, and it looks like this issue is at least a part of the problem.

fredrivett avatar Oct 06 '24 17:10 fredrivett