Class: Cyborg::Assets::Javascripts

Inherits:
AssetType
  • Object
show all
Defined in:
lib/cyborg/plugin/assets/javascripts.rb

Instance Attribute Summary

Attributes inherited from AssetType

#base, #plugin

Instance Method Summary collapse

Methods inherited from AssetType

#build_msg, #change, #compress, #destination, #file_event, #filter_files, #find_files, #find_node_module, #initialize, #npm_command, #url, #urls, #versioned, #watch

Constructor Details

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

Instance Method Details

#asset_tag(*args) ⇒ Object



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

def asset_tag(*args)
  javascript_include_tag(args)
end

#buildObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/cyborg/plugin/assets/javascripts.rb', line 16

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|
      system build_command(file)

      if Cyborg.production?
        compress(destination(file))
      end

      if File.exist? destination(file)
        puts build_msg(file)
      else
        puts "FAILED TO WRITE: #{file}."
      end
    end
  else
    abort "JS BUILD FAILED: browserifyinc NPM module not found.\n" << "Please add browserifyinc to your package.json and run `npm install`"
  end
end

#build_command(file) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/cyborg/plugin/assets/javascripts.rb', line 43

def build_command(file)
  dest = destination(file).sub(/\.js$/,'')
  options = " -t babelify --standalone #{plugin.name} -o #{dest}.js -d"

  cmd = if Cyborg.production?
    npm_path "browserify #{file} #{options}"
  else
    npm_path "browserifyinc --cachefile #{cache_file(File.basename(dest))} #{file} #{options}"
  end

  if Cyborg.production?
    cmd += " -p [ minifyify --map #{url(file).sub(/\.js$/,'')}.map.json --output #{dest}.map.json ]"
  end

  cmd
end

#cache_file(name = nil) ⇒ Object



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

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

#extObject



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

def ext
  "js"
end

#npm_path(cmd) ⇒ Object



39
40
41
# File 'lib/cyborg/plugin/assets/javascripts.rb', line 39

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