Class: BrowserifyRails::DirectiveProcessor
- Inherits:
-
Sprockets::DirectiveProcessor
- Object
- Sprockets::DirectiveProcessor
- BrowserifyRails::DirectiveProcessor
show all
- Defined in:
- lib/browserify-rails/directive_processor.rb
Defined Under Namespace
Classes: BrowserifyError, ModuleDepsError
Constant Summary
collapse
- BROWSERIFY_CMD =
'./node_modules/.bin/browserify'.freeze
- MODULE_DEPS_CMD =
'./node_modules/.bin/module-deps'.freeze
- COFFEEIFY_PATH =
'./node_modules/coffeeify'.freeze
Instance Method Summary
collapse
Instance Method Details
#commonjs_module?(data) ⇒ Boolean
49
50
51
|
# File 'lib/browserify-rails/directive_processor.rb', line 49
def commonjs_module?(data)
data.to_s.include?('module.exports') || data.to_s.include?('require')
end
|
#evaluate(context, locals, &block) ⇒ Object
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
|
# File 'lib/browserify-rails/directive_processor.rb', line 16
def evaluate(context, locals, &block)
super
if commonjs_module?(data)
browserify_cmd = File.join(context.environment.root, BROWSERIFY_CMD)
module_deps_cmd = File.join(context.environment.root, MODULE_DEPS_CMD)
raise ArgumentError, "#{browserify_cmd} could not be found. Please run npm install." unless File.exist?(browserify_cmd)
raise ArgumentError, "#{module_deps_cmd} could not be found. Please run npm install." unless File.exist?(module_deps_cmd)
deps = JSON.parse(run_command("#{module_deps_cmd} #{pathname}"))
deps.each do |dep|
path = File.basename(dep['id'], context.environment.root)
next if path == File.basename(pathname)
if path =~ /<([^>]+)>/
path = $1
else
path = "./#{path}" unless relative?(path)
end
context.depend_on_asset(path)
end
params = "-d"
params += " -t coffeeify --extension='.coffee'" if File.directory?(COFFEEIFY_PATH)
run_command("#{browserify_cmd} #{params} #{pathname}")
else
data
end
end
|
#run_command(command) ⇒ Object
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
# File 'lib/browserify-rails/directive_processor.rb', line 53
def run_command(command)
stdin, stdout, stderr = Open3.popen3("#{command}")
begin
result = stdout.read
result_error = stderr.read.strip
if result_error.empty?
result
else
raise ModuleDepsError, result_error
end
ensure
stdin.close
stdout.close
stderr.close
end
end
|