Method: ImportJS::JSModule#open_file_path

Defined in:
lib/import_js/js_module.rb

#open_file_path(path_to_current_file) ⇒ String

Parameters:

  • path_to_current_file (String)

Returns:

  • (String)


142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/import_js/js_module.rb', line 142

def open_file_path(path_to_current_file)
  if @file_path
    # There is a file_path. This happens for JSModules that are not aliases.
    return @file_path unless @file_path.end_with?('/package.json')

    # The file_path points to a package.json file, so we want to look in
    # that package.json file for a `main` configuration value and open that
    # file instead.
    return @file_path.sub(/package\.json$/, main_file)
  end

  # There is no file_path. This likely means that we are working with an
  # alias, so we want to expand it to a full path if we can.

  if @import_path.start_with?('.')
    # The import path in the alias starts with a ".", which means that it is
    # relative to the current file. In order to open this file, we need to
    # expand it to a full path.
    return File.expand_path(
      @import_path, File.dirname(path_to_current_file))
  end

  # This is likely an alias that points to a package, so let's try to find
  # its main file from its package.json file.
  file_path = "node_modules/#{@import_path}/package.json"
  _, main = self.class.resolve_import_path_and_main(file_path, [])
  return "node_modules/#{@import_path}/#{main}" if main

  @import_path
end