Skip to content

Instantly share code, notes, and snippets.

@rwjblue
Created March 3, 2014 22:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rwjblue/9336392 to your computer and use it in GitHub Desktop.
Save rwjblue/9336392 to your computer and use it in GitHub Desktop.
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)
}
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)
inputs.each do |input|
file = File.read(input.fullpath)
file.gsub! "ember-metal", "m"
file.gsub! "ember-runtime", "r"
output.write file
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,ember-debug.js}" do
concat ["ember-testing.js", "ember-debug.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
match "{#{name}.js,#{name}.prod.js}" do
filter HandlebarsPrecompiler
filter EmberDefeatureify
filter AddMicroLoader unless name == "ember-template-compiler"
filter EmberStub if name == "ember-template-compiler"
#filter DetangleTranspiledGlobalsFilter
#filter ArgumentsHack
#filter ManglePackageNames
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
uglify{ "#{name}.min.js" }
end
end
end
# vim: filetype=ruby
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment