Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Rails: beyond the asset pipeline (RubyC)
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
Alex Coles
May 31, 2015
Technology
150
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Rails: beyond the asset pipeline (RubyC)
Alex Coles
May 31, 2015
More Decks by Alex Coles
See All by Alex Coles
How a language reflects its people (Brighton Ruby)
myabc
0
120
How a language reflects its people
myabc
0
110
Taking Rails beyond the asset pipeline (Workshop at RubyDay Italia)
myabc
0
90
Frontend Choices (Tropical Ruby)
myabc
1
810
Frontend Choices (RubyConf Portugal)
myabc
1
480
Frontend Choices
myabc
0
72
Frontend Choices
myabc
0
310
Putting the Hype back in Hypermedia
myabc
1
250
DataMapper
myabc
2
240
Other Decks in Technology
See All in Technology
穢れた技術選定について
watany
19
6.2k
Webアプリ認証の全体像 / The Big Picture of Web App Authentication
kitano_yuichi
1
430
AI Native なプロダクト組織の立ち上げ方 : 生産性 100 倍への挑戦
mikesorae
0
1.3k
『モデル + ハーネス』で読み解く AIエージェント入門
oracle4engineer
PRO
2
170
OPENLOGI Company Profile for engineer
hr01
1
74k
インシデント事例と パッケージの全量解析に学ぶ ソフトウェアサプライチェーンの守り方 / supply-chain-attack-defense
flatt_security
0
890
ゴールデンパスは敷いただけでは道にならない ─ 企画部門のエンジニアが技術標準を事業価値に変えるまで
mhrtech
1
260
文字起こし基盤の信頼性
abnoumaru
0
120
どこまでAIに任せるか 〜確率論と決定論の境界決定〜
shukob
0
450
AIとハーネスで育てるトランスコンパイラ / 20260722 Yasushi Katayama
shift_evolve
PRO
3
780
仕様駆動開発、導入半年。「本当に速くなってるの?」にデータで答える / AICon2026_hirakawa
rakus_dev
0
320
AI x 開発生産性を取り巻く予算戦略と投資対効果
i35_267
7
2.9k
Featured
See All Featured
The Director’s Chair: Orchestrating AI for Truly Effective Learning
tmiket
1
220
AI Search: Where Are We & What Can We Do About It?
aleyda
0
7.7k
We Analyzed 250 Million AI Search Results: Here's What I Found
joshbly
1
1.6k
Design in an AI World
tapps
1
270
Java REST API Framework Comparison - PWX 2021
mraible
34
9.5k
Measuring & Analyzing Core Web Vitals
bluesmoon
9
900
DevOps and Value Stream Thinking: Enabling flow, efficiency and business value
helenjbeal
1
260
Impact Scores and Hybrid Strategies: The future of link building
tamaranovitovic
0
340
Building Better People: How to give real-time feedback that sticks.
wjessup
370
20k
A Modern Web Designer's Workflow
chriscoyier
698
190k
Keith and Marios Guide to Fast Websites
keithpitt
413
23k
A designer walks into a library…
pauljervisheath
211
24k
Transcript
Rails: beyond the asset pipeline RubyC, Київ, Україна | 31
May 2015
Frontend has changed
asana
typecast
blocs
So how can Rails keep up?
psst… ditch the asset pipeline
Привіт RubyC! Привет RubyC!
Привіт Київ! Привет Киев!
About me @myabc github.com/myabc alexbcoles.com
Де я живу Где я живу
Де я працюю Где я работаю
Те, що я працюю на То, что я работаю на
31 August 2011 Hello Rails 3.1
gem wrappers
$ gem list --remote jquery | wc -l 300
Bower
Three different ways to use Bower 1. gem Using an
integration like bower-rails or bower gem gem install bower-rails source 'https://rubygems.org' gem 'bower-rails'
Three different ways to use Bower 2. Load path config.assets.paths
<< File.join(Rails.root, 'bower_components') requires Rails 4 (Sprockets 2+)
Three different ways to use Bower 3. rails-assets.org source 'https://rubygems.org'
gem 'rails' source 'https://rails-assets.org' do gem 'rails-assets-bootstrap' gem 'rails-assets-angular' gem 'rails-assets-leaflet' end requires Bundler >= 1.8.4
Torba torba-rb/Torba
Bower only helps solve dependency management
Things you might need Dependency management Pre and post-processing Code
loading Code bundling
Sprockets plugins alexspeller/non-stupid-digest-assets
Sprockets plugins Post-processing JS/CSS ai/autoprefixer-rails uses PostCSS project autoprefixer TannerRogalsky/sprockets-es6
uses Babel (formerly 6to5), requires Sprockets 3
Problem with Sprockets No standard plugin configuration style Cannot control
pre/post-processing order Asset dependencies
Things you might need Dependency management Pre and post-processing Code
loading Code bundling
Webpack
Webpack configuration + entry point // webpack.config.js module.exports = {
context: __dirname + '/app', entry: 'rubyc-app.js', output: { filename: '[name].js', path: path.join(__dirname, '..', 'app', 'assets', 'javascripts', 'bundles'), publicPath: '/assets/bundles' } }
Requiring JS single files and dependencies require('./another-file'); //= require ./another-file
(Sprockets) var angular = require('angular'); var jQuery = require('jquery'); require('jquery-ui');
Requiring JS a tree var requireTemplate = require.context('./app/controllers', true, /\.js$/);
requireTemplate.keys().forEach(requireTemplate); //= require_tree ./app/controllers (Sprockets)
Requiring assets require('jquery-ui/ui/jquery-ui'); // .js (default) require('jquery-ui/themes/base/jquery.ui.core.css'); require('jquery-ui/themes/base/jquery.ui.datepicker.css'); require('select2/select2'); //
.js (default) require('select2/select2.css');
Requiring assets quick start require('jquery-ui/ui/jquery-ui'); // .js (default) require('!style-loader!css-loader!jquery-ui/themes/base/jquery.ui.core.css'); require('!style-loader!css-loader!jquery-ui/themes/base/jquery.ui.datepicker.css');
Requiring assets with a bit of config // webpack.config.js module.exports
= { context: __dirname + '/app', entry: 'rubyc-app.js', module: { loaders: [ { test: /\.css$/, loader: 'style-loader!css-loader' }, { test: /\.png$/, loader: 'url-loader?limit=100000&mimetype=image/png' }, { test: /\.gif$/, loader: 'file-loader' }, { test: /\.jpg$/, loader: 'file-loader' } ]} } require('jquery-ui/ui/jquery-ui'); // .js (default) require('jquery-ui/themes/base/jquery.ui.core.css'); require('jquery-ui/themes/base/jquery.ui.datepicker.css');
Requiring assets body { background: url(file:///Users/alexbcoles/git-repos/talk-rails-beyond-asset-pipeline//assets/bundles/backgroun } /* border-image: url(file:///Users/alexbcoles/git-repos/talk-rails-beyond-asset-pipeline/border-image.png);
*/ .box { border-image: url(file:///Users/alexbcoles/git-repos/talk-rails-beyond-asset-pipeline/data:image/png;base64,i }
Loaders and plugins Webpack is built on the concept of
loaders and plugins
Loaders
Loaders Loaders are transformations that are applied on files. They
preprocess files. I. e. they can transform CoffeeScript to JavaScript.
Chaining Loaders eslint ← coffee json ← yaml style ←
postcss ← css ← sass ngtemplate-loader ← markdown
None
Requiring Rails-style translation files $ npm install --save-dev json-loader yaml-loader
I18n.translations = I18n.translations || {}; I18n.translations.en = require('!json!yaml!config/locales/en_US.yml').en; I18n.translations.de = require('!json!yaml!config/locales/en_DE.yml').de;
Transpiling (ES6 → ES5) $ npm install --save-dev babel-loader module:
{ loaders: [ { test: /\.js$/, exclude: /node_modules/, loader: 'babel-loader'} ] }
Transpiling (ES6 → ES5) // app-defaults.js export default { favouriteConf:
'Ruby C' }; // app.js import appDefaults from './app-defaults.js'; class ExampleApp { constructor() { console.log(appDefaults.favouriteConf); } } export default ExampleApp;
Legacy code support $ npm install --save-dev exports-loader module: {
loaders: [ { test: /[\/]angular\.js$/, loader: 'exports?angular' } ] }
How to integrate with Rails rake webpack – or –
rake assets:precompile
Rails integration /// manifest-84b43dda218a2c29ce11f4f7b9ca4e5f.json { "assets": { "1downarrow.png": "1downarrow-d2055955ce2927de07f2e33abdbfdc1b.png", "1uparrow.png":
"1uparrow-a4eef1942dd999e6a16e84c1c8122b8a.png", "2downarrow.png": "2downarrow-e8bc5b59fa922f68637dc22b4a467f5c.png" } }
Rails integration # app/helpers/application_helper.rb def webpack_bundle_tag(bundle) src = if Rails.configuration.webpack[:use_manifest]
manifest = Rails.configuration.webpack[:asset_manifest] filename = manifest[bundle] "#{compute_asset_host}/assets/#{filename}" else "#{compute_asset_host}/assets/#{bundle}-bundle" end javascript_include_tag(src) end http://clarkdave.net/2015/01/how-to-use-webpack-with-rails/
Rails integration Running everything together foreman start # Procfile web:
bundle exec rails server -e ${RAILS_ENV:="development"} assets: $(npm bin)/webpack --colors --watch --progress
How does CSS fit into the picture?
Core application CSS 1. Stick with the Rails asset pipeline
for CSS 2. Using gulp rather than Webpack
Gulp for Sass/CSS $ npm install -g gulp $ gulp
sass gulp.task('sass', function() { return gulp.src('app/assets/css/default.css.sass') .pipe(sass({ loadPath: [ './bower_components/bourbon/app/assets/stylesheets' ] })) .pipe(autoprefixer({ cascade: false })) .on('error', function(err) { console.log(err.message); }) .pipe(gulp.dest('public/assets/css')); });
One more thing…
http://xkcd.com/303/
Hot reloading
Hot reloading colektivo/song-song-song gaearon/react-hot-loader
Conclusion
Alternatives (why not Ruby?)
Ruby alternatives lotus/assets livingsocial/rake-pipeline (including wycats/rake-pipeline-web-filters)
Спасибі Спасибо
Questions?