Skip to content

Commit e409a62

Browse files
author
Robert Mosolgo
committed
Merge pull request #376 from rmosolgo/build-with-webpack
feat(JS) add webpack build process
2 parents 3c3a8eb + ecf89aa commit e409a62

24 files changed

+165188
-41305
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@ test/dummy/tmp
55
gemfiles/*.lock
66
*.swp
77
/vendor/react
8+
react-builds/node_modules
9+
react-builds/build

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ MyApp::Application.configure do
194194
config.react.server_renderer_timeout ||= 20 # seconds
195195
config.react.server_renderer = React::ServerRendering::SprocketsRenderer
196196
config.react.server_renderer_options = {
197-
files: ["react.js", "components.js"], # files to load for prerendering
197+
files: ["react-server.js", "components.js"], # files to load for prerendering
198198
replay_console: true, # if true, console.* will be replayed client-side
199199
}
200200
end

Rakefile

+26-14
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,35 @@ end
66

77
Bundler::GemHelper.install_tasks
88

9-
require 'pathname'
9+
def copy_react_asset(webpack_file, destination_file)
10+
full_webpack_path = File.expand_path("../react-builds/build/#{webpack_file}", __FILE__)
11+
full_destination_path = File.expand_path("../lib/assets/react-source/#{destination_file}", __FILE__)
12+
FileUtils.cp(full_webpack_path, full_destination_path)
13+
end
14+
1015
namespace :react do
11-
task :update do
12-
FileUtils.rm_f('vendor/react/.bower.json')
13-
`bower install react`
14-
assets_path = Pathname.new(File.dirname(__FILE__)).join('lib/assets/')
15-
copy_react_asset('JSXTransformer.js', assets_path.join('javascripts/JSXTransformer.js'))
16-
copy_react_asset('react.js', assets_path.join('react-source/development/react.js'))
17-
copy_react_asset('react.min.js', assets_path.join('react-source/production/react.js'))
18-
copy_react_asset('react-with-addons.js', assets_path.join('react-source/development-with-addons/react.js'))
19-
copy_react_asset('react-with-addons.min.js', assets_path.join('react-source/production-with-addons/react.js'))
16+
desc "Run the JS build process to put files in the gem source"
17+
task update: [:build, :copy]
18+
19+
desc "Build the JS bundles with Webpack"
20+
task :build do
21+
Dir.chdir("react-builds") do
22+
`webpack`
23+
`NODE_ENV=production webpack`
24+
end
2025
end
2126

22-
def copy_react_asset(source, destination)
23-
vendor_path = Pathname.new(File.dirname(__FILE__)).join('vendor/react')
24-
FileUtils.mkdir_p(destination.dirname.to_s)
25-
FileUtils.cp(vendor_path.join(source), destination.to_s)
27+
desc "Copy browser-ready JS files to the gem's asset paths"
28+
task :copy do
29+
environments = ["development", "production"]
30+
environments.each do |environment|
31+
# Without addons:
32+
copy_react_asset("#{environment}/react-browser.js", "#{environment}/react.js")
33+
copy_react_asset("#{environment}/react-server.js", "#{environment}/react-server.js")
34+
# With addons:
35+
copy_react_asset("#{environment}/react-browser-with-addons.js", "#{environment}-with-addons/react.js")
36+
copy_react_asset("#{environment}/react-server-with-addons.js", "#{environment}-with-addons/react-server.js")
37+
end
2638
end
2739
end
2840

0 commit comments

Comments
 (0)