webpack-rails 0.0.1 → 0.9.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a1dd95fbc45c74fae0baf21f9a6ea1913ce20bc6
4
- data.tar.gz: 1290831b5deaf7579b449f14a717f0c60e4c84a8
3
+ metadata.gz: e8ed91330119b96f41c0089cd1fa500192ee48de
4
+ data.tar.gz: 487700e5d02b4f62d4d6d69a1a0229035b42bff5
5
5
  SHA512:
6
- metadata.gz: fb372999b1751a5526f88cbd3014f35f159170f7ea41d0f1f30c53e1ea749f8363060b3229633968c587231e8b54fccce1b93999657041aa1ce7b72901326824
7
- data.tar.gz: 4e796dafeac7ba5d2667609936164654cc5c15165def8e1730be189d81d14b0942121d4f0570657a262be80253864ca5843f9c0e9971390475f727d0514905d2
6
+ metadata.gz: 4e198a0d4f06183ca3e44c53e3d6b37ac04eff2c61524084e6b75a118c3b2aa851f9d5d44a20f54131cd63ef47311f94f08c2ad578680ad0107fea9ea7a2ca32
7
+ data.tar.gz: 71b2eb10a882de41005f18221cca32b86daa7fa4d66c9e9692fbd76b9454c3e9f9d1cd5371a6e1b711240f96f3673d832b7a3f3d2466347ce8c3a887c5321b4a
@@ -1,6 +1,4 @@
1
- Copyright (c) 2014 Leonard Garvey
2
-
3
- MIT License
1
+ Copyright 2015 Michael Pearson
4
2
 
5
3
  Permission is hereby granted, free of charge, to any person obtaining
6
4
  a copy of this software and associated documentation files (the
data/README.md CHANGED
@@ -1,31 +1,70 @@
1
- # Webpack::Rails
1
+ # webpack-rails
2
2
 
3
- TODO: Write a gem description
3
+ [![Build Status](https://travis-ci.org/mipearson/webpack-rails.svg?branch=master)](https://travis-ci.org/mipearson/webpack-rails)
4
4
 
5
- ## Installation
5
+ **webpack-rails** gives you tools to integrate Webpack in to an existing Ruby on Rails application.
6
6
 
7
- Add this line to your application's Gemfile:
7
+ It will happily co-exist with sprockets but does not use it for production fingerprinting or asset serving. **webpack-rails** is designed with the assumption that if you're using Webpack you treat Javascript as a first-class citizen. This means that you control the webpack config, package.json, and use npm to install Webpack & its plugins.
8
8
 
9
- ```ruby
10
- gem 'webpack-rails'
9
+ In development mode [webpack-dev-server](http://webpack.github.io/docs/webpack-dev-server.html) is used to serve webpacked entry points and offer hot module reloading. In production entry points are built in to `public/webpack`. **webpack-rails** uses [stats-webpack-plugin](https://www.npmjs.com/package/stats-webpack-plugin) to translate entry points in to asset paths.
10
+
11
+ It was designed for use at [Marketplacer](http://www.marketplacer.com) to assist us in migrating our Javascript (and possibly our SCSS) off of Sprockets. It first saw production use in June 2015.
12
+
13
+ As this is pre-1.0 software its API and configuration may change as we become more familiar with Webpack. This gem has been developed against Ruby 2.2 and Rails 4.2. Previous versions might work, but I haven't tested them. Assume Rails 3.2 and Ruby 1.9 as an absolute minimum.
14
+
15
+ ## Getting Started
16
+
17
+ Have a look at the files in the `examples` directory. Of note:
18
+
19
+ * We use [foreman](https://github.com/ddollar/foreman) and a `Procfile` to run our rails server & the webpack dev server in development at the same time
20
+ * The webpack and gem configuration must be in sync - look at our railtie for configuration options
21
+ * We require that **stats-webpack-plugin** is loaded to automatically generate a production manifest & resolve paths during development
22
+
23
+ To access webpacked assets from your views:
24
+
25
+ ```erb
26
+ <%= javascript_include_tag *webpack_asset_paths("entry_point_name") %>
11
27
  ```
12
28
 
13
- And then execute:
29
+ Take note of the splat (`*`): `webpack_asset_paths` returns an array, as one entry point can map to multiple paths, especially if hot reloading is enabled in Webpack.
14
30
 
15
- $ bundle
31
+ ### Configuration Defaults
16
32
 
17
- Or install it yourself as:
33
+ * Webpack configuration lives in `config/webpack.config.js`
34
+ * Webpack & Webpack Dev Server binaries are in `node_modules/.bin/`
35
+ * Webpack Dev Server will run on port 3808 on localhost
36
+ * Webpack Dev Server is enabled in development & test, but not in production
37
+ * Webpacked assets will be compiled to `public/webpack`
38
+ * The manifest file is named `manifest.json`
18
39
 
19
- $ gem install webpack-rails
40
+ ### Working with browser tests
20
41
 
21
- ## Usage
42
+ In development, we make sure that the `webpack-dev-server` is running when browser tests are running. In CI, we manually run `webpack` to compile the assets to public and set `config.webpack.dev_server.enabled` to false.
22
43
 
23
- TODO: Write usage instructions here
44
+ ### Production Deployment
45
+
46
+ Add `rake webpack:compile` to your deployment. It serves a similar purpose as Sprockets' `assets:precompile` task. If you're using Webpack and Sprockets (as we are at Marketplacer) you'll need to run both tasks - but it doesn't matter which order they're run in.
47
+
48
+ If you're using `[chunkhash]` in your build asset filenames (which you should be, if you want to cache them in production), you'll need to persist built assets between deployments. Consider in-flight requests at the time of deployment: they'll receive paths based on the old `manifest.json`, not the new one.
49
+
50
+ ## TODO
51
+
52
+ * Release gem
53
+ * travisci & codeclimate & gem badges
54
+ * Drive config via JSON, have webpack.config.js read same JSON?
55
+ * Generators for webpack config, Gemfile, Procfile, package.json
56
+ * Custom webpack-dev-server that exposes errors, stats, etc
57
+ * [react-rails](https://github.com/reactjs/react-rails) fork for use with this workflow
58
+ * Integration tests
24
59
 
25
60
  ## Contributing
26
61
 
27
- 1. Fork it ( https://github.com/[my-github-username]/webpack-rails/fork )
28
- 2. Create your feature branch (`git checkout -b my-new-feature`)
29
- 3. Commit your changes (`git commit -am 'Add some feature'`)
30
- 4. Push to the branch (`git push origin my-new-feature`)
31
- 5. Create a new Pull Request
62
+ Pull requests & issues welcome. Advice & criticism regarding webpack config approach also welcome.
63
+
64
+ Please ensure that pull requests pass both rubocop & rspec. New functionality should be discussed in an issue first.
65
+
66
+ ## Acknowledgements
67
+
68
+ * Len Garvey for his [webpack-rails](https://github.com/lgarvey/webpack-rails) gem which inspired this implementation
69
+ * Sebastian Porto for [Rails with Webpack](https://reinteractive.net/posts/213-rails-with-webpack-why-and-how)
70
+ * Clark Dave for [How to use Webpack with Rails](http://clarkdave.net/2015/01/how-to-use-webpack-with-rails/)
data/Rakefile CHANGED
@@ -1,2 +1,24 @@
1
- require "bundler/gem_tasks"
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
2
6
 
7
+ require 'rdoc/task'
8
+ require 'rspec/core/rake_task'
9
+ require 'rubocop/rake_task'
10
+
11
+ RuboCop::RakeTask.new
12
+ RSpec::Core::RakeTask.new(:spec)
13
+
14
+ RDoc::Task.new(:rdoc) do |rdoc|
15
+ rdoc.rdoc_dir = 'rdoc'
16
+ rdoc.title = 'WebpackRails'
17
+ rdoc.options << '--line-numbers'
18
+ rdoc.rdoc_files.include('README.md')
19
+ rdoc.rdoc_files.include('lib/**/*.rb')
20
+ end
21
+
22
+ Bundler::GemHelper.install_tasks
23
+
24
+ task default: [:rubocop, :spec]
@@ -0,0 +1,18 @@
1
+ namespace :webpack do
2
+ desc "Compile webpack bundles"
3
+ task compile: :environment do
4
+ ENV["TARGET"] = 'production'
5
+ webpack_bin = ::Rails.root.join(::Rails.configuration.webpack.binary)
6
+ config_file = ::Rails.root.join(::Rails.configuration.webpack.config_file)
7
+
8
+ unless File.exist?(webpack_bin)
9
+ raise "Can't find our webpack executable at #{webpack_bin} - have you run `npm install`?"
10
+ end
11
+
12
+ unless File.exist?(config_file)
13
+ raise "Can't find our webpack config file at #{config_file}"
14
+ end
15
+
16
+ sh "#{webpack_bin} --bail --config #{config_file}"
17
+ end
18
+ end
@@ -1,4 +1,2 @@
1
1
  require 'webpack/rails/version'
2
- if defined? Rails::Railtie
3
- require 'webpack/railtie'
4
- end
2
+ require 'webpack/railtie' if defined? ::Rails::Railtie
@@ -1,33 +1,31 @@
1
1
  require 'action_view'
2
+ require 'webpack/rails/manifest'
2
3
 
3
4
  module Webpack
4
5
  module Rails
6
+ # Asset path helpers for use with webpack
5
7
  module Helper
6
- include ActionView::Helpers::AssetUrlHelper
7
- include ActionView::Helpers::AssetTagHelper
8
+ # Return asset paths for a particular webpack entry point.
9
+ #
10
+ # Response may either be full URLs (eg http://localhost/...) if the dev server
11
+ # is in use or a host-relative URl (eg /webpack/...) if assets are precompiled.
12
+ #
13
+ # Will raise an error if our manifest can't be found or the entry point does
14
+ # not exist.
15
+ def webpack_asset_paths(source)
16
+ return "" unless source.present?
8
17
 
9
- def stylesheet_link_tag(*sources)
10
- options = sources.extract_options!.stringify_keys
18
+ paths = Webpack::Rails::Manifest.asset_paths(source)
19
+ host = ::Rails.configuration.webpack.dev_server.host
20
+ port = ::Rails.configuration.webpack.dev_server.port
11
21
 
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)
22
+ if ::Rails.configuration.webpack.dev_server.enabled
23
+ paths.map! do |p|
24
+ "http://#{host}:#{port}#{p}"
25
+ end
18
26
  end
19
- end
20
-
21
- def javascript_include_tag(*sources)
22
- options = sources.extract_options!.stringify_keys
23
27
 
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
28
+ paths
31
29
  end
32
30
  end
33
31
  end
@@ -0,0 +1,87 @@
1
+ require 'net/http'
2
+ require 'uri'
3
+
4
+ module Webpack
5
+ module Rails
6
+ # Webpack manifest loading, caching & entry point retrieval
7
+ class Manifest
8
+ # Raised if we can't read our webpack manifest for whatever reason
9
+ class ManifestLoadError < StandardError
10
+ def initialize(message, orig)
11
+ super "#{message} (original error #{orig})"
12
+ end
13
+ end
14
+
15
+ # Raised if a supplied entry point does not exist in the webpack manifest
16
+ class EntryPointMissingError < StandardError
17
+ end
18
+
19
+ class << self
20
+ # :nodoc:
21
+ def asset_paths(source)
22
+ paths = manifest["assetsByChunkName"][source]
23
+ if paths
24
+ # Can be either a string or an array of strings
25
+ [paths].flatten.map do |p|
26
+ "/#{::Rails.configuration.webpack.public_path}/#{p}"
27
+ end
28
+ else
29
+ raise EntryPointMissingError, "Can't find entry point '#{source}' in webpack manifest"
30
+ end
31
+ end
32
+
33
+ private
34
+
35
+ def manifest
36
+ if ::Rails.configuration.webpack.dev_server.enabled
37
+ # Don't cache if we're in dev server mode, manifest may change ...
38
+ load_manifest
39
+ else
40
+ # ... otherwise cache at class level, as JSON loading/parsing can be expensive
41
+ @manifest ||= load_manifest
42
+ end
43
+ end
44
+
45
+ def load_manifest
46
+ data = if ::Rails.configuration.webpack.dev_server.enabled
47
+ load_dev_server_manifest
48
+ else
49
+ load_static_manifest
50
+ end
51
+ JSON.parse(data)
52
+ end
53
+
54
+ def load_dev_server_manifest
55
+ Net::HTTP.get(
56
+ "localhost",
57
+ dev_server_path,
58
+ ::Rails.configuration.webpack.dev_server.port
59
+ )
60
+ rescue => e
61
+ raise ManifestLoadError.new("Could not load manifest from webpack-dev-server at #{dev_server_url} - is it running, and is stats-webpack-plugin loaded?", e)
62
+ end
63
+
64
+ def load_static_manifest
65
+ File.read(static_manifest_path)
66
+ rescue => e
67
+ raise ManifestLoadError.new("Could not load compiled manifest from #{static_manifest_path} - have you run `rake webpack:compile`?", e)
68
+ end
69
+
70
+ def static_manifest_path
71
+ ::Rails.root.join(
72
+ ::Rails.configuration.webpack.output_dir,
73
+ ::Rails.configuration.webpack.manifest_filename
74
+ )
75
+ end
76
+
77
+ def dev_server_path
78
+ "/#{::Rails.configuration.webpack.public_path}/#{::Rails.configuration.webpack.manifest_filename}"
79
+ end
80
+
81
+ def dev_server_url
82
+ "http://localhost:#{::Rails.configuration.webpack.dev_server.port}#{dev_server_path}"
83
+ end
84
+ end
85
+ end
86
+ end
87
+ end
@@ -1,5 +1,6 @@
1
1
  module Webpack
2
+ # :nodoc:
2
3
  module Rails
3
- VERSION = "0.0.1"
4
+ VERSION = "0.9.1"
4
5
  end
5
6
  end
@@ -1,18 +1,32 @@
1
1
  require 'rails'
2
2
  require 'rails/railtie'
3
3
  require 'webpack/rails/helper'
4
- require 'webpack/rails/environment'
5
4
 
6
5
  module Webpack
6
+ # :nodoc:
7
7
  class Railtie < ::Rails::Railtie
8
- config.after_initialize do |app|
8
+ config.after_initialize do
9
9
  ActiveSupport.on_load(:action_view) do
10
10
  include Webpack::Rails::Helper
11
11
  end
12
+ end
12
13
 
13
- app.routes.prepend do
14
- mount Webpack::Rails::Environment.new(File.join(app.config.root, 'tmp', 'assets')) => '/assets'
15
- end
14
+ config.webpack = ActiveSupport::OrderedOptions.new
15
+ config.webpack.config_file = 'config/webpack.config.js'
16
+ config.webpack.binary = 'node_modules/.bin/webpack'
17
+
18
+ config.webpack.dev_server = ActiveSupport::OrderedOptions.new
19
+ config.webpack.dev_server.host = 'localhost'
20
+ config.webpack.dev_server.port = 3808
21
+ config.webpack.dev_server.binary = 'node_modules/.bin/webpack-dev-server'
22
+ config.webpack.dev_server.enabled = !::Rails.env.production?
23
+
24
+ config.webpack.output_dir = "public/webpack"
25
+ config.webpack.public_path = "webpack"
26
+ config.webpack.manifest_filename = "manifest.json"
27
+
28
+ rake_tasks do
29
+ load "tasks/webpack.rake"
16
30
  end
17
31
  end
18
32
  end
metadata CHANGED
@@ -1,62 +1,47 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: webpack-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.9.1
5
5
  platform: ruby
6
6
  authors:
7
- - Leonard Garvey
7
+ - Michael Pearson
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-11-16 00:00:00.000000000 Z
11
+ date: 2015-09-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: bundler
14
+ name: rails
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '1.7'
20
- type: :development
19
+ version: 3.2.0
20
+ type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '1.7'
27
- - !ruby/object:Gem::Dependency
28
- name: rake
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - "~>"
32
- - !ruby/object:Gem::Version
33
- version: '10.0'
34
- type: :development
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - "~>"
39
- - !ruby/object:Gem::Version
40
- version: '10.0'
41
- description: Replaces sprockets with webpack
26
+ version: 3.2.0
27
+ description: webpack-dev-server usage in development and dynamic asset filenames in
28
+ production via clever helpers
42
29
  email:
43
- - lengarvey@gmail.com
30
+ - mipearson@gmail.com
44
31
  executables: []
45
32
  extensions: []
46
33
  extra_rdoc_files: []
47
34
  files:
48
- - ".gitignore"
49
- - Gemfile
50
- - LICENSE.txt
35
+ - MIT-LICENSE
51
36
  - README.md
52
37
  - Rakefile
38
+ - lib/tasks/webpack.rake
53
39
  - lib/webpack/rails.rb
54
- - lib/webpack/rails/environment.rb
55
40
  - lib/webpack/rails/helper.rb
41
+ - lib/webpack/rails/manifest.rb
56
42
  - lib/webpack/rails/version.rb
57
43
  - lib/webpack/railtie.rb
58
- - webpack-rails.gemspec
59
- homepage: ''
44
+ homepage: http://github.com/mipearson/webpack-rails
60
45
  licenses:
61
46
  - MIT
62
47
  metadata: {}
@@ -76,8 +61,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
76
61
  version: '0'
77
62
  requirements: []
78
63
  rubyforge_project:
79
- rubygems_version: 2.2.2
64
+ rubygems_version: 2.4.5
80
65
  signing_key:
81
66
  specification_version: 4
82
- summary: Webpack for Rails
67
+ summary: Webpack & Rails integration toolset
83
68
  test_files: []
data/.gitignore DELETED
@@ -1,14 +0,0 @@
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
data/Gemfile DELETED
@@ -1,4 +0,0 @@
1
- source 'https://rubygems.org'
2
-
3
- # Specify your gem's dependencies in webpack-rails.gemspec
4
- gemspec
@@ -1,10 +0,0 @@
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
@@ -1,23 +0,0 @@
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