Skip to content

xoengineering/bootstrap-vendor

Repository files navigation

bootstrap-vendor

Rake tasks to vendor Bootstrap CSS and JS into your Rails app. No asset pipeline, no npm, no build system. Just static files in vendor/.

Install

Add to your Gemfile:

gem 'bootstrap-vendor'

Then:

bundle install

In a Rails app, the rake tasks are loaded automatically via Railtie.

Quick start

rake bootstrap:vendor

This creates a .bootstrap-version file and downloads the latest Bootstrap CSS and JS into vendor/stylesheets/ and vendor/javascript/.

Usage

In Rails

Add Bootstrap to your asset paths. In config/initializers/assets.rb:

Rails.application.config.assets.paths << Rails.root.join('vendor/stylesheets')
Rails.application.config.assets.paths << Rails.root.join('vendor/javascript')

Or with importmaps, pin the JS in config/importmap.rb:

pin 'bootstrap', to: 'bootstrap.bundle.js'

And add a stylesheet link in your layout:

<%= stylesheet_link_tag 'bootstrap', 'data-turbo-track': 'reload' %>

In non-Rails

Add the gem:

gem install bootstrap-vendor

Create a Rakefile (or add to an existing one):

require 'bootstrap/vendor'
load 'tasks/bootstrap.rake'

Then use the rake tasks as normal:

rake bootstrap:vendor

From zsh (command line shell)

zsh interprets [] as glob patterns. Escape the brackets when passing arguments to rake tasks:

rake 'bootstrap:init[overwrite]'
rake bootstrap:init\[overwrite\]
noglob rake bootstrap:init[overwrite]

Rake tasks

bootstrap:vendor

The opinionated, does-it-all shortcut. Creates .bootstrap-version, downloads the latest Bootstrap files, and you're done.

rake bootstrap:vendor

bootstrap:version

Prints the current version from .bootstrap-version.

rake bootstrap:version
# 5.3.8

bootstrap:latest

Prints the latest known Bootstrap version. Accepts an optional constraint.

rake bootstrap:latest
# 5.3.8

rake bootstrap:latest[5]
# 5.3.8

rake bootstrap:latest[4]
# 4.6.2

rake bootstrap:latest[5.2]
# 5.2.3

bootstrap:status

Compares your current version to the latest upstream.

rake bootstrap:status
# Current version 5.3.8 is up to date

rake bootstrap:status
# Current version 5.2.3 is behind latest version 5.3.8

bootstrap:init

Creates a .bootstrap-version file. Defaults to the latest version.

rake bootstrap:init
# A .bootstrap-version file was created.
# Version: 5.3.8

rake bootstrap:init[4]
# A .bootstrap-version file was created.
# Version: 4.6.2

rake bootstrap:init[overwrite]
# Overwrites an existing .bootstrap-version

bootstrap:install

Downloads Bootstrap CSS and JS files. Fails if already installed (use update instead).

rake bootstrap:install

bootstrap:update

Downloads Bootstrap CSS and JS files, overwriting existing ones.

rake bootstrap:update
# Bootstrap updated from 5.3.5 to 5.3.8

bootstrap:uninstall

Removes vendored Bootstrap files and .bootstrap-version.

rake bootstrap:uninstall

Default files

By default, these files are downloaded:

vendor/stylesheets/bootstrap.css
vendor/stylesheets/bootstrap.css.map
vendor/javascript/bootstrap.bundle.js
vendor/javascript/bootstrap.bundle.js.map

Configuration

Customize which files are downloaded with environment variables. These are the defaults:

ENV variable Default Effect
BOOTSTRAP_VENDOR_CSS_LTR true Download LTR CSS
BOOTSTRAP_VENDOR_CSS_RTL false Download RTL CSS
BOOTSTRAP_VENDOR_CSS_MAP true Download CSS source maps
BOOTSTRAP_VENDOR_CSS_MIN false Download minified CSS
BOOTSTRAP_VENDOR_JS_BUNDLE true Download bundle (includes Popper) vs plain
BOOTSTRAP_VENDOR_JS_MAP true Download JS source maps
BOOTSTRAP_VENDOR_JS_MIN false Download minified JS

At least one of CSS_LTR or CSS_RTL must be true.

Example: download minified files without source maps:

BOOTSTRAP_VENDOR_CSS_MIN=true BOOTSTRAP_VENDOR_CSS_MAP=false BOOTSTRAP_VENDOR_JS_MIN=true BOOTSTRAP_VENDOR_JS_MAP=false rake bootstrap:install

Download sources

Files are downloaded from multiple sources with automatic fallback for resilience:

  1. jsdelivr CDN (npm)
  2. jsdelivr CDN (GitHub)
  3. GitHub raw files
  4. GitHub releases (zip)

Pin a specific source with:

BOOTSTRAP_VENDOR_SOURCE=github_raw rake bootstrap:install

Valid source names: jsdelivr_npm, jsdelivr_github, github_raw, github_api.

Optional arguments

Path, where to install

Most tasks accept an optional path argument. By default, tasks operate in the current directory. Pass a path to target a different location:

rake 'bootstrap:vendor[public/assets]'
rake 'bootstrap:version[public/assets]'

The .bootstrap-version file and vendor/ directories are created relative to the given path.

Tasks that accept path: vendor, version, status, init, install, update, uninstall.

For tasks that accept both version and path (status, init, install, update), version is the first positional arg. To pass only a path, leave version empty:

rake 'bootstrap:install[5, public/assets]'
rake 'bootstrap:install[, public/assets]'

Version constraint, what to install

latest, status, init, install, and update accept version constraints. The constraint finds the latest version within that major or minor:

Constraint Resolves to
5 Latest 5.x.y (e.g., 5.3.8)
5.3 Latest 5.3.x (e.g., 5.3.8)
5.3.5 Latest 5.3.x (e.g., 5.3.8)
4 Latest 4.x.y (e.g., 4.6.2)

Requirements

  • Ruby >= 4.0
  • Rails (for automatic Railtie loading) or any Ruby project with Rake

License

MIT. See LICENSE.txt.

About

Rubygem of Rake tasks to vendor Bootstrap CSS and JS, not just for Rails apps

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Contributors