Class: SparkEngine::Assets::Stylesheets

Inherits:
AssetType
  • Object
show all
Defined in:
lib/spark_engine/plugin/assets/stylesheets.rb

Instance Attribute Summary

Attributes inherited from AssetType

#base, #plugin

Instance Method Summary collapse

Methods inherited from AssetType

#build_failure, #build_success, #change, #compress, #destination, #file_event, #filter_files, #find_files, #find_node_module, #initialize, #local_path, #log_error, #log_success, #npm_command, #pathname, #url, #urls, #watch

Constructor Details

This class inherits a constructor from SparkEngine::Assets::AssetType

Instance Method Details

#asset_tag(*args) ⇒ Object



25
26
27
# File 'lib/spark_engine/plugin/assets/stylesheets.rb', line 25

def asset_tag(*args)
  stylesheet_link_tag(args)
end

#autoprefixer_configObject



14
15
16
17
18
19
20
21
22
23
# File 'lib/spark_engine/plugin/assets/stylesheets.rb', line 14

def autoprefixer_config
  @autoprefixer_config ||= begin
    config_path = File.join( SparkEngine.plugin.root, 'config', 'autoprefixer.yml' )
    if File.exist?( config_path )
      YAML.load_file( config_path ).deep_symbolize_keys
    else
      {}
    end
  end
end

#build(ext = nil) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/spark_engine/plugin/assets/stylesheets.rb', line 29

def build(ext=nil)
  files = find_files
  files = files.reject {|f| !f.match(/\.#{ext}/) } if ext

  files.each do |file|

    begin
      if File.extname(file).match(/\.css/)
        build_css(file)
      elsif File.extname(file).match(/\.s[ca]ss/)
        build_sass(file)
      end

      puts build_success(file)

    rescue Exception => e
      build_failure file

      if e.backtrace.is_a? Array
        log_error "Error in file: #{local_path(e.backtrace[0])}"
      end

    log_error "  #{e.message}\n" if e.message
    end
  end
end

#build_css(file) ⇒ Object



56
57
58
59
60
61
# File 'lib/spark_engine/plugin/assets/stylesheets.rb', line 56

def build_css(file)
  css = prefix_css( File.read(file) )
  File.open(destination(file), 'w') { |io| io.write(css) }

  compress(destination(file))
end

#build_sass(file) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/spark_engine/plugin/assets/stylesheets.rb', line 87

def build_sass(file)
  css = prefix_css begin
    render_sassc(file)
  rescue LoadError => e
    render_sass(file)
  end

  dest = destination(file)

  File.open(dest, 'w') { |io| io.write(css) }
  compress(dest)
end

#dataObject



112
113
114
115
116
117
118
119
120
# File 'lib/spark_engine/plugin/assets/stylesheets.rb', line 112

def data
  return @data if @data && SparkEngine.production?

  @data = Dir[File.join(base, "**/*.yml")].each_with_object({}) do |file, data|
    key = File.basename(file, '.*').sub(/^_/,'')
    data[key] = SassYaml.new(file: file).to_yaml
    data
  end
end

#extObject



10
11
12
# File 'lib/spark_engine/plugin/assets/stylesheets.rb', line 10

def ext
  "*[ca]ss"
end

#load_pathsObject



100
101
102
# File 'lib/spark_engine/plugin/assets/stylesheets.rb', line 100

def load_paths
  [SparkEngine.plugin.paths[:stylesheets], SparkEngine.plugin.paths[:components]]
end

#prefix_css(css) ⇒ Object



104
105
106
107
108
109
110
# File 'lib/spark_engine/plugin/assets/stylesheets.rb', line 104

def prefix_css(css)
  if defined? AutoprefixerRails
    AutoprefixerRails.process(css, autoprefixer_config).css
  else
    css
  end
end

#render_sass(file) ⇒ Object



63
64
65
66
67
68
# File 'lib/spark_engine/plugin/assets/stylesheets.rb', line 63

def render_sass(file)
  require "spark_engine/sass/engine.rb"

  Sass.logger.log_level = :error if SparkEngine.production?
  Sass.compile_file(file, style: sass_style)
end

#render_sassc(file) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/spark_engine/plugin/assets/stylesheets.rb', line 70

def render_sassc(file)
  require "spark_engine/sassc/importer"

  source = File.open(file, 'rb') { |f| f.read }
  options = {
    importer: SassC::SparkEngine::Importer,
    load_paths: load_paths,
    style: sass_style
  }

  SassC::Engine.new(source, options).render
end

#sass_styleObject



83
84
85
# File 'lib/spark_engine/plugin/assets/stylesheets.rb', line 83

def sass_style
  SparkEngine.production? ? "compressed" : 'nested'
end

#versioned(file) ⇒ Object

Convert extension



123
124
125
# File 'lib/spark_engine/plugin/assets/stylesheets.rb', line 123

def versioned(file)
  super(file.sub(/(\.css)?\.s[ca]ss$/i,'.css'))
end