Skip to content

Commit 7801399

Browse files
committed
Update the codebase based on Shakapacker
1 parent 6aea114 commit 7801399

File tree

5 files changed

+43
-47
lines changed

5 files changed

+43
-47
lines changed

Appraisals

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ end
1616

1717
appraise 'shakapacker' do
1818
gem 'rails', '~> 7.0.x'
19-
gem 'shakapacker', '6.6.0'
19+
gem 'shakapacker', '7.0.2'
2020
end
2121

2222
appraise 'base' do

lib/generators/react/component_generator.rb

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class ComponentGenerator < ::Rails::Generators::NamedBase
66
source_root File.expand_path "../templates", __dir__
77
desc <<-DESC.strip_heredoc
88
Description:
9-
Scaffold a React component into `components/` of your Webpacker source or asset pipeline.
9+
Scaffold a React component into `components/` of your Shakapacker source or asset pipeline.
1010
The generated component will include a basic render function and a PropTypes
1111
hash to help with development.
1212
@@ -125,8 +125,8 @@ class ComponentGenerator < ::Rails::Generators::NamedBase
125125

126126
def create_component_file
127127
template_extension = detect_template_extension
128-
# Prefer webpacker to sprockets:
129-
if webpacker?
128+
# Prefer Shakapacker to Sprockets:
129+
if shakapacker?
130130
new_file_name = file_name.camelize
131131
extension = if options[:coffee]
132132
"coffee"
@@ -152,33 +152,36 @@ def create_component_file
152152
private
153153

154154
def webpack_configuration
155-
Webpacker.respond_to?(:config) ? Webpacker.config : Webpacker::Configuration
155+
Shakapacker.respond_to?(:config) ? Shakapacker.config : Shakapacker::Configuration
156156
end
157157

158158
def component_name
159159
file_name.camelize
160160
end
161161

162162
def file_header
163-
if webpacker?
163+
if shakapacker?
164164
return %(import * as React from "react"\n) if options[:ts]
165165

166-
%(import React from "react"\nimport PropTypes from "prop-types"\n)
166+
<<~JS
167+
import React from "react"
168+
import PropTypes from "prop-types"
169+
JS
167170
else
168171
""
169172
end
170173
end
171174

172175
def file_footer
173-
if webpacker?
176+
if shakapacker?
174177
%(export default #{component_name})
175178
else
176179
""
177180
end
178181
end
179182

180-
def webpacker?
181-
defined?(Webpacker)
183+
def shakapacker?
184+
defined?(Shakapacker)
182185
end
183186

184187
def parse_attributes!

lib/generators/react/install_generator.rb

Lines changed: 26 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -20,32 +20,32 @@ class InstallGenerator < ::Rails::Generators::Base
2020

2121
# Make an empty `components/` directory in the right place:
2222
def create_directory
23-
components_dir = if webpacker?
24-
Pathname.new(javascript_dir).parent.to_s
25-
else
26-
javascript_dir
27-
end
28-
empty_directory File.join(components_dir, "components")
29-
return if options[:skip_git]
30-
31-
create_file File.join(components_dir, "components/.keep")
23+
components_dir = if shakapacker?
24+
Pathname.new(javascript_dir).parent.to_s
25+
else
26+
javascript_dir
27+
end
28+
empty_directory File.join(components_dir, 'components')
29+
unless options[:skip_git]
30+
create_file File.join(components_dir, 'components/.keep')
31+
end
3232
end
3333

3434
# Add requires, setup UJS
3535
def setup_react
36-
if webpacker?
37-
setup_react_webpacker
36+
if shakapacker?
37+
setup_react_shakapacker
3838
else
3939
setup_react_sprockets
4040
end
4141
end
4242

4343
def create_server_rendering
4444
if options[:skip_server_rendering]
45-
nil
46-
elsif webpacker?
47-
ssr_manifest_path = File.join(javascript_dir, "server_rendering.js")
48-
template("server_rendering_pack.js", ssr_manifest_path)
45+
return
46+
elsif shakapacker?
47+
ssr_manifest_path = File.join(javascript_dir, 'server_rendering.js')
48+
template('server_rendering_pack.js', ssr_manifest_path)
4949
else
5050
ssr_manifest_path = File.join(javascript_dir, "server_rendering.js")
5151
template("server_rendering.js", ssr_manifest_path)
@@ -56,13 +56,13 @@ def create_server_rendering
5656

5757
private
5858

59-
def webpacker?
60-
!!defined?(Webpacker)
59+
def shakapacker?
60+
!!defined?(Shakapacker)
6161
end
6262

6363
def javascript_dir
64-
if webpacker?
65-
webpack_source_path
64+
if shakapacker?
65+
shakapacker_source_path
6666
.relative_path_from(::Rails.root)
6767
.to_s
6868
else
@@ -96,33 +96,26 @@ def setup_react_sprockets
9696
create_file components_file, components_js
9797
end
9898

99-
WEBPACKER_SETUP_UJS = <<~JS
99+
SHAKAPACKER_SETUP_UJS = <<~JS
100100
// Support component names relative to this directory:
101101
var componentRequireContext = require.context("components", true);
102102
var ReactRailsUJS = require("react_ujs");
103103
ReactRailsUJS.useContext(componentRequireContext);
104104
JS
105105

106-
def setup_react_webpacker
106+
def setup_react_shakapacker
107107
`yarn add react_ujs`
108108
if manifest.exist?
109-
append_file(manifest, WEBPACKER_SETUP_UJS)
109+
append_file(manifest, SHAKAPACKER_SETUP_UJS)
110110
else
111-
create_file(manifest, WEBPACKER_SETUP_UJS)
111+
create_file(manifest, SHAKAPACKER_SETUP_UJS)
112112
end
113113
end
114114

115-
def webpack_source_path
116-
if Webpacker.respond_to?(:config)
117-
Webpacker.config.source_entry_path # Webpacker >3
118-
else
119-
Webpacker::Configuration.source_path.join(Webpacker::Configuration.entry_path) # Webpacker <3
120-
end
121-
end
115+
private
122116

123-
def reloaded_webpacker_config
124-
Webpacker.instance.instance_variable_set(:@config, nil)
125-
Webpacker.config
117+
def shakapacker_source_path
118+
Shakapacker.config.source_entry_path
126119
end
127120
end
128121
end

lib/react/server_rendering/bundle_renderer.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
module React
99
module ServerRendering
1010
# Extends ExecJSRenderer for the Rails environment
11-
# - fetches JS code from the Rails app (webpacker or sprockets)
11+
# - fetches JS code from the Rails app (Shakapacker or Sprockets)
1212
# - stringifies props
1313
# - implements console replay
1414
class BundleRenderer < ExecJSRenderer

lib/react/server_rendering/separate_server_bundle_container.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44

55
module React
66
module ServerRendering
7-
# Get a compiled file from Webpacker's output path
7+
# Get a compiled file from Shakapacker's output path
88
class SeparateServerBundleContainer
99
def self.compatible?
10-
!!defined?(Webpacker)
10+
!!defined?(Shakapacker)
1111
end
1212

1313
def find_asset(filename)
14-
asset_path = Webpacker.config.public_output_path.join(filename).to_s
14+
asset_path = Shakapacker.config.public_output_path.join(filename).to_s
1515
File.read(asset_path)
1616
end
1717
end

0 commit comments

Comments
 (0)