Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix sourcemap with path prefix when using esbuild with publicPath #501

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

vzaramel
Copy link

Esbuild puts the publicPath as a prefix in the sourceMappingURL and because I am using editor/editor to generate the script tag sprockets-rails generating the wrong path and removing the sourceMappingURL from the compiled file.

 <%= javascript_include_tag 'editor/editor', defer: true, integrity: true, crossorigin: "anonymous" %>
// build.js
esbuild
  .build({
    bundle: true,
    sourcemap: true,
    format: 'esm',
    outdir: path.join(process.cwd(), '../../app/assets/builds/editor'),
    publicPath: './editor',
    entryPoints: ['packs/editor.js'],
    ...
//  app/assets/builds/editor/editor.js
.
.
.
//# sourceMappingURL=editor/editor.js.map

This PR fixes it by only running the special case path when sourceMappingURL does not have a directory prefix.

@lcmen
Copy link

lcmen commented Feb 27, 2024

Hey @vzaramel, thanks for this PR!
Unfortunatelly, this code doesn't work for our case (publicPath set to /assets so static files can be correctly resolved when imported in JS files) as our sourcefile was application but sourcemap was /assets/sourcemap.js.map.

I took the inspiration from your code and fixed it (temporarily) like this:

Sprockets::Rails::SourcemappingUrlProcessor.class_eval do
  def self.combine_sourcemap_logical_path(sourcefile:, sourcemap:)
    if (parts = sourcefile.split('/')).many?
      parts[0..-2].append(sourcemap).join('/')
    elsif (parts = sourcemap.split('/')).many?
      parts.last
    else
      sourcemap
    end
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants