Module: Opal::Builder::Directory

Included in:
Opal::Builder
Defined in:
opal/lib/opal/builder/directory.rb

Overview

This module is included into Builder, provides abstracted data about a new paradigm of compiling Opal applications into a directory.

Instance Method Summary collapse

Instance Method Details

#compile_to_directory(dir = nil, single_file: nil, with_source_map: true) ⇒ Object

Output method #compile_to_directory depends on a directory compiler option being set, so that imports are generated correctly.

Raises:

  • (ArgumentError)


18
19
20
21
22
23
24
25
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
58
59
# File 'opal/lib/opal/builder/directory.rb', line 18

def compile_to_directory(dir = nil, single_file: nil, with_source_map: true)
  raise ArgumentError, 'no directory provided' if dir.nil? && single_file.nil?

  catch(:file) do
    index = []

    processed.each do |file|
      module_name = Compiler.module_name(file.filename)
      last_segment_name = File.basename(module_name)
      depth = module_name.split('/').length - 1
      file_name = Pathname(file.filename).cleanpath.to_s

      index << module_name if file.options[:load] || !file.options[:requirable]

      compiled_filename = "#{version_prefix}/#{module_name}.#{output_extension}"
      try_building_single_file(dir, compiled_filename, single_file) do
        compiled_source = file.to_s
        compiled_source += "\n//# sourceMappingURL=./#{last_segment_name}.map" if with_source_map
        compiled_source
      end

      if with_source_map
        source_map_filename = "#{version_prefix}/#{module_name}.map"
        try_building_single_file(dir, source_map_filename, single_file) do
          # Correct the map to point to source files and remove embedded source
          source_map = file.source_map.to_h.dup
          source_map[:sourceRoot] = "./#{'../' * depth}../../#{source_prefix}"
          source_map[:sources] = [file_name]
          source_map.delete(:sourcesContent)
          source_map.to_json
        end

        source_filename = "#{source_prefix}/#{file_name}"
        try_building_single_file(dir, source_filename, single_file) do
          file.original_source
        end
      end
    end

    compile_index(dir, index: index, single_file: single_file)
  end
end

#source_prefixObject



12
13
14
# File 'opal/lib/opal/builder/directory.rb', line 12

def source_prefix
  'opal/src'
end

#version_prefixObject



8
9
10
# File 'opal/lib/opal/builder/directory.rb', line 8

def version_prefix
  "opal/#{Opal::VERSION_MAJOR_MINOR}"
end