class BrowserifyRails::BrowserifyProcessor
Constants
- BROWSERIFY_CMD
Public Instance Methods
# File lib/browserify-rails/browserify_processor.rb, line 11 def evaluate(context, locals, &block) if commonjs_module? asset_dependencies(context.environment.paths).each do |path| context.depend_on(path) end browserify else data end end
# File lib/browserify-rails/browserify_processor.rb, line 8 def prepare end
Private Instance Methods
This primarily filters out required files from node modules
@return [<String>] Paths of dependencies, that are in asset directories
# File lib/browserify-rails/browserify_processor.rb, line 32 def asset_dependencies(asset_paths) dependencies.select do |path| path.start_with?(*asset_paths) end end
# File lib/browserify-rails/browserify_processor.rb, line 46 def browserify # Only generate source maps in development if Rails.env == "development" options = "-d" else options = "" end run_browserify(options) end
# File lib/browserify-rails/browserify_processor.rb, line 57 def browserify_cmd cmd = File.join(Rails.root, BROWSERIFY_CMD) if !File.exist?(cmd) raise BrowserifyRails::BrowserifyError.new("browserify could not be found at #{cmd}. Please run npm install.") end cmd end
# File lib/browserify-rails/browserify_processor.rb, line 25 def commonjs_module? data.to_s.include?("module.exports") || data.to_s.include?("require") end
@return [<String>] Paths of files, that this file depends on
# File lib/browserify-rails/browserify_processor.rb, line 39 def dependencies run_browserify("--list").lines.map(&:strip).select do |path| # Filter the temp file, where browserify caches the input stream File.exists?(path) end end
Run browserify with `data` on standard input.
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 options [String] Options for browserify @return [String] Output on standard out
# File lib/browserify-rails/browserify_processor.rb, line 79 def run_browserify(options) command = "#{browserify_cmd} #{options}" directory = File.dirname(file) stdout, stderr, status = Open3.capture3(command, stdin_data: data, chdir: directory) if !status.success? raise BrowserifyRails::BrowserifyError.new("Error while running `#{command}`:\n\n#{stderr}") end stdout end