webpack-rails-react 0.9.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +101 -0
- data/Rakefile +24 -0
- data/example/Procfile +4 -0
- data/example/dot_gitignore +12 -0
- data/example/package.json +19 -0
- data/example/webpack.config.js +85 -0
- data/lib/generators/webpack_rails/install_generator.rb +115 -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 +32 -0
- data/lib/webpack/rails/manifest.rb +89 -0
- data/lib/webpack/rails/version.rb +6 -0
- data/lib/webpack/railtie.rb +33 -0
- metadata +74 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: a11fdc7f90667286b9ead38db9f12d238b2f9122
|
4
|
+
data.tar.gz: c6940d59b10f4f1578a762b62f33440524ebce2d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a5e9a18c7cc297c191ee8ee46fd28fbe8bd595ee609826bee233206bbcf69c60cbdb9122777e073adb386c4f314071be27eb27502c3c3fe8d6d6fc36e23e6212
|
7
|
+
data.tar.gz: 84b15d55ad4aa77194f9fb819afaf5073076a056c33a167b8fcb5e00b68443be3c2a55142523a681a639be8d938b304d8a9d3ab068f7010f8ebc8a78e08e91c4
|
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,101 @@
|
|
1
|
+
[](https://travis-ci.org/mipearson/webpack-rails) [](http://badge.fury.io/rb/webpack-rails)
|
2
|
+
|
3
|
+
# webpack-rails
|
4
|
+
|
5
|
+
**webpack-rails** gives you tools to integrate Webpack in to an existing Ruby on Rails application.
|
6
|
+
|
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
|
+
|
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
|
+
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.
|
14
|
+
|
15
|
+
This gem has been tested against Rails 4.2 and Ruby 2.2. Earlier versions of Rails (>= 3.2) and Ruby (>= 1.9) may work, but we haven't tested them.
|
16
|
+
|
17
|
+
## Using webpack-rails
|
18
|
+
|
19
|
+
**We have a demo application: [webpack-rails-demo](https://github.com/mipearson/webpack-rails-demo)**
|
20
|
+
|
21
|
+
### Installation
|
22
|
+
|
23
|
+
1. Add `webpack-rails` to your gemfile
|
24
|
+
1. Run `bundle install` to install the gem
|
25
|
+
1. Run `bundle exec rails generate webpack_rails:install` to copy across example files
|
26
|
+
1. Run `foreman start` to start `webpack-dev-server` and `rails server` at the same time
|
27
|
+
1. Add the webpack entry point to your layout (see next section)
|
28
|
+
1. Edit `client/application.js` and write some code
|
29
|
+
|
30
|
+
|
31
|
+
### Adding the entry point to your Rails application
|
32
|
+
|
33
|
+
To add your webpacked javascript in to your app, add the following to the `<head>` section of your to your `layout.html.erb`:
|
34
|
+
|
35
|
+
```erb
|
36
|
+
<%= javascript_include_tag *webpack_asset_paths("application") %>
|
37
|
+
```
|
38
|
+
|
39
|
+
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.
|
40
|
+
|
41
|
+
#### Use with webpack-dev-server live reload
|
42
|
+
|
43
|
+
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:
|
44
|
+
|
45
|
+
``` html
|
46
|
+
<script src="http://localhost:3808/webpack-dev-server.js"></script>
|
47
|
+
```
|
48
|
+
|
49
|
+
### How it works
|
50
|
+
|
51
|
+
Have a look at the files in the `examples` directory. Of note:
|
52
|
+
|
53
|
+
* 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
|
54
|
+
* The webpack and gem configuration must be in sync - look at our railtie for configuration options
|
55
|
+
* We require that **stats-webpack-plugin** is loaded to automatically generate a production manifest & resolve paths during development
|
56
|
+
|
57
|
+
### Configuration Defaults
|
58
|
+
|
59
|
+
* Webpack configuration lives in `config/webpack.config.js`
|
60
|
+
* Webpack & Webpack Dev Server binaries are in `node_modules/.bin/`
|
61
|
+
* Webpack Dev Server will run on port 3808 on localhost via HTTP
|
62
|
+
* Webpack Dev Server is enabled in development & test, but not in production
|
63
|
+
* Webpacked assets will be compiled to `public/webpack`
|
64
|
+
* The manifest file is named `manifest.json`
|
65
|
+
|
66
|
+
### Working with browser tests
|
67
|
+
|
68
|
+
In development, we make sure that the `webpack-dev-server` is running when browser tests are running.
|
69
|
+
|
70
|
+
#### Continuous Integration
|
71
|
+
|
72
|
+
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`:
|
73
|
+
|
74
|
+
``` ruby
|
75
|
+
config.webpack.dev_server.enabled = !ENV['CI']
|
76
|
+
```
|
77
|
+
|
78
|
+
### Production Deployment
|
79
|
+
|
80
|
+
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.
|
81
|
+
|
82
|
+
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.
|
83
|
+
|
84
|
+
## TODO
|
85
|
+
|
86
|
+
* Drive config via JSON, have webpack.config.js read same JSON?
|
87
|
+
* Custom webpack-dev-server that exposes errors, stats, etc
|
88
|
+
* [react-rails](https://github.com/reactjs/react-rails) fork for use with this workflow
|
89
|
+
* Integration tests
|
90
|
+
|
91
|
+
## Contributing
|
92
|
+
|
93
|
+
Pull requests & issues welcome. Advice & criticism regarding webpack config approach also welcome.
|
94
|
+
|
95
|
+
Please ensure that pull requests pass both rubocop & rspec. New functionality should be discussed in an issue first.
|
96
|
+
|
97
|
+
## Acknowledgements
|
98
|
+
|
99
|
+
* Len Garvey for his [webpack-rails](https://github.com/lengarvey/webpack-rails) gem which inspired this implementation
|
100
|
+
* Sebastian Porto for [Rails with Webpack](https://reinteractive.net/posts/213-rails-with-webpack-why-and-how)
|
101
|
+
* 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,19 @@
|
|
1
|
+
{
|
2
|
+
"name": "webpack-rails-react",
|
3
|
+
"version": "0.0.1",
|
4
|
+
"license": "MIT",
|
5
|
+
"dependencies": {
|
6
|
+
"react": "^15.1.0",
|
7
|
+
"react-dom": "^15.1.0",
|
8
|
+
"stats-webpack-plugin": "^0.2.1",
|
9
|
+
"webpack": "^1.9.11"
|
10
|
+
},
|
11
|
+
"devDependencies": {
|
12
|
+
"babel-core": "^6.9.1",
|
13
|
+
"babel-loader": "^6.2.4",
|
14
|
+
"babel-preset-es2015": "^6.9.0",
|
15
|
+
"babel-preset-react": "^6.5.0",
|
16
|
+
"babel-preset-stage-0": "^6.5.0",
|
17
|
+
"webpack-dev-server": "^1.9.0"
|
18
|
+
}
|
19
|
+
}
|
@@ -0,0 +1,85 @@
|
|
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 TARGET=production on the environment to add asset fingerprints
|
12
|
+
var production = process.env.TARGET === '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
|
+
root: path.join(__dirname, '..', 'webpack'),
|
33
|
+
extensions: ["", ".js", ".jsx", ".es6"]
|
34
|
+
},
|
35
|
+
|
36
|
+
module: {
|
37
|
+
loaders: [
|
38
|
+
{
|
39
|
+
test: /\.jsx?$/, // Match both .js and .jsx files
|
40
|
+
exclude: /node_modules/,
|
41
|
+
loader: "babel",
|
42
|
+
query:
|
43
|
+
{
|
44
|
+
presets:['es2015', 'react', 'stage-0']
|
45
|
+
}
|
46
|
+
}
|
47
|
+
]
|
48
|
+
},
|
49
|
+
|
50
|
+
plugins: [
|
51
|
+
// must match config.webpack.manifest_filename
|
52
|
+
new StatsPlugin('manifest.json', {
|
53
|
+
// We only need assetsByChunkName
|
54
|
+
chunkModules: false,
|
55
|
+
source: false,
|
56
|
+
chunks: false,
|
57
|
+
modules: false,
|
58
|
+
assets: true
|
59
|
+
})]
|
60
|
+
};
|
61
|
+
|
62
|
+
if (production) {
|
63
|
+
config.plugins.push(
|
64
|
+
new webpack.NoErrorsPlugin(),
|
65
|
+
new webpack.optimize.UglifyJsPlugin({
|
66
|
+
compressor: { warnings: false },
|
67
|
+
sourceMap: false
|
68
|
+
}),
|
69
|
+
new webpack.DefinePlugin({
|
70
|
+
'process.env': { NODE_ENV: JSON.stringify('production') }
|
71
|
+
}),
|
72
|
+
new webpack.optimize.DedupePlugin(),
|
73
|
+
new webpack.optimize.OccurenceOrderPlugin()
|
74
|
+
);
|
75
|
+
} else {
|
76
|
+
config.devServer = {
|
77
|
+
port: devServerPort,
|
78
|
+
headers: { 'Access-Control-Allow-Origin': '*' }
|
79
|
+
};
|
80
|
+
config.output.publicPath = '//localhost:' + devServerPort + '/webpack/';
|
81
|
+
// Source maps
|
82
|
+
config.devtool = 'cheap-module-eval-source-map';
|
83
|
+
}
|
84
|
+
|
85
|
+
module.exports = config;
|
@@ -0,0 +1,115 @@
|
|
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
|
+
import React from 'react';
|
29
|
+
import ReactDOM from 'react-dom';
|
30
|
+
import App from './App';
|
31
|
+
|
32
|
+
ReactDOM.render(
|
33
|
+
<App />,
|
34
|
+
Document.findElementById('app')
|
35
|
+
)
|
36
|
+
EOF
|
37
|
+
end
|
38
|
+
|
39
|
+
create_file "webpack/App.js" do
|
40
|
+
<<-EOF.strip_heredoc
|
41
|
+
import React from 'react';
|
42
|
+
|
43
|
+
class App extends React.Component {
|
44
|
+
render() {
|
45
|
+
return(
|
46
|
+
<div>
|
47
|
+
Hello World
|
48
|
+
</div>
|
49
|
+
)
|
50
|
+
}
|
51
|
+
}
|
52
|
+
|
53
|
+
export default App;
|
54
|
+
EOF
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def add_to_gitignore
|
59
|
+
append_to_file ".gitignore" do
|
60
|
+
<<-EOF.strip_heredoc
|
61
|
+
# Added by webpack-rails
|
62
|
+
/node_modules
|
63
|
+
/public/webpack
|
64
|
+
EOF
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def run_npm_install
|
69
|
+
run "npm install" if yes?("Would you like us to run 'npm install' for you?")
|
70
|
+
end
|
71
|
+
|
72
|
+
def run_bundle_install
|
73
|
+
run "bundle install" if yes?("Would you like us to run 'bundle install' for you?")
|
74
|
+
end
|
75
|
+
|
76
|
+
def whats_next
|
77
|
+
puts <<-EOF.strip_heredoc
|
78
|
+
|
79
|
+
We've set up the basics of webpack-rails for you, but you'll still
|
80
|
+
need to:
|
81
|
+
|
82
|
+
1. Add the 'application' entry point in to your layout, and
|
83
|
+
e.g. <%= javascript_include_tag *webpack_asset_paths('application') %>
|
84
|
+
2. Add an element with an id of 'app' to your layout
|
85
|
+
3. Enable hot module replacement by adding <script src="http://localhost:3808/webpack-dev-server.js"></script> to your layout
|
86
|
+
4. Run 'foreman start' to run the webpack-dev-server and rails server
|
87
|
+
|
88
|
+
Example app/views/layouts/application.html.erb
|
89
|
+
<!DOCTYPE html>
|
90
|
+
<html>
|
91
|
+
<head>
|
92
|
+
<title>WebpackDemo</title>
|
93
|
+
<%= stylesheet_link_tag 'application', media: 'all' %>
|
94
|
+
<%= javascript_include_tag 'application' %>
|
95
|
+
<script src="http://localhost:3808/webpack-dev-server.js"></script>
|
96
|
+
<%= csrf_meta_tags %>
|
97
|
+
</head>
|
98
|
+
<body>
|
99
|
+
|
100
|
+
<%= yield %>
|
101
|
+
<%= javascript_include_tag *webpack_asset_paths('application') %>
|
102
|
+
</body>
|
103
|
+
</html>
|
104
|
+
|
105
|
+
|
106
|
+
See the README.md for this gem at
|
107
|
+
https://github.com/wdjungst/webpack-rails-react/blob/master/README.md
|
108
|
+
for more info.
|
109
|
+
|
110
|
+
Thanks for using webpack-rails-react!
|
111
|
+
|
112
|
+
EOF
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
@@ -0,0 +1,19 @@
|
|
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
|
+
result = `#{webpack_bin} --bail --config #{config_file} 2>&1`
|
17
|
+
raise result unless $? == 0
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'webpack/rails'
|
@@ -0,0 +1,32 @@
|
|
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)
|
16
|
+
return "" unless source.present?
|
17
|
+
|
18
|
+
paths = Webpack::Rails::Manifest.asset_paths(source)
|
19
|
+
host = ::Rails.configuration.webpack.dev_server.host
|
20
|
+
port = ::Rails.configuration.webpack.dev_server.port
|
21
|
+
|
22
|
+
if ::Rails.configuration.webpack.dev_server.enabled
|
23
|
+
paths.map! do |p|
|
24
|
+
"//#{host}:#{port}#{p}"
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
paths
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,89 @@
|
|
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
|
+
# Do not include source maps as they are not javascript
|
26
|
+
[paths].flatten.reject { |p| p =~ /.*\.map$/ }.map do |p|
|
27
|
+
"/#{::Rails.configuration.webpack.public_path}/#{p}"
|
28
|
+
end
|
29
|
+
else
|
30
|
+
raise EntryPointMissingError, "Can't find entry point '#{source}' in webpack manifest"
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
private
|
35
|
+
|
36
|
+
def manifest
|
37
|
+
if ::Rails.configuration.webpack.dev_server.enabled
|
38
|
+
# Don't cache if we're in dev server mode, manifest may change ...
|
39
|
+
load_manifest
|
40
|
+
else
|
41
|
+
# ... otherwise cache at class level, as JSON loading/parsing can be expensive
|
42
|
+
@manifest ||= load_manifest
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def load_manifest
|
47
|
+
data = if ::Rails.configuration.webpack.dev_server.enabled
|
48
|
+
load_dev_server_manifest
|
49
|
+
else
|
50
|
+
load_static_manifest
|
51
|
+
end
|
52
|
+
JSON.parse(data)
|
53
|
+
end
|
54
|
+
|
55
|
+
def load_dev_server_manifest
|
56
|
+
http = Net::HTTP.new(
|
57
|
+
"localhost",
|
58
|
+
::Rails.configuration.webpack.dev_server.port)
|
59
|
+
http.use_ssl = ::Rails.configuration.webpack.dev_server.https
|
60
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
61
|
+
http.get(dev_server_path).body
|
62
|
+
rescue => e
|
63
|
+
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)
|
64
|
+
end
|
65
|
+
|
66
|
+
def load_static_manifest
|
67
|
+
File.read(static_manifest_path)
|
68
|
+
rescue => e
|
69
|
+
raise ManifestLoadError.new("Could not load compiled manifest from #{static_manifest_path} - have you run `rake webpack:compile`?", e)
|
70
|
+
end
|
71
|
+
|
72
|
+
def static_manifest_path
|
73
|
+
::Rails.root.join(
|
74
|
+
::Rails.configuration.webpack.output_dir,
|
75
|
+
::Rails.configuration.webpack.manifest_filename
|
76
|
+
)
|
77
|
+
end
|
78
|
+
|
79
|
+
def dev_server_path
|
80
|
+
"/#{::Rails.configuration.webpack.public_path}/#{::Rails.configuration.webpack.manifest_filename}"
|
81
|
+
end
|
82
|
+
|
83
|
+
def dev_server_url
|
84
|
+
"http://localhost:#{::Rails.configuration.webpack.dev_server.port}#{dev_server_path}"
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
@@ -0,0 +1,33 @@
|
|
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.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.https = false # note - this will use OpenSSL::SSL::VERIFY_NONE
|
22
|
+
config.webpack.dev_server.binary = 'node_modules/.bin/webpack-dev-server'
|
23
|
+
config.webpack.dev_server.enabled = !::Rails.env.production?
|
24
|
+
|
25
|
+
config.webpack.output_dir = "public/webpack"
|
26
|
+
config.webpack.public_path = "webpack"
|
27
|
+
config.webpack.manifest_filename = "manifest.json"
|
28
|
+
|
29
|
+
rake_tasks do
|
30
|
+
load "tasks/webpack.rake"
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
metadata
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: webpack-rails-react
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.9.9
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Dave Jugnst
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-06-07 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
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
|
+
description: Production-tested, JavaScript-first tooling to use webpack within your
|
28
|
+
Rails application
|
29
|
+
email:
|
30
|
+
- djungst@gmail.com
|
31
|
+
executables: []
|
32
|
+
extensions: []
|
33
|
+
extra_rdoc_files: []
|
34
|
+
files:
|
35
|
+
- MIT-LICENSE
|
36
|
+
- README.md
|
37
|
+
- Rakefile
|
38
|
+
- example/Procfile
|
39
|
+
- example/dot_gitignore
|
40
|
+
- example/package.json
|
41
|
+
- example/webpack.config.js
|
42
|
+
- lib/generators/webpack_rails/install_generator.rb
|
43
|
+
- lib/tasks/webpack.rake
|
44
|
+
- lib/webpack-rails.rb
|
45
|
+
- lib/webpack/rails.rb
|
46
|
+
- lib/webpack/rails/helper.rb
|
47
|
+
- lib/webpack/rails/manifest.rb
|
48
|
+
- lib/webpack/rails/version.rb
|
49
|
+
- lib/webpack/railtie.rb
|
50
|
+
homepage: https://github.com/wdjungst/webpack-rails-react
|
51
|
+
licenses:
|
52
|
+
- MIT
|
53
|
+
metadata: {}
|
54
|
+
post_install_message:
|
55
|
+
rdoc_options: []
|
56
|
+
require_paths:
|
57
|
+
- lib
|
58
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
63
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '0'
|
68
|
+
requirements: []
|
69
|
+
rubyforge_project:
|
70
|
+
rubygems_version: 2.4.5
|
71
|
+
signing_key:
|
72
|
+
specification_version: 4
|
73
|
+
summary: Webpack / Rails / React
|
74
|
+
test_files: []
|