Rails config.assets.precompile setting to process all CSS and JS files in app/assets

后端 未结 8 1589
粉色の甜心
粉色の甜心 2020-11-28 06:42

I wish to precompile all the CSS and JS files in my project\'s app/assets folder. I do NOT want to precompile everything in vendor/assets or lib/assets, only th

8条回答
  •  再見小時候
    2020-11-28 07:03

    I'm revisiting this post at the year 2017.

    Our product is still heavily using RoR, we've been continuously modifying our precompile configurations by appending Rails.application.config.assets.precompile as we're adding new modules. Recently I was trying to optimize this by adding a regex pattern, I found that the following glob pattern works:

    Rails.application.config.assets.precompile += %w(**/bundle/*.js)
    

    However I still need to exclude certain modules, so I tried hard to use regex instead of glob.

    Until I looked into sprockets source code: https://github.com/rails/sprockets-rails/blob/master/lib/sprockets/railtie.rb#L108 , I found that they're already using regex:

    app.config.assets.precompile +=
      [LOOSE_APP_ASSETS, /(?:\/|\\|\A)application\.(css|js)$/]
    

    So I change my code into:

    Rails.application.config.assets.precompile +=
      [/^((?!my_excluded_module)\w)*\/bundle\/\w*\.js$/]
    

    Which works well.

提交回复
热议问题