Class: SparkEngine::Assets::Javascripts

Inherits:
AssetType
  • Object
show all
Defined in:
lib/spark_engine/plugin/assets/javascripts.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, #find_files, #find_node_module, #initialize, #local_path, #log_error, #log_success, #npm_command, #pathname, #url, #urls, #versioned, #watch

Constructor Details

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

Instance Method Details

#asset_tag(*args) ⇒ Object



8
9
10
# File 'lib/spark_engine/plugin/assets/javascripts.rb', line 8

def asset_tag(*args)
  javascript_include_tag(args)
end

#buildObject



23
24
25
26
27
28
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
55
56
57
58
59
60
61
62
# File 'lib/spark_engine/plugin/assets/javascripts.rb', line 23

def build
  files = find_files
  FileUtils.mkdir_p(File.dirname(cache_file)) if !files.empty?

  if Open3.capture3("npm ls browserify-incremental")[1].empty?
    files.each do |file|

      dest = destination(file)

      FileUtils.rm(dest) if File.exists?(dest)

      response = Open3.capture3(build_command(file))

      # If the file exists and is not empty (a failed build is empty)
      if response.last.success?
        compress(dest) if SparkEngine.production?
        build_success file
      else
        build_failure file

        response = response.map { |l| l.to_s.split("\n") }.flatten

        response.each do |line|
          if !line.empty? &&                      # Don't print empty lines
             !line.match(/node_modules/i) &&      # Ignore stack traces from installed modules
             !line.match(/pid (\d+?) exit/i) &&   # Ignore pid exit stack line
             !line.match(/\[BABEL\] Note:/i)      # Notices from Bable are noisy
            log_error line.gsub(plugin.root+'/','')
          end
        end

        puts ""
      end
    end
  else
    log_error "JS BUILD FAILED: browserifyinc NPM module not found.\n" << "Please add browserifyinc to your package.json and run `npm install`"
    abort
  end
  puts ""
end

#build_command(file) ⇒ Object



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

def build_command(file)
  dest = destination(file)
  basename = pathname(file)
  options = "--standalone #{File.basename(file, ".*")} -d"

  cmd = if SparkEngine.production?
          npm_path %Q{browserify #{file} #{options} | #{npm_path('uglifyjs')} --output #{dest} --source-map "url='#{basename}.map',filename='#{basename}',content='inline',includeSources='true'"}
        else
          npm_path "browserify #{file} #{options} > #{dest}"
        end
  puts "Running: #{cmd}" if plugin.debug?

  cmd
end

#cache_file(name = nil) ⇒ Object



12
13
14
# File 'lib/spark_engine/plugin/assets/javascripts.rb', line 12

def cache_file(name=nil)
  SparkEngine.rails_path("tmp/cache/assets/.browserify-cache-#{name}.json")
end

#extObject



4
5
6
# File 'lib/spark_engine/plugin/assets/javascripts.rb', line 4

def ext
  "js"
end

#filter_files(names) ⇒ Object

Find all asset files matching array of names



17
18
19
20
21
# File 'lib/spark_engine/plugin/assets/javascripts.rb', line 17

def filter_files(names)
  [find_files, plugin.svgs.build_paths].flatten.select do |f|
    names.include? File.basename(f).sub(/(\..+)$/,'')
  end
end

#npm_path(cmd) ⇒ Object



64
65
66
# File 'lib/spark_engine/plugin/assets/javascripts.rb', line 64

def npm_path(cmd)
  File.join SparkEngine.gem_path, "node_modules/.bin", cmd
end