Class: Pakyow::Assets::Types::JS

Inherits:
Asset
  • Object
show all
Defined in:
lib/pakyow/assets/types/js.rb

Instance Attribute Summary

Attributes inherited from Asset

#dependencies, #logical_path, #mime_suffix, #mime_type

Instance Method Summary collapse

Methods inherited from Asset

#bytesize, #disable_source_map, #each, #fingerprint, #fingerprinted_filename, #freeze, inherited, load, new_from_path, #public_path, #read, #source_map, update_path_for_emitted_type

Constructor Details

#initializeJS

Returns a new instance of JS.



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/pakyow/assets/types/js.rb', line 14

def initialize(*)
  super

  @options = @config.babel.to_h

  if @config.source_maps
    @options[:source_file_name] = Pathname.new(@local_path).relative_path_from(
      Pathname.new(Pakyow.config.root)
    ).to_s
  end
end

Instance Method Details

#process(content) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/pakyow/assets/types/js.rb', line 26

def process(content)
  result = if transformable?
    transformed = Babel.transform(content, **@options)
    { content: transformed["code"], map: transformed["map"] }
  else
    { content: content, map: "" }
  end

  if @config.minify
    result = minify(result)
  end

  @source_map = result[:map]
  result[:content]
rescue StandardError => error
  Pakyow.logger.error "[#{self.class}] #{error}"

  # Be sure to return a string.
  #
  content
end

#source_map?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/pakyow/assets/types/js.rb', line 48

def source_map?
  transformable? || @config.minify
end

#source_map_contentObject



52
53
54
55
# File 'lib/pakyow/assets/types/js.rb', line 52

def source_map_content
  ensure_content
  @source_map
end