-
Notifications
You must be signed in to change notification settings - Fork 756
/
Copy pathinstall_generator.rb
121 lines (103 loc) · 3.6 KB
/
install_generator.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
module React
module Generators
class InstallGenerator < ::Rails::Generators::Base
source_root File.expand_path '../../templates', __FILE__
desc 'Create default react.js folder layout and prep application.js'
class_option :skip_git,
type: :boolean,
aliases: '-g',
default: false,
desc: 'Skip Git keeps'
class_option :skip_server_rendering,
type: :boolean,
default: false,
desc: "Don't generate server_rendering.js or config/initializers/react_server_rendering.rb"
# Make an empty `components/` directory in the right place:
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')
if !options[:skip_git]
create_file File.join(components_dir, 'components/.gitkeep')
end
end
# Add requires, setup UJS
def setup_react
if webpacker?
setup_react_webpacker
else
setup_react_sprockets
end
end
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
private
def webpacker?
!!defined?(Webpacker)
end
def javascript_dir
if webpacker?
webpack_configuration.source_path
.join(webpack_configuration.entry_path)
.relative_path_from(::Rails.root)
.to_s
else
'app/assets/javascripts'
end
end
def manifest
Pathname.new(destination_root).join(javascript_dir, 'application.js')
end
def setup_react_sprockets
require_react = "//= require react\n//= require react_ujs\n//= require components\n"
if manifest.exist?
manifest_contents = File.read(manifest)
if match = manifest_contents.match(/\/\/=\s+require\s+turbolinks\s+\n/)
inject_into_file manifest, require_react, { after: match[0] }
elsif match = manifest_contents.match(/\/\/=\s+require_tree[^\n]*/)
inject_into_file manifest, require_react, { before: match[0] }
else
append_file manifest, require_react
end
else
create_file manifest, require_react
end
components_js = "//= require_tree ./components\n"
components_file = File.join(javascript_dir, "components.js")
create_file components_file, components_js
end
WEBPACKER_SETUP_UJS = <<-JS
// Support component names relative to this directory:
var componentRequireContext = require.context("components", true)
var ReactRailsUJS = require("react_ujs")
ReactRailsUJS.useContext(componentRequireContext)
JS
def setup_react_webpacker
`yarn add react_ujs`
if manifest.exist?
append_file(manifest, WEBPACKER_SETUP_UJS)
else
create_file(manifest, WEBPACKER_SETUP_UJS)
end
end
private
def webpack_configuration
Webpacker.respond_to?(:config) ? Webpacker.config : webpack_configuration
end
end
end
end