从Symfony的Webpack Encore中的 * 多个 * 目录加载刺激控制器

ifsvaxew  于 2022-11-16  发布在  Webpack
关注(0)|答案(1)|浏览(159)

使用默认bootstrap.js(来自symfony/webpack-encore-bundle)...

import { startStimulusApp } from '@symfony/stimulus-bridge';

// Registers Stimulus controllers from controllers.json and in the controllers/ directory
export const app = startStimulusApp(require.context(
    '@symfony/stimulus-bridge/lazy-controller-loader!./controllers',
    true,
    /\.[jt]sx?$/,
));

...刺激控制器从assets/controllers/加载。
有没有办法也从另一个目录加载控制器?

fcg9iug3

fcg9iug31#

将此import添加到controllers.js

import { definitionsFromContext } from '@hotwired/stimulus-webpack-helpers';

在底部:

app.load(definitionsFromContext(require.context(
    '@symfony/stimulus-bridge/lazy-controller-loader!../foo/bar/more_controllers',
    true,
    /\.[jt]sx?$/
)));

来源:

  • 这个想法来自https://github.com/symfony/stimulus-bridge/issues/44#issuecomment-1064176223
  • ......并与刺激文档相结合:wwwhttps://stimulus.hotwired.dev/handbook/installing#using-webpack-helpers
  • 另一个相关问题:https://github.com/symfony/stimulus-bridge/issues/29

相关问题