Class: SprocketsEsbuild::TransformerBase

Inherits:
Object
  • Object
show all
Includes:
Sprockets
Defined in:
lib/sprockets-esbuild/transformers.rb

Direct Known Subclasses

JsxTransformer, TsTransformer, TsxTransformer

Constant Summary collapse

ESBUILD =
[File.expand_path('../../exe/esbuild', __dir__)]

Instance Method Summary collapse

Instance Method Details

#cache_keyObject



22
23
24
# File 'lib/sprockets-esbuild/transformers.rb', line 22

def cache_key
  @cache_key ||= "#{self.class.name}::#{VERSION}".freeze
end

#call(input) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/sprockets-esbuild/transformers.rb', line 26

def call(input)
  data = input[:data]

  input[:cache].fetch([cache_key, data]) do

    out, err, status = Open3.capture3(*ESBUILD, '--sourcemap',
      "--sourcefile=#{input[:filename]}", "--loader=#{loader}",
      stdin_data: input[:data])

    match = out.match %r{^//# sourceMappingURL=data:application/json;base64,(.*)\s*}

    if match
      # extract sourcemap from output and then format and combine it
      out[match.begin(0)..match.end(0)] = ''
      map = JSON.parse(Base64.decode64(match[1]))
      map = SourceMapUtils.format_source_map(map, input)
      map = SourceMapUtils.combine_source_maps(input[:metadata][:map], map)
    else
      map = nil
    end

    if status.success? and err.empty?
      if map
        { data: out, map: map }
      else
        out
      end
    else
      raise Error, "esbuild exit status=#{status.exitstatus}\n#{err}"
    end
  end
end