GithubHelp home page GithubHelp logo

foca / asset_manifest Goto Github PK

View Code? Open in Web Editor NEW
2.0 2.0 0.0 14 KB

Utilities for managing static assets in ruby apps.

License: MIT License

Shell 0.73% Makefile 5.55% Ruby 93.46% CSS 0.14% JavaScript 0.12%

asset_manifest's Introduction

Asset Manifest

Tiny set of utilities to compute subresource integrity and cache busting checksums for static assets.

How it works

Asset Manifest provides two main utilities:

  • An executable that you should run when compiling your static assets. This generates a manifest file that includes the SRI hash and a checksum (for cache-busting purposes) of each of your assets.

  • A ruby library to generate the proper links to the assets taking into account the data from your manifest.

Usage

Somewhere in your makefile, you'll want to generate the public/manifest.json file like so:

# Assuming you keep an ASSETS list with all the assets you're compiling...
ASSETS += public/css/app.css
ASSETS += public/css/app.min.css
ASSETS += public/js/app.js
ASSETS += public/js/app.min.js

# ...you'd want a rule like this one:
public/manifest.json: $(ASSETS)
	asset-manifest -d public $^ > $@

Then, in your app, you'll want to initialize AssetManifest::Helpers passing the contents of this JSON file:

assets = AssetManifest::Helpers.new(
  JSON.parse(File.read("./public/manifest.json")),
  { minify: ENV["RACK_ENV"] == "production" }
)

Finally, you'll want to pass this object to your views, so that you can do the following:

<%= assets.stylesheet_tag("/css/app.css") %>
<%= assets.script_tag("/js/app.js") %>

This would produce output similar to:

<link rel="stylesheet"
      href="/css/app-07915293e2bb992a67c618e1aa335d978efc3734.css"
      integrity="sha256-yqqr5VJwz1IM5iTlSram51zrBvuE21FbiYgNnD2fwgE=">
<script src="/js/app-1a4aefaba81b61c7ea763d42fcb39584e5784c32.js"
        integrity="sha256-ZmTdnjlqI4ppv9cW8Y5i6PixtV4CQlVUvxB2iySwU94="></script>

(Whitespace added for clarity)

With Rake instead of Make

Assuming you have an ASSETS constant with the list of assets, you can use the following example in your Rakefile to re-generate your manifest when your assets change.

rule "public/manifest.json" => ASSETS do |t|
  sh "asset-manifest -d public #{t.prerequisites.join(" ")} > #{t.name}"
end

task assets: ASSETS
task assets: "public/manifest.json"

You can add this file to your assets so that the file is automatically generated when your source assets change.

Cuba plugin

If you use Cuba, then you can just do this:

require "asset_manifest/cuba"
Cuba.plugin AssetManifest::Cuba

This will set up an assets helper available in all your apps that you can use / pass to the templates.

License

This project is shared under the MIT license. See the attached LICENSE file for details.

asset_manifest's People

Contributors

foca avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

asset_manifest's Issues

Beautiful

Beautiful code, exquisitely commented. Kudos!.ship-shipping-i-it-SlEXm15CKaZuU

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.