webpacker-react 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0b2bf95c452f63279e5f6ca108129cd39e7a9a0e
4
+ data.tar.gz: 17f0a6a4b80916879c8c0a400aa6e38462f4ef93
5
+ SHA512:
6
+ metadata.gz: c7ef3bb80a1aa78f930deaf1c8defe431eba0d84f4329d3663ee563d51c8dd567ee2cb1da45bba9b17267011e98ccf4a221833f0217072b86ce354c8b59f7cbb
7
+ data.tar.gz: a5ca92600867b135ed67e4c0dae9171ea79931259ac8f6f22b0e02224eed61f5c842c670e7d2a6cbf1cabee0cb8af58ad52a9a0c2e12513c7faec3607b07b6b9
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ node_modules/
2
+ /.bundle/
3
+ /.yardoc
4
+ /Gemfile.lock
5
+ /_yardoc/
6
+ /coverage/
7
+ /doc/
8
+ /pkg/
9
+ /spec/reports/
10
+ /tmp/
11
+
12
+ /javascript/webpacker_react-npm-module/dist/*.js
13
+
14
+ test/example_app/log/*
15
+ test/example_app/tmp/*
16
+ test/example_app/public/packs
17
+
18
+ .rvmrc
data/.rubocop.yml ADDED
@@ -0,0 +1,118 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.2
3
+ # RuboCop has a bunch of cops enabled by default. This setting tells RuboCop
4
+ # to ignore them, so only the ones explicitly set in this file are enabled.
5
+ DisabledByDefault: true
6
+ Exclude:
7
+ - '**/vendor/**/*'
8
+
9
+ # Prefer &&/|| over and/or.
10
+ Style/AndOr:
11
+ Enabled: true
12
+
13
+ # Do not use braces for hash literals when they are the last argument of a
14
+ # method call.
15
+ Style/BracesAroundHashParameters:
16
+ Enabled: true
17
+
18
+ # Align `when` with `case`.
19
+ Style/CaseIndentation:
20
+ Enabled: true
21
+
22
+ # Align comments with method definitions.
23
+ Style/CommentIndentation:
24
+ Enabled: true
25
+
26
+ # No extra empty lines.
27
+ Style/EmptyLines:
28
+ Enabled: true
29
+
30
+ # In a regular class definition, no empty lines around the body.
31
+ Style/EmptyLinesAroundClassBody:
32
+ Enabled: true
33
+
34
+ # In a regular module definition, no empty lines around the body.
35
+ Style/EmptyLinesAroundModuleBody:
36
+ Enabled: true
37
+
38
+ # Use Ruby >= 1.9 syntax for hashes. Prefer { a: :b } over { :a => :b }.
39
+ Style/HashSyntax:
40
+ Enabled: true
41
+
42
+ # Method definitions after `private` or `protected` isolated calls need one
43
+ # extra level of indentation.
44
+ Style/IndentationConsistency:
45
+ Enabled: true
46
+ EnforcedStyle: rails
47
+
48
+ # Two spaces, no tabs (for indentation).
49
+ Style/IndentationWidth:
50
+ Enabled: true
51
+
52
+ Style/SpaceAfterColon:
53
+ Enabled: true
54
+
55
+ Style/SpaceAfterComma:
56
+ Enabled: true
57
+
58
+ Style/SpaceAroundEqualsInParameterDefault:
59
+ Enabled: true
60
+
61
+ Style/SpaceAroundKeyword:
62
+ Enabled: true
63
+
64
+ Style/SpaceAroundOperators:
65
+ Enabled: true
66
+
67
+ Style/SpaceBeforeFirstArg:
68
+ Enabled: true
69
+
70
+ # Defining a method with parameters needs parentheses.
71
+ Style/MethodDefParentheses:
72
+ Enabled: true
73
+
74
+ # Use `foo {}` not `foo{}`.
75
+ Style/SpaceBeforeBlockBraces:
76
+ Enabled: true
77
+
78
+ # Use `foo { bar }` not `foo {bar}`.
79
+ Style/SpaceInsideBlockBraces:
80
+ Enabled: true
81
+
82
+ # Use `{ a: 1 }` not `{a:1}`.
83
+ Style/SpaceInsideHashLiteralBraces:
84
+ Enabled: true
85
+
86
+ Style/SpaceInsideParens:
87
+ Enabled: true
88
+
89
+ # Check quotes usage according to lint rule below.
90
+ Style/StringLiterals:
91
+ Enabled: true
92
+ EnforcedStyle: double_quotes
93
+
94
+ # Detect hard tabs, no hard tabs.
95
+ Style/Tab:
96
+ Enabled: true
97
+
98
+ # Blank lines should not have any spaces.
99
+ Style/TrailingBlankLines:
100
+ Enabled: true
101
+
102
+ # No trailing whitespace.
103
+ Style/TrailingWhitespace:
104
+ Enabled: true
105
+
106
+ # Use quotes for string literals when they are enough.
107
+ Style/UnneededPercentQ:
108
+ Enabled: true
109
+
110
+ # Align `end` with the matching keyword or starting expression except for
111
+ # assignments, where it should be aligned with the LHS.
112
+ Lint/EndAlignment:
113
+ Enabled: true
114
+ EnforcedStyleAlignWith: variable
115
+
116
+ # Use my_method(my_arg) not my_method( my_arg ) or my_method my_arg.
117
+ Lint/RequireParentheses:
118
+ Enabled: true
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.3
5
+ before_install: gem install bundler -v 1.13.6
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source "https://rubygems.org"
2
+
3
+ gem "rubocop", ">= 0.47", require: false
4
+ gem "rails", github: "rails/rails"
5
+ gem "arel", github: "rails/arel"
6
+ gem "webpacker", github: "rails/webpacker"
7
+
8
+ # Specify your gem's dependencies in webpacker-react.gemspec
9
+ gemspec
data/README.md ADDED
@@ -0,0 +1,222 @@
1
+ # Webpacker::React [![CircleCI](https://circleci.com/gh/renchap/webpacker-react.svg?style=svg)](https://circleci.com/gh/renchap/webpacker-react)
2
+
3
+ Webpacker-React makes it easy to use [React](https://facebook.github.io/react/) with [Webpacker](https://github.com/rails/webpacker) in your Rails applications.
4
+
5
+ Important note: Webpacker is not yet officially released. It will be included in Rails 5.1 but is highly experimental for now.
6
+
7
+ An example application is available: https://github.com/renchap/webpacker-react-example/
8
+
9
+ This gem is a work in progress. Final feature list:
10
+
11
+ - [x] uses the new Webpacker way of integrating Javascript with Rails (using packs)
12
+ - [x] render React components from views using a `react_component` helper
13
+ - [x] render React components from controllers using `render react_component: 'name'`
14
+ - [x] support for [hot reloading](https://github.com/gaearon/react-hot-loader)
15
+ - [ ] render components server-side
16
+ - [ ] use a Rails generator to create new components
17
+
18
+ ## Installation
19
+
20
+ Your Rails application needs to use Webpacker and have the React integration done. Please refer to their documentation documentation for this: https://github.com/rails/webpacker/blob/master/README.md#ready-for-react
21
+
22
+ First, you need to add the webpacker-react gem to your Rails app Gemfile:
23
+
24
+ ```ruby
25
+ gem 'webpacker-react', "~>0.1.0"
26
+ ```
27
+
28
+ Once done, run `bundler` to install the gem.
29
+
30
+ Then you need to update your `vendor/package.json` file to include the `webpacker-react` NPM module:
31
+ ```json
32
+ "dependencies": {
33
+ "..."
34
+ "webpacker-react": "~>0.1.0"
35
+ },
36
+ ```
37
+
38
+ Finally, run `./bin/yarn` to install the module. You are now all set!
39
+
40
+ ### Note about versions
41
+
42
+ Webpacker-React contains two parts: a Javascript module and a Ruby gem. Both of those components respect [semantic versioning](http://semver.org). **When upgrading the gem, you need to upgrade the NPM module to the same minor version**. New patch versions can be released for each of the two independently, so it is ok to have the NPM module at version `A.X.Y` and the gem at version `A.X.Z`, but you should never have a different `A` or `X`.
43
+
44
+ ## Usage
45
+
46
+ The first step is to register your root components (those you want to load from your HTML).
47
+ In your pack file (`app/javascript/packs/*.js`), import your components as well as `webpacker-react` and register them. Considering you have a component in `app/javascript/components/hello.js`:
48
+
49
+ ```javascript
50
+ import Hello from 'components/hello'
51
+ import WebpackerReact from 'webpacker-react'
52
+
53
+ WebpackerReact.register(Hello)
54
+ ```
55
+
56
+ Now you can render React components from your views or your controllers.
57
+
58
+ ### Rendering from a view
59
+
60
+ Use the `react_component` helper:
61
+
62
+ ```erb
63
+ <%= react_component('Hello', name: 'React') %>
64
+ ```
65
+
66
+ ### Rendering from a controller
67
+
68
+ ```rb
69
+ class PageController < ApplicationController
70
+ def main
71
+ render react_component: 'Hello', props: { name: 'React' }
72
+ end
73
+ end
74
+ ```
75
+
76
+ You can pass any of the usual arguments to render in this call: `layout`, `status`, `content_type`, etc.
77
+
78
+ *Note: you need to have [Webpack process your code](https://github.com/rails/webpacker#binstubs) before it is available to the browser, either by manually running `./bin/webpack` or having the `./bin/webpack-watcher` process running.*
79
+
80
+ ### Hot Module Replacement
81
+
82
+ [HMR](https://webpack.js.org/guides/hmr-react/) allows to reload / add / remove modules live in the browser without
83
+ reloading the page. This allows any change you make to your React components to be applied as soon as you save,
84
+ preserving their current state.
85
+
86
+ First, install `react-hot-loader`:
87
+
88
+ ```
89
+ ./bin/yarn add react-hot-loader@3.0.0-beta.6
90
+ ```
91
+
92
+ You then need to update your Webpack config.
93
+
94
+ We provide a convenience function to add the necessary changes to your config if it's not
95
+ significantly different than the standard Webpacker config:
96
+
97
+ ```js
98
+ //development.js
99
+ ...
100
+
101
+ var configureHotModuleReplacement = require('webpacker-react/configure-hot-module-replacement')
102
+
103
+ var sharedConfig = require('./shared.js')
104
+ sharedConfig = configureHotModuleReplacement(sharedConfig)
105
+
106
+ module.exports = merge(sharedConfig, ...)
107
+ ```
108
+
109
+ If you need to change your configuration manually:
110
+
111
+ 1. set the public URL used to load `webpack-dev-server` assets
112
+ ```js
113
+ {
114
+ output: {
115
+ publicPath: 'http://localhost:8080'
116
+ }
117
+ }
118
+ ```
119
+
120
+ 2. add `react-hot-loader/babel` to your `babel-loader` rules:
121
+ ```javascript
122
+ {
123
+ module: {
124
+ rules: [
125
+ {
126
+ test: /\.jsx?(.erb)?$/,
127
+ exclude: /node_modules/,
128
+ loader: 'babel-loader',
129
+ options: {
130
+ presets: [
131
+ 'react',
132
+ [ 'latest', { 'es2015': { 'modules': false } } ]
133
+ ],
134
+ plugins: ['react-hot-loader/babel']
135
+ }
136
+ }
137
+ }
138
+ ```
139
+
140
+ 3. prepend `react-hot-loader/patch` to your entries:
141
+ ```javascript
142
+ {
143
+ entry:
144
+ { application: [ 'react-hot-loader/patch', '../app/javascript/packs/application.js' ],
145
+ ...
146
+ }
147
+ ```
148
+
149
+ 4. you now need to use `webpack-dev-server` (in place of `webpack` or `webpack-watcher`). Make sure the following line is in your development.rb:
150
+ ```ruby
151
+ config.x.webpacker[:dev_server_host] = 'http://localhost:8080/'
152
+ ```
153
+ and start `webpack-dev-server` in hot replacement mode:
154
+ ```
155
+ ./bin/webpack-dev-server --hot
156
+ ```
157
+
158
+ 5. finally opt in to HMR from your pack files:
159
+ ```es6
160
+ import SomeRootReactComponent from 'components/some-root-react-component'
161
+ import WebpackerReact from 'webpacker-react/hmr'
162
+
163
+ WebpackerReact.register(SomeRootReactComponent)
164
+ if (module.hot)
165
+ module.hot.accept('components/some-root-react-component', () =>
166
+ WebpackerReact.renderOnHMR(SomeRootReactComponent) )
167
+ ```
168
+
169
+ ## Development
170
+
171
+ To work on this gem locally, you first need to clone and setup [the example application](https://github.com/renchap/webpacker-react-example).
172
+
173
+ Then you need to change the example app Gemfile to point to your local repository and run bundle afterwise:
174
+
175
+ ```ruby
176
+ gem 'webpacker-react', path: '~/code/webpacker-react/'
177
+ ```
178
+
179
+ Finally, you need to tell Yarn to use your local copy of the NPM module in this application, using [`yarn link`](https://yarnpkg.com/en/docs/cli/link):
180
+
181
+ ```
182
+ $ cd ~/code/webpacker-react/javascript/webpacker_react-npm-module/dist/
183
+ $ yarn # builds the code
184
+ $ yarn link
185
+ success Registered "webpacker-react".
186
+ info You can now run `yarn link "webpacker-react"` in the projects where you want to use this module and it will be used instead.
187
+ $ cd ~/code/webpacker-react-example/
188
+ success Registered "webpacker-react".
189
+ ```
190
+
191
+ After launching `./bin/webpack-watcher` and `./bin/rails server` in your example app directory, you can now change the Ruby or Javascript code in your local `webpacker-react` repository, and test it immediately using the example app.
192
+
193
+ ## Testing
194
+
195
+ Make sure you run first:
196
+
197
+ ```sh
198
+ $ test/example_app/bin/yarn
199
+ $ test/example_app/bin/webpack
200
+ ```
201
+
202
+ And optionally:
203
+
204
+ ```sh
205
+ $ cd test/example_app/vendor/
206
+ $ yarn link "webpacker-react"
207
+ ```
208
+
209
+ Finally, run the test suite:
210
+
211
+ ```sh
212
+ $ rake test
213
+ ```
214
+
215
+ ## Contributing
216
+
217
+ Bug reports and pull requests are welcome on GitHub at https://github.com/renchap/webpacker-react.
218
+ Please feel free to open issues about your needs and features you would like to be added.
219
+
220
+ ### Thanks
221
+
222
+ This gem has been inspired by the awesome work on [react-rails](https://github.com/reactjs/react-rails) and [react_on_rails](https://github.com/shakacode/react_on_rails/). Many thanks to their authors!
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task default: :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "webpacker/react"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/circle.yml ADDED
@@ -0,0 +1,17 @@
1
+ machine:
2
+ node:
3
+ version: 6.1.0
4
+ checkout:
5
+ post:
6
+ - yarn:
7
+ pwd: javascript/webpacker_react-npm-module
8
+ - yarn:
9
+ pwd: javascript/webpacker_react-npm-module/dist
10
+ - yarn link:
11
+ pwd: javascript/webpacker_react-npm-module/dist
12
+ test:
13
+ pre:
14
+ - test/example_app/bin/yarn
15
+ - yarn link webpacker-react:
16
+ pwd: test/example_app/vendor
17
+ - test/example_app/bin/webpack
@@ -0,0 +1,5 @@
1
+ # webpacker-react
2
+
3
+ Javascript part for the `webpacker_react` Ruby gem.
4
+
5
+ Please see the project homepage for more details : https://github.com/renchap/webpacker-react