如何将所有JS控制器文件自动加载在Rails 7中,将Esbuild作为JavaScript Bundler加载
我正在尝试配置Rails 7应用程序以自动加载所有JS刺激控制器。
我将Esbuild
用作JavaScript Bundler。
如果我们将刺激与导入地图一起使用刺激,则集成将自动从app/javascript/Controllers
中加载所有控制器文件。
// app/javascript/controllers/index.js
import { application } from "controllers/application"
import { eagerLoadControllersFrom } from "@hotwired/stimulus-loading"
eagerLoadControllersFrom("controllers", application)
此@hotwired/impotulus-loading
软件包来自importmap
# config/importmap.rb
pin "@hotwired/stimulus-loading", to: "stimulus-loading.js", preload: true
,但看起来@hotwired/strigulus-loading
尚未发布到NPM和仅在Rails中仅与ImportMaps
一起使用。
当我们将Web Packer用作JavaScript Bundler时,我们可以使用@hotwired/stimulus-webpack-helpers
软件包以获取相同形式的自动加载行为。
// app/javascript/application.js
import { Application } from "@hotwired/stimulus"
import { definitionsFromContext } from "@hotwired/stimulus-webpack-helpers"
window.Stimulus = Application.start()
const context = require.context("./controllers", true, /\.js$/)
Stimulus.load(definitionsFromContext(context))
但是,当我们使用eSbuild
javascript bundler在官方刺激建议单独导入每个控制器的页面。
刺激也可以与其他构建系统一起使用,但没有支持控制器自动加载。相反,您必须在应用程序实例上明确加载和注册控制器文件:
// app/javascript/application.js
import { Application } from "@hotwired/stimulus"
import HelloController from "./controllers/hello_controller"
import ClipboardController from "./controllers/clipboard_controller"
window.Stimulus = Application.start()
Stimulus.register("hello", HelloController)
Stimulus.register("clipboard", ClipboardController)
,所以我的问题是:
是否可以将Rails 7用Esbuild
作为JavaScript Bundler配置为加载所有JS app/javaScript/Controllers
的刺激控制器文件会自动?
I'm trying to configure Rails 7 app to load all js Stimulus controllers automatically.
I use esbuild
as JavaScript bundler.
If we're using Stimulus for Rails together with an import map, the integration will automatically load all controller files from app/javascript/controllers
.
// app/javascript/controllers/index.js
import { application } from "controllers/application"
import { eagerLoadControllersFrom } from "@hotwired/stimulus-loading"
eagerLoadControllersFrom("controllers", application)
This @hotwired/stimulus-loading
package comes from importmap
# config/importmap.rb
pin "@hotwired/stimulus-loading", to: "stimulus-loading.js", preload: true
But it looks like @hotwired/stimulus-loading
has not been published to NPM yet and is meant to be used only with importmaps
in Rails.
When we're using Webpacker as JavaScript bundler we can use @hotwired/stimulus-webpack-helpers
package to get the same form of autoloading behavior.
// app/javascript/application.js
import { Application } from "@hotwired/stimulus"
import { definitionsFromContext } from "@hotwired/stimulus-webpack-helpers"
window.Stimulus = Application.start()
const context = require.context("./controllers", true, /\.js$/)
Stimulus.load(definitionsFromContext(context))
But when we're using esbuild
JavaScript bundler on the official Stimulus page recommended to import each controller individually.
Stimulus works with other build systems too, but without support for controller autoloading. Instead, you must explicitly load and register controller files with your application instance:
// app/javascript/application.js
import { Application } from "@hotwired/stimulus"
import HelloController from "./controllers/hello_controller"
import ClipboardController from "./controllers/clipboard_controller"
window.Stimulus = Application.start()
Stimulus.register("hello", HelloController)
Stimulus.register("clipboard", ClipboardController)
So my question is:
Is it possible to configure Rails 7 with esbuild
as JavaScript bundler to load all js Stimulus controller files from app/javascript/controllers
automatically?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
手动运行一个任务以包含所有刺激控制器
包括您在
app/javascript/controllers/index.js
文件中手动创建的新刺激控制器,您可以运行:自动包括新创建的刺激控制器
当您使用发电机从终端创建刺激控制器时,Rails将自动运行清单:更新命令,因此包括您的控制器在JS构建中。
观看刺激控制器的实时文件更改
以观察刺激控制器的现场更改,需要额外的手表过程。安装Esbuild后,此附加的手表过程将附加到
procfile.dev
文件。纱线构建-Watch
命令触发Packacke.json文件中指定的构建命令。Manually running a task to include all stimulus controllers
To include a new stimulus controller that you manually created in the
app/javascript/controllers/index.js
file, you can run:Automatically including a newly created stimulus controller
When you create a stimulus controller from the terminal with the generator, rails will automatically run the manifest:update command and so include your controller in js build.
Watching for live file changes to stimulus controllers
To watch for live changes to your stimulus controllers, an additional watch process is needed. This additional watch process is appended to the
Procfile.dev
file after installing esbuild.The
yarn build --watch
command triggers the build command specified in the packacke.json file.