webpack2-rails 2.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +132 -0
- data/Rakefile +24 -0
- data/example/Procfile +4 -0
- data/example/dot_gitignore +12 -0
- data/example/package.json +10 -0
- data/example/webpack.config.js +69 -0
- data/lib/generators/webpack_rails/install_generator.rb +69 -0
- data/lib/tasks/webpack.rake +19 -0
- data/lib/webpack-rails.rb +1 -0
- data/lib/webpack/rails.rb +2 -0
- data/lib/webpack/rails/helper.rb +37 -0
- data/lib/webpack/rails/manifest.rb +102 -0
- data/lib/webpack/rails/version.rb +6 -0
- data/lib/webpack/railtie.rb +45 -0
- metadata +88 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: a35c39ab0f02b61cb81f31f341b07c8a05b5017a
|
4
|
+
data.tar.gz: 80b1af3675c38f23b06552d67501d6a0be1a2034
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0440e8a4223da974648485913a65bbc7787f244ab2e0f4dba56e01bab1e0997e8dc501d0a44c2a5bb2984a9526ea65ab5264680e77bf213eb5f573454e5a595d
|
7
|
+
data.tar.gz: 0b1452c2a628564190d66b0aeafbea1cf177b3314e4031937b6114f7ad28d7df1a67bcbfdeaea3085bb18691700cdd22b618b75b9a0032b335d5e6ec5762f081
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2015 Michael Pearson
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,132 @@
|
|
1
|
+
[![Build Status](https://travis-ci.org/mipearson/webpack-rails.svg?branch=master)](https://travis-ci.org/mipearson/webpack-rails) [![Gem Version](https://badge.fury.io/rb/webpack-rails.svg)](http://badge.fury.io/rb/webpack-rails)
|
2
|
+
|
3
|
+
# Notice!
|
4
|
+
|
5
|
+
**As of 19th Sep 2016 there is an issue between the latest version of `webpack-dev-middleware` & `stats-webpack-plugin` where the manifest file is not appearing when the dev server is run. See https://github.com/mipearson/webpack-rails/issues/58 for more information and a workaround.**
|
6
|
+
|
7
|
+
# webpack-rails
|
8
|
+
|
9
|
+
**webpack-rails** gives you tools to integrate Webpack in to an existing Ruby on Rails application.
|
10
|
+
|
11
|
+
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.
|
12
|
+
|
13
|
+
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.
|
14
|
+
|
15
|
+
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.
|
16
|
+
|
17
|
+
Our examples show **webpack-rails** co-existing with sprockets (as that's how environment works), but sprockets is not used or required for development or production use of this gem.
|
18
|
+
|
19
|
+
This gem has been tested against Rails 4.2 and Ruby 2.2. Earlier versions of Rails (>= 3.2) and Ruby (>= 2.0) may work, but we haven't tested them.
|
20
|
+
|
21
|
+
## Using webpack-rails
|
22
|
+
|
23
|
+
**We have a demo application: [webpack-rails-demo](https://github.com/mipearson/webpack-rails-demo)**
|
24
|
+
|
25
|
+
### Installation
|
26
|
+
|
27
|
+
1. Add `webpack-rails` to your gemfile
|
28
|
+
1. Run `bundle install` to install the gem
|
29
|
+
1. Run `bundle exec rails generate webpack_rails:install` to copy across example files
|
30
|
+
1. Run `foreman start` to start `webpack-dev-server` and `rails server` at the same time
|
31
|
+
1. Add the webpack entry point to your layout (see next section)
|
32
|
+
1. Edit `webpack/application.js` and write some code
|
33
|
+
|
34
|
+
|
35
|
+
### Adding the entry point to your Rails application
|
36
|
+
|
37
|
+
To add your webpacked javascript in to your app, add the following to the `<head>` section of your to your `layout.html.erb`:
|
38
|
+
|
39
|
+
```erb
|
40
|
+
<%= javascript_include_tag *webpack_asset_paths("application") %>
|
41
|
+
```
|
42
|
+
|
43
|
+
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.
|
44
|
+
|
45
|
+
If your webpack is configured to output both CSS and JS, you can use the `extension:` argument to filter which files are returned by the helper:
|
46
|
+
|
47
|
+
```erb
|
48
|
+
<%= javascript_include_tag *webpack_asset_paths('application', extension: 'js') %>
|
49
|
+
<%= stylesheet_link_tag *webpack_asset_paths('application', extension: 'css') %>
|
50
|
+
```
|
51
|
+
|
52
|
+
#### Use with webpack-dev-server live reload
|
53
|
+
|
54
|
+
If you're using the webpack dev server's live reload feature (not the React hot reloader), you'll also need to include the following in your layout template:
|
55
|
+
|
56
|
+
``` html
|
57
|
+
<script src="http://localhost:3808/webpack-dev-server.js"></script>
|
58
|
+
```
|
59
|
+
|
60
|
+
### How it works
|
61
|
+
|
62
|
+
Have a look at the files in the `examples` directory. Of note:
|
63
|
+
|
64
|
+
* 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
|
65
|
+
* The webpack and gem configuration must be in sync - look at our railtie for configuration options
|
66
|
+
* We require that **stats-webpack-plugin** is loaded to automatically generate a production manifest & resolve paths during development
|
67
|
+
|
68
|
+
### Configuration Defaults
|
69
|
+
|
70
|
+
* Webpack configuration lives in `config/webpack.config.js`
|
71
|
+
* Webpack & Webpack Dev Server binaries are in `node_modules/.bin/`
|
72
|
+
* Webpack Dev Server will run on port 3808 on localhost via HTTP
|
73
|
+
* Webpack Dev Server is enabled in development & test, but not in production
|
74
|
+
* Webpacked assets will be compiled to `public/webpack`
|
75
|
+
* The manifest file is named `manifest.json`
|
76
|
+
|
77
|
+
#### Dynamic host
|
78
|
+
|
79
|
+
To have the host evaluated at request-time, set `host` to a proc:
|
80
|
+
|
81
|
+
```ruby
|
82
|
+
config.webpack.dev_server.host = proc { request.host }
|
83
|
+
```
|
84
|
+
|
85
|
+
This is useful when accessing your Rails app over the network (remember to bind both your Rails app and your WebPack server to `0.0.0.0`).
|
86
|
+
|
87
|
+
#### Use with docker-compose
|
88
|
+
|
89
|
+
If you're running `webpack-dev-server` as part of docker compose rather than `foreman`, you might find that the host and port that rails needs to use to retrieve the manifest isn't the same as the host and port that you'll be giving to the browser to retrieve the assets.
|
90
|
+
|
91
|
+
If so, you can set the `manifest_host` and `manifest_port` away from their default of `localhost` and port 3808.
|
92
|
+
|
93
|
+
### Working with browser tests
|
94
|
+
|
95
|
+
In development, we make sure that the `webpack-dev-server` is running when browser tests are running.
|
96
|
+
|
97
|
+
#### Continuous Integration
|
98
|
+
|
99
|
+
In CI, we manually run `webpack` to compile the assets to public and set `config.webpack.dev_server.enabled` to `false` in our `config/environments/test.rb`:
|
100
|
+
|
101
|
+
``` ruby
|
102
|
+
config.webpack.dev_server.enabled = !ENV['CI']
|
103
|
+
```
|
104
|
+
|
105
|
+
### Production Deployment
|
106
|
+
|
107
|
+
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.
|
108
|
+
|
109
|
+
If you deploy to Heroku, you can add the special
|
110
|
+
[webpack-rails-buildpack](https://github.com/febeling/webpack-rails-buildpack)
|
111
|
+
in order to perform this rake task on each deployment.
|
112
|
+
|
113
|
+
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.
|
114
|
+
|
115
|
+
## TODO
|
116
|
+
|
117
|
+
* Drive config via JSON, have webpack.config.js read same JSON?
|
118
|
+
* Custom webpack-dev-server that exposes errors, stats, etc
|
119
|
+
* [react-rails](https://github.com/reactjs/react-rails) fork for use with this workflow
|
120
|
+
* Integration tests
|
121
|
+
|
122
|
+
## Contributing
|
123
|
+
|
124
|
+
Pull requests & issues welcome. Advice & criticism regarding webpack config approach also welcome.
|
125
|
+
|
126
|
+
Please ensure that pull requests pass both rubocop & rspec. New functionality should be discussed in an issue first.
|
127
|
+
|
128
|
+
## Acknowledgements
|
129
|
+
|
130
|
+
* Len Garvey for his [webpack-rails](https://github.com/lengarvey/webpack-rails) gem which inspired this implementation
|
131
|
+
* Sebastian Porto for [Rails with Webpack](https://reinteractive.net/posts/213-rails-with-webpack-why-and-how)
|
132
|
+
* Clark Dave for [How to use Webpack with Rails](http://clarkdave.net/2015/01/how-to-use-webpack-with-rails/)
|
data/Rakefile
ADDED
@@ -0,0 +1,24 @@
|
|
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
|
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]
|
data/example/Procfile
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
// Example webpack configuration with asset fingerprinting in production.
|
2
|
+
'use strict';
|
3
|
+
|
4
|
+
var path = require('path');
|
5
|
+
var webpack = require('webpack');
|
6
|
+
var StatsPlugin = require('stats-webpack-plugin');
|
7
|
+
|
8
|
+
// must match config.webpack.dev_server.port
|
9
|
+
var devServerPort = 3808;
|
10
|
+
|
11
|
+
// set NODE_ENV=production on the environment to add asset fingerprints
|
12
|
+
var production = process.env.NODE_ENV === 'production';
|
13
|
+
|
14
|
+
var config = {
|
15
|
+
entry: {
|
16
|
+
// Sources are expected to live in $app_root/webpack
|
17
|
+
'application': './webpack/application.js'
|
18
|
+
},
|
19
|
+
|
20
|
+
output: {
|
21
|
+
// Build assets directly in to public/webpack/, let webpack know
|
22
|
+
// that all webpacked assets start with webpack/
|
23
|
+
|
24
|
+
// must match config.webpack.output_dir
|
25
|
+
path: path.join(__dirname, '..', 'public', 'webpack'),
|
26
|
+
publicPath: '/webpack/',
|
27
|
+
|
28
|
+
filename: production ? '[name]-[chunkhash].js' : '[name].js'
|
29
|
+
},
|
30
|
+
|
31
|
+
resolve: {
|
32
|
+
modules: [
|
33
|
+
path.join(__dirname, '..', 'webpack'),
|
34
|
+
"node_modules"
|
35
|
+
]
|
36
|
+
},
|
37
|
+
|
38
|
+
plugins: [
|
39
|
+
// must match config.webpack.manifest_filename
|
40
|
+
new StatsPlugin('manifest.json', {
|
41
|
+
// We only need assetsByChunkName
|
42
|
+
chunkModules: false,
|
43
|
+
source: false,
|
44
|
+
chunks: false,
|
45
|
+
modules: false,
|
46
|
+
assets: true
|
47
|
+
})]
|
48
|
+
};
|
49
|
+
|
50
|
+
if (production) {
|
51
|
+
config.plugins.push(
|
52
|
+
new webpack.NoErrorsPlugin(),
|
53
|
+
new webpack.optimize.UglifyJsPlugin(),
|
54
|
+
new webpack.DefinePlugin({
|
55
|
+
'process.env': { NODE_ENV: JSON.stringify('production') }
|
56
|
+
}),
|
57
|
+
new webpack.optimize.DedupePlugin()
|
58
|
+
);
|
59
|
+
} else {
|
60
|
+
config.devServer = {
|
61
|
+
port: devServerPort,
|
62
|
+
headers: { 'Access-Control-Allow-Origin': '*' }
|
63
|
+
};
|
64
|
+
config.output.publicPath = '//localhost:' + devServerPort + '/webpack/';
|
65
|
+
// Source maps
|
66
|
+
config.devtool = 'cheap-module-eval-source-map';
|
67
|
+
}
|
68
|
+
|
69
|
+
module.exports = config;
|
@@ -0,0 +1,69 @@
|
|
1
|
+
module WebpackRails
|
2
|
+
# :nodoc:
|
3
|
+
class InstallGenerator < ::Rails::Generators::Base
|
4
|
+
source_root File.expand_path("../../../../example", __FILE__)
|
5
|
+
|
6
|
+
desc "Install everything you need for a basic webpack-rails integration"
|
7
|
+
|
8
|
+
def add_foreman_to_gemfile
|
9
|
+
gem 'foreman'
|
10
|
+
end
|
11
|
+
|
12
|
+
def copy_procfile
|
13
|
+
copy_file "Procfile", "Procfile"
|
14
|
+
end
|
15
|
+
|
16
|
+
def copy_package_json
|
17
|
+
copy_file "package.json", "package.json"
|
18
|
+
end
|
19
|
+
|
20
|
+
def copy_webpack_conf
|
21
|
+
copy_file "webpack.config.js", "config/webpack.config.js"
|
22
|
+
end
|
23
|
+
|
24
|
+
def create_webpack_application_js
|
25
|
+
empty_directory "webpack"
|
26
|
+
create_file "webpack/application.js" do
|
27
|
+
<<-EOF.strip_heredoc
|
28
|
+
console.log("Hello world!");
|
29
|
+
EOF
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def add_to_gitignore
|
34
|
+
append_to_file ".gitignore" do
|
35
|
+
<<-EOF.strip_heredoc
|
36
|
+
# Added by webpack-rails
|
37
|
+
/node_modules
|
38
|
+
/public/webpack
|
39
|
+
EOF
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def run_npm_install
|
44
|
+
run "npm install" if yes?("Would you like us to run 'npm install' for you?")
|
45
|
+
end
|
46
|
+
|
47
|
+
def run_bundle_install
|
48
|
+
run "bundle install" if yes?("Would you like us to run 'bundle install' for you?")
|
49
|
+
end
|
50
|
+
|
51
|
+
def whats_next
|
52
|
+
puts <<-EOF.strip_heredoc
|
53
|
+
|
54
|
+
We've set up the basics of webpack-rails for you, but you'll still
|
55
|
+
need to:
|
56
|
+
|
57
|
+
1. Add the 'application' entry point in to your layout, and
|
58
|
+
2. Run 'foreman start' to run the webpack-dev-server and rails server
|
59
|
+
|
60
|
+
See the README.md for this gem at
|
61
|
+
https://github.com/mipearson/webpack-rails/blob/master/README.md
|
62
|
+
for more info.
|
63
|
+
|
64
|
+
Thanks for using webpack-rails!
|
65
|
+
|
66
|
+
EOF
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
namespace :webpack do
|
2
|
+
desc "Compile webpack bundles"
|
3
|
+
task compile: :environment do
|
4
|
+
ENV["TARGET"] = 'production' # TODO: Deprecated, use NODE_ENV instead
|
5
|
+
ENV["NODE_ENV"] ||= 'production'
|
6
|
+
webpack_bin = ::Rails.root.join(::Rails.configuration.webpack.binary)
|
7
|
+
config_file = ::Rails.root.join(::Rails.configuration.webpack.config_file)
|
8
|
+
|
9
|
+
unless File.exist?(webpack_bin)
|
10
|
+
raise "Can't find our webpack executable at #{webpack_bin} - have you run `npm install`?"
|
11
|
+
end
|
12
|
+
|
13
|
+
unless File.exist?(config_file)
|
14
|
+
raise "Can't find our webpack config file at #{config_file}"
|
15
|
+
end
|
16
|
+
|
17
|
+
sh "#{webpack_bin} --config #{config_file} --bail"
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'webpack/rails'
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'action_view'
|
2
|
+
require 'webpack/rails/manifest'
|
3
|
+
|
4
|
+
module Webpack
|
5
|
+
module Rails
|
6
|
+
# Asset path helpers for use with webpack
|
7
|
+
module Helper
|
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, extension: nil)
|
16
|
+
return "" unless source.present?
|
17
|
+
|
18
|
+
paths = Webpack::Rails::Manifest.asset_paths(source)
|
19
|
+
paths = paths.select { |p| p.ends_with? ".#{extension}" } if extension
|
20
|
+
|
21
|
+
port = ::Rails.configuration.webpack.dev_server.port
|
22
|
+
protocol = ::Rails.configuration.webpack.dev_server.https ? 'https' : 'http'
|
23
|
+
|
24
|
+
host = ::Rails.configuration.webpack.dev_server.host
|
25
|
+
host = instance_eval(&host) if host.respond_to?(:call)
|
26
|
+
|
27
|
+
if ::Rails.configuration.webpack.dev_server.enabled
|
28
|
+
paths.map! do |p|
|
29
|
+
"#{protocol}://#{host}:#{port}#{p}"
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
paths
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,102 @@
|
|
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 webpack couldn't build one of your entry points
|
16
|
+
class WebpackError < StandardError
|
17
|
+
def initialize(errors)
|
18
|
+
super "Error in webpack compile, details follow below:\n#{errors.join("\n\n")}"
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
# Raised if a supplied entry point does not exist in the webpack manifest
|
23
|
+
class EntryPointMissingError < StandardError
|
24
|
+
end
|
25
|
+
|
26
|
+
class << self
|
27
|
+
# :nodoc:
|
28
|
+
def asset_paths(source)
|
29
|
+
raise WebpackError, manifest["errors"] unless manifest_bundled?
|
30
|
+
|
31
|
+
paths = manifest["assetsByChunkName"][source]
|
32
|
+
if paths
|
33
|
+
# Can be either a string or an array of strings.
|
34
|
+
# Do not include source maps as they are not javascript
|
35
|
+
[paths].flatten.reject { |p| p =~ /.*\.map$/ }.map do |p|
|
36
|
+
"/#{::Rails.configuration.webpack.public_path}/#{p}"
|
37
|
+
end
|
38
|
+
else
|
39
|
+
raise EntryPointMissingError, "Can't find entry point '#{source}' in webpack manifest"
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
private
|
44
|
+
|
45
|
+
def manifest_bundled?
|
46
|
+
!manifest["errors"].any? { |error| error.include? "Module build failed" }
|
47
|
+
end
|
48
|
+
|
49
|
+
def manifest
|
50
|
+
if ::Rails.configuration.webpack.dev_server.enabled
|
51
|
+
# Don't cache if we're in dev server mode, manifest may change ...
|
52
|
+
load_manifest
|
53
|
+
else
|
54
|
+
# ... otherwise cache at class level, as JSON loading/parsing can be expensive
|
55
|
+
@manifest ||= load_manifest
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def load_manifest
|
60
|
+
data = if ::Rails.configuration.webpack.dev_server.enabled
|
61
|
+
load_dev_server_manifest
|
62
|
+
else
|
63
|
+
load_static_manifest
|
64
|
+
end
|
65
|
+
JSON.parse(data)
|
66
|
+
end
|
67
|
+
|
68
|
+
def load_dev_server_manifest
|
69
|
+
host = ::Rails.configuration.webpack.dev_server.manifest_host
|
70
|
+
port = ::Rails.configuration.webpack.dev_server.manifest_port
|
71
|
+
http = Net::HTTP.new(host, port)
|
72
|
+
http.use_ssl = ::Rails.configuration.webpack.dev_server.https
|
73
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
74
|
+
http.get(dev_server_path).body
|
75
|
+
rescue => e
|
76
|
+
raise ManifestLoadError.new("Could not load manifest from webpack-dev-server at http://#{host}:#{port}#{dev_server_path} - is it running, and is stats-webpack-plugin loaded?", e)
|
77
|
+
end
|
78
|
+
|
79
|
+
def load_static_manifest
|
80
|
+
File.read(static_manifest_path)
|
81
|
+
rescue => e
|
82
|
+
raise ManifestLoadError.new("Could not load compiled manifest from #{static_manifest_path} - have you run `rake webpack:compile`?", e)
|
83
|
+
end
|
84
|
+
|
85
|
+
def static_manifest_path
|
86
|
+
::Rails.root.join(
|
87
|
+
::Rails.configuration.webpack.output_dir,
|
88
|
+
::Rails.configuration.webpack.manifest_filename
|
89
|
+
)
|
90
|
+
end
|
91
|
+
|
92
|
+
def dev_server_path
|
93
|
+
"/#{::Rails.configuration.webpack.public_path}/#{::Rails.configuration.webpack.manifest_filename}"
|
94
|
+
end
|
95
|
+
|
96
|
+
def dev_server_url
|
97
|
+
"http://#{::Rails.configuration.webpack.dev_server.host}:#{::Rails.configuration.webpack.dev_server.port}#{dev_server_path}"
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'rails'
|
2
|
+
require 'rails/railtie'
|
3
|
+
require 'webpack/rails/helper'
|
4
|
+
|
5
|
+
module Webpack
|
6
|
+
# :nodoc:
|
7
|
+
class Railtie < ::Rails::Railtie
|
8
|
+
config.after_initialize do
|
9
|
+
ActiveSupport.on_load(:action_view) do
|
10
|
+
include Webpack::Rails::Helper
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
config.webpack = ActiveSupport::OrderedOptions.new
|
15
|
+
config.webpack.dev_server = ActiveSupport::OrderedOptions.new
|
16
|
+
|
17
|
+
config.webpack.config_file = 'config/webpack.config.js'
|
18
|
+
config.webpack.binary = 'node_modules/.bin/webpack'
|
19
|
+
|
20
|
+
# Host & port to use when generating asset URLS in the manifest helpers in dev
|
21
|
+
# server mode. Defaults to the requested host rather than localhost, so
|
22
|
+
# that requests from remote hosts work.
|
23
|
+
config.webpack.dev_server.host = proc { respond_to?(:request) ? request.host : 'localhost' }
|
24
|
+
config.webpack.dev_server.port = 3808
|
25
|
+
|
26
|
+
# The host and port to use when fetching the manifest
|
27
|
+
# This is helpful for e.g. docker containers, where the host and port you
|
28
|
+
# use via the web browser is not the same as those that the containers use
|
29
|
+
# to communicate among each other
|
30
|
+
config.webpack.dev_server.manifest_host = 'localhost'
|
31
|
+
config.webpack.dev_server.manifest_port = 3808
|
32
|
+
|
33
|
+
config.webpack.dev_server.https = false # note - this will use OpenSSL::SSL::VERIFY_NONE
|
34
|
+
config.webpack.dev_server.binary = 'node_modules/.bin/webpack-dev-server'
|
35
|
+
config.webpack.dev_server.enabled = ::Rails.env.development? || ::Rails.env.test?
|
36
|
+
|
37
|
+
config.webpack.output_dir = "public/webpack"
|
38
|
+
config.webpack.public_path = "webpack"
|
39
|
+
config.webpack.manifest_filename = "manifest.json"
|
40
|
+
|
41
|
+
rake_tasks do
|
42
|
+
load "tasks/webpack.rake"
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
metadata
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: webpack2-rails
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 2.2.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Michael Pearson
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-02-25 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: railties
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 3.2.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 3.2.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rails
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 3.2.0
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 3.2.0
|
41
|
+
description: Production-tested, JavaScript-first tooling to use Webpack 2 within your
|
42
|
+
Rails application
|
43
|
+
email:
|
44
|
+
- mipearson@gmail.com
|
45
|
+
executables: []
|
46
|
+
extensions: []
|
47
|
+
extra_rdoc_files: []
|
48
|
+
files:
|
49
|
+
- MIT-LICENSE
|
50
|
+
- README.md
|
51
|
+
- Rakefile
|
52
|
+
- example/Procfile
|
53
|
+
- example/dot_gitignore
|
54
|
+
- example/package.json
|
55
|
+
- example/webpack.config.js
|
56
|
+
- lib/generators/webpack_rails/install_generator.rb
|
57
|
+
- lib/tasks/webpack.rake
|
58
|
+
- lib/webpack-rails.rb
|
59
|
+
- lib/webpack/rails.rb
|
60
|
+
- lib/webpack/rails/helper.rb
|
61
|
+
- lib/webpack/rails/manifest.rb
|
62
|
+
- lib/webpack/rails/version.rb
|
63
|
+
- lib/webpack/railtie.rb
|
64
|
+
homepage: http://github.com/davetron5000/webpack2-rails
|
65
|
+
licenses:
|
66
|
+
- MIT
|
67
|
+
metadata: {}
|
68
|
+
post_install_message:
|
69
|
+
rdoc_options: []
|
70
|
+
require_paths:
|
71
|
+
- lib
|
72
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: 2.0.0
|
77
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
78
|
+
requirements:
|
79
|
+
- - ">="
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: '0'
|
82
|
+
requirements: []
|
83
|
+
rubyforge_project:
|
84
|
+
rubygems_version: 2.5.1
|
85
|
+
signing_key:
|
86
|
+
specification_version: 4
|
87
|
+
summary: Webpack & Rails integration made easier
|
88
|
+
test_files: []
|