From 385d31d209a957946378307e50aa739ccfadfea1 Mon Sep 17 00:00:00 2001 From: Javan Makhmali Date: Wed, 16 Jan 2019 14:09:10 -0500 Subject: [PATCH] Minimize boilerplate setup code for JavaScript libraries --- actionview/app/assets/javascripts/README.md | 3 +-- activestorage/README.md | 3 +-- guides/source/action_cable_overview.md | 4 ++-- guides/source/active_storage_overview.md | 3 +-- .../templates/app/javascript/channels/consumer.js | 4 ++-- .../app/javascript/packs/application.js.tt | 14 ++++---------- railties/test/application/rake_test.rb | 2 +- railties/test/generators/shared_generator_tests.rb | 4 ++-- 8 files changed, 14 insertions(+), 23 deletions(-) diff --git a/actionview/app/assets/javascripts/README.md b/actionview/app/assets/javascripts/README.md index 2b110e604f..b9682b61e2 100644 --- a/actionview/app/assets/javascripts/README.md +++ b/actionview/app/assets/javascripts/README.md @@ -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: ```javascript -import Rails from "@rails/ujs" -Rails.start() +require("@rails/ujs").start() ``` ## How to run tests diff --git a/activestorage/README.md b/activestorage/README.md index 4a683dd8cd..f658b8d542 100644 --- a/activestorage/README.md +++ b/activestorage/README.md @@ -118,8 +118,7 @@ Active Storage, with its included JavaScript library, supports uploading directl ``` Using the npm package: ```js - import * as ActiveStorage from "@rails/activestorage" - ActiveStorage.start() + require("@rails/activestorage").start() ``` 2. Annotate file inputs with the direct upload URL. diff --git a/guides/source/action_cable_overview.md b/guides/source/action_cable_overview.md index df02d5bd91..8f5c44849a 100644 --- a/guides/source/action_cable_overview.md +++ b/guides/source/action_cable_overview.md @@ -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. // 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. diff --git a/guides/source/active_storage_overview.md b/guides/source/active_storage_overview.md index 6d07d34dd7..474a93c83e 100644 --- a/guides/source/active_storage_overview.md +++ b/guides/source/active_storage_overview.md @@ -489,8 +489,7 @@ directly from the client to the cloud. Using the npm package: ```js - import * as ActiveStorage from "@rails/activestorage" - ActiveStorage.start() + require("@rails/activestorage").start() ``` 2. Annotate file inputs with the direct upload URL. diff --git a/railties/lib/rails/generators/rails/app/templates/app/javascript/channels/consumer.js b/railties/lib/rails/generators/rails/app/templates/app/javascript/channels/consumer.js index eec7e54b8a..0eceb59b18 100644 --- a/railties/lib/rails/generators/rails/app/templates/app/javascript/channels/consumer.js +++ b/railties/lib/rails/generators/rails/app/templates/app/javascript/channels/consumer.js @@ -1,6 +1,6 @@ // 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. -import ActionCable from "@rails/actioncable" +import { createConsumer } from "@rails/actioncable" -export default ActionCable.createConsumer() +export default createConsumer() diff --git a/railties/lib/rails/generators/rails/app/templates/app/javascript/packs/application.js.tt b/railties/lib/rails/generators/rails/app/templates/app/javascript/packs/application.js.tt index de91713546..908487d500 100644 --- a/railties/lib/rails/generators/rails/app/templates/app/javascript/packs/application.js.tt +++ b/railties/lib/rails/generators/rails/app/templates/app/javascript/packs/application.js.tt @@ -3,19 +3,13 @@ // a relevant structure within app/javascript and only use these pack files to reference // that code so it'll be compiled. -import Rails from "@rails/ujs" -Rails.start() +require("@rails/ujs").start() <%- unless options[:skip_turbolinks] -%> - -import Turbolinks from "turbolinks" -Turbolinks.start() +require("turbolinks").start() <%- end -%> <%- unless skip_active_storage? -%> - -import * as ActiveStorage from "@rails/activestorage" -ActiveStorage.start() +require("@rails/activestorage").start() <%- end -%> <%- unless options[:skip_action_cable] -%> - -import "channels" +require("channels") <%- end -%> diff --git a/railties/test/application/rake_test.rb b/railties/test/application/rake_test.rb index 44e3b0f66b..830c36671c 100644 --- a/railties/test/application/rake_test.rb +++ b/railties/test/application/rake_test.rb @@ -118,7 +118,7 @@ def test_should_not_eager_load_model_for_rake end 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") end diff --git a/railties/test/generators/shared_generator_tests.rb b/railties/test/generators/shared_generator_tests.rb index f673832caa..26ce487c5f 100644 --- a/railties/test/generators/shared_generator_tests.rb +++ b/railties/test/generators/shared_generator_tests.rb @@ -206,7 +206,7 @@ def test_generator_for_active_storage unless generator_class.name == "Rails::Generators::PluginGenerator" 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 @@ -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}/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 assert_file "#{application_path}/config/environments/development.rb" do |content| -- GitLab