Roda::AssetsManifest
A Roda plugin for serving assets with a manifest.json (such as those generated by Vite, Webpack, esbuild, etc), it works smoothly with https://www.npmjs.com/package/esbuild-plugin-manifest.
It automatically handles generating <script type="module"> and <link rel="stylesheet"> tags. In development, it links directly to your assets. In production, it resolves asset names to their hashed filenames via the manifest.json file.
Installation
Install the gem and add to the application's Gemfile by executing:
bundle add roda-assets_manifestIf bundler is not being used to manage dependencies, install the gem by executing:
gem install roda-assets_manifestUsage
In your Roda application, load the plugin:
class App < Roda
plugin :assets_manifest
route do |r|
r.root do
<<~HTML
<!DOCTYPE html>
<html>
<head>
#{css_entrypoint_tag('app')}
</head>
<body>
<h1>Hello World</h1>
#{js_entrypoint_tag('app')}
</body>
</html>
HTML
end
end
endDefault host for assets is '/public/assets'
Default manifest location is 'public/assets/manifest.json'
You can configure for your needs.
Entrypoint Tags
The plugin provides two helper methods available at both the class and instance levels:
-
js_entrypoint_tag('app'): Generates a<script type="module" src="...">tag. -
css_entrypoint_tag('app'): Generates a<link rel="stylesheet" href="...">tag.
The tags are cached after the first generation for performance.
Configuration
You can customize the behavior of the plugin by passing options when loading it:
plugin :assets_manifest,
# Set the host for the assets.
# Can be a single string for all environments, or a hash to specify per-environment hosts.
# Default: "/public/assets"
host: {
development: "/public/assets",
production: "https://cdn.example.com/assets"
},
# Force the environment mode.
# Default: ENV["RACK_ENV"] == "production"
is_production: true,
# The path to your manifest.json file
# (remote not supported, this is a local file path that is loaded in the plugin boot).
# Only used in production to map entrypoints to their hashed filenames.
location: "public/assets/manifest.json",
# Alternatively, you can provide a proc that returns the manifest hash directly.
# manifest: -> { JSON.parse(File.read("path/to/manifest.json")) }How it Works
-
Development Mode:
js_entrypoint_tag('app')outputs<script type="module" src="/public/assets/app.js"></script>. -
Production Mode: If
manifest.jsoncontains{"app.js": "app-12345.js"}, the same method outputs<script type="module" src="https://cdn.example.com/assets/app-12345.js"></script>.
Development
After checking out the repo, run bundle install to install dependencies. Then, run bundle exec rspec to run the tests.
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/henrique-ft/roda-assets_manifest.
License
The gem is available as open source under the terms of the MIT License.