/ Rakefile
Rakefile
  1  require 'rake/testtask'
  2  require 'uglifier'
  3  require 'openssl'
  4  require 'json'
  5  require 'open3'
  6  require 'fileutils'
  7  
  8  Encoding.default_external = 'UTF-8'
  9  
 10  include Rake
 11  
 12  def minify_js(basename)
 13    root = 'js'
 14    
 15    u = Uglifier.new(
 16      :harmony => true
 17      #screw_ie8: true,
 18      #source_map: {
 19      #  source_filename: "#{basename}#{version}.js",
 20      #  output_filename: "#{basename}.min#{version}.js"
 21      #}
 22    )
 23    
 24    js, sm = u.compile_with_map(File.read("#{root}/#{basename}.js"))
 25    
 26    #hash = OpenSSL::Digest::MD5.hexdigest(sm)[0,8]
 27    #js << "\n//# sourceMappingURL=#{basename}.min.map?#{hash}"
 28    
 29    out = if (basename =~ /-unminified$/)
 30      "#{root}/#{basename.sub(/-.+$/, '')}.js"
 31    else
 32      "#{root}/#{basename}.min.js"
 33    end
 34    
 35    File.open(out, 'w') { |f| f.write js }
 36    #File.open("#{root}/#{basename}.min.map", 'w') { |f| f.write sm }
 37  end
 38  
 39  desc 'Minify JavaScript and generate source maps'
 40  task :minify, [:js] do |t, args|
 41    puts "Compiling #{args[:js]}.js"
 42    minify_js(args[:js])
 43  end
 44  
 45  task :jshint, [:js] do |t, args|
 46    root = 'js'
 47    
 48    basename = args[:js]
 49    
 50    if !basename
 51      abort 'File not found.'
 52    end
 53    
 54    file = "#{root}/#{basename}.js"
 55    
 56    if !File.exist?(file)
 57      abort 'File not found.'
 58    end
 59    
 60    opts = {
 61      laxbreak: true,
 62      esversion: 6,
 63      boss: true,
 64      expr: true,
 65      sub: true,
 66      browser: true,
 67      devel: true,
 68      strict: 'implied',
 69      multistr: true,
 70      scripturl: true,
 71      unused: 'vars',
 72      evil: true,
 73      '-W079' => true # no-native-reassign
 74    }
 75  
 76    opts[:globals] = Hash[[
 77      '$', '$L', 'Chart', 'Feedback', 'Tip', 'APP', 'Tegaki', 'MathJax', 'Main', 'UA',
 78      'Draggable', 'Config', 'Parser', 'ThreadUpdater', 'SettingsMenu', 'QR', 'FC',
 79      'grecaptcha', 'Recaptcha', 'ados_refresh', 'style_group', 'StickyNav',
 80      'PostMenu', 'StorageSync', 'OGVPlayer', 'TCaptcha'
 81    ].collect { |v| [v, false] }]
 82    
 83    cfg_path = 'tmp_jshint.json'
 84    
 85    File.write(cfg_path, opts.to_json)
 86    
 87    puts "--> #{file}"
 88    output, outerr, status = Open3.capture3('jshint', file, '--config', cfg_path)
 89    puts output
 90    
 91    FileUtils.rm(cfg_path)
 92  end
 93  
 94  namespace :concat do
 95    desc 'Concatenate painter.js files'
 96    task :painter do
 97      puts 'Building painter.js'
 98      
 99      root = 'js'
100      out_file = "#{root}/painter.js"
101      js = []
102      
103      ['tegaki.js', 'painter-strings.js'].each do |file|
104        js << File.binread("#{root}/#{file}")
105      end
106      
107      File.binwrite(out_file, js.join("\n"))
108    end
109  end