我使用Importmap for Rails 7实现了JavaScript模块模式,但在生产环境中加载定制helpers
JS文件时出现错误.
我的应用程序/Java脚本的文件系统 struct 如下:
> javascript
> controllers
> application.js
> index.js
> first_controller.js
> second_controller.js
> ...
> helpers
> index.js
> useMixinOne.js
> useMixinTwo.js
> ...
> application.js
以下是从与问题相关的文件中摘录的内容:
# config/importmap.rb
pin "application", preload: true
pin "@hotwired/turbo-rails", to: "turbo.min.js", preload: true
pin "@hotwired/stimulus", to: "stimulus.min.js", preload: true
pin "@hotwired/stimulus-loading", to: "stimulus-loading.js", preload: true
pin_all_from "app/javascript/controllers", under: "controllers"
pin_all_from "app/javascript/helpers", under: "helpers"
# app/javascript/application.js
import "@hotwired/turbo-rails"
import "controllers"
import "helpers"
# app/javascript/controllers/first_controller.js
import { Controller } from "@hotwired/stimulus"
import { useMixinOne, useMixinTwo } from "helpers"
// Connects to data-controller="first"
export default class extends Controller {
connect() {
useMixinOne(this);
...
}
}
# app/javascript/helpers/index.js
export * from "./useMixinOne"
export * from "./useMixinTwo"
...
# app/javascript/helpers/useMixinOne.js
export const useMixinOne = controller => {
Object.assign(controller, {
customFunctionFromOne(event) {
console.log("Hello from One")
}
});
};
# app/javascript/helpers/useMixinTwo.js
export const useMixinTwo = controller => {
Object.assign(controller, {
customFunctionFromTwo(event) {
console.log("Hello from Two")
}
});
};
...
当我在开发中使用上面的内容时,它起作用了.
然而,当我在生产中使用它时,它不起作用:在浏览器控制台中,我收到如下错误
GET https://www.mywebsite.com/assets/helpers/useMixinOne net::ERR_ABORTED 404 (Not Found)
index-1ee3e17fa548653761aab66bd7ab6d677cc5dc8be43616b1c86e694aee4c4b27.js:1
GET https://www.mywebsite.com/assets/helpers/useMixinTwo net::ERR_ABORTED 404 (Not Found)
index-1ee3e17fa548653761aab66bd7ab6d677cc5dc8be43616b1c86e694aee4c4b27.js:1
...
怎么了?如何使用Importmap解决与helpers
个JSassets资源 相关的错误?