rs-webpack-rails 0.10.0
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 +156 -0
- data/Rakefile +24 -0
- data/example/Procfile +4 -0
- data/example/dot_gitignore +12 -0
- data/example/package.json +10 -0
- data/example/public/webpack/manifest.json +3 -0
- data/example/webpack.config.js +78 -0
- data/example/webpack/application.js +0 -0
- data/example/yarn.lock +3030 -0
- data/lib/generators/webpack_rails/install_generator.rb +69 -0
- data/lib/rs-webpack-rails.rb +1 -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 +42 -0
- data/lib/webpack/rails/manifest.rb +114 -0
- data/lib/webpack/rails/version.rb +6 -0
- data/lib/webpack/railtie.rb +51 -0
- metadata +91 -0
@@ -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_yarn_install
|
44
|
+
run "yarn install" if yes?("Would you like us to run 'yarn 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 @@
|
|
1
|
+
require 'webpack/rails'
|
@@ -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,42 @@
|
|
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
|
+
if ::Rails.configuration.webpack.manifest_type == "manifest"
|
19
|
+
extension = "js" if extension.nil?
|
20
|
+
paths = Webpack::Rails::Manifest.manifest_asset_paths(source + "." + extension)
|
21
|
+
elsif ::Rails.configuration.webpack.manifest_type == "stats"
|
22
|
+
paths = Webpack::Rails::Manifest.asset_paths(source)
|
23
|
+
paths = paths.select { |p| p.ends_with? ".#{extension}" } if extension
|
24
|
+
end
|
25
|
+
|
26
|
+
port = ::Rails.configuration.webpack.dev_server.port
|
27
|
+
protocol = ::Rails.configuration.webpack.dev_server.https ? 'https' : 'http'
|
28
|
+
|
29
|
+
host = ::Rails.configuration.webpack.dev_server.host
|
30
|
+
host = instance_eval(&host) if host.respond_to?(:call)
|
31
|
+
|
32
|
+
if ::Rails.configuration.webpack.dev_server.enabled && ::Rails.configuration.webpack.manifest_type == "stats"
|
33
|
+
paths.map! do |p|
|
34
|
+
"#{protocol}://#{host}:#{port}#{p}"
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
paths
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,114 @@
|
|
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
|
+
def manifest_asset_paths(source)
|
44
|
+
raise WebpackError, manifest["errors"] unless manifest_bundled?
|
45
|
+
|
46
|
+
paths = manifest[source]
|
47
|
+
if paths
|
48
|
+
paths
|
49
|
+
else
|
50
|
+
raise EntryPointMissingError, "Can't find entry point '#{source}' in webpack manifest"
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
private
|
55
|
+
|
56
|
+
def manifest_bundled?
|
57
|
+
return true unless manifest.key?("errors")
|
58
|
+
!manifest["errors"].any? { |error| error.include? "Module build failed" }
|
59
|
+
end
|
60
|
+
|
61
|
+
def manifest
|
62
|
+
if ::Rails.configuration.webpack.dev_server.enabled
|
63
|
+
# Don't cache if we're in dev server mode, manifest may change ...
|
64
|
+
load_manifest
|
65
|
+
else
|
66
|
+
# ... otherwise cache at class level, as JSON loading/parsing can be expensive
|
67
|
+
@manifest ||= load_manifest
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
def load_manifest
|
72
|
+
data = if ::Rails.configuration.webpack.dev_server.enabled
|
73
|
+
load_dev_server_manifest
|
74
|
+
else
|
75
|
+
load_static_manifest
|
76
|
+
end
|
77
|
+
JSON.parse(data)
|
78
|
+
end
|
79
|
+
|
80
|
+
def load_dev_server_manifest
|
81
|
+
host = ::Rails.configuration.webpack.dev_server.manifest_host
|
82
|
+
port = ::Rails.configuration.webpack.dev_server.manifest_port
|
83
|
+
http = Net::HTTP.new(host, port)
|
84
|
+
http.use_ssl = ::Rails.configuration.webpack.dev_server.https
|
85
|
+
http.verify_mode = ::Rails.configuration.webpack.dev_server.https_verify_peer ? OpenSSL::SSL::VERIFY_PEER : OpenSSL::SSL::VERIFY_NONE
|
86
|
+
http.get(dev_server_path).body
|
87
|
+
rescue => e
|
88
|
+
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)
|
89
|
+
end
|
90
|
+
|
91
|
+
def load_static_manifest
|
92
|
+
File.read(static_manifest_path)
|
93
|
+
rescue => e
|
94
|
+
raise ManifestLoadError.new("Could not load compiled manifest from #{static_manifest_path} - have you run `rake webpack:compile`?", e)
|
95
|
+
end
|
96
|
+
|
97
|
+
def static_manifest_path
|
98
|
+
::Rails.root.join(
|
99
|
+
::Rails.configuration.webpack.output_dir,
|
100
|
+
::Rails.configuration.webpack.manifest_filename
|
101
|
+
)
|
102
|
+
end
|
103
|
+
|
104
|
+
def dev_server_path
|
105
|
+
"/#{::Rails.configuration.webpack.public_path}/#{::Rails.configuration.webpack.manifest_filename}"
|
106
|
+
end
|
107
|
+
|
108
|
+
def dev_server_url
|
109
|
+
"http://#{::Rails.configuration.webpack.dev_server.host}:#{::Rails.configuration.webpack.dev_server.port}#{dev_server_path}"
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
@@ -0,0 +1,51 @@
|
|
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
|
34
|
+
# Below will default to 'true' in 1.0 release
|
35
|
+
config.webpack.dev_server.https_verify_peer = false
|
36
|
+
config.webpack.dev_server.binary = 'node_modules/.bin/webpack-dev-server'
|
37
|
+
config.webpack.dev_server.enabled = ::Rails.env.development? || ::Rails.env.test?
|
38
|
+
|
39
|
+
config.webpack.output_dir = "public/webpack"
|
40
|
+
config.webpack.public_path = "webpack"
|
41
|
+
config.webpack.manifest_filename = "manifest.json"
|
42
|
+
|
43
|
+
# manifest: https://github.com/danethurber/webpack-manifest-plugin
|
44
|
+
# stats: https://github.com/unindented/stats-webpack-plugin
|
45
|
+
config.webpack.manifest_type = "stats"
|
46
|
+
|
47
|
+
rake_tasks do
|
48
|
+
load "tasks/webpack.rake"
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
metadata
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rs-webpack-rails
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.10.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Michael Pearson
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-12-27 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: No longer maintained - use webpacker instead.
|
42
|
+
email:
|
43
|
+
- mipearson@gmail.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- MIT-LICENSE
|
49
|
+
- README.md
|
50
|
+
- Rakefile
|
51
|
+
- example/Procfile
|
52
|
+
- example/dot_gitignore
|
53
|
+
- example/package.json
|
54
|
+
- example/public/webpack/manifest.json
|
55
|
+
- example/webpack.config.js
|
56
|
+
- example/webpack/application.js
|
57
|
+
- example/yarn.lock
|
58
|
+
- lib/generators/webpack_rails/install_generator.rb
|
59
|
+
- lib/rs-webpack-rails.rb
|
60
|
+
- lib/tasks/webpack.rake
|
61
|
+
- lib/webpack-rails.rb
|
62
|
+
- lib/webpack/rails.rb
|
63
|
+
- lib/webpack/rails/helper.rb
|
64
|
+
- lib/webpack/rails/manifest.rb
|
65
|
+
- lib/webpack/rails/version.rb
|
66
|
+
- lib/webpack/railtie.rb
|
67
|
+
homepage: http://github.com/mipearson/webpack-rails
|
68
|
+
licenses:
|
69
|
+
- MIT
|
70
|
+
metadata: {}
|
71
|
+
post_install_message:
|
72
|
+
rdoc_options: []
|
73
|
+
require_paths:
|
74
|
+
- lib
|
75
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
76
|
+
requirements:
|
77
|
+
- - ">="
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: 2.0.0
|
80
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
81
|
+
requirements:
|
82
|
+
- - ">="
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: '0'
|
85
|
+
requirements: []
|
86
|
+
rubyforge_project:
|
87
|
+
rubygems_version: 2.6.14
|
88
|
+
signing_key:
|
89
|
+
specification_version: 4
|
90
|
+
summary: No longer maintained - use webpacker instead.
|
91
|
+
test_files: []
|