Created
January 4, 2012 07:33
-
-
Save jinze/1558977 to your computer and use it in GitHub Desktop.
A obfuscator for JS & CSS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
source "http://rubygems.org" | |
gem "json" | |
gem "uglifier" | |
gem "sass" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
# A obfuscator for JS & CSS | |
# author [email protected] | |
# at least need ( ruby 1.9 || JRuby 1.6.5 && JRUBY_OPTS="--1.9" ) && NodeJS | |
require "uglifier" | |
require "sass" | |
require "rake/clean" | |
#Change it if the work dir is not same as the rake file. | |
#Dir.chdir("WebRoot") | |
ExecJS.runtime = ExecJS::Runtimes::Node #need NodeJS | |
MultiJson.engine = :json_gem #(encode bug on JRuby) | |
#MultiJson.engine = :ok_json #(encode bug, defalut but slow?) | |
#MultiJson.engine = :yajl #(encode bug && couldn't run under JRuby 1.6.4 && ruby 1.9.2) | |
#MultiJson.engine = :json_pure #(a little slow...but encode right on JRuby) | |
#Obfuscate config | |
develop_dir = "d" | |
release_dir = "r" | |
css_prefix = "c" | |
js_prefix = "j" | |
css_source_dir = "src/css" | |
js_source_dir = "src/js" | |
build_temp_dir = "temp" | |
CLEAN.include(build_temp_dir) | |
CLOBBER.include(release_dir) | |
task :default => ["combineJS", "combineCSS", "clean"] | |
SRC_ENCODING = 'gb18030' | |
#JS配置 | |
JS_DEV = FileList["#{develop_dir}/#{js_prefix}/*.js"] | |
JS_RLS = JS_DEV.sub(Regexp.new("^#{develop_dir}"), "#{release_dir}") | |
JS_SRC = FileList["#{js_source_dir}/**/*.js"] | |
JS_OFS = JS_SRC.sub(Regexp.new("^#{js_source_dir}"), "#{build_temp_dir}/#{js_source_dir}") | |
#CSS配置 | |
CSS_DEV = FileList["#{develop_dir}/#{css_prefix}/*.js"] | |
CSS_RLS = CSS_DEV.sub(Regexp.new("^#{develop_dir}"), "#{release_dir}").ext(newext="css") | |
CSS_SRC = FileList["#{css_source_dir}/**/*.css"] | |
CSS_OFS = CSS_SRC.sub(Regexp.new("^#{css_source_dir}"), "#{build_temp_dir}/#{css_source_dir}").ext(newext="css") | |
#生成混淆的临时JS文件 | |
JS_SRC.each do |src| | |
obj = "#{build_temp_dir}/#{src}" | |
file obj => src do | |
puts "Obfuscating #{src} ==> #{obj}" | |
FileUtils.mkdir_p(File.dirname(obj)) #unless File.directory?(File.dirname(obj)) == false | |
File.open(obj, 'w') do |file| | |
file << Uglifier.compile(File.read(src).force_encoding(SRC_ENCODING), :ascii_only => false, :copyright => false).encode(SRC_ENCODING) + ';' | |
end | |
end | |
end | |
#生成混淆的临时CSS文件 | |
CSS_SRC.each do |src| | |
obj = "#{build_temp_dir}/#{src}" | |
file obj => src do | |
puts "Obfuscating #{src} ==> #{obj}" | |
FileUtils.mkdir_p(File.dirname(obj)) #unless File.directory?(File.dirname(obj)) == false | |
File.open(obj, 'w') do |file| | |
file << Sass::Engine.new(File.read(src).force_encoding(SRC_ENCODING), :syntax => :scss, :style => :compressed).render.encode(SRC_ENCODING) | |
end | |
end | |
end | |
#合并混效后的JS文件 | |
JS_DEV.each do |src| | |
filelist = [] | |
File.new(src, 'r').each_line do |line| | |
line.scan(/(['"])(.*?)\1/) do |m| | |
filename = m[1] | |
filelist.push "#{build_temp_dir}/#{js_source_dir}/#{filename}" | |
end | |
end | |
rls = src.sub(Regexp.new("^#{develop_dir}"), "#{release_dir}") | |
file rls => filelist do | |
FileUtils.mkdir_p(File.dirname(rls)) | |
File.open(rls, 'w') do |file| | |
filelist.each do |fn| | |
# file.puts "/* #{fn} */" | |
file.puts File.read(fn) | |
end | |
end | |
puts "Combined #{rls}" | |
end | |
end | |
#合并混效后的CSS文件 | |
CSS_DEV.each do |src| | |
filelist = [] | |
File.new(src, 'r').each_line do |line| | |
line.scan(/(['"])(.*?)\1/) do |m| | |
filename = m[1] | |
filelist.push "#{build_temp_dir}/#{css_source_dir}/#{filename}" | |
end | |
end | |
rls = src.sub(Regexp.new("^#{develop_dir}"), "#{release_dir}").sub(Regexp.new(".js$"), ".css") | |
file rls => filelist do | |
FileUtils.mkdir_p(File.dirname(rls)) | |
File.open(rls, 'w') do |file| | |
filelist.each do |fn| | |
# file.puts "/* #{fn} */" | |
file.puts File.read(fn) | |
end | |
end | |
puts "Combined #{rls}" | |
end | |
end | |
task :combineJS => JS_RLS | |
task :obfusJS => JS_OFS | |
task :combineCSS => CSS_RLS | |
task :obfusCSS => CSS_OFS |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment