-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathRakefile
111 lines (85 loc) · 2.71 KB
/
Rakefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
require "bundler/setup"
require "erb"
require "uglifier"
require "sproutcore"
LICENSE = File.read("generators/license.js")
module SproutCore
module Compiler
class Entry
def body
"\n(function(exports) {\n#{@raw_body}\n})({});\n"
end
end
end
end
## HELPERS ##
def strip_require(file)
result = File.read(file)
result.gsub!(%r{^\s*require\(['"]([^'"])*['"]\);?\s*}, "")
result
end
def strip_ember_assert(file)
result = File.read(file)
result.gsub!(%r{^(\s)+ember_assert\((.*)\).*$}, "")
result
end
def uglify(file)
uglified = Uglifier.compile(File.read(file))
"#{LICENSE}\n#{uglified}"
end
SproutCore::Compiler.intermediate = "tmp/intermediate"
SproutCore::Compiler.output = "tmp/static"
# Create a compile task for an Ember package. This task will compute
# dependencies and output a single JS file for a package.
def compile_package_task(input, output=input)
js_tasks = SproutCore::Compiler::Preprocessors::JavaScriptTask.with_input "packages/#{input}/lib/**/*.js", "."
SproutCore::Compiler::CombineTask.with_tasks js_tasks, "#{SproutCore::Compiler.intermediate}/#{output}"
end
## TASKS ##
# Create ember:package tasks for each of the Ember packages
namespace :sproutcore do
%w(statechart).each do |package|
task package => compile_package_task("sproutcore-#{package}", "sproutcore-#{package}")
end
end
# Create a build task that depends on all of the package dependencies
task :build => ["sproutcore:statechart"]
distributions = {
"sproutcore-statechart" => ["sproutcore-statechart"]
}
distributions.each do |name, libraries|
# Strip out require lines. For the interim, requires are
# precomputed by the compiler so they are no longer necessary at runtime.
file "dist/#{name}.js" => :build do
puts "Generating #{name}.js"
mkdir_p "dist"
File.open("dist/#{name}.js", "w") do |file|
libraries.each do |library|
file.puts strip_require("tmp/static/#{library}.js")
end
end
end
# Minified distribution
file "dist/#{name}.min.js" => "dist/#{name}.js" do
require 'zlib'
print "Generating #{name}.min.js... "
STDOUT.flush
File.open("dist/#{name}.prod.js", "w") do |file|
file.puts strip_ember_assert("dist/#{name}.js")
end
minified_code = uglify("dist/#{name}.prod.js")
File.open("dist/#{name}.min.js", "w") do |file|
file.puts minified_code
end
gzipped_kb = Zlib::Deflate.deflate(minified_code).bytes.count / 1024
puts "#{gzipped_kb} KB gzipped"
rm "dist/#{name}.prod.js"
end
end
desc "Build Ember.js"
task :dist => distributions.keys.map {|name| "dist/#{name}.min.js"}
desc "Clean build artifacts from previous builds"
task :clean do
sh "rm -rf tmp && rm -rf dist"
end
task :default => :dist