将刺激和导入映射添加到现有的 Rails 6.1 应用程序

地图7

我有一个 Rails 6.1 应用程序,它已经从 Rails 4 -> 5 -> 6 升级,所以它很成熟。

它仍然使用资产管道,并且随着 Rails 7.0 的未来,我将保持这种方式。我想开始使用stimulus + importmaps,所以我跟着安装。

通过热线安装导入映射和刺激后,我在 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/[email protected]/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"

我的 application.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 %>

manifest.js 是;

//= link_tree ../images
//= link_directory ../javascripts .js
//= link_directory ../stylesheets .css
//= link_tree ../javascripts

编辑

检查 Chrome 中的 Elements 选项卡,我已经加载了 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 本身不支持导入映射。您需要从https://github.com/guybedford/es-module-shims添加一个 polyfill

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

将 ruby on rails 添加到现有的 React Native 应用程序

Ruby on Rails:将api添加到现有的Web应用程序

将外部.js文件添加到新的Rails 6 Web应用程序

将 Typescript 添加到 Vue + Rails 应用程序 - 导入不再有效?

将核心数据添加到现有的选项卡式应用程序(ios swift,Xcode6)

将Google字体添加到Rails应用程序

将API端点添加到现有的Rails 5应用

Rails 6数据未在Heroku本地和应用程序上显示

将Rails应用程序从5.0.7.2更新到6,是否需要一个简单的项目?

是否有人在生产Rails应用程序中使用Babel / 6-to-5?

连接到现有的Rails应用程序

如何使用Apartment将现有的Rails应用程序迁移到多租户应用程序?

Rails 3.2.1:%1不是有效的Win32应用程序。-c:/ RailsInstaller

带有 glassFish6 的 Netbeans 12.4 - 无法构建简单的 hello1 应用程序

使用Nginx映射Rails应用程序

Rails 6应用程序由于缓存目录将所有权更改为根而失败

如何将现有的Angular1 Web应用程序转换为Cordova应用程序?

如何将现有的Rails应用程序转换为gem

如何将Docker MySQL映像连接到现有的Rails应用程序?

导入现有的Struts 1应用程序使用MyEclipse开发到Intellij IDEA 15.0.3

添加/删除ENV变量时,Elastic Beanstalk导致我的Rails 6应用程序预编译资产损坏

ReferenceError:在具有webpacker的新rails6应用程序中找不到变量:d3

将网站添加到现有的Azure移动应用程序(应用程序服务)

将Redux添加到现有的React应用程序

将GWT添加到现有的Maven Web应用程序

使用 .Net 4 将 MVC 项目添加到现有的 Web 应用程序

将TypeScript添加到现有的create-react-app应用程序

如何将LiveView添加到现有的Elixir / Phoenix应用程序?

尝试将 @ionic/angular 添加到现有的 angular 12 应用程序失败