GithubHelp home page GithubHelp logo

alexey-dc / generated-assets Goto Github PK

View Code? Open in Web Editor NEW

This project forked from camertron/generated-assets

0.0 1.0 0.0 14 KB

Programmatically generate assets for the Rails asset pipeline.

License: MIT License

Ruby 100.00%

generated-assets's Introduction

generated-assets

Programmatically generate assets for the Rails asset pipeline.

Installation

gem install generated-assets

or put it in your Gemfile:

gem 'generated-assets'

This gem is designed to work with Rails. Adding it to your Gemfile should be enough to add it to your project - you shouldn't need to manually require it.

Rationale

The Rails asset pipeline is great, but not super extensible. Generally speaking, any file you want it to recognize has to be on disk somewhere. That's pretty limiting, especially if you have a bunch of similar resources you'd like to add, but don't want to create individual files for. A great example is translations in Javascript. You probably don't want to include the translations for every locale in your Javascript bundle. Wouldn't it be nice if you could iterate over a list of locales and add translation files programmatically? This gem can help.

Getting Started

The generated-assets gem adds an attribute called generated to the Rails configuration object. Adding assets is easy:

# config/application.rb

I18n.available_locales.each do |locale|
  config.assets.generated.add("translations-#{locale}.js") do
    "window.translations ||= {}; window.translations.#{locale} = { ... }"
  end
end

The add method expects to be given the path of the new asset and a block. The return value of the block gets written inside the new asset file.

Asset Precompilation

The gem also supports adding any generated assets to your application's precompile list. Just include precompile: true:

config.assets.generated.add("my_asset.css", precompile: true) do
  ...
end

How Does it Work?

Under the covers, generated-assets creates a temporary directory that it adds to the asset pipeline's load path. It then writes each file you've requested to the temporary directory. As far as the asset pipeline is concerned, those files are accessible and available as if they lived in your app/assets directory.

License

Licensed under the MIT license. See LICENSE for details.

Authors

generated-assets's People

Contributors

camertron avatar

Watchers

Alexey avatar

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.