react_on_rails 6.3.5 → 6.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 78e5d4be2abbd58fca99fd4a02ddbc01bce6aafc
4
- data.tar.gz: c83eec060ca67a7bd3c39b807b9318c258b831f9
3
+ metadata.gz: 6a4f8bdac2245e1f3a17da6fd63341e9ce892b36
4
+ data.tar.gz: 18f926d01a9634607fa41e217c0981b751ed6dbd
5
5
  SHA512:
6
- metadata.gz: d6dff6ff73ef1b1d542b82f8abd18ff3b11bcf9447b9ea01c87a97c3d0ec79fc4229f7c545187e04a6e417cb0486d77d8d37958ad7e667916980439ddc394d1d
7
- data.tar.gz: a1eb40e9408fd5d7de117e646c90319a8f691acdf9715af70718c75434e14074693afe9a6a122ede08447e301a03e4d56d1c6483f3ed5b125317507d1d95f463
6
+ metadata.gz: 3c38887fd29781c9342661fc2f3a88cd50fa494610d1aed43342e0b0522d84762ebc1a0aa3b176a75fbec61a149ab6ee7f9c700ba4f109d2ee0d66091f8fa494
7
+ data.tar.gz: d5cdcc84850799bea50d3d346a45dbbdb6627cb409138e6c0ec4ebbca2f8720018fa35cfac24fccbf5053b8822635d2d4e0a2580f266e68d61bf17b14f5df75e
data/CHANGELOG.md CHANGED
@@ -6,7 +6,15 @@ Contributors: please follow the recommendations outlined at [keepachangelog.com]
6
6
  ## [Unreleased]
7
7
  *Please add entries here for your pull requests.*
8
8
 
9
- ## [6.3.5] - 2016-1-6
9
+ ## [6.4.0] - 2017-1-12
10
+
11
+ ### Fixed
12
+ - Removed foreman as a dependency. [#678](https://github.com/shakacode/react_on_rails/pull/678) by [x2es](https://github.com/x2es).
13
+
14
+ ### Added
15
+ - Automatically generate __i18n__ javascript files for `react-intl` when the serve starts up. [#642](https://github.com/shakacode/react_on_rails/pull/642) by [JasonYCHuang](https://github.com/JasonYCHuang).
16
+
17
+ ## [6.3.5] - 2017-1-6
10
18
  ### Fixed
11
19
  - The redux generator now creates a HelloWorld component that uses redux rather than local state. [#669](https://github.com/shakacode/react_on_rails/issues/669) by [justin808](https://github.com/justin808).
12
20
 
@@ -417,7 +425,8 @@ Best done with Object destructing:
417
425
  ##### Fixed
418
426
  - Fix several generator related issues.
419
427
 
420
- [Unreleased]: https://github.com/shakacode/react_on_rails/compare/6.3.5...master
428
+ [Unreleased]: https://github.com/shakacode/react_on_rails/compare/6.4.0...master
429
+ [6.4.0]: https://github.com/shakacode/react_on_rails/compare/6.3.5...6.4.0
421
430
  [6.3.5]: https://github.com/shakacode/react_on_rails/compare/6.3.4...6.3.5
422
431
  [6.3.4]: https://github.com/shakacode/react_on_rails/compare/6.3.3...6.3.4
423
432
  [6.3.3]: https://github.com/shakacode/react_on_rails/compare/6.3.2...6.3.3
data/CONTRIBUTING.md CHANGED
@@ -126,11 +126,11 @@ npm install -g phantomjs
126
126
 
127
127
  Note this *must* be installed globally for the dummy test project rspec runner to see it properly.
128
128
 
129
- ### NPM link
130
- Because the example and dummy apps rely on the react-on-rails node package, they should link directly to your local version to pick up any changes you may have made to that package. To achieve this, switch to the app's root directory and run:
129
+ ### Local Node Package
130
+ Because the example and dummy apps rely on the react-on-rails node package, they should link directly to your local version to pick up any changes you may have made to that package. To achieve this, switch to the teat app's root directory and run this command below which runs something like [this script](spec/dummy/package.json#L14)
131
131
 
132
132
  ```sh
133
- npm run node_package
133
+ npm run install-react-on-rails
134
134
  ```
135
135
 
136
136
  From now on, the example and dummy apps will use your local node_package folder as the react-on-rails node package. This will also be done automatically for you via the `rake examples:prepare_all` rake task.
@@ -141,6 +141,7 @@ From now on, the example and dummy apps will use your local node_package folder
141
141
  resolve: {
142
142
  alias: {
143
143
  react: path.resolve('./node_modules/react'),
144
+ 'react-dom': path.resolve('./node_modules/react-dom'),
144
145
  },
145
146
  },
146
147
  ```
@@ -182,11 +183,11 @@ npm run test
182
183
 
183
184
  ```sh
184
185
  cd <top level>
185
- node_package/scripts/ci
186
+ npm run check
186
187
  ```
187
188
 
188
189
  ### Starting the Dummy App
189
- To run the test app, it's **CRITICAL** to not just run `rails s`. You have to run `foreman start`. If you don't do this, then `webpack` will not generate a new bundle, and you will be seriously confused when you change JavaScript and the app does not change. If you change the webpack configs, then you need to restart foreman. If you change the JS code for react-on-rails, you need to run `node_package/scripts/build`. Since the react-on-rails package should be sym linked, you don't have to `npm i react-on-rails` after every change.
190
+ To run the test app, it's **CRITICAL** to not just run `rails s`. You have to run `foreman start`. If you don't do this, then `webpack` will not generate a new bundle, and you will be seriously confused when you change JavaScript and the app does not change. If you change the webpack configs, then you need to restart foreman. If you change the JS code for react-on-rails, you need to run `npm run build`. Since the react-on-rails package should be sym linked, you don't have to `npm i react-on-rails` after every change.
190
191
 
191
192
  ### RSpec Testing
192
193
  Run `rake` for testing the gem and `spec/dummy`. Otherwise, the `rspec` command only works for testing within the sample apps, like `spec/dummy`.
@@ -217,7 +218,7 @@ The main installer can be run with ```rails generate react_on_rails:install```
217
218
  The generators are covered by generator tests using Rails's generator testing helpers, but it never hurts to do a sanity check and explore the API. See [generator_testing_script.md](generator_testing_script.md) for a script on how to run the generator on a fresh project.
218
219
 
219
220
  ### Linting
220
- All linting is performed from the docker container for CI. You will need docker and docker-compose installed locally to lint code changes via the lint container. You can lint locally by running `node_package/scripts/lint`
221
+ All linting is performed from the docker container for CI. You will need docker and docker-compose installed locally to lint code changes via the lint container. You can lint locally by running `npm run lint && npm run flow`
221
222
 
222
223
  * [Install Docker Toolbox for Mac](https://www.docker.com/toolbox)
223
224
  * [Install Docker Compose for Linux](https://docs.docker.com/compose/install/)
data/Gemfile CHANGED
@@ -25,7 +25,7 @@ gem "mini_racer"
25
25
  if ENV["ENABLE_TURBOLINKS_5"].nil? || ENV["ENABLE_TURBOLINKS_5"].strip.empty?
26
26
  gem "turbolinks", "2.5.3"
27
27
  else
28
- gem "turbolinks", "~> 5.0.0.beta"
28
+ gem "turbolinks", "~> 5.0"
29
29
  end
30
30
  gem "uglifier", ">= 2.7.2"
31
31
  gem "web-console", "~> 2.0", group: :development
data/README.md CHANGED
@@ -50,6 +50,7 @@ React on Rails integrates Facebook's [React](https://github.com/facebook/react)
50
50
  - [Installation Summary](#installation-summary)
51
51
  - [Initializer Configuration: config/initializers/react_on_rails.rb](#initializer-configuration)
52
52
  - [Including your React Component in your Rails Views](#including-your-react-component-in-your-rails-views)
53
+ - [I18n](#i18n)
53
54
  + [How it Works](#how-it-works)
54
55
  - [Client-Side Rendering vs. Server-Side Rendering](#client-side-rendering-vs-server-side-rendering)
55
56
  - [Building the Bundles](#building-the-bundles)
@@ -120,6 +121,8 @@ We're definitely not doing that. With react_on_rails, webpack is mainly generati
120
121
 
121
122
  6. Start your Rails server:
122
123
 
124
+ with foreman installed (`gem install foreman`)
125
+
123
126
  ```bash
124
127
  foreman start -f Procfile.dev
125
128
  ```
@@ -168,7 +171,15 @@ Configure the `config/initializers/react_on_rails.rb`. You can adjust some neces
168
171
  // inside your React component
169
172
  this.props.name // "Stranger"
170
173
  ```
171
-
174
+
175
+ ### I18n
176
+
177
+ You can enable the i18n functionality with [react-intl](https://github.com/yahoo/react-intl).
178
+
179
+ React on Rails provides an option for automatic conversions of Rails `*.yml` locale files into `*.js` files for `react-intl`.
180
+
181
+ See the [How to add I18n](docs/basics/i18n.md) for a summary of adding I18n.
182
+
172
183
  ## NPM
173
184
  All JavaScript in React On Rails is loaded from npm: [react-on-rails](https://www.npmjs.com/package/react-on-rails). To manually install this (you did not use the generator), assuming you have a standard configuration, run this command:
174
185
 
@@ -243,11 +254,6 @@ The `railsContext` has: (see implementation in file [react_on_rails_helper.rb](a
243
254
  pathname: uri.path, # /posts
244
255
  search: uri.query, # id=30&limit=5
245
256
 
246
- # Locale settings
247
- i18nLocale: I18n.locale,
248
- i18nDefaultLocale: I18n.default_locale,
249
- httpAcceptLanguage: request.env["HTTP_ACCEPT_LANGUAGE"],
250
-
251
257
  # Other
252
258
  serverSide: boolean # Are we being called on the server or client? NOTE, if you conditionally
253
259
  # render something different on the server than the client, then React will only show the
@@ -259,9 +265,6 @@ The `railsContext` has: (see implementation in file [react_on_rails_helper.rb](a
259
265
  ##### Needing the current url path for server rendering
260
266
  Suppose you want to display a nav bar with the current navigation link highlighted by the URL. When you server render the code, you will need to know the current URL/path if that is what you want your logic to be based on. The new `railsContext` has this information so the application of an "active" class can be done server side.
261
267
 
262
- ##### Needing the I18n.locale
263
- Suppose you want to server render your react components with localization applied given the current Rails locale. The `railsContext` contains the I18n.locale.
264
-
265
268
  ##### Configuring different code for server side rendering
266
269
  Suppose you want to turn off animation when doing server side rendering. The `serverSide` value is just what you need.
267
270
 
@@ -0,0 +1,15 @@
1
+ # Foreman Issues
2
+
3
+ ## It is not recomended to include foreman into Gemfile
4
+
5
+ See: https://github.com/ddollar/foreman
6
+
7
+ > Ruby users should take care not to install foreman in their project's Gemfile.
8
+
9
+ ## Known issues
10
+
11
+ * With `foreman 0.82.0` npm `react-s3-uploader` was failing to finish upload file to S3 when server was started by `foreman -f Procfile.dev`,
12
+ at the same time the same code works fine when ruby server started by `bundle exec rails s`.
13
+
14
+ * The same Procfile with different versions of `foreman` in combination with different versions of `bundler` may produce different output of `ps aux`.
15
+ This may brake bash tools which rely on `ps` output.
@@ -0,0 +1,90 @@
1
+ # How to add I18n
2
+
3
+ Here's a summary of adding the I18n functionality.
4
+
5
+ You can refer to [react-webpack-rails-tutorial](https://github.com/shakacode/react-webpack-rails-tutorial) for a complete example.
6
+
7
+ 1. Add `react-intl` & `intl` to `client/package.json`, and remember to `bundle && npm install`.
8
+
9
+ ```js
10
+ "dependencies": {
11
+ ...
12
+ "intl": "^1.2.5",
13
+ "react-intl": "^2.1.5",
14
+ ...
15
+ }
16
+ ```
17
+
18
+ 2. In `client/webpack.client.base.config.js`, set `react-intl` as an entry point.
19
+
20
+ ```js
21
+ module.exports = {
22
+ ...
23
+ entry: {
24
+ ...
25
+ vendor: [
26
+ ...
27
+ 'react-intl',
28
+ ],
29
+ ...
30
+ ```
31
+
32
+ 3. `react-intl` requires locale files in json format. React on Rails will help you to generate or update `translations.js` & `default.js` automatically after you configured the following settings.
33
+
34
+ > `translations.js`: All your locales in json format.
35
+ >
36
+ > `default.js`: Default settings in json format.
37
+ >
38
+ > You can add them to `.gitignore` and `.eslintignore`.
39
+
40
+ Update settings in `config/initializers/react_on_rails.rb` to what you need:
41
+
42
+ ```ruby
43
+ # Replace the following line to the location where you keep translation.js & default.js.
44
+ config.i18n_dir = Rails.root.join("PATH_TO", "YOUR_JS_I18N_FOLDER")
45
+ ```
46
+
47
+ Add following lines to `config/application.rb`, this will help you to generate `translations.js` & `default.js` automatically when you starts the server.
48
+
49
+ ```js
50
+ module YourModule
51
+ class Application < Rails::Application
52
+ ...
53
+ config.after_initialize do
54
+ ReactOnRails::LocalesToJs.new
55
+ end
56
+ end
57
+ end
58
+ ```
59
+
60
+ 5. In React, you need to initialize `react-intl`, and set parameters for it.
61
+
62
+ ```js
63
+ ...
64
+ import { addLocaleData } from 'react-intl';
65
+ import en from 'react-intl/locale-data/en';
66
+ import de from 'react-intl/locale-data/de';
67
+ import { translations } from 'path_to/i18n/translations';
68
+ import { defaultLocale } from 'path_to/i18n/default';
69
+ ...
70
+ // Initizalize all locales for react-intl.
71
+ addLocaleData([...en, ...de]);
72
+ ...
73
+ // set locale and messages for IntlProvider.
74
+ const locale = method_to_get_current_locale() || defaultLocale;
75
+ const messages = translations[locale];
76
+ ...
77
+ return (
78
+ <IntlProvider locale={locale} key={locale} messages={messages}>
79
+ <CommentScreen {...{ actions, data }} />
80
+ </IntlProvider>
81
+ )
82
+ ```
83
+ ```js
84
+ // In your component.
85
+ import { defaultMessages } from 'path_to/i18n/default';
86
+ ...
87
+ return (
88
+ { formatMessage(defaultMessages.yourLocaleKeyInCamelCase) }
89
+ )
90
+ ```
@@ -59,6 +59,12 @@ ReactOnRails.configure do |config|
59
59
  config.server_renderer_pool_size = 1 # increase if you're on JRuby
60
60
  config.server_renderer_timeout = 20 # seconds
61
61
 
62
+ ################################################################################
63
+ # I18N OPTIONS
64
+ ################################################################################
65
+ # Replace the following line to the location where you keep translation.js & default.js.
66
+ config.i18n_dir = Rails.root.join("client", "app", "libs", "i18n")
67
+
62
68
  ################################################################################
63
69
  # MISCELLANEOUS OPTIONS
64
70
  ################################################################################
@@ -16,3 +16,4 @@ require "react_on_rails/test_helper/webpack_assets_compiler"
16
16
  require "react_on_rails/test_helper/webpack_assets_status_checker"
17
17
  require "react_on_rails/test_helper/ensure_assets_compiled"
18
18
  require "react_on_rails/test_helper/node_process_launcher"
19
+ require "react_on_rails/locales_to_js"
@@ -72,6 +72,7 @@ module ReactOnRails
72
72
  server_render_method: "",
73
73
  symlink_non_digested_assets_regex: /\.(png|jpg|jpeg|gif|tiff|woff|ttf|eot|svg|map)/,
74
74
  npm_build_test_command: "",
75
+ i18n_dir: "",
75
76
  npm_build_production_command: ""
76
77
  )
77
78
  end
@@ -84,6 +85,7 @@ module ReactOnRails
84
85
  :skip_display_none, :generated_assets_dirs, :generated_assets_dir,
85
86
  :webpack_generated_files, :rendering_extension, :npm_build_test_command,
86
87
  :npm_build_production_command,
88
+ :i18n_dir,
87
89
  :server_render_method, :symlink_non_digested_assets_regex
88
90
 
89
91
  def initialize(server_bundle_js_file: nil, prerender: nil, replay_console: nil,
@@ -94,12 +96,14 @@ module ReactOnRails
94
96
  generated_assets_dir: nil, webpack_generated_files: nil,
95
97
  rendering_extension: nil, npm_build_test_command: nil,
96
98
  npm_build_production_command: nil,
99
+ i18n_dir: nil,
97
100
  server_render_method: nil, symlink_non_digested_assets_regex: nil)
98
101
  self.server_bundle_js_file = server_bundle_js_file
99
102
  self.generated_assets_dirs = generated_assets_dirs
100
103
  self.generated_assets_dir = generated_assets_dir
101
104
  self.npm_build_test_command = npm_build_test_command
102
105
  self.npm_build_production_command = npm_build_production_command
106
+ self.i18n_dir = i18n_dir
103
107
 
104
108
  self.prerender = prerender
105
109
  self.replay_console = replay_console
@@ -0,0 +1,119 @@
1
+ require "erb"
2
+
3
+ module ReactOnRails
4
+ class LocalesToJs
5
+ def initialize
6
+ return unless obsolete?
7
+ @translations, @defaults = generate_translations
8
+ convert
9
+ end
10
+
11
+ private
12
+
13
+ def obsolete?
14
+ return true if exist_js_files.empty?
15
+ js_files_are_outdated
16
+ end
17
+
18
+ def exist_js_files
19
+ @exist_js_files ||= js_files.select(&File.method(:exist?))
20
+ end
21
+
22
+ def js_files_are_outdated
23
+ latest_yml = locale_files.map(&File.method(:mtime)).max
24
+ earliest_js = exist_js_files.map(&File.method(:mtime)).min
25
+ latest_yml > earliest_js
26
+ end
27
+
28
+ def js_file_names
29
+ %w(translations default)
30
+ end
31
+
32
+ def js_files
33
+ @js_files ||= js_file_names.map { |n| js_file(n) }
34
+ end
35
+
36
+ def js_file(name)
37
+ "#{i18n_dir}/#{name}.js"
38
+ end
39
+
40
+ def locale_files
41
+ @locale_files ||= Rails.application.config.i18n.load_path
42
+ end
43
+
44
+ def i18n_dir
45
+ @i18n_dir ||= ReactOnRails.configuration.i18n_dir
46
+ end
47
+
48
+ def default_locale
49
+ @default_locale ||= I18n.default_locale.to_s || "en"
50
+ end
51
+
52
+ def convert
53
+ js_file_names.each do |name|
54
+ template = send("template_#{name}")
55
+ path = js_file(name)
56
+ generate_js_file(template, path)
57
+ end
58
+ end
59
+
60
+ def generate_js_file(template, path)
61
+ result = ERB.new(template).result()
62
+ File.open(path, "w") do |f|
63
+ f.write(result)
64
+ end
65
+ end
66
+
67
+ def generate_translations
68
+ translations = {}
69
+ defaults = {}
70
+ locale_files.each do |f|
71
+ translation = YAML.load(File.open(f))
72
+ key = translation.keys[0]
73
+ val = flatten(translation[key])
74
+ translations = translations.deep_merge(key => val)
75
+ defaults = defaults.deep_merge(flatten_defaults(val)) if key == default_locale
76
+ end
77
+ [translations.to_json, defaults.to_json]
78
+ end
79
+
80
+ def format(input)
81
+ input.to_s.tr(".", "_").camelize(:lower).to_sym
82
+ end
83
+
84
+ def flatten_defaults(val)
85
+ flatten(val).each_with_object({}) do |(k, v), h|
86
+ key = format(k)
87
+ h[key] = { id: k, defaultMessage: v }
88
+ end
89
+ end
90
+
91
+ def flatten(translations)
92
+ translations.each_with_object({}) do |(k, v), h|
93
+ if v.is_a? Hash
94
+ flatten(v).map { |hk, hv| h["#{k}.#{hk}".to_sym] = hv }
95
+ else
96
+ h[k] = v
97
+ end
98
+ end
99
+ end
100
+
101
+ def template_translations
102
+ <<-JS
103
+ export const translations = #{@translations};
104
+ JS
105
+ end
106
+
107
+ def template_default
108
+ <<-JS
109
+ import { defineMessages } from 'react-intl';
110
+
111
+ const defaultLocale = \'#{default_locale}\';
112
+
113
+ const defaultMessages = defineMessages(#{@defaults});
114
+
115
+ export { defaultMessages, defaultLocale };
116
+ JS
117
+ end
118
+ end
119
+ end
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
  module ReactOnRails
3
- VERSION = "6.3.5".freeze
3
+ VERSION = "6.4.0".freeze
4
4
  end
data/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-on-rails",
3
- "version": "6.3.5",
3
+ "version": "6.4.0",
4
4
  "description": "react-on-rails JavaScript for react_on_rails Ruby gem",
5
5
  "main": "node_package/lib/ReactOnRails.js",
6
6
  "directories": {
@@ -25,7 +25,6 @@ Gem::Specification.new do |s|
25
25
  s.add_dependency "execjs", "~> 2.5"
26
26
  s.add_dependency "rainbow", "~> 2.1"
27
27
  s.add_dependency "rails", ">= 3.2"
28
- s.add_dependency "foreman"
29
28
  s.add_dependency "addressable"
30
29
 
31
30
  s.add_development_dependency "bundler", "~> 1.10"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: react_on_rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 6.3.5
4
+ version: 6.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Justin Gordon
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-01-08 00:00:00.000000000 Z
11
+ date: 2017-01-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: connection_pool
@@ -66,20 +66,6 @@ dependencies:
66
66
  - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: '3.2'
69
- - !ruby/object:Gem::Dependency
70
- name: foreman
71
- requirement: !ruby/object:Gem::Requirement
72
- requirements:
73
- - - ">="
74
- - !ruby/object:Gem::Version
75
- version: '0'
76
- type: :runtime
77
- prerelease: false
78
- version_requirements: !ruby/object:Gem::Requirement
79
- requirements:
80
- - - ">="
81
- - !ruby/object:Gem::Version
82
- version: '0'
83
69
  - !ruby/object:Gem::Dependency
84
70
  name: addressable
85
71
  requirement: !ruby/object:Gem::Requirement
@@ -336,6 +322,7 @@ files:
336
322
  - docs/LICENSE
337
323
  - docs/additional-reading/babel.md
338
324
  - docs/additional-reading/code-splitting.md
325
+ - docs/additional-reading/foreman-issues.md
339
326
  - docs/additional-reading/heroku-deployment.md
340
327
  - docs/additional-reading/hot-reloading-rails-development.md
341
328
  - docs/additional-reading/node-dependencies-and-npm.md
@@ -357,6 +344,7 @@ files:
357
344
  - docs/api/ruby-api-hot-reload-view-helpers.md
358
345
  - docs/api/ruby-api.md
359
346
  - docs/basics/generator.md
347
+ - docs/basics/i18n.md
360
348
  - docs/basics/installation-overview.md
361
349
  - docs/basics/migrating-from-react-rails.md
362
350
  - docs/coding-style/style.md
@@ -407,6 +395,7 @@ files:
407
395
  - lib/react_on_rails/controller.rb
408
396
  - lib/react_on_rails/engine.rb
409
397
  - lib/react_on_rails/git_utils.rb
398
+ - lib/react_on_rails/locales_to_js.rb
410
399
  - lib/react_on_rails/prerender_error.rb
411
400
  - lib/react_on_rails/react_component/options.rb
412
401
  - lib/react_on_rails/server_rendering_pool.rb