-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathneuter.rb
114 lines (88 loc) · 3.07 KB
/
neuter.rb
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
require 'rake-pipeline'
module Neuter
DEBUG = false
class SpadeWrapper < Rake::Pipeline::FileWrapper
REQUIRE_RE = %r{^\s*require\(['"]([^'"]*)['"]\);?\s*}
# Keep track of required files
@@required = []
# Yes, it's called package because module is a reserved word
def package
@package ||= path.split('/lib/')[0].split('/').last.split('.js').last
end
def read
source = super
# bail if this class is used for an intermediate file (it is!)
return source if !path.include?("/lib/")
# Replace all requires with emptiness, and accumulate dependency sources
prepend = ''
source.gsub! REQUIRE_RE do |m|
req = $1
# Find a reason to fail
reason = @@required.include?(req) ? 'Already required' : false
reason ||= is_package_req(req) ? 'Required package' : false
reason ||= is_external_req(package, req) ? "External package for #{package}" : false
if reason
p "Skipped #{req} required in #{path} (#{reason})" if DEBUG
else
@@required << req
req_file = File.join(package, "lib", "#{req.gsub(package, '')}.js")
prepend = prepend + self.class.new(root, req_file, encoding).read
p "Required #{req_file} as #{req} in #{path} in package #{package}" if DEBUG
end
''
end
source = "(function(exports) {\n#{source}\n})({});\n\n"
"#{prepend}#{source}"
end
protected
def is_package_req(req)
!(req.include? '/')
end
def is_external_req(package, req)
!(req.split('/')[0] == package)
end
end
module Filters
class NeuterFilter < Rake::Pipeline::ConcatFilter
# Allows selective concat by passing packages array (or all if nil)
def initialize(packages=nil, string=nil, &block)
@packages = packages
@file_wrapper_class = SpadeWrapper
super(string, &block)
end
def generate_output(inputs, output)
inputs.each do |input|
if !(@packages.nil? || @packages.include?(input.path.split('/').first))
p "Not neutering: #{input.path}" if DEBUG
next
end
spade = SpadeWrapper.new(input.root, input.path, input.encoding)
p "Neutering #{input.path} into #{output.path}" if DEBUG
output.write spade.read
end
end
end
class SelectFilter < Rake::Pipeline::ConcatFilter
# Allows selective concat by passing packages array (or all if nil)
def initialize(packages=nil, string=nil, &block)
@packages = packages || []
super(string, &block)
end
def generate_output(inputs, output)
inputs.each do |input|
next unless @packages.include?(input.path)
output.write input.read
end
end
end
end
module Helpers
def neuter(*args, &block)
filter(Neuter::Filters::NeuterFilter, *args, &block)
end
def select(*args, &block)
filter(Neuter::Filters::SelectFilter, *args, &block)
end
end
end
Rake::Pipeline::DSL::PipelineDSL.send(:include, Neuter::Helpers)