Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Rails: beyond the asset pipeline (RubyC)

Rails: beyond the asset pipeline (RubyC)

Alex Coles

May 31, 2015
Tweet

More Decks by Alex Coles

Other Decks in Technology

Transcript

  1. 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'
  2. Three different ways to use Bower 2. Load path config.assets.paths

    << File.join(Rails.root, 'bower_components') requires Rails 4 (Sprockets 2+)
  3. 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
  4. 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' } }
  5. Requiring JS single files and dependencies require('./another-file'); //= require ./another-file

    (Sprockets) var angular = require('angular'); var jQuery = require('jquery'); require('jquery-ui');
  6. Requiring JS a tree var requireTemplate = require.context('./app/controllers', true, /\.js$/);

    requireTemplate.keys().forEach(requireTemplate); //= require_tree ./app/controllers (Sprockets)
  7. 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');
  8. Loaders Loaders are transformations that are applied on files. They

    preprocess files. I. e. they can transform CoffeeScript to JavaScript.
  9. Chaining Loaders eslint ← coffee json ← yaml style ←

    postcss ← css ← sass ngtemplate-loader ← markdown
  10. 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;
  11. Transpiling (ES6 → ES5) $ npm install --save-dev babel-loader module:

    { loaders: [ { test: /\.js$/, exclude: /node_modules/, loader: 'babel-loader'} ] }
  12. 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;
  13. Legacy code support $ npm install --save-dev exports-loader module: {

    loaders: [ { test: /[\/]angular\.js$/, loader: 'exports?angular' } ] }
  14. Rails integration /// manifest-84b43dda218a2c29ce11f4f7b9ca4e5f.json { "assets": { "1downarrow.png": "1downarrow-d2055955ce2927de07f2e33abdbfdc1b.png", "1uparrow.png":

    "1uparrow-a4eef1942dd999e6a16e84c1c8122b8a.png", "2downarrow.png": "2downarrow-e8bc5b59fa922f68637dc22b4a467f5c.png" } }
  15. 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/
  16. Rails integration Running everything together foreman start # Procfile web:

    bundle exec rails server -e ${RAILS_ENV:="development"} assets: $(npm bin)/webpack --colors --watch --progress
  17. Core application CSS 1. Stick with the Rails asset pipeline

    for CSS 2. Using gulp rather than Webpack
  18. 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')); });