如何将所有JS控制器文件自动加载在Rails 7中,将Esbuild作为JavaScript Bundler加载

发布于 2025-02-13 21:38:18 字数 2170 浏览 0 评论 0原文

我正在尝试配置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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

止于盛夏 2025-02-20 21:38:18

手动运行一个任务以包含所有刺激控制器

包括您在app/javascript/controllers/index.js文件中手动创建的新刺激控制器,您可以运行:

rails stimulus:manifest:update

自动包括新创建的刺激控制器

当您使用发电机从终端创建刺激控制器时,Rails将自动运行清单:更新命令,因此包括您的控制器在JS构建中。

rails generate stimulus controllerName

观看刺激控制器的实时文件更改

以观察刺激控制器的现场更改,需要额外的手表过程。安装Esbuild后,此附加的手表过程将附加到procfile.dev文件。

# Procfilde.dev

web: rails s -p 3000
css: yarn build:css --watch
js: yarn build --watch

纱线构建-Watch命令触发Packacke.json文件中指定的构建命令。

{
  "name": "myapplication",
  "scripts": {
    "build": "esbuild app/javascript/*.* --bundle --outdir=app/assets/builds"
   }
}

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:

rails stimulus:manifest:update

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.

rails generate stimulus controllerName

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.

# Procfilde.dev

web: rails s -p 3000
css: yarn build:css --watch
js: yarn build --watch

The yarn build --watch command triggers the build command specified in the packacke.json file.

{
  "name": "myapplication",
  "scripts": {
    "build": "esbuild app/javascript/*.* --bundle --outdir=app/assets/builds"
   }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文