Module: Webpacker::Routes

Defined in:
lib/webpacker/routes.rb,
lib/webpacker/routes/railtie.rb,
lib/webpacker/routes/version.rb

Defined Under Namespace

Classes: Engine

Constant Summary collapse

JAVASCRIPT_VARIABLE_NAME_REGEX =
/\A[_$a-z][_$a-z0-9]*\z/i
IGNORED_OPTIONS =
%i[controller action]
VERSION =
'0.1.5'

Class Method Summary collapse

Class Method Details

.generate(app) ⇒ Object



16
17
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/webpacker/routes.rb', line 16

def generate(app)
  config = app.config.webpacker.routes
  var_name = -> (name) { config.camel_case ? name.camelize(:lower) : name }

  default_url_options = app.default_url_options.dup
  default_url_options[:relative_url_root] = app.config.relative_url_root if app.config.relative_url_root
  default_url_options.merge!(config.default_url_options)
  default_url_options.except!(*IGNORED_OPTIONS)

  parent_spec_var = var_name.call('parent_spec')
  default_url_options_var = var_name.call('default_url_options')

  route_sets = [[app.routes, nil, Webpacker.config.routes_path]]
  visited_directories = []

  while (route_set, parent, directory = route_sets.shift)
    directory.mkpath
    visited_directories << directory
    js_file = directory.join('index.js')

    catch(:identical) do
      File.atomic_write(js_file) do |temp_file|
        parent_var_definition = if parent
          "import { #{parent} as #{parent_spec_var} } from '../'"
        else
          "const #{parent_spec_var} = null"
        end

        temp_file.write(<<-JAVASCRIPT.strip_heredoc)
          import { urlFor, pathFor } from 'webpacker-routes'
          #{parent_var_definition}
          const #{default_url_options_var} = #{js(default_url_options)}
        JAVASCRIPT

        route_set.named_routes.sort_by(&:first).each do |name, route|
          raise `Invalid route name for javascript: ${name}` unless JAVASCRIPT_VARIABLE_NAME_REGEX =~ name

          spec = route.path.spec.to_s
          segment_keys = route.segment_keys.uniq
          options = route.defaults.except(*IGNORED_OPTIONS)

          spec_var = var_name.call("#{name}_spec")
          url_var = var_name.call("#{name}_url")
          path_var = var_name.call("#{name}_path")

          temp_file.write(<<-JAVASCRIPT.strip_heredoc)
            export const #{spec_var} = [#{js(spec)}, #{js(segment_keys)}, { ...#{default_url_options_var}, ...#{js(options)} }, #{parent_spec_var}]
            export const #{url_var} = (...args) => urlFor(#{spec_var}, ...args)
            export const #{path_var} = (...args) => pathFor(#{spec_var}, ...args)
          JAVASCRIPT

          if engine?(route)
            engine = rack_app(route)
            engine_name = engine.railtie_name

            raise `Invalid engine name for javascript: ${engine_name}` unless JAVASCRIPT_VARIABLE_NAME_REGEX =~ engine_name

            engine_name_var = var_name.call(engine_name)

            route_sets << [engine.routes, spec_var, directory.join(engine_name_var)]
          end
        end

        temp_file.close
        if identical?(js_file.to_s, temp_file.path)
          temp_file.unlink
          throw :identical
        end
      end
    end
  end

  extra_directories = Webpacker.config.routes_path.glob('**/*').select(&:directory?) - visited_directories
  extra_directories.sort_by { |directory| directory.to_s.size }.reverse_each(&:rmtree)
end