Class: React::Generators::InstallGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Defined in:
lib/generators/react/install_generator.rb

Instance Method Summary collapse

Instance Method Details

#create_directoryObject

Make an empty ‘components/` directory in the right place:



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/generators/react/install_generator.rb', line 33

def create_directory
  components_dir = if webpacker?
    Pathname.new(javascript_dir).parent.to_s
  else
    javascript_dir
  end
  empty_directory File.join(components_dir, 'components')
  unless options[:skip_git]
    create_file File.join(components_dir, 'components/.keep')
  end
end

#create_server_renderingObject



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/generators/react/install_generator.rb', line 54

def create_server_rendering
  if options[:skip_server_rendering]
    return
  elsif webpacker?
    ssr_manifest_path = File.join(javascript_dir, 'server_rendering.js')
    template('server_rendering_pack.js', ssr_manifest_path)
  else
    ssr_manifest_path = File.join(javascript_dir, 'server_rendering.js')
    template('server_rendering.js', ssr_manifest_path)
    initializer_path = 'config/initializers/react_server_rendering.rb'
    template('react_server_rendering.rb', initializer_path)
  end
end

#modify_webpacker_ymlObject

For Shakapacker below version 7, we need to set relative path for source_entry_path



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/generators/react/install_generator.rb', line 20

def modify_webpacker_yml
  webpacker_yml_path = 'config/webpacker.yml'
  if webpacker? && Pathname.new(webpacker_yml_path).exist?
    gsub_file(
      webpacker_yml_path,
      "source_entry_path: /\n",
      "source_entry_path: packs\n"
    )
    reloaded_webpacker_config
  end
end

#setup_reactObject

Add requires, setup UJS



46
47
48
49
50
51
52
# File 'lib/generators/react/install_generator.rb', line 46

def setup_react
  if webpacker?
    setup_react_webpacker
  else
    setup_react_sprockets
  end
end