-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refs #4: Add rails helper files from Gulp Starter
- Loading branch information
1 parent
45f7f92
commit b26f133
Showing
3 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# Gulp Starter on Rails | ||
|
||
Using gulp-starter with Rails (instead of the built in asset pipeline) is actually pretty easy. This directory contains the extra pieces you'll need to get going. | ||
|
||
## Overview | ||
Firstly, note that we're going to leave the existing asset pipeline in place. Many gems rely on it (e.g, Active Admin), and it's a good idea to keep it around. That said, it's important to keep it separated. We won't be mixing gulp-generated assets with rails generated ones. | ||
|
||
Source files should be in a `src` directory in the root of the project, **not** in `app/assets`. You'll also no longer be using the built in Rails asset helpers like `javascript_include_tag` and the like. Instead, you'll use a set of **gulp_asset_helpers** with regular markup. | ||
|
||
#### app/helpers/gulp_asset_helper.rb | ||
In production (`npm run production`), filenames get hashed so you can cache them forever. When the file or any of it's referenced assets changes, the hash changes. This works just like the Rails asset pipeline, and we have similar helpers to ensure that the correct filenames are referenced in production: | ||
|
||
```ruby | ||
gulp_asset_path('image/asset.jpg') # -> /image/logo-n39o4orb81.png | ||
gulp_js_path('app.js') # -> /javascripts/app-f43e9abc11.js | ||
gulp_css_path('app.css') # -> /stylesheets/app-d29e4cdb76.css | ||
gulp_img_path('logo.png') # -> /images/logo-n39o4orb81.png | ||
``` | ||
|
||
So instead of this: | ||
```erb | ||
<%= image_tag 'logo.png', alt: 'logo' %> | ||
``` | ||
|
||
You would do this: | ||
```ruby | ||
<img src="<%= gulp_img_path('logo.png') %>" alt="logo"> | ||
``` | ||
|
||
##### Sprite helper | ||
There's also a `<%= sprite('id') %>` helper included for use with the `svgSpriteTask` task. It looks like: | ||
|
||
``` | ||
def sprite(id, classes = "", viewBox = "0 0 24 24") | ||
"<svg class='sprite -#{id} #{classes}' aria-hidden='true' preserveAspectRatio viewBox='#{viewBox}'><use xlink:href='#{gulp_image_path('sprites.svg')}##{id}' /></use></svg>".html_safe | ||
end | ||
``` | ||
|
||
Modify as needed. | ||
|
||
#### config/initializers/gulp.rb | ||
The asset path helpers check for the existence a rev-manifest.json file, generated by the gulp `rev` tasks. It may look something like this: | ||
|
||
```json | ||
{ | ||
"javascripts/app.js": "app-f43e9abc11.js", | ||
"stylesheets/app.css": "app-d29e4cdb76.css" | ||
} | ||
``` | ||
And so on. This initializer file reads and caches the object in production. In development, the file is constantly checked for and read. The asset helpers read this and find strings matching the key, and replace them with their hashed value. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
GULP_CONFIG = JSON.parse(File.read('gulpfile.js/config.json')) | ||
REV_MANIFEST_PATH = File.join(GULP_CONFIG['root']['dest'], 'rev-manifest.json') | ||
|
||
if File.exist?(REV_MANIFEST_PATH) | ||
REV_MANIFEST = JSON.parse(File.read(REV_MANIFEST_PATH)) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
module GulpAssetHelper | ||
def gulp_asset_path(path, type = nil) | ||
rev_manifest = nil | ||
|
||
# In development, check for the manifest every time | ||
if Rails.env.development? | ||
rev_manifest = JSON.parse(File.read(REV_MANIFEST_PATH)) if File.exist?(REV_MANIFEST_PATH) | ||
# In production, use the manifest cached in initializers/gulp.rb | ||
else | ||
rev_manifest = REV_MANIFEST if defined?(REV_MANIFEST) | ||
end | ||
|
||
root = GULP_CONFIG['root']['dest'].gsub(/(.*)public\//, '/') | ||
asset_path = type ? File.join(GULP_CONFIG['tasks'][type]['dest'], path) : path | ||
asset_path = rev_manifest[asset_path] if rev_manifest | ||
asset_path = File.join(root, asset_path) | ||
File.absolute_path(asset_path, '/') | ||
end | ||
|
||
def gulp_js_path(path) | ||
gulp_asset_path(path, 'js') | ||
end | ||
|
||
def gulp_css_path(path) | ||
gulp_asset_path(path, 'css') | ||
end | ||
|
||
def gulp_image_path(path) | ||
gulp_asset_path(path, 'images') | ||
end | ||
|
||
def sprite(id, classes = "", viewBox = "0 0 24 24") | ||
"<svg class='sprite -#{id} #{classes}' aria-hidden='true' preserveAspectRatio viewBox='#{viewBox}'><use xlink:href='#{gulp_image_path('sprites.svg')}##{id}' /></use></svg>".html_safe | ||
end | ||
end |