[英]Adding stimulus and importmaps to an existing Rails 6.1 app
我有一個 Rails 6.1 應用程序,它已從 Rails 4 -> 5 -> 6 升級,所以它很成熟。
它仍然使用資產管道,隨着 Rails 7.0 的未來,我將保持這種方式。 我想開始使用stimulus + importmaps ,所以我跟着安裝。
通過熱線安裝 importmaps 和 stimulus 后,我在加載我的應用程序時在 Firefox 91 中收到以下錯誤;
Uncaught SyntaxError: import declarations may only appear at top level of a module
Uncaught TypeError: Error resolving module specifier “application”. Relative module specifiers must start with “./”, “../” or “/”.
我的importmap.rb
Rails.application.config.importmap.draw do
pin "@hotwired/stimulus", to: "stimulus.js"
pin "@hotwired/stimulus-importmap-autoloader", to: "stimulus-importmap-autoloader.js"
pin_all_from "app/javascript/controllers", under: "controllers"
pin "application"
# Use libraries available via the asset pipeline (locally or via gems). # Rails 7.0 required
# pin "@rails/actioncable", to: "actioncable.esm.js"
# pin "@rails/activestorage", to: "activestorage.esm.js"
# Use libraries directly from JavaScript CDNs (see https://www.skypack.dev, https://esm.sh, https://www.jsdelivr.com/esm)
# pin "vue", to: "https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.esm.browser.js"
# pin "d3", to: "https://esm.sh/d3?bundle"
# Pin vendored modules by first adding the following to app/assets/config/manifest.js:
# //= link_tree ../../../vendor/assets/javascripts .js
# pin_all_from "vendor/assets/javascripts"
end
我將所有 javascript 保存在 app/assets/javascripts 目錄中,該目錄仍應由資產管道處理。
我有以下 app/assets/javascripts/importmap.json.erb
{
"imports": {
"turbo": "<%= asset_path "turbo" %>",
<%= importmap_list_with_stimulus_from "app/assets/javascripts/controllers", "app/assets/javascripts/libraries" %>
}
}
application.js 是;
// This is the main application.js, there can only be one.
//
// Configure your import map in config/importmap.rb
// import "@rails/actioncable" // Rails 7.0 required
// import "@rails/activestorage" // Rails 7.0 required
import "@hotwired/stimulus-importmap-autoloader"
我的應用程序中也有以下行。html.erb
<%= stylesheet_link_tag "application" %>
<%= javascript_include_tag "application" %>
<%= csrf_meta_tags %>
<%= javascript_include_tag "turbo", type: "module-shim" %>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<%= yield :head %>
<%= javascript_importmap_tags %>
清單.js 是;
//= link_tree ../images
//= link_directory ../javascripts .js
//= link_directory ../stylesheets .css
//= link_tree ../javascripts
編輯
檢查 Chrome 中的元素選項卡,我已經加載了 es-module-shims 但在 Firefox 中我仍然收到錯誤;
Uncaught SyntaxError: import declarations may only appear at top level of a module
Uncaught TypeError: Error resolving module specifier “application”. Relative module specifiers must start with “./”, “../” or “/”.
這是加載 es-module-shim 的代碼
<script src="/assets/es-module-shims-e320a414bc4656be79c9c722c91afd9bc40140edf48616fbf72fb2da3c1fdcaa.js" async="async" data-turbo-track="reload"></script>
<script type="module">import "application"</script>
編輯
可以安全地忽略以下錯誤;
Uncaught TypeError: Error resolving module specifier “application”. Relative module specifiers must start with “./”, “../” or “/”.
參考: https://github.com/rails/importmap-rails#expected-errors-from-using-the-es-module-shim
編輯
當我將 application.html.erb 更改為以下內容時,另一個錯誤消失了
<%= csrf_meta_tags %>
<%= csp_meta_tag %>
<%= stylesheet_link_tag "application" %>
<%= javascript_importmap_tags %>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<%= yield :head %>
Firefox 本身不支持 importmaps。 您需要從https://github.com/guybedford/es-module-shims添加一個 polyfill。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.