Skip to content

Commit 7059579

Browse files
committed
inline all images into css
* embedded images as data URIs * rake task to generate multipart js file with embeded images for IE * move images into a separate directory outside of src or css and keep them there for reference * clean up Rakefile and ruby code * .gitignore update * don't penalize IE 8+ with an extra request to the ie-compat.js file
1 parent d0e55bf commit 7059579

File tree

14 files changed

+182
-150
lines changed

14 files changed

+182
-150
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
angular-minified.map
2-
externs.js
32
angular.js
43
angular-minified.js
54
angular-debug.js
5+
angular-ie-compat.js
66
angular-scenario.js
77
angularjs.netrc
88
jstd.log

Rakefile

Lines changed: 137 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -38,36 +38,38 @@ GENERATED_FILES = [
3838
'angular-debug.js',
3939
'angular-minified.js',
4040
'angular-minified.map',
41+
'angular-ie-compat.js',
4142
'angular-scenario.js',
4243
]
4344

4445
task :default => [:compile, :test]
4546

47+
48+
desc 'Clean Generated Files'
49+
task :clean do
50+
FileUtils.rm(GENERATED_FILES, :force => true)
51+
end
52+
53+
4654
desc 'Generate Externs'
4755
task :compile_externs do
48-
out = File.new("externs.js", "w")
56+
File.open('externs.js', 'w') do |out|
57+
out.write("function jQuery(){};\n")
4958

50-
out.write("function jQuery(){};\n")
51-
file = File.new("lib/jquery/jquery-1.4.2.js", "r")
52-
while (line = file.gets)
53-
if line =~ /^\s*(\w+)\s*:\s*function.*$/
54-
out.write("jQuery.#{$1}=function(){};\n")
59+
File.open('lib/jquery/jquery-1.4.2.js', 'r') do |file|
60+
while (line = file.gets)
61+
if line =~ /^\s*(\w+)\s*:\s*function.*$/
62+
out.write("jQuery.#{$1}=function(){};\n")
63+
end
64+
end
5565
end
56-
end
57-
file.close
58-
out.write("jQuery.scope=function(){};\n")
59-
out.write("jQuery.controller=function(){};\n")
6066

61-
out.close
62-
end
63-
64-
desc 'Clean Generated Files'
65-
task :clean do
66-
GENERATED_FILES.each do |file|
67-
`rm #{file}`
67+
out.write("jQuery.scope=function(){};\n")
68+
out.write("jQuery.controller=function(){};\n")
6869
end
6970
end
7071

72+
7173
desc 'Compile Scenario'
7274
task :compile_scenario do
7375

@@ -78,28 +80,89 @@ task :compile_scenario do
7880
ANGULAR_SCENARIO,
7981
'src/scenario/angular.suffix',
8082
]
81-
css = %x(cat css/angular-scenario.css)
83+
8284
concat = 'cat ' + deps.flatten.join(' ')
83-
f = File.new("angular-scenario.js", 'w')
84-
f.write(%x{#{concat}})
85-
f.write('document.write(\'<style type="text/css">\n')
86-
f.write(css.gsub(/'/, "\\'").gsub(/\n/, "\\n"));
87-
f.write('\n</style>\');')
88-
f.close
85+
86+
File.open('angular-scenario.js', 'w') do |f|
87+
f.write(%x{#{concat}})
88+
f.write(gen_css('css/angular.css'))
89+
f.write(gen_css('css/angular-scenario.css'))
90+
end
91+
end
92+
93+
94+
95+
96+
97+
desc 'Generate IE css js patch'
98+
task :generate_ie_compat do
99+
css = File.open('css/angular.css', 'r') {|f| f.read }
100+
101+
# finds all css rules that contain backround images and extracts the rule name(s), content type of
102+
# the image and base64 encoded image data
103+
r = /\n([^\{\n]+)\s*\{[^\}]*background-image:\s*url\("data:([^;]+);base64,([^"]+)"\);[^\}]*\}/
104+
105+
images = css.scan(r)
106+
107+
# create a js file with multipart header containing the extracted images. the entire file *must*
108+
# be CRLF (\r\n) delimited
109+
File.open('angular-ie-compat.js', 'w') do |f|
110+
f.write("/*\r\n" +
111+
"Content-Type: multipart/related; boundary=\"_\"\r\n" +
112+
"\r\n")
113+
images.each_index do |idx|
114+
f.write("--_\r\n" +
115+
"Content-Location:img#{idx}\r\n" +
116+
"Content-Transfer-Encoding:base64\r\n" +
117+
"\r\n" +
118+
images[idx][2] + "\r\n")
119+
end
120+
121+
f.write("--_--\r\n" +
122+
"*/\r\n")
123+
124+
# generate a css string containing *background-image rules for IE that point to the mime type
125+
# images in the header
126+
cssString = ''
127+
images.each_index do |idx|
128+
cssString += "#{images[idx][0]}{*background-image:url(\"mhtml:' + jsUri + '!img#{idx}\")}"
129+
end
130+
131+
# generate a javascript closure that contains a function which will append the generated css
132+
# string as a stylesheet to the current html document
133+
jsString = "(function(){ \r\n" +
134+
" var jsUri = document.location.href.replace(/\\/[^\/]+(#.*)?$/, '/') + " +
135+
" document.getElementById('ng-ie-compat').src; \r\n" +
136+
" var css = '#{cssString}' \r\n" +
137+
" var s = document.createElement('style'); \r\n" +
138+
" s.setAttribute('type', 'text/css'); \r\n" +
139+
" if (s.styleSheet) { \r\n" +
140+
" s.styleSheet.cssText = css; \r\n" +
141+
" } else { \r\n" +
142+
" s.appendChild(document.createTextNode(css)); \r\n" +
143+
" } \r\n" +
144+
" document.getElementsByTagName('head')[0].appendChild(s); \r\n" +
145+
"})();\r\n"
146+
147+
f.write(jsString)
148+
end
89149
end
90150

151+
91152
desc 'Compile JavaScript'
92-
task :compile => [:compile_externs, :compile_scenario] do
153+
task :compile => [:compile_externs, :compile_scenario, :generate_ie_compat] do
93154

94155
deps = [
95156
'src/angular.prefix',
96157
ANGULAR,
97158
'src/angular.suffix',
98159
]
99-
f = File.new("angular-debug.js", 'w')
100-
concat = 'cat ' + deps.flatten.join(' ')
101-
f.write(%x{#{concat}})
102-
f.close
160+
161+
File.open('angular-debug.js', 'w') do |f|
162+
concat = 'cat ' + deps.flatten.join(' ')
163+
f.write(%x{#{concat}})
164+
f.write(gen_css('css/angular.css', true))
165+
end
103166

104167
%x(java -jar lib/compiler-closure/compiler.jar \
105168
--compilation_level SIMPLE_OPTIMIZATIONS \
@@ -109,6 +172,7 @@ task :compile => [:compile_externs, :compile_scenario] do
109172
--js_output_file angular-minified.js)
110173
end
111174

175+
112176
desc 'Create angular distribution'
113177
task :package => :compile do
114178
date = Time.now.strftime('%y%m%d_%H%M')
@@ -117,44 +181,81 @@ task :package => :compile do
117181

118182
%x(cp test/angular-mocks.js ./)
119183

120-
%x(tar -cf #{filename} \
184+
%x(tar -czf #{filename} \
121185
angular-debug.js \
122186
angular-minified.js \
123187
angular-scenario.js \
124188
angular-mocks.js \
125-
css/angular.css \
126-
css/angular_images/ )
189+
angular-ie-compat.js )
127190

128191
%x( rm angular-mocks.js)
129192

130193
puts "Package created: #{filename}"
131194
end
132195

196+
133197
namespace :server do
198+
134199
desc 'Run JsTestDriver Server'
135200
task :start do
136201
sh %x(java -jar lib/jstestdriver/JsTestDriver.jar --browser open --port 9876)
137202
end
138203

139-
desc "Run JavaScript tests against the server"
204+
desc 'Run JavaScript tests against the server'
140205
task :test do
141206
sh %(java -jar lib/jstestdriver/JsTestDriver.jar --tests all)
142207
end
208+
143209
end
144210

145-
desc "Run JavaScript tests"
211+
212+
desc 'Run JavaScript tests'
146213
task :test do
147214
sh %(java -jar lib/jstestdriver/JsTestDriver.jar --tests all --browser open --port 9876)
148215
end
149216

217+
150218
desc 'Lint'
151219
task :lint do
152220
out = %x(lib/jsl/jsl -conf lib/jsl/jsl.default.conf)
153221
print out
154222
end
155223

224+
156225
desc 'push_angularjs'
157-
task :push_angularjs do
158-
Rake::Task['compile'].execute 0
226+
task :push_angularjs => :compile do
159227
sh %(cat angularjs.ftp | ftp -N angularjs.netrc angularjs.org)
160228
end
229+
230+
231+
232+
###################
233+
# utility methods #
234+
###################
235+
236+
237+
##
238+
# generates css snippet from a given files and optionally applies simple minification rules
239+
#
240+
def gen_css(cssFile, minify = false)
241+
css = ''
242+
File.open(cssFile, 'r') do |f|
243+
css = f.read
244+
end
245+
246+
if minify
247+
css.gsub! /\n/, ''
248+
css.gsub! /\/\*.*?\*\//, ''
249+
css.gsub! /:\s+/, ':'
250+
css.gsub! /\s*\{\s*/, '{'
251+
css.gsub! /\s*\}\s*/, '}'
252+
css.gsub! /\s*\,\s*/, ','
253+
css.gsub! /\s*\;\s*/, ';'
254+
end
255+
256+
#escape for js
257+
css.gsub! /'/, "\\'"
258+
css.gsub! /\n/, "\\n"
259+
260+
return %Q{document.write('<style type="text/css">#{css}</style>');}
261+
end

0 commit comments

Comments
 (0)