Search code examples
ruby-on-railsrubyember.jsember-cliember-cli-rails

Ember Engines inside Rails Engines via ember-cli-rails


We have a high modular rails5 app, which is splitted in rails engines over serveral repositories and integrated as ruby gems.

Now we want to introduce EmberJS by using ember-cli-rails. The main rails application contains the main ember application in the frontend directory while each of the rails engines contain an ember engine (via ember-engine) in a frontend directory.

How to mount the ember engines of the modules into the main ember engine?


Solution

  • Due the fact that I've found no other solution so far, I've created an initializer which symlinks the ember engine directories of all rails engines into the node_modules of the consuming ember engine in the consuming rails app:

    # Get the node_modules dir
    nm_dir = Rails.root.join('frontend', 'node_modules')
    
    # Delete existing symlinks
    Dir.new(nm_dir.to_s).each { |entry| FileUtils.rm_rf(entry) if entry =~ /^.+\-frontend$/ }
    
    # MODULES contains an array of the rails engine gem names
    MODULES.each do |module_name|
      # Each module has a Manifest class, load that
      manifest = load_manifest(module_name)
    
      # Get the path to the frontend dir of the rails engine
      source = Pathname.new(manifest.method(:setup).source_location[0].split('/lib/')[0]).join('frontend').to_s
    
      # Symlink destination
      destination = nm_dir.join("#{module_name}-frontend").to_s
    
      # Symlink it
      FileUtils.symlink source, destination, force: true
    end
    

    This approach is probably not very clean, but it seems to work.