Class: Rake::Pipeline::Web::Filters::ES6ModuleFilter

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

Overview

A filter that transpiles ES6 to either AMD or CommonJS JavaScript.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}, &block) ⇒ ES6ModuleFilter

Create an instance of this filter.

Possible options: module_id_generator: provide a Proc to convert an input to a

module identifier (AMD only)

Other options are passed along to the RubyES6ModuleTranspiler and then to

the node transpiler. See https://github.com/square/es6-module-transpiler
for more info.

Parameters:

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

    options (see above)

  • block (Proc)

    the output name generator block


20
21
22
23
24
# File 'lib/rake-pipeline-web-filters/es6_module_filter.rb', line 20

def initialize(options = {}, &block)
  @module_id_generator = options[:module_id_generator]
  super(&block)
  @options = options
end

Instance Attribute Details

#optionsHash (readonly)

Returns a hash of options to pass to Transpiler

Returns:

  • (Hash)

    a hash of options to pass to Transpiler


7
8
9
# File 'lib/rake-pipeline-web-filters/es6_module_filter.rb', line 7

def options
  @options
end

Instance Method Details

#external_dependenciesObject


48
49
50
# File 'lib/rake-pipeline-web-filters/es6_module_filter.rb', line 48

def external_dependencies
  [ "ruby_es6_module_transpiler" ]
end

#generate_output(inputs, output) ⇒ Object

The body of the filter. Compile each input file into a ES6 Module Transpiled output file.

Parameters:

  • inputs (Array)

    an Array of FileWrapper objects.

  • output (FileWrapper)

    a FileWrapper object


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/rake-pipeline-web-filters/es6_module_filter.rb', line 31

def generate_output(inputs, output)
  inputs.each do |input|
    begin
      body = input.read if input.respond_to?(:read)
      local_opts = {}
      if @module_id_generator
        local_opts[:moduleName] = @module_id_generator.call(input)
      end
      opts = @options.merge(local_opts)
      opts.delete(:module_id_generator)
      output.write RubyES6ModuleTranspiler.transpile(body, opts)
    rescue ExecJS::Error => error
      raise error, "Error compiling #{input.path}. #{error.message}"
    end
  end
end