Class: ImportJS::JSModule

Inherits:
Object
  • Object
show all
Defined in:
lib/import_js/js_module.rb

Overview

Class that represents a js module found in the file system

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(import_path: nil) ⇒ JSModule

Returns a new instance of JSModule.

Parameters:

  • import_path (String) (defaults to: nil)


70
71
72
# File 'lib/import_js/js_module.rb', line 70

def initialize(import_path: nil)
  self.import_path = import_path
end

Instance Attribute Details

#file_pathObject

Returns the value of attribute file_path.



8
9
10
# File 'lib/import_js/js_module.rb', line 8

def file_path
  @file_path
end

#has_named_exportsObject

Returns the value of attribute has_named_exports.



10
11
12
# File 'lib/import_js/js_module.rb', line 10

def has_named_exports
  @has_named_exports
end

#import_pathObject

Returns the value of attribute import_path.



6
7
8
# File 'lib/import_js/js_module.rb', line 6

def import_path
  @import_path
end

#lookup_pathObject

Returns the value of attribute lookup_path.



7
8
9
# File 'lib/import_js/js_module.rb', line 7

def lookup_path
  @lookup_path
end

#main_fileObject

Returns the value of attribute main_file.



9
10
11
# File 'lib/import_js/js_module.rb', line 9

def main_file
  @main_file
end

Class Method Details

.construct(lookup_path: nil, relative_file_path: nil, strip_file_extensions: nil, make_relative_to: nil, strip_from_path: nil) ⇒ Object

Parameters:

  • lookup_path (String) (defaults to: nil)

    the lookup path in which this module was found

  • relative_file_path (String) (defaults to: nil)

    a full path to the file, relative to the project root.

  • strip_file_extensions (Array) (defaults to: nil)

    a list of file extensions to strip, e.g. [‘.js’, ‘.jsx’]

  • make_relative_to (String|nil) (defaults to: nil)

    a path to a different file which the resulting import path should be relative to.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/import_js/js_module.rb', line 19

def self.construct(lookup_path: nil,
                   relative_file_path: nil,
                   strip_file_extensions: nil,
                   make_relative_to: nil,
                   strip_from_path: nil)
  js_module = new
  js_module.lookup_path = normalize_path(lookup_path)
  js_module.file_path = normalize_path(relative_file_path)

  import_path, main_file = resolve_import_path_and_main(
    js_module.file_path, strip_file_extensions)

  return unless import_path

  import_path = import_path.sub(
    %r{^#{Regexp.escape(js_module.lookup_path)}/}, '')

  js_module.import_path = import_path
  js_module.main_file = main_file
  js_module.make_relative_to(make_relative_to) if make_relative_to
  js_module.strip_from_path(strip_from_path) unless make_relative_to
  js_module
end

.normalize_path(path) ⇒ String

Parameters:

  • path (String)

Returns:

  • (String)


45
46
47
48
# File 'lib/import_js/js_module.rb', line 45

def self.normalize_path(path)
  return unless path
  path.sub(%r{^\./?}, '')
end

.resolve_import_path_and_main(file_path, strip_file_extensions) ⇒ String

Parameters:

  • file_path (String)
  • strip_file_extensions (Array)

Returns:

  • (String, String)


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/import_js/js_module.rb', line 53

def self.resolve_import_path_and_main(file_path, strip_file_extensions)
  if file_path.end_with? '/package.json'
    main_file = JSON.parse(File.read(file_path))['main']
    return [nil, nil] unless main_file
    match = file_path.match(%r{(.*)/package\.json})
    return match[1], main_file
  end

  match = file_path.match(%r{(.*)/(index\.js[^/]*)$})
  return match[1], match[2] if match

  extensions = strip_file_extensions.map { |str| Regexp.escape(str) }
  import_path = file_path.sub(/(?:#{extensions.join('|')})$/, '')
  [import_path, nil]
end

Instance Method Details

#display_nameString

Returns a readable description of the module.

Returns:

  • (String)

    a readable description of the module



104
105
106
107
108
# File 'lib/import_js/js_module.rb', line 104

def display_name
  parts = [import_path]
  parts << " (main: #{@main_file})" if @main_file
  parts.join('')
end

#make_relative_to(make_relative_to) ⇒ Object

Parameters:

  • make_relative_to (String)


75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/import_js/js_module.rb', line 75

def make_relative_to(make_relative_to)
  return unless lookup_path
  # First, strip out any absolute path up until the current directory
  make_relative_to = make_relative_to.sub("#{Dir.pwd}/", '')

  # Ignore if the file to relate to is part of a different lookup_path
  return unless make_relative_to.start_with? lookup_path

  # Strip out the lookup_path
  make_relative_to = make_relative_to.sub(
    %r{^#{Regexp.escape(lookup_path)}/}, '')

  path = Pathname.new(import_path).relative_path_from(
    Pathname.new(File.dirname(make_relative_to))
  ).to_s

  # `Pathname.relative_path_from` will not add "./" automatically
  path = './' + path unless path.start_with?('.')

  self.import_path = path
end

#open_file_path(path_to_current_file) ⇒ String

Parameters:

  • path_to_current_file (String)

Returns:

  • (String)


112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/import_js/js_module.rb', line 112

def open_file_path(path_to_current_file)
  if file_path && file_path.end_with?('/package.json')
    return file_path.sub(/package\.json$/, main_file)
  end
  return file_path if file_path

  if import_path.start_with?('.')
    return File.expand_path(import_path, File.dirname(path_to_current_file))
  end

  import_path
end

#strip_from_path(prefix) ⇒ Object

Parameters:

  • prefix (String)


98
99
100
101
# File 'lib/import_js/js_module.rb', line 98

def strip_from_path(prefix)
  return unless prefix
  self.import_path = import_path.sub(/^#{Regexp.escape(prefix)}/, '')
end

#to_import_statement(variable_name, config) ⇒ ImportJS::ImportStatement

Parameters:

Returns:



128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/import_js/js_module.rb', line 128

def to_import_statement(variable_name, config)
  ImportJS::ImportStatement.new.tap do |statement|
    if has_named_exports
      statement.inject_named_import(variable_name)
    else
      statement.default_import = variable_name
    end
    statement.path = import_path
    statement.declaration_keyword = config.get('declaration_keyword',
                                               from_file: file_path)
    statement.import_function = config.get('import_function',
                                           from_file: file_path)
  end
end