react-webpack-rails 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0fd2bddc740ba893006fb0ac87e13a560f7f25b8
4
+ data.tar.gz: 223f3c1d0a1800b818f0bdbdd26375673b1d2636
5
+ SHA512:
6
+ metadata.gz: 1c6fbd3e9464517dd5d2cee5adc43d4475c0750ac61cc38dc3997c2284b50272cca4d37bc0b89f50fd90a39a2e8625bc159f67e4f97444f4ceb285acfa490a33
7
+ data.tar.gz: c34cd4e6aefdfb2baa7e038a202addc3f4683acbb9b3356f78a66ef1ad250b31247edefbd8931f243c64c2229925a333f5400b6770f82b0a5ae867ed2e1bbbd7
@@ -0,0 +1,104 @@
1
+ # React::Webpack::Rails
2
+ A simple installation tool for getting up and running with React + Webpack in Rails.
3
+
4
+ _* This is still in the early stages of development, and is experimental. Feedback is appreciated, and pull requests are welcome._
5
+
6
+ ## OVERVIEW
7
+ This is a minimalist approach to integrating React and Webpack with Rails. There's nothing really hidden in the gem. The point is to be transparent about what is being added to your app and why.
8
+
9
+ There are several goals for this gem:
10
+
11
+ * Provide a lightweight gem for integrating React and Webpack to Rails
12
+ * Easy asset compiling for development and production
13
+ * Fluid development workflow
14
+ * Easy npm module integration
15
+ * single step server for development
16
+ * single asset compilation for deployment
17
+
18
+ ## INSTALLATION
19
+
20
+ Add this line to your application's Gemfile:
21
+
22
+ ```ruby
23
+ gem 'react-webpack-rails'
24
+ ```
25
+
26
+ And then execute:
27
+
28
+ $ bundle
29
+
30
+ Or install it yourself as:
31
+
32
+ $ gem install react-webpack-rails
33
+
34
+
35
+ ## GENERATOR TOOL
36
+ _* not currently supporting HAML_
37
+
38
+ $ rails g react:webpack:install
39
+
40
+ ### WHAT THIS GENERATES
41
+ **EMPTY DIRECTORIES**
42
+ * `client/components`
43
+ * `app/assets/webpack`
44
+
45
+ **BASE FILES**
46
+ * `webpack.config.js`
47
+ * `.babelrc`
48
+ * `app/helpers/react_helper.rb`
49
+ * `client/index.js`
50
+ * `Procfile`
51
+ * `lib/tasks/assets.rake`
52
+
53
+ **GEMS**
54
+
55
+ * `foreman`
56
+
57
+ _* you can skip immediate installation_
58
+
59
+ **NODE MODULES**
60
+
61
+ * `babel-core`
62
+ * `babel-loader`
63
+ * `babel-preset-es2015`
64
+ * `babel-preset-react`
65
+ * `classnames`
66
+ * `react`
67
+ * `react-dom`
68
+ * `webpack`
69
+
70
+ _* you can skip immediate installation_
71
+
72
+ **APPENDING ASSETS INTO THE PIPELINE**
73
+
74
+ ## DEVELOPMENT
75
+ The generator installs Foreman and adds a Procfile to fire up Webpack and the Rails server in a single command:
76
+
77
+ $ foreman start
78
+
79
+ You can also run these separately if you'd like.
80
+
81
+ ```
82
+ npm run watch
83
+ ```
84
+ ```
85
+ rails server
86
+ ```
87
+
88
+ If everything went well, you should be able to view your app at http://localhost:3000
89
+
90
+ You can add your components to `client/components/` and be sure to list them in `client/index.js`
91
+
92
+ You can call components in the view with an `erb` tag for your components and props: `<%= react_component :Dropdown, listItems: [1,2,3,4] %>`
93
+
94
+ ## PRODUCTION
95
+ A rake task for precompiling the webpack assets is added by the generator. This essentially adds the Webpack/Babel transpiling to the precompilation task that runs when you deploy to production. So you should never need to have two separate build tasks.
96
+
97
+ ## Contributing
98
+
99
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/react-webpack-rails. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
100
+
101
+
102
+ ## License
103
+
104
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,98 @@
1
+ require 'rails/generators'
2
+
3
+ module React
4
+ module Webpack
5
+ module Generators
6
+ class InstallGenerator < ::Rails::Generators::Base
7
+ source_root File.expand_path('../templates', __FILE__)
8
+
9
+ desc "Installs necessary framework for react-webpack integration"
10
+
11
+ def create_base_directories
12
+ empty_directory("clients/components")
13
+ empty_directory("app/assets/webpack")
14
+ end
15
+
16
+ def create_base_files
17
+ puts "Creating base files"
18
+ copy_file "webpack.config.js", "webpack.config.js"
19
+ copy_file "babelrc", ".babelrc"
20
+ copy_file "react_helper.rb", "app/helpers/react_helper.rb"
21
+ copy_file "index.js", "client/index.js"
22
+ copy_file "Procfile", "Procfile"
23
+ copy_file "assets.rake", "lib/tasks/assets.rake"
24
+ end
25
+
26
+ def install_foreman
27
+ puts "Adding foreman gem"
28
+ gem "foreman"
29
+ if yes?("Run bundle install?")
30
+ system("bundle install")
31
+ else
32
+ puts "Cool. Just remember to run 'bundle install' later."
33
+ end
34
+ puts "DONE"
35
+ end
36
+
37
+ def install_node_modules
38
+ if npm_and_node_installed?
39
+ initialize_npm_and_node_modules
40
+ else
41
+ puts "You'll need to install node & npm to be able to use React."
42
+ puts "Visit https://nodejs.org/en/ to install."
43
+ end
44
+ end
45
+
46
+ def inject_assets_into_pipeline
47
+ puts "Injecting assets into pipeline"
48
+ append_to_file "app/views/layouts/application.html.erb", "<%= javascript_include_tag 'bundle' %>"
49
+ append_to_file "config/initializers/assets.rb", "Rails.application.config.assets.precompile += %w( bundle.js )"
50
+ end
51
+
52
+ private
53
+
54
+ def npm_and_node_installed?
55
+ system("which npm") && system("which node")
56
+ end
57
+
58
+ def initialize_npm_and_node_modules
59
+ puts "Initalizing package.json"
60
+ system("npm init -y")
61
+ if yes?("Install node modules?")
62
+ puts "Installing..."
63
+ system("npm install --save babel-core babel-loader babel-preset-es2015 babel-preset-react classnames react react-dom webpack")
64
+ append_npm_run_tasks
65
+ else
66
+ puts "Cool. We'll add a standard `package.json` with the appropriate dependencies for you now. Just remember to run 'npm install' later."
67
+ inject_into_file "package.json", node_dependencies, { after: '#readme"' }
68
+ append_npm_run_tasks
69
+ end
70
+ end
71
+
72
+ def append_npm_run_tasks
73
+ puts "Adding webpack watch task"
74
+ inject_into_file "package.json", webpack_watch_task, { after: '"test": "echo \"Error: no test specified\" && exit 1"'}
75
+ end
76
+
77
+ def webpack_watch_task
78
+ ',
79
+ "watch": "webpack --watch --colors --progress"'
80
+ end
81
+
82
+ def node_dependencies
83
+ ',
84
+ "dependencies": {
85
+ "babel-core": "^6.7.7",
86
+ "babel-loader": "^6.2.4",
87
+ "babel-preset-es2015": "^6.6.0",
88
+ "babel-preset-react": "^6.5.0",
89
+ "classnames": "^2.2.5",
90
+ "react": "^15.0.1",
91
+ "react-dom": "^15.0.1",
92
+ "webpack": "^1.13.0"
93
+ }'
94
+ end
95
+ end
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,2 @@
1
+ webpack: npm run watch
2
+ rails: rails server
@@ -0,0 +1,8 @@
1
+ namespace :assets do
2
+ task :precompile => :webpack
3
+ end
4
+
5
+ task :webpack do
6
+ sh "npm install"
7
+ sh "./node_modules/.bin/webpack"
8
+ end
@@ -0,0 +1,3 @@
1
+ {
2
+ "presets": ["es2015", "react"]
3
+ }
@@ -0,0 +1,25 @@
1
+ import React from 'react';
2
+ import ReactDOM from 'react-dom';
3
+ // import SomeComponent from './components/SomeComponent';
4
+ // import AnotherComponent from './components/AnotherComponent';
5
+
6
+ const components = {
7
+ // Your components go here:
8
+ // 'SomeComponent': SomeComponent,
9
+ // 'AnotherComponent': AnotherComponent
10
+ }
11
+
12
+ const reactTargetDivs = document.getElementsByClassName('react-component-target');
13
+
14
+ let componentName, componentProps
15
+ Array.prototype.forEach.call(reactTargetDivs, function (targetDiv) {
16
+ componentName = targetDiv.getAttribute('data-react-class')
17
+ componentProps = JSON.parse(targetDiv.getAttribute('data-react-props'))
18
+ ReactDOM.render(
19
+ React.createElement(
20
+ components[componentName],
21
+ componentProps
22
+ ),
23
+ targetDiv
24
+ )
25
+ });
@@ -0,0 +1,15 @@
1
+ require 'json'
2
+
3
+ module ReactHelper
4
+ def react_component(component_name, props = {})
5
+ content_tag(
6
+ :div,
7
+ nil,
8
+ class: 'react-component-target',
9
+ data: {
10
+ react_class: component_name,
11
+ react_props: props.to_json
12
+ }
13
+ )
14
+ end
15
+ end
@@ -0,0 +1,23 @@
1
+ var webpack = require('webpack');
2
+ var path = require('path');
3
+
4
+ module.exports = {
5
+ entry: [
6
+ './client/'
7
+ ],
8
+ module: {
9
+ loaders: [
10
+ { test: /\.js?$/, loader: 'babel', exclude: /node_modules/ }
11
+ ]
12
+ },
13
+ resolve: {
14
+ extensions: ['', '.js']
15
+ },
16
+ output: {
17
+ path: path.join(__dirname, '/app/assets/webpack/'),
18
+ publicPath: '/',
19
+ filename: 'bundle.js'
20
+ },
21
+ plugins: [
22
+ ]
23
+ };
@@ -0,0 +1,9 @@
1
+ require "react/webpack/rails/version"
2
+
3
+ module React
4
+ module Webpack
5
+ module Rails
6
+ # Your code goes here...
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,7 @@
1
+ module React
2
+ module Webpack
3
+ module Rails
4
+ VERSION = "0.1.0"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,8 @@
1
+ namespace :assets do
2
+ task :precompile => :webpack
3
+ end
4
+
5
+ task :webpack do
6
+ sh "npm install"
7
+ sh "./node_modules/.bin/webpack"
8
+ end
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: react-webpack-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Alan Smith
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-05-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.11'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ description: ''
56
+ email:
57
+ - a.bax.smith@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - README.md
63
+ - lib/generators/react/webpack/install_generator.rb
64
+ - lib/generators/react/webpack/templates/Procfile
65
+ - lib/generators/react/webpack/templates/assets.rake
66
+ - lib/generators/react/webpack/templates/babelrc
67
+ - lib/generators/react/webpack/templates/index.js
68
+ - lib/generators/react/webpack/templates/react_helper.rb
69
+ - lib/generators/react/webpack/templates/webpack.config.js
70
+ - lib/react/webpack/rails.rb
71
+ - lib/react/webpack/rails/version.rb
72
+ - lib/tasks/assets.rake
73
+ homepage: https://github.com/alanbsmith/react-webpack-rails
74
+ licenses:
75
+ - MIT
76
+ metadata: {}
77
+ post_install_message:
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ requirements: []
92
+ rubyforge_project:
93
+ rubygems_version: 2.5.1
94
+ signing_key:
95
+ specification_version: 4
96
+ summary: Integration tool for React + Webpack with Rails
97
+ test_files: []