forked from emberjs/ember.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Assetfile
223 lines (177 loc) · 5.65 KB
/
Assetfile
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
require 'ember-dev'
distros = {
"runtime" => %w(ember-metal rsvp container ember-runtime),
"template-compiler" => %w(ember-handlebars-compiler),
"data-deps" => %w(ember-metal rsvp container ember-runtime),
"full" => %w(ember-metal rsvp container ember-runtime ember-views metamorph handlebars ember-handlebars-compiler ember-handlebars ember-routing ember-application ember-extension-support ember)
}
class AddMicroLoader < Rake::Pipeline::Filter
LOADER = File.expand_path("packages/loader/lib/main.js")
def initialize(options={}, &block)
super(&block)
@global = options[:global]
end
def generate_output(inputs, output)
output.write "(function() {\n" unless @global
output.write File.read(LOADER)
inputs.each do |input|
output.write input.read
end
output.write "\n})();\n" unless @global
end
def additional_dependencies(input)
[ LOADER ]
end
end
#MEGAHAX
ember_spade_postprocess = "filter AddMicroLoader, :global => true"
instance_eval File.read(EmberDev.support_path.join('Assetfile'))
class DetangleTranspiledGlobals
attr_accessor :source
def initialize(source)
self.source = source
end
def imports
@imports ||= parse_imports
end
def process
output = []
raw_exports = source =~ /(^\s+\/\/ BEGIN EXPORTS$.*END EXPORTS$)/m && $1
return unless raw_exports
raw_exports.split("\n").each do |line|
line =~ /(\s+)(.+)\s+= (.+);/
if $2 && $3
override_value = imports[$3] || $3
output << "#{$1}#{$2} = #{override_value};"
else
output << line
end
end
output.join("\n")
end
private
def parse_imports
declarations = {}
raw_declarations = source =~ /BEGIN IMPORTS$(.*)END IMPORTS$/m && $1
raw_declarations.split("\n").each do |line|
line =~ /var (.+)\s+= (.+);/
next unless $1 && $2
declarations[$1] = $2
end
declarations
end
end
class DetangleTranspiledGlobalsFilter < Rake::Pipeline::Filter
def generate_output(inputs, output)
inputs.each do |input|
matches = []
file = File.read(input.fullpath)
file.scan(/^\s+\/\/ BEGIN IMPORTS$.*?END EXPORTS$/m) do |match|
matches << [match, DetangleTranspiledGlobals.new(match).process]
end
matches.each do |(source, transformed)|
file.gsub!(source, transformed)
end
output.write file
end
end
end
class ArgumentsHack < Rake::Pipeline::Filter
def generate_output(inputs, output)
inputs.each do |input|
file = File.read(input.fullpath)
file.gsub!(/function\((__dependency\d+__,? ?|__exports__)+\) {\s+"use strict";\s+/, "function() { \nvar __transpiler = arguments;\nvar __transpiler_exports=__transpiler[__transpiler.length - 1];\n")
file.gsub!(/__dependency(\d+)__/) { "__transpiler[#{$1.to_i - 1}]" }
file.gsub!('__exports__', '__transpiler_exports')
output.write file
end
end
end
class ManglePackageNames < Rake::Pipeline::Filter
def generate_output(inputs, output)
uuid = 0
lookup_table = {}
inputs.each do |input|
file = File.read(input.fullpath)
file.gsub!(/"ember-(?:metal|runtime)\/.+?"/) do |match|
lookup_table[match] ||= uuid = uuid + 1
"#{lookup_table[match]}"
end
output.write file
end
end
end
class MangleDefaultExports < Rake::Pipeline::Filter
def generate_output(inputs, output)
inputs.each do |input|
file = File.read(input.fullpath)
file.gsub!('["default"]', '.__d')
output.write file
end
end
end
class RemoveUseStrict< Rake::Pipeline::Filter
def generate_output(inputs, output)
inputs.each do |input|
file = File.read(input.fullpath)
file.gsub!('"use strict"', '')
output.write file
end
end
end
class EmberTemplateCompiler < Rake::Pipeline::Filter
def generate_output(inputs, output)
inputs.each do |input|
out = "(function() {\nvar Ember = { assert: function() {}, FEATURES: { isEnabled: function() {} } };\n"
out << File.read('packages_es6/ember-handlebars-compiler/lib/main.js')
out.gsub!('import Ember from "ember-metal/core";', '')
out.gsub!('export default EmberHandlebars;', '')
out << "\nexports.precompile = EmberHandlebars.precompile;"
out << "\nexports.EmberHandlebars = EmberHandlebars;"
out << "\n})();"
output.write out
end
end
end
distros.each do |name, modules|
name = name == "full" ? "ember" : "ember-#{name}"
input "dist/modules" do
# Add ember-testing to ember distro
if name == "ember"
match "{ember-testing.js}" do
concat ["ember-testing.js"], "#{name}.js"
end
end
module_paths = modules.map{|m| "#{m}.js" }
match "{#{module_paths.join(',')}}" do
concat(module_paths){ ["#{name}.js", "#{name}.prod.js"] }
end
if name == "ember"
match "{ember-debug.js}" do
concat ["ember-debug.js"], "#{name}.js"
end
end
match "{#{name}.js,#{name}.prod.js}" do
filter HandlebarsPrecompiler
filter EmberDefeatureify
filter AddMicroLoader unless name == "ember-template-compiler"
filter EmberTemplateCompiler if name == "ember-template-compiler"
filter DetangleTranspiledGlobalsFilter
filter EmberLicenseFilter
filter AddProjectVersionNumber
end
# Strip dev code
match "#{name}.prod.js" do
filter(EmberStripDebugMessagesFilter) { ["#{name}.prod.js", "min/#{name}.js"] }
end
# Minify
match "min/#{name}.js" do
filter ArgumentsHack
#filter ManglePackageNames
#filter MangleDefaultExports
filter RemoveUseStrict
uglify{ "#{name}.min.js" }
end
end
end
# vim: filetype=ruby