I'm on a Ruby on Rails project using ReactJS, so almost 100% Javascript (well, coffeescript + jsx).
I've made a very simple preprocessor for sprockets to replace $imagePath("xxx") to the real asset path.
It looks like this:
module JSAssetPathPreprocessor
@in_processing = {}
class << self
def call(input)
# For some reason we can have an infinite loop here...
unless @in_processing[input[:filename]]
@in_processing[input[:filename]] = true
out = input[:data].gsub(/\$imagePath\(['"](.*?)['"]\)/){ "'#{ActionController::Base.helpers.image_path($1)}'" }
@in_processing.delete(input[:filename])
return { data: out }
else
return { data: input[:data] }
end
end
end
end
Then I register it in initializers
Sprockets.register_preprocessor('application/javascript', JSAssetPathPreprocessor)
And tada! It's working !
(coffeescript + reactJS)
# ...
<Image src=$imagePath('banners/blocks/01.jpg') />
# ...
Output:
<img src="/assets/banners/blocks/01-69a091fe87763b439602a274a03fb63e5357deed56802a55d1547cd67091bd18.jpg">
Except once I deploy in production, the path is not the good one:
<img src="/images/banners/blocks/01.jpg">
I'm using capistrano and do rake tmp:cache:clear assets:clobber assets:precompile
.
I've no clues about what's wrong, doing all of this in local works well, I checked my generated application.js. But not on my server.
I even tested with RAILS_ENV=production in local... And it works T_T.
The assets are regenerated well in production (eg. if I add console.log("xxx") it will display on next release, so no caching problem) but the path still remain wrong.
Any clues or ideas on where I can check ? I guess my preprocessor sucks but I get hard time finding documentation for this version of sprockets.
Useful gems version.
- rails (= 5.0.0.beta3)
- sprockets (3.6.0)
- browserify + react-rails + sprockets-coffee-react
Aucun commentaire:
Enregistrer un commentaire