Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can a Rails engine be adapted to support both Propshaft and Sprockets based installations #219

Open
westonganger opened this issue Nov 18, 2024 · 1 comment

Comments

@westonganger
Copy link

westonganger commented Nov 18, 2024

With the release of Rails 8 and new apps having Propshaft instead of Sprockets, How are Rails engines being made to work for both styles? For example many gems contains a Web UI which is a Rails engine.

So far whats been suggested is using /public for assets and using CDN script/link tags for external assets. This is mostly an approach I have already been using.

Im hoping there should be a better story than this.

So far this is the only sprockets configuration that may be relevant to this conversation. Just including it here for completeness.

  initializer "foo_app.assets.precompile" do |app|
    app.config.assets.precompile << "foo_app_manifest.js" ### manifest file required
    app.config.assets.precompile << "foo_app/favicon.ico"

    ### Automatically precompile assets in specified folders
    ["app/assets/images/"].each do |folder|
      dir = app.root.join(folder)

      if Dir.exist?(dir)
        Dir.glob(File.join(dir, "**/*")).each do |f|
          asset_name = f.to_s
            .split(folder).last # Remove fullpath
            .sub(/^\/*/, '') ### Remove leading '/'

          app.config.assets.precompile << asset_name
        end
      end
    end
  end

Also posted this question to reddit: https://www.reddit.com/r/rails/comments/1gtin65/how_can_a_rails_engine_be_adapted_to_support_both/

@theodorton
Copy link
Collaborator

Propshaft doesn't use config.assets.precompile at all, it adds asset folders from gems to the load path and assumes the folder structure conventions are followed.

If you're not using vanilla CSS and Javascript, and you need to precompile from Sass or some other language supported in Sprockets, some options you have for now are either:

  1. Precompile assets into app/assets/builds for the gem as part of the workflow to publish the gem - propshaft will be able to use these assets
  2. Add a Gem.post_install hook that runs the precompilation for the gem

Option 1 should work fine for gems installed from Rubygems, but I assume you don't want to push your build artifacts to Github, so any gems installed with git will not have the prebuilt assets.

Option 2 should work for both scenarios, but will probably require the consumer to install more runtime dependencies.

builds folders should be given higher priority in the load path, so any asset that is precompiled and named the same in two locations will be served as the builds variant.

Now that Propshaft is stable, and Rails 8 is release, I can write some documentation for gem authors that wants to add support for Propshaft.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants