提交 385d31d2 编写于 作者: J Javan Makhmali

Minimize boilerplate setup code for JavaScript libraries

上级 90536ebf
...@@ -40,8 +40,7 @@ In a conventional Rails application that uses the asset pipeline, require `rails ...@@ -40,8 +40,7 @@ In a conventional Rails application that uses the asset pipeline, require `rails
If you're using the Webpacker gem or some other JavaScript bundler, add the following to your main JS file: If you're using the Webpacker gem or some other JavaScript bundler, add the following to your main JS file:
```javascript ```javascript
import Rails from "@rails/ujs" require("@rails/ujs").start()
Rails.start()
``` ```
## How to run tests ## How to run tests
......
...@@ -118,8 +118,7 @@ Active Storage, with its included JavaScript library, supports uploading directl ...@@ -118,8 +118,7 @@ Active Storage, with its included JavaScript library, supports uploading directl
``` ```
Using the npm package: Using the npm package:
```js ```js
import * as ActiveStorage from "@rails/activestorage" require("@rails/activestorage").start()
ActiveStorage.start()
``` ```
2. Annotate file inputs with the direct upload URL. 2. Annotate file inputs with the direct upload URL.
......
...@@ -181,9 +181,9 @@ established using the following JavaScript, which is generated by default by Rai ...@@ -181,9 +181,9 @@ established using the following JavaScript, which is generated by default by Rai
// Action Cable provides the framework to deal with WebSockets in Rails. // Action Cable provides the framework to deal with WebSockets in Rails.
// You can generate new channels where WebSocket features live using the `rails generate channel` command. // You can generate new channels where WebSocket features live using the `rails generate channel` command.
import ActionCable from "@rails/actioncable" import { createConsumer } from "@rails/actioncable"
export default ActionCable.createConsumer() export default createConsumer()
``` ```
This will ready a consumer that'll connect against `/cable` on your server by default. This will ready a consumer that'll connect against `/cable` on your server by default.
......
...@@ -489,8 +489,7 @@ directly from the client to the cloud. ...@@ -489,8 +489,7 @@ directly from the client to the cloud.
Using the npm package: Using the npm package:
```js ```js
import * as ActiveStorage from "@rails/activestorage" require("@rails/activestorage").start()
ActiveStorage.start()
``` ```
2. Annotate file inputs with the direct upload URL. 2. Annotate file inputs with the direct upload URL.
......
// Action Cable provides the framework to deal with WebSockets in Rails. // Action Cable provides the framework to deal with WebSockets in Rails.
// You can generate new channels where WebSocket features live using the `rails generate channel` command. // You can generate new channels where WebSocket features live using the `rails generate channel` command.
import ActionCable from "@rails/actioncable" import { createConsumer } from "@rails/actioncable"
export default ActionCable.createConsumer() export default createConsumer()
...@@ -3,19 +3,13 @@ ...@@ -3,19 +3,13 @@
// a relevant structure within app/javascript and only use these pack files to reference // a relevant structure within app/javascript and only use these pack files to reference
// that code so it'll be compiled. // that code so it'll be compiled.
import Rails from "@rails/ujs" require("@rails/ujs").start()
Rails.start()
<%- unless options[:skip_turbolinks] -%> <%- unless options[:skip_turbolinks] -%>
require("turbolinks").start()
import Turbolinks from "turbolinks"
Turbolinks.start()
<%- end -%> <%- end -%>
<%- unless skip_active_storage? -%> <%- unless skip_active_storage? -%>
require("@rails/activestorage").start()
import * as ActiveStorage from "@rails/activestorage"
ActiveStorage.start()
<%- end -%> <%- end -%>
<%- unless options[:skip_action_cable] -%> <%- unless options[:skip_action_cable] -%>
require("channels")
import "channels"
<%- end -%> <%- end -%>
...@@ -118,7 +118,7 @@ def test_should_not_eager_load_model_for_rake ...@@ -118,7 +118,7 @@ def test_should_not_eager_load_model_for_rake
end end
def test_code_statistics_sanity def test_code_statistics_sanity
assert_match "Code LOC: 32 Test LOC: 0 Code to Test Ratio: 1:0.0", assert_match "Code LOC: 29 Test LOC: 0 Code to Test Ratio: 1:0.0",
rails("stats") rails("stats")
end end
......
...@@ -206,7 +206,7 @@ def test_generator_for_active_storage ...@@ -206,7 +206,7 @@ def test_generator_for_active_storage
unless generator_class.name == "Rails::Generators::PluginGenerator" unless generator_class.name == "Rails::Generators::PluginGenerator"
assert_file "#{application_path}/app/javascript/packs/application.js" do |content| assert_file "#{application_path}/app/javascript/packs/application.js" do |content|
assert_match(/^import \* as ActiveStorage from "@rails\/activestorage"\nActiveStorage.start\(\)/, content) assert_match(/^require\("@rails\/activestorage"\)\.start\(\)/, content)
end end
end end
...@@ -267,7 +267,7 @@ def test_generator_does_not_generate_active_storage_contents_if_skip_active_reco ...@@ -267,7 +267,7 @@ def test_generator_does_not_generate_active_storage_contents_if_skip_active_reco
assert_file "#{application_path}/config/application.rb", /#\s+require\s+["']active_storage\/engine["']/ assert_file "#{application_path}/config/application.rb", /#\s+require\s+["']active_storage\/engine["']/
assert_file "#{application_path}/app/javascript/packs/application.js" do |content| assert_file "#{application_path}/app/javascript/packs/application.js" do |content|
assert_no_match(/^import * as ActiveStorage from "@rails\/activestorage"\nActiveStorage.start\(\)/, content) assert_no_match(/^require\("@rails\/activestorage"\)\.start\(\)/, content)
end end
assert_file "#{application_path}/config/environments/development.rb" do |content| assert_file "#{application_path}/config/environments/development.rb" do |content|
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册