How do I use config.assets.precompile for directories rather than single files?

前端 未结 5 1668
情书的邮戳
情书的邮戳 2021-01-01 18:34

How do I use config.assets.precompile in production to only include the files in \'lib/assets/javascripts\', \'lib/assets/stylesheets\', \'vendor/assets/javascripts\' and \'

相关标签:
5条回答
  • 2021-01-01 19:00

    You can simply write it like this:

    config.assets.precompile += ['directory/*']
    
    0 讨论(0)
  • 2021-01-01 19:03

    As of Sprockets 3 you can use a manifest.js file to declare which files or directories are precompiled. See upgrade docs. So in your config you would add:

    config.assets.precompile = %w(manifest.js)
    

    Then in app/assets/config/manifest.js you can have

    //= link_directory ../pages .js
    

    Use link_tree if you want js files in nested sub-directories to be precompiled too.

    0 讨论(0)
  • 2021-01-01 19:11

    I found this link, and think may be it helpful for you, please see

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

    I think you can do like following.

    # In production.rb
    config.assets.precompile << Proc.new { |path|
      if path =~ /\.(css|js)\z/
        full_path = Rails.application.assets.resolve(path).to_path
        app_assets_path = Rails.root.join('app', 'assets').to_path
        if full_path.starts_with? app_assets_path
          puts "excluding asset: " + full_path
          false
        else
          puts "including asset: " + full_path
          true
        end
      else
        false
      end
    }
    
    0 讨论(0)
  • 2021-01-01 19:16

    It's probably a bit better to include this as an asset path(as per your example solution it would be):

    config.assets.paths << Rails.root.join('app', 'assets', 'javascripts', 'pages')
    

    It also allows you to include paths not in the assets directory (for example with including the bootstrap-sass). These paths are then added to your assets folder in your public directory, and pushed to your asset location if using something like asset_sync.

    0 讨论(0)
  • 2021-01-01 19:18

    The point of compiling assets is to build one (or a small number of) files to minimize the number of HTTP requests from the browser.

    If you're going to serve each file individually, then why not just disable precompile?

    To use precompile as intended, build an entire directory into one file using Sprockets' require_directory:

    //= require_directory ./awesome_js_app
    

    ...and list that file in your config.assets.precompile array.

    By default, all CSS is built into application.css & JS into application.js. You can add more top-level files to compile with the precompile directive in config/environments/production.rb (and other envs if you wish.) For example:

    config.assets.precompile += %w( public.css public.js )
    

    Then the Sprockets //= require ... directives in those top-level files will determine the composition of final compiled file.

    You can use these additional top-level files in your layouts to have different CSS & JS for certain views.

    0 讨论(0)
提交回复
热议问题