Class: Brut::CLI::Apps::BuildAssets::Css

Inherits:
BaseCommand show all
Defined in:
lib/brut/cli/apps/build_assets.rb

Instance Attribute Summary

Attributes inherited from Commands::BaseCommand

#parent_command

Instance Method Summary collapse

Methods inherited from BaseCommand

#bootstrap?, #default_rack_env, #friendly_name, #opts

Methods inherited from Commands::BaseCommand

#accepts, #args_description, #argv, #bootstrap?, #commands, #default_rack_env, #delegate_to_command, #env, #env_vars, #execute, #name, #options, #opts, #puts, #stdin, #system!, #theme

Instance Method Details

#descriptionObject



91
# File 'lib/brut/cli/apps/build_assets.rb', line 91

def description = "Builds a single CSS file suitable for sending to the browser"

#detailed_descriptionObject



93
94
95
96
97
# File 'lib/brut/cli/apps/build_assets.rb', line 93

def detailed_description = %{
  This produces a hashed file in every environment, in order to keep environments consistent and reduce differences.  If your CSS file references images, fonts, or other assets via `url()` or other CSS functions, those files will be hashed and copied into the output directory where CSS is served.

  To ensure this happens correctly, your `url()` or other function must reference the file as a relative file from where your actual source CSS file is located. For example, a font named `some-font.ttf` would be in `app/src/front_end/fonts`. To reference this from `app/src/front_end/css/index.css` you'd use `url("../fonts/some-font.ttf")`
}

#runObject



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/brut/cli/apps/build_assets.rb', line 99

def run
  css_bundle          = Brut.container.css_bundle_output_dir / "styles.css"
  css_bundle_source   = Brut.container.front_end_src_dir / "css" / "index.css"
  esbuild_metafile    = Brut.container.tmp_dir / "build-css-meta.json"
   = Brut.container.

  if options.clean?(default: options.env != "production")
    puts "Cleaning old CSS files from #{theme.code.render(friendly_name(Brut.container.css_bundle_output_dir.to_s))}"
    Dir[Brut.container.css_bundle_output_dir / "*.*"].each do |file|
      if File.file?(file)
        puts theme.weak.render("  Deleting #{theme.code.render(friendly_name(file))}")
        FileUtils.rm(file)
      end
    end
  end

  # NOTE: esbuild outputs its normal messages on stderr which is fucking stupid
  command = "npx esbuild --loader:.ttf=copy --loader:.otf=copy --metafile=#{esbuild_metafile} --entry-names=[name]-[hash] --sourcemap --bundle #{css_bundle_source} --outfile=#{css_bundle} 2>&1"
  puts "Building CSS bundle '#{theme.code.render(friendly_name(css_bundle))}'"

  system!(command)

  if !File.exist?(esbuild_metafile)
    error "'#{esbuild_metafile}' was not generated"
    puts theme.error.render("esbuild did not generate the metafile we asked for (#{esbuild_metafile})")
    puts theme.error.render("This file is required to continue")
    return 1
  end

   = Brut::FrontEnd::AssetMetadata.new(asset_metadata_file:,logger:execution_context.logger)
  .merge!(extension: ".css", esbuild_metafile:)
  .save!
  0
end