├── .gitignore ├── Gemfile ├── LICENSE.txt ├── README.md ├── Rakefile ├── lib └── webpack │ ├── rails.rb │ ├── rails │ ├── environment.rb │ ├── helper.rb │ └── version.rb │ └── railtie.rb └── webpack-rails.gemspec /.gitignore: -------------------------------------------------------------------------------- 1 | /.bundle/ 2 | /.yardoc 3 | /Gemfile.lock 4 | /_yardoc/ 5 | /coverage/ 6 | /doc/ 7 | /pkg/ 8 | /spec/reports/ 9 | /tmp/ 10 | *.bundle 11 | *.so 12 | *.o 13 | *.a 14 | mkmf.log 15 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source 'https://rubygems.org' 2 | 3 | # Specify your gem's dependencies in webpack-rails.gemspec 4 | gemspec 5 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2014 Leonard Garvey 2 | 3 | MIT License 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining 6 | a copy of this software and associated documentation files (the 7 | "Software"), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 20 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 21 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 22 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Webpack::Rails 2 | 3 | Replace Sprockets with webpack. 4 | 5 | Why? Because Sprockets is 2011 and we've moved on. We shouldn't be treating our JS and CSS as a big ball of mud, but as code with depedencies we can track. We should be supporting the creation of both simple Rails applications and complex, modern, modular Javascript web applications. Webpack lets us do this but integrating it with Rails is tricky. This gem hopes to solve that. 6 | 7 | **This is very much still alpha quality software. Do not use in your production system** 8 | 9 | ## Installation 10 | 11 | Add this line to your application's Gemfile: 12 | 13 | ```ruby 14 | gem 'webpack-rails' 15 | ``` 16 | 17 | Comment out the sprockets railstie in `application.rb` 18 | 19 | ```ruby 20 | # require "sprockets/railtie" 21 | require "webpack/railtie" 22 | ``` 23 | 24 | Change your `application.css` and `application.js` to remove any sprockets manifest directives. 25 | Instead change your `application.js` to: 26 | 27 | ```js 28 | require('../stylesheets/application.css'); 29 | ``` 30 | 31 | at the top. Finally the webpack config file `config/webpack.development.js` 32 | 33 | ```js 34 | var ExtractTextPlugin = require("extract-text-webpack-plugin"); 35 | module.exports = { 36 | context: __dirname + '/../app/assets/javascripts', 37 | entry: './application.js', 38 | output: { 39 | path: __dirname + '/../tmp/assets', 40 | filename: 'application.js' 41 | }, 42 | module: { 43 | loaders: [ 44 | { test: /\.css$/, loader: ExtractTextPlugin.extract("style-loader", "css-loader") }, 45 | { 46 | test: /\.scss$/, 47 | loader: "style!css!sass?outputStyle=expanded&includePaths[]=" + 48 | (path.resolve(__dirname, './bower_components/bootstrap-sass-official')) 49 | } 50 | ] 51 | }, 52 | plugins: [ 53 | new ExtractTextPlugin("application.css") 54 | ] 55 | }; 56 | ``` 57 | 58 | ## TODO: 59 | 60 | See the issues list for specifics but roughly: 61 | 62 | 1. Create a rails generator installer task. 63 | 2. Document the node_modules required and install these as part of the installation process. 64 | 3. Explore the possibility of hiding the node_modules into `vendor` 65 | 4. Change the development compilation step not run webpack on every request to `/assets/*` 66 | 5. Explore how to do production deploys. 67 | 6. Create a `rake assets:precompile` task. 68 | 69 | 70 | ## Contributing 71 | 72 | 1. Fork it ( https://github.com/[my-github-username]/webpack-rails/fork ) 73 | 2. Create your feature branch (`git checkout -b my-new-feature`) 74 | 3. Commit your changes (`git commit -am 'Add some feature'`) 75 | 4. Push to the branch (`git push origin my-new-feature`) 76 | 5. Create a new Pull Request 77 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | require "bundler/gem_tasks" 2 | 3 | -------------------------------------------------------------------------------- /lib/webpack/rails.rb: -------------------------------------------------------------------------------- 1 | require 'webpack/rails/version' 2 | if defined? Rails::Railtie 3 | require 'webpack/railtie' 4 | end 5 | -------------------------------------------------------------------------------- /lib/webpack/rails/environment.rb: -------------------------------------------------------------------------------- 1 | module Webpack 2 | module Rails 3 | class Environment < Rack::File 4 | def call(env) 5 | `webpack --config config/webpack.development.js` 6 | super(env) 7 | end 8 | end 9 | end 10 | end 11 | -------------------------------------------------------------------------------- /lib/webpack/rails/helper.rb: -------------------------------------------------------------------------------- 1 | require 'action_view' 2 | 3 | module Webpack 4 | module Rails 5 | module Helper 6 | include ActionView::Helpers::AssetUrlHelper 7 | include ActionView::Helpers::AssetTagHelper 8 | 9 | def stylesheet_link_tag(*sources) 10 | options = sources.extract_options!.stringify_keys 11 | 12 | if options['debug'] != false 13 | sources.map do |source| 14 | super("/assets/#{source}", options) 15 | end.flatten.uniq.join('\n').html_safe 16 | else 17 | super(*sources) 18 | end 19 | end 20 | 21 | def javascript_include_tag(*sources) 22 | options = sources.extract_options!.stringify_keys 23 | 24 | if options['debug'] != false 25 | sources.map do |source| 26 | super("/assets/#{source}", options) 27 | end.flatten.uniq.join('\n').html_safe 28 | else 29 | super(*sources) 30 | end 31 | end 32 | end 33 | end 34 | end 35 | -------------------------------------------------------------------------------- /lib/webpack/rails/version.rb: -------------------------------------------------------------------------------- 1 | module Webpack 2 | module Rails 3 | VERSION = "0.0.1" 4 | end 5 | end 6 | -------------------------------------------------------------------------------- /lib/webpack/railtie.rb: -------------------------------------------------------------------------------- 1 | require 'rails' 2 | require 'rails/railtie' 3 | require 'webpack/rails/helper' 4 | require 'webpack/rails/environment' 5 | 6 | module Webpack 7 | class Railtie < ::Rails::Railtie 8 | config.after_initialize do |app| 9 | ActiveSupport.on_load(:action_view) do 10 | include Webpack::Rails::Helper 11 | end 12 | 13 | app.routes.prepend do 14 | mount Webpack::Rails::Environment.new(File.join(app.config.root, 'tmp', 'assets')) => '/assets' 15 | end 16 | end 17 | end 18 | end 19 | -------------------------------------------------------------------------------- /webpack-rails.gemspec: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | lib = File.expand_path('../lib', __FILE__) 3 | $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) 4 | require 'webpack/rails/version' 5 | 6 | Gem::Specification.new do |spec| 7 | spec.name = "webpack-rails" 8 | spec.version = Webpack::Rails::VERSION 9 | spec.authors = ["Leonard Garvey"] 10 | spec.email = ["lengarvey@gmail.com"] 11 | spec.summary = %q{Webpack for Rails} 12 | spec.description = %q{Replaces sprockets with webpack} 13 | spec.homepage = "" 14 | spec.license = "MIT" 15 | 16 | spec.files = `git ls-files -z`.split("\x0") 17 | spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) } 18 | spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) 19 | spec.require_paths = ["lib"] 20 | 21 | spec.add_development_dependency "bundler", "~> 1.7" 22 | spec.add_development_dependency "rake", "~> 10.0" 23 | end 24 | --------------------------------------------------------------------------------