ruby-on-rails Rails 7/StimulusJS相对导入:从事开发工作,而不是生产

x759pob2  于 2023-05-08  发布在  Ruby
关注(0)|答案(1)|浏览(175)

我在离开几年后再次使用Rails(上次使用Rails 4)。我有多个刺激控制器,它们引用一个名为metric_defaults.js的文件。该文件只包含一组平面定义,例如:

Chart.defaults.elements.line.tension = 0.25;
Chart.defaults.elements.line.borderWidth = 5;

在rails 7开发环境中,这个导入可以很好地处理来自每个刺激控制器的import '../metric_defaults.js',但是在生产环境中,我得到了:

Failed to load resource: the server responded with a status of 404 (Not Found) (metric_defaults.js)

我花了一天的时间试图找到它,但所有的努力都失败了。一些花絮:

  • 我使用的是标准的Rails 7配置(importmaps、sprocketball)
  • 我已经确认生产服务器正在正确地预编译资产。这包括metric_defaults. js,但它的名称中有一个指纹(metric_defaults-9032 be 9 e.... js),而404似乎试图访问未指纹的文件?
  • 生产服务器是Heroku示例
  • metric_defaults. js文件的路径为app/javascript/metric_defaults. js
  • 刺激控制器位于app/javascript/controllers/
  • 如前所述,在开发中运行良好
  • 我不知所措

有什么想法吗

4xy9mtcn

4xy9mtcn1#

设置:

# config/importmap.rb
pin "application"
pin "plugin"
// app/javascript/application.js
import "./plugin";

请参见生成的导入Map:

$ bin/importmap json
{
  "imports": {
    "application": "/assets/application-6aad68dfc16d361773f71cfe7fe74ae0ace4eea0b74067bc717475bbbbf4e580.js",
    "plugin":      "/assets/plugin-04024382391bb910584145d8113cf35ef376b55d125bb4516cebeb14ce788597.js"
  }#  ^              ^
}  # imports        urls
   # for you        for browser

很简单

import "plugin";
// will match "plugin" from import-maps
"plugin": "/assets/plugin-04024382391bb910584145d8113cf35ef376b55d125bb4516cebeb14ce788597.js"
// and turn it into
import "/assets/plugin-04024382391bb910584145d8113cf35ef376b55d125bb4516cebeb14ce788597.js";
// browser sends request to this url ^

但是:

import "./plugin";
// is relative to `application.js`, because that's where the import is.
// application.js is imported correctly with `import "application"` in the layout
"application": "/assets/application-6aad68dfc16d361773f71cfe7fe74ae0ace4eea0b74067bc717475bbbbf4e580.js"
//                      ^^^^^^^^^^^
// so "./plugin" is relative to this, which resolves to "/assets/plugin"

import "/assets/plugin"; // doesn't match
               "plugin"  // the import-map

import "/assets/plugin";
//      ^
// browser sends request in development and production

我很确定这是我第一次关闭config.assets.quiet

Started GET "/assets/application-6aad68dfc16d361773f71cfe7fe74ae0ace4eea0b74067bc717475bbbbf4e580.js" for 127.0.0.1 at 2023-04-27 00:28:21 -0400
Started GET "/assets/es-module-shims.js-32db422c5db541b7129a2ce936aed905edc2cd481748f8d67ffe84e28313158a.map" for 127.0.0.1 at 2023-04-27 00:28:21 -0400
Started GET "/assets/plugin" for 127.0.0.1 at 2023-04-27 00:28:21 -0400
#                    ^
# NOTE: see how this one didn't get mapped to anything, it is just a plain url.

在开发中,/assets被路由到 * sprocket *,它可以处理消化和未消化的资产,并且工作正常:

>> Rails.application.routes.routes.detect {|i| i.ast.to_s =~ /assets/ }.app.app.class
=> Sprockets::Environment
#      ^ same thing  v                     undigested path  v
>> Rails.application.assets.call(Rack::MockRequest.env_for("plugin")).last
=> #<Sprockets::Asset:fe718 "file:///home/alex/code/stackoverflow/app/javascript/plugin.js?type=application/javascript&id=187d193631f6880345ca4c2a2ac5d3a7c06ec09a64d4fbbd2cc1eed3a614997e">

在生产环境中,Web服务器代替它完成工作,它只有预编译的资产,/assets/plugin得到404。

>> Rails.application.routes.routes.detect {|i| i.ast.to_s =~ /assets/ }
=> nil

修复#1

停止使用相对导入。

修复#2

创建一个与相对导入匹配的导入Map:

import "./plugin";      // this will be
import "/assets/plugin" // resolved to this
       "/assets/plugin" // and will match the import-map
Started GET "/assets/application-6aad68dfc16d361773f71cfe7fe74ae0ace4eea0b74067bc717475bbbbf4e580.js" for 127.0.0.1 at 2023-04-27 03:52:47 -0400
Started GET "/assets/plugin-c8122d51d5713808bd0206fb036b098e74b576f45c42480e977eb11b9040f1f4.js" for 127.0.0.1 at 2023-04-27 03:52:47 -0400
Started GET "/assets/es-module-shims.js-32db422c5db541b7129a2ce936aed905edc2cd481748f8d67ffe84e28313158a.map" for 127.0.0.1 at 2023-04-27 03:52:47 -0400

如果你想走这条路,我会借用@cesoid的helper方法,它会让你开始:

# config/importmap.rb

def pin_all_relative(dir_name)
  pin_all_from "app/javascript/#{dir_name}",
    under: "#{Rails.application.config.assets.prefix}/#{dir_name}",
    to: dir_name
end

pin_all_relative "controllers"
# etc

相关问题