# Sharing stimulus application between rails engine and rails application

**URL:** <https://discuss.hotwired.dev/t/sharing-stimulus-application-between-rails-engine-and-rails-application/851>\
**Category:** General\
**Created:** [July 16, 2019, 9:33am UTC](https://discuss.hotwired.dev/t/sharing-stimulus-application-between-rails-engine-and-rails-application/851 "2019-07-16T09:33:31Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![asgerb](https://avatars.discourse-cdn.com/v4/letter/a/e5b9ba/32.png) [@asgerb](https://discuss.hotwired.dev/u/asgerb)\
**Post date:** [July 16, 2019, 9:33am UTC](https://discuss.hotwired.dev/t/sharing-stimulus-application-between-rails-engine-and-rails-application/851/1 "2019-07-16T09:33:31Z")

</div>

Just getting started with stimulus and it’s a great experience so far – thanks for the wonderful framework!

I’m working in a Rails app which mounts a Rails Engine and I’m wondering what would be the ideal way to setup and use Stimulus in both. They are setup with webpack and I’m currently instantiating a Stimulus application in both using the neat `webpack-helpers`.

In the engine my setup looks like this:

```auto
import { Application } from "stimulus"
import { definitionsFromContext } from "stimulus/webpack-helpers"

const application = Application.start()
const context = require.context("./controllers", true, /\.js$/)
application.load(definitionsFromContext(context))

```

In the app I include the engine’s js and then instantiate yet another stimulus application:

```auto
import '@asgerb/engine'

import { Application } from "stimulus"
import { definitionsFromContext } from "stimulus/webpack-helpers"

const application = Application.start()
const context = require.context("./controllers", true, /\.js$/)
application.load(definitionsFromContext(context))

```

This sorta feels right conceptually, as the engine and the app could be considered two apps, but it feels wrong from a performance standpoint and I’m worried about possible side effects.

I’m hoping someone has an idea for how best to do this?

---

<div class="post-metadata">

**Author:** ![la-ruby](https://yyz1.discourse-cdn.com/flex027/user_avatar/discuss.hotwired.dev/la-ruby/32/2087_2.png) [@la-ruby](https://discuss.hotwired.dev/u/la-ruby)\
**Post date:** [February 9, 2022, 8:09pm UTC](https://discuss.hotwired.dev/t/sharing-stimulus-application-between-rails-engine-and-rails-application/851/2 "2022-02-09T20:09:30Z")

</div>

@asgerb  
I was trying to provide a stimulus controller to parent app.  
here’s my question: [https://www.reddit.com/r/rails/comments/somiwf/can\_an\_engine\_add\_a\_stimulus\_controller\_rails\_7/](https://www.reddit.com/r/rails/comments/somiwf/can_an_engine_add_a_stimulus_controller_rails_7/)
