Skip to content

Struggling with esbuild + Propshaft + CDN, and source mapping URL #257

@Alexander-Blair

Description

@Alexander-Blair

I'm hoping that I'm missing something really obvious, and I have a workaround to ensure the sourceMappingURL appears in my final JavaScript assets, however I've tried lots of things that haven't worked.

My esbuild configuration boiled down to its most important settings:

const buildOptions = {
  entryPoints: ['app/javascript/admin.js', 'app/javascript/employee.js'],
  outdir: 'app/assets/builds',
  assetNames: '[name]-[hash].digested',
  bundle: true,
  sourcemap: true,
  loader: {
    '.js': 'jsx',
    '.jpeg': 'file',
    '.png': 'file',
    '.svg': 'file',
    '.ts': 'ts',
  },
  publicPath: `${process.env.ASSET_HOST}/assets`,
};

Rails config: config.asset_host = ENV.fetch('ASSET_HOST', nil), where ASSET_HOST would be set to something like https://assets.blobby.com

So esbuild would create a file in app/assets/builds with an absolute path for the source map e.g.

//# sourceMappingURL=https://assets.blobby.com/assets/admin.js.map

And it seems that one of 2 things is true:

  • Something is misconfigured on my end, causing the issue. I did notice that Rails mentions you don't actually need to prefix the http / https on the asset_host, although I did play with that and it made no difference
  • Propshaft can't currently convert a linked source map living at an absolute URL

Here's the solution I used to get it working in the end:

ciass Propshaft::Compiler::SourceMappingUrls
  def url_prefix
    if (host = Rails.configuration.asset_host)
      "#{host}#{config.prefix}"
    else
      super
    end
  end
end

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions