Class: Rake::Pipeline::Web::Filters::MinispadeFilter

Inherits:
Filter
  • Object
show all
Defined in:
lib/rake-pipeline-web-filters/minispade_filter.rb

Overview

A filter that wraps JavaScript files in a minispade.register closure for use in minispade.

Examples:

Rake::Pipeline.build do
  input "app/assets", "**/*.js"
  output "public"

  # Wrap each JS file in a minispade.register closure.
  filter Rake::Pipeline::Web::Filters::MinispadeFilter
end

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ MinispadeFilter

Returns a new instance of MinispadeFilter.

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :use_strict (Boolean)

    Whether to add “use strict” to each outputted function; defaults to false.

  • :module_id_generator (Proc)

    a proc to use to generate the minispade module id.

  • :rewrite_requires (Boolean)

    If true, change calls to require in the source to minispade.require.

  • :string (Boolean)

    If true, compiles the output as a String instead of a closure. This means that @sourceURL can be appended for good stack traces and debugging.



26
27
28
29
30
31
32
33
# File 'lib/rake-pipeline-web-filters/minispade_filter.rb', line 26

def initialize(options = {})
  super()
  @use_strict = options[:use_strict]
  @module_id_generator = options[:module_id_generator] ||
    proc { |input| input.fullpath.sub(Dir.pwd, '') }
  @rewrite_requires = options[:rewrite_requires]
  @string_module = options[:string]
end

Instance Method Details

#generate_output(inputs, output) ⇒ Object

Implement the #generate_output method required by the Filter API. Wraps each input file in a minispade.register closure.

Parameters:

  • inputs (Array<FileWrapper>)

    an Array of FileWrapper objects representing the inputs to this filter.

  • output (FileWrapper)

    a single FileWrapper object representing the output.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/rake-pipeline-web-filters/minispade_filter.rb', line 44

def generate_output(inputs, output)
  inputs.each do |input|
    code = input.read
    code.gsub!(%r{^\s*require\s*\(\s*}, 'minispade.require(') if @rewrite_requires
    code.gsub!(%r{^\s*requireAll\s*\(\s*}, 'minispade.requireAll(') if @rewrite_requires
    code = %["use strict";\n] + code if @use_strict

    module_id = @module_id_generator.call(input)

    if @string_module
      contents = %{(function() {#{code}\n})();\n//@ sourceURL=#{module_id}}.to_json
    else
      contents = "function() {#{code}\n}"
    end
    ret = "minispade.register('#{module_id}', #{contents});"
    output.write ret
  end
end