Rails::Assets::Manifest
Load all assets in your Rails application from a manifest.json
, e.g. generated by webpack-assets-manifest.
This gem does not make any assumption on which tool to use to make your assets not how to configure or invoke it. It only requires a manifest JSON. The manifest must either be a simple string map or must map to objects having a 'src'
attribute. Sub-resource integrity is supported too:
{
"application.css": {
"src": "2b16adf6f756625a0194.css",
"integrity": "sha384-/oreyvcV6U6htGZD0fDWR8/Txezke8KhD0QNgHb660hSaW7M+ZzxxuB4Vo+PuAC9"
},
"application.js": {
"src": "2b16adf6f756625a0194.js",
"integrity": "sha384-iJ55fQQApbQGxWEWSbWStBabi+yNGxZSQy/010+1Dhxl+rymyhGF4NtjUkOsYv7B"
}
}
This gem does not add new helper methods but extends the existing helpers. SRI is automatically added if available and within a secure context. A crossorigin="anonymous"
attribute is automatically added if non is present.
html
head
= stylesheet_link_tag 'application', media: 'all'
= javascript_include_tag 'application'
<link rel="stylesheet" media="all" href="2b16adf6f756625a0194.css" integrity="sha384-/oreyvcV6U6htGZD0fDWR8/Txezke8KhD0QNgHb660hSaW7M+ZzxxuB4Vo+PuAC9" crossorigin="anonymous">
<script src="2b16adf6f756625a0194.js" integrity="sha384-iJ55fQQApbQGxWEWSbWStBabi+yNGxZSQy/010+1Dhxl+rymyhGF4NtjUkOsYv7B" crossorigin="anonymous"></script>
Installation
Add this line to your application's Gemfile:
gem 'rails-assets-manifest'
Usage
The manifest path can be configured e.g. in an environments/*.rb
file:
config.assets_manifest.path = "public/.asset-manifest.json"
If config.cache_classes
is set to true
the manifest file be loaded once on boot. Assets included with integrity: true
will raise an error if the integrity option is missing in the manifest.
Webpack Assets Manifest Example
The following snippet provides an example configuration for thewebpack-assets-manifest plugin to generating a compatible assets manifest file.
new WebpackAssetsManifest({
integrity: true,
integrityHashes: ["sha384"],
writeToDisk: true,
entrypoints: false,
entrypointsUseAssets: true,
publicPath,
// Ignore a source maps and compressed files.
customize(e) {
if (e.key.endsWith(".map") || e.key.endsWith(".gz")) {
return false;
}
return e;
},
})
Specifying writeToDisk: true
allows running the webpack-dev-server
as proxy to the Rails application, and still generate correct assets links in Rails code. rails-assets-manifest
defaults to using public/assets/assets-manifest.json
as the manifest path.
TODO
- Override/Join with
asset_host
URL?
Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit specs for your feature so that I do not break it later
- Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request
MIT License
Copyright (c) 2018-2022 Jan Graichen
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.