An open API service indexing awesome lists of open source software.

https://github.com/jgraichen/rails-assets-manifest

Load all assets from a manifest (e.g. from webpack) including SRI support
https://github.com/jgraichen/rails-assets-manifest

Last synced: over 1 year ago
JSON representation

Load all assets from a manifest (e.g. from webpack) including SRI support

Awesome Lists containing this project

README

          

# Rails::Assets::Manifest

[![Gem Version](https://img.shields.io/gem/v/rails-assets-manifest?logo=ruby)](https://rubygems.org/gems/rails-assets-manifest)
[![GitHub Actions Workflow Status](https://img.shields.io/github/actions/workflow/status/jgraichen/rails-assets-manifest/test.yml?logo=github)](https://github.com/jgraichen/rails-assets-manifest/actions)

Load all assets in your Rails application from a `manifest.json`, e.g. generated by [webpack-assets-manifest](https://github.com/webdeveric/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:

```json
{
"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.

```slim
html
head
= stylesheet_link_tag 'application', media: 'all'
= javascript_include_tag 'application'
```

```html

```

## Installation

Add this line to your application's Gemfile:

```ruby
gem 'rails-assets-manifest'
```

## Usage

The manifest path can be configured e.g. in an `environments/*.rb` file:

```ruby
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 the[webpack-assets-manifest](https://github.com/webdeveric/webpack-assets-manifest) plugin to generating a compatible assets manifest file.

```ts
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

1. [Fork it](http://github.com/jgraichen/rails-assets-manifest/fork)
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit specs for your feature so that I do not break it later
4. Commit your changes (`git commit -am 'Add some feature'`)
5. Push to the branch (`git push origin my-new-feature`)
6. 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.