/
browserify_processor.rb
228 lines (182 loc) · 7.45 KB
/
browserify_processor.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
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
224
225
226
227
228
require "open3"
require "fileutils"
require "tempfile"
module BrowserifyRails
class BrowserifyProcessor < Tilt::Template
def prepare
ensure_tmp_dir_exists!
ensure_commands_exist!
end
def evaluate(context, locals, &block)
# If there's nothing to do, we just return the data we received
return data unless should_browserify?
# Signal dependencies to sprockets to ensure we track changes
evaluate_dependencies(context.environment.paths).each do |path|
context.depend_on(path)
end
run_browserify(context.logical_path)
end
private
def config
Rails.application.config.browserify_rails
end
def tmp_path
@tmp_path ||= Rails.root.join("tmp", "cache", "browserify-rails").freeze
end
def browserify_cmd
@browserify_cmd ||= File.join(config.node_bin, "browserify").freeze
end
def browserifyinc_cmd
@browserifyinc_cmd ||= File.join(config.node_bin, "browserifyinc").freeze
end
def ensure_tmp_dir_exists!
FileUtils.mkdir_p(rails_path(tmp_path))
end
def ensure_commands_exist!
error = ->(cmd) { "Unable to run #{cmd}. Ensure you have installed it with npm." }
# Browserify has to be installed in any case
if !File.exists?(rails_path(browserify_cmd))
raise BrowserifyRails::BrowserifyError.new(error.call(browserify_cmd))
end
# If the user wants to use browserifyinc, we need to ensure it's there too
if config.use_browserifyinc && !File.exists?(rails_path(browserifyinc_cmd))
raise BrowserifyRails::BrowserifyError.new(error.call(browserifyinc_cmd))
end
end
def should_browserify?
config.force || (in_path? && !browserified? && commonjs_module?)
end
# Is this file in any of the configured paths?
def in_path?
config.paths.any? do |path_spec|
path_spec === file
end
end
# Is this file already packaged for the browser?
def browserified?
data.to_s.include?("define.amd") || data.to_s.include?("_dereq_")
end
# Is this a commonjs module?
#
# Be here as strict as possible, so that non-commonjs files are not
# preprocessed.
def commonjs_module?
data.to_s.include?("module.exports") || data.present? && data.to_s.include?("require") && dependencies.length > 0
end
def asset_paths
@asset_paths ||= Rails.application.config.assets.paths.collect { |p| p.to_s }.join(":") || ""
end
# This primarily filters out required files from node modules
#
# @return [<String>] Paths of dependencies to evaluate
def evaluate_dependencies(asset_paths)
return dependencies if config.evaluate_node_modules
dependencies.select do |path|
path.start_with?(*asset_paths)
end
end
# @return [<String>] Paths of files, that this file depends on
def dependencies
@dependencies ||= begin
# We forcefully run browserify (avoiding browserifyinc) with the --list
# option to get a list of files.
list = run_browserify(nil, "--list")
list.lines.map(&:strip).select do |path|
# Filter the temp file, where browserify caches the input stream
File.exists?(path)
end
end
end
# Environtment to run browserify in:
#
# NODE_PATH https://nodejs.org/api/all.html#all_loading_from_the_global_folders
# but basically allows one to have multiple locations for non-relative requires
# to be resolved to.
#
# NODE_ENV is set to the Rails.env. This is used by some modules to determine
# how to build. Example: https://facebook.github.io/react/downloads.html#npm
def env
{
"NODE_PATH" => asset_paths,
"NODE_ENV" => Rails.env
}
end
# Run the requested version of browserify (browserify or browserifyinc)
# based on configuration or the use_browserifyinc parameter if present.
#
# We are passing the data via stdin, so that earlier preprocessing steps are
# respected. If you had, say, an "application.js.coffee.erb", passing the
# filename would fail, because browserify would read the original file with
# ERB tags and fail. By passing the data via stdin, we get the expected
# behavior of success, because everything has been compiled to plain
# javascript at the time this processor is called.
#
# @raise [BrowserifyRails::BrowserifyError] if browserify does not succeed
# @param logical_path [String] Sprockets's logical path for the file
# @param extra_options [String] Options to be included in the command
# @param force_browserifyinc [Boolean] Causes browserifyinc to be used if true
# @return [String] Output of the command
def run_browserify(logical_path=nil, extra_options=nil, force_browserifyinc=nil)
command_options = "#{options} #{extra_options} #{granular_options(logical_path)}".strip
# Browserifyinc uses a special cache file. We set up the path for it if
# we're going to use browserifyinc.
if uses_browserifyinc(force_browserifyinc)
cache_file_path = rails_path(tmp_path, "browserifyinc-cache.json")
command_options << " --cachefile=#{cache_file_path.inspect}"
end
# Create a temporary file for the output. Such file is necessary when
# using browserifyinc, but we use it in all instances for consistency
output_file = Tempfile.new("output", rails_path(tmp_path))
command_options << " -o #{output_file.path.inspect}"
# Compose the full command (using browserify or browserifyinc as necessary)
command = "#{browserify_command(force_browserifyinc)} #{command_options} -"
# The directory the command will be executed from
base_directory = File.dirname(file)
Logger::log "Browserify: #{command}"
stdout, stderr, status = Open3.capture3(env, command, stdin_data: data, chdir: base_directory)
if !status.success?
raise BrowserifyRails::BrowserifyError.new("Error while running `#{command}`:\n\n#{stderr}")
end
# Read the output that was stored in the temp file
output = output_file.read
# Destroy the temp file (good practice)
output_file.close
output_file.unlink
# Some command flags (such as --list) make the output go to stdout,
# ignoring -o. If this happens, we give out stdout instead.
if stdout.present?
stdout
else
output
end
end
def uses_browserifyinc(force=nil)
!force.nil? ? force : config.use_browserifyinc
end
def browserify_command(force=nil)
rails_path(uses_browserifyinc(force) ? browserifyinc_cmd : browserify_cmd)
end
def options
options = []
options.push("-d") if config.source_map_environments.include?(Rails.env)
options += Array(config.commandline_options) if config.commandline_options.present?
options.uniq.join(" ")
end
def get_granular_config(logical_path)
granular_config = config.granular["javascript"]
granular_config && granular_config[logical_path]
end
def granular_options(logical_path)
granular_config = get_granular_config(logical_path)
return nil if granular_config.blank?
# We set separate options for each of the items in granular_config
options = granular_config.keys.collect do |key|
granular_config[key].collect { |value| "--#{key} #{value}" }
end
options.flatten.join(" ") if options
end
def rails_path(*paths)
Rails.root.join(*paths).to_s
end
end
end