-
Notifications
You must be signed in to change notification settings - Fork 144
-
Notifications
You must be signed in to change notification settings - Fork 144
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
Esbuild Configuration for Javascript Debugging #40
Comments
For more context. The compiled assets can be found in The bundled JS that is actually loaded is http://localhost:3000/assets/application.debug-fc91062bf3c7540aaa82649048a601114b102e175ab6b54a445960fb29a8ebc2.js. but I'm also seeing this warning: |
Source mapping is still not fully supported. Need alterations to Sprockets. |
While sprockets is not compatible, you can setup a custom route like so: if Rails.env.development?
redirector = ->(params) { ApplicationController.helpers.asset_path(params[:name].split("-").first.append(".map")) }
constraint = ->(request) { request.path.ends_with?(".map") }
get "assets/*name", to: redirect(redirector), constraints: constraint
end If the assets pipeline cannot find a file corresponding to the a given sourcemap file name, Rails' routing will pick up any URL starting with Works well for us with |
I had to slightly modify the above example for my Rails 6.1.4.1 app, but it does work: if Rails.env.development?
redirector = lambda { |params, _req|
ApplicationController.helpers.asset_path(params[:name].split('-').first + '.map')
}
constraint = ->(request) { request.path.ends_with?('.map') }
get 'assets/*name', to: redirect(redirector), constraints: constraint
end I now have full sourcemaps working for now. |
Figured I'd share my esbuild file as I've been working on it for a few weeks now and had to dig for a lot of this myself. The inline sourcemaps are not ideal for production but they work for now. Also svgr, loader, injecting shims and adding watch was a bit tricker than I hoped for but well worth it so far. Hopefully this helps someone. #!/usr/bin/env node
const esBuild = require('esbuild')
const svgrPlugin = require('esbuild-plugin-svgr')
const watch = process.argv.includes('--watch')
const minify = process.argv.includes('--minify')
const watchOptions = {
onRebuild: (error, result) => {
if (error) {
console.error('watch build failed:', error)
} else {
console.log(result)
console.log('watch build succeeded. ')
}
}
}
const svgrOptions = {
typescript: false
}
const loaders = {
'.png': 'file'
}
esBuild.build({
entryPoints: ['app/javascript/application.js'],
logLevel: 'info',
bundle: true,
outdir: 'app/assets/builds',
plugins: [
svgrPlugin(svgrOptions)
],
watch: watch && watchOptions,
sourcemap: 'inline',
loader: loaders,
publicPath: '/assets',
inject: ['.esbuild/shims/react-shim.js'],
minify
}).then(result => {
console.log(result)
if (watch) {
console.log('Build finished, watching for changes...')
} else {
console.log('Build finished, Congrats')
}
}).catch(result => {
console.log(result)
process.exit(1)
}) |
@dhh Do you know if there is an issue tracked somewhere that lays out what needs to change with sprockets? I went looking and found nothing that matches directly. |
Don't think we have anything fully recorded. We need to find a way to produce source maps with digested file names that can stay stable. Maybe @brenogazzola has an idea? There's a straight shot for this for webpack. |
@dhh I'll update the regex in Propshaft since it currently does not support multiple file extensions after I haven't switched to esbuild yet, but I've taken a look through its documentation and I think that
And here's the webpack equivalent:
If this configuration works for esbuild, and the generated source map ends as something like |
Sourcemaps generated by esbuild (or any other transpiler) are now supported since sprockets-rails 3.4.0. |
I switched to I've worked around this by removing the |
Can you try to produce an example app that triggers this? It's working for me when I try. |
FWIW, source maps via esbuild -> Sprockets working for me as well. 👍 |
Just hopping in here to say that setting with config.assets.debug set to true, the AddSourceMapCommentToAssetProcessor ( https://github.com/rails/sprockets/blob/24c97270fbf6de11b4ffff0311bb427b7a8a3a83/lib/sprockets/add_source_map_comment_to_asset_processor.rb) processor in sprockets appends a second, incorrect source map comment |
Setting |
@dhh I think this is still an issue, but it's intermittent. Just randomly stopped working after I made a change to a stimulus controller. Tried the The map files are generated each time but the browser seems to be looking for the wrong file. UPDATE: So it seems that
|
I was having the same problem with migrating from webpacker to jsbundling-rails with esbuild, every time I changed a js file the css file lost the styles. Then I noticed that some js files were doing:
I removed all of them from the js files and it seems it solved my problem, I don't know if it has anything to do with this situation. |
@danieldocki yeah that's a known issue. see: https://github.com/rails/jsbundling-rails#why-does-esbuild-overwrite-my-applicationcss |
I'm currently in the middle of migrating a After checking the following:
My sprockets version is If someone can explain why this is happening or guide me in the right direction to be able to have debug = true it would be awesome, ty! |
@nachoal, Can't guarantee help but if you could share your esbuild script/config as well as any other relevant asset configuration that would help to diagnose your problem I'll take a look. |
Thanks @jhirn
Having |
Had to modify this approach a little bit, here's the one that's staying: if Rails.env.development?
redirector = ->(params, _) { ApplicationController.helpers.asset_path("#{params[:name].split('-').first}.map") }
constraint = ->(request) { request.path.ends_with?(".map") }
get "assets/*name", to: redirect(redirector), constraints: constraint
end |
Ah I see. I only ever got inline to work. Will give this a shot if work resumes on that project. Despite the bit of yak shaving, esbuild is so freaking awesome. If webpacke(r) were a thing, I'd burn it in a public event. |
I've spent few hours struggling with this. Setting Then I set At the end I added suggested #40 (comment) to |
For some reason Sprockets 4.2.0 breaks this for me.
I'm not sure which change exactly affects this. I downgraded back to 4.1.1 to get sourcemaps working again. |
I'm getting this |
Sprockets 4.2.0 also broke sourcemaps for me (esbuild + Rails 7.0.4.1). @dhh? |
Has anyone found a fix for this? |
sourcemap is working (Rails 7.0.3.1) however the console show the wrong stimulus controller name, the logged line is correct, only the file name is not This issue seems to happen only on chrome and brave browsers, I tested in firefox and it's working fine |
@dhh This seems to have recently broken for @salimhb , @navidemad, @kkurcz and myself (at least). |
Here's what worked for me on Rails 7.0.4.3, since the redirection route approach didn't work. app/middleware/sourcemap_redirect_middleware.rb class SourcemapRedirectMiddleware
def initialize(app)
@app = app
end
def call(env)
if env['PATH_INFO'].match?(/.*\.(js|css)-.*\.map/)
asset_name = env['PATH_INFO'].split('-').first
new_path = ActionController::Base.helpers.asset_path("#{asset_name}.map")
# Preventing redirection loop
if env['PATH_INFO'] != new_path
return [301, { 'Location' => new_path }, []]
end
end
@app.call(env)
end
end have your development.rb configured like this: require_relative '../../app/middleware/sourcemap_redirect_middleware'
config.middleware.use SourcemapRedirectMiddleware
config.assets.debug = true Hope it works for you guys! |
@alexandrepalma's solution worked for me with a small tweak. I had to replace the line
with
to handle cases where the path has multiple hyphens. |
Worked for us, too... but found out that we needed a backtrace silencer to preserve stack traces:
|
Hello guys, any update regarding this issue ? |
up |
Is this fixed with Rails 7.1.2? |
@gap777 if you find a definitive solution please let me know |
Still an issue |
Migrated from Webpacker to Esbuild using jsbundling-rails and it seems like Javascript debugging no longer works for either VSCode nor Rubymine.
This is the ESBuild settings I am using.
The text was updated successfully, but these errors were encountered: