Merge pull request #39521 from rossta/patch/use-import-syntax-in-application-js-template

Use ES module import syntax in application js template
This commit is contained in:
Javan Makhmali 2020-06-18 07:43:09 -04:00 committed by GitHub
commit 664e94989e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 21 additions and 10 deletions

View File

@ -40,7 +40,8 @@ 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
require("@rails/ujs").start() import Rails from "@rails/ujs"
Rails.start()
``` ```
## How to run tests ## How to run tests

View File

@ -151,7 +151,8 @@ Active Storage, with its included JavaScript library, supports uploading directl
``` ```
Using the npm package: Using the npm package:
```js ```js
require("@rails/activestorage").start() import * as ActiveStorage from "@rails/activestorage"
ActiveStorage.start()
``` ```
2. Annotate file inputs with the direct upload URL. 2. Annotate file inputs with the direct upload URL.

View File

@ -578,7 +578,8 @@ directly from the client to the cloud.
Using the npm package: Using the npm package:
```js ```js
require("@rails/activestorage").start() import * as ActiveStorage from "@rails/activestorage"
ActiveStorage.start()
``` ```
2. Annotate file inputs with the direct upload URL. 2. Annotate file inputs with the direct upload URL.

View File

@ -3,17 +3,24 @@
// 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.
require("@rails/ujs").start() import Rails from "@rails/ujs"
<%- unless options[:skip_turbolinks] -%> <%- unless options[:skip_turbolinks] -%>
require("turbolinks").start() import Turbolinks from "turbolinks"
<%- end -%> <%- end -%>
<%- unless skip_active_storage? -%> <%- unless skip_active_storage? -%>
require("@rails/activestorage").start() import * as ActiveStorage from "@rails/activestorage"
<%- end -%> <%- end -%>
<%- unless options[:skip_action_cable] -%> <%- unless options[:skip_action_cable] -%>
require("channels") import "channels"
<%- end -%> <%- end -%>
Rails.start()
<%- unless options[:skip_turbolinks] -%>
Turbolinks.start()
<%- end -%>
<%- unless skip_active_storage? -%>
ActiveStorage.start()
<%- end -%>
// Uncomment to copy all static images under ../images to the output folder and reference // Uncomment to copy all static images under ../images to the output folder and reference
// them with the image_pack_tag helper in views (e.g <%%= image_pack_tag 'rails.png' %>) // them with the image_pack_tag helper in views (e.g <%%= image_pack_tag 'rails.png' %>)

View File

@ -157,7 +157,7 @@ module ApplicationTests
end end
def test_code_statistics_sanity def test_code_statistics_sanity
assert_match "Code LOC: 29 Test LOC: 3 Code to Test Ratio: 1:0.1", assert_match "Code LOC: 32 Test LOC: 3 Code to Test Ratio: 1:0.1",
rails("stats") rails("stats")
end end

View File

@ -203,7 +203,8 @@ module SharedGeneratorTests
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(/^require\("@rails\/activestorage"\)\.start\(\)/, content) assert_match(/^import \* as ActiveStorage from "@rails\/activestorage"/, content)
assert_match(/^ActiveStorage.start\(\)/, content)
end end
end end
@ -264,7 +265,7 @@ module SharedGeneratorTests
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(/^require\("@rails\/activestorage"\)\.start\(\)/, content) assert_no_match(/activestorage/i, content)
end end
assert_file "#{application_path}/config/environments/development.rb" do |content| assert_file "#{application_path}/config/environments/development.rb" do |content|