Git Product home page Git Product logo

rack-scriptstacker's Introduction

Rack::ScriptStacker

Painless static file handling for Rack apps.

  • Automatically configures Rack::Static by default.
  • Glob and inject JavaScript/CSS files into served HTML.
    • Inserts CSS before </head> and JS before </body> by default.
    • Change the inject mode to use placeholder slots instead.

Usage

<!-- index.html //-->

<!doctype html>
<html lang="en">
    <head>
    </head>
    <body>
    </body>
</html>

and

# config.ru

require 'rack/scriptstacker'

class App
  def call env
    [
      200,
      {'Content-Type' => 'text/html'},
      [File.read('index.html')]
    ]
  end
end

use Rack::ScriptStacker do
  css 'static/css'
  javascript 'static/javascript'
end

run app

Results in...

<!doctype html>
<html lang="en">
    <head>
      <link rel="stylesheet" type="text/css" href="/static/css/main.css" />
    </head>
    <body>
      <script type="text/javascript" src="/static/javascript/main.js"></script>
      <script type="text/javascript" src="/static/javascript/util.js"></script>
    </body>
</html>

Configure static serving yourself

use Rack::ScriptStacker, configure_static: false do
  css 'static/css'
  javascript 'static/javascript'
end

use Rack::Static, url: ['static']

Serve multiple sets of files in order

use Rack::ScriptStacker do
  css 'static/css'
  javascript 'vendor/javascript'
  javascript 'static/javascript'
end

Serve files at a different path

use Rack::ScriptStacker, configure_static: false do
  css 'css' => 'stylesheets'
  javascript 'js' => 'scripts'
end

Change the template for a stacker

use Rack::ScriptStacker,
    stackers: {
      javascript: { template: '<script src="%s"></script>' }
    } do
  javascript 'static/javascript'
end

Use placeholder slots

<!-- index.html //-->

<!doctype html>
<html lang="en">
    <head>
      <!-- ScriptStacker: CSS //-->
    </head>
    <body>
      <!-- ScriptStacker: JAVASCRIPT //-->
    </body>
</html>

use Rack::ScriptStacker, inject_mode: :slot do
  css 'static/css'
  javascript 'static/javascript'
end

rack-scriptstacker's People

Contributors

changemewtf avatar swrobel avatar

Watchers

 avatar James Cloos avatar  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.