webpack-rails-helper 0.1.0

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: cd694a44510dcf73b8765e931ef75f095d8082fa
4
+ data.tar.gz: 85b6586048fc817f92f21c0b37589089ac2c38bf
5
+ SHA512:
6
+ metadata.gz: 3bc47eb214e6368e1f114f5eec61b46fcd8558eefe70b56f9d4aa944ef0bb0419e0717fddf6f3aa1f79c7782f4020c310438ae6b943ef79db3b3c4733a76b277
7
+ data.tar.gz: 704fc44f673d3481cdbc58e411c57480d260e5a7b73c72675d1d591189448730bef69b59faea11369e3efae1e615aacf5af4c7f48a3326586a6388f65017a654
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ Copyright (C) 2017 shellyBits
2
+ Copyright (C) 2015 Michael Pearson
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining
5
+ a copy of this software and associated documentation files (the
6
+ "Software"), to deal in the Software without restriction, including
7
+ without limitation the rights to use, copy, modify, merge, publish,
8
+ distribute, sublicense, and/or sell copies of the Software, and to
9
+ permit persons to whom the Software is furnished to do so, subject to
10
+ the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,135 @@
1
+ # webpack-rails-helper
2
+
3
+ This gem integrates assets from webpack into a Ruby on Rails application. This
4
+ basically converts between the asset name and the asset name as emitted by
5
+ webpack with hashes and stuff added.
6
+
7
+
8
+ This works by using a JSON manifest file. When using together with
9
+ `webpack-dev-server`, the manifest is re-read on every call to one of the
10
+ helpers. This isn't exactly efficient but isn't much of a problem in development
11
+ mode. In production mode, the manifest is read once.
12
+
13
+
14
+ ## Installation
15
+
16
+
17
+ * Add this to `Gemfile`:
18
+
19
+ ```
20
+ gem 'webpack-rails-helper'
21
+ ```
22
+
23
+ * Run `bundle`
24
+
25
+ * Add this to `"devDependencies"` of `package.json`:
26
+
27
+ ```
28
+ "webpack-manifest-plugin": "^1.2.1"
29
+ ```
30
+
31
+ * Run `yarn` (or `npm install` if you must)
32
+
33
+ * In `webpack.config.js`, add the plugin
34
+
35
+ ```
36
+ ...
37
+ plugins: [
38
+ // other plugins here
39
+ new ManifestPlugin()
40
+ ]
41
+ ...
42
+ ```
43
+
44
+ ## Configuration
45
+
46
+ In `config/application.rb` or in a `config/environments/*.rb`, configure it.
47
+ Defaults are as follows:
48
+
49
+ ```
50
+ config.webpack.dev_server.host = 'localhost'
51
+ config.webpack.dev_server.port = 3808
52
+
53
+ # The host and port to use when fetching the manifest
54
+ # This is helpful for e.g. docker containers, where the host and port you
55
+ # use via the web browser is not the same as those that the containers use
56
+ # to communicate among each other. Uses 'host' and 'port' when set to nil.
57
+ config.webpack.dev_server.manifest_host = nil
58
+ config.webpack.dev_server.manifest_port = nil
59
+
60
+ config.webpack.dev_server.https = false # note - this will use OpenSSL::SSL::VERIFY_NONE
61
+ config.webpack.dev_server.enabled = ::Rails.env.development?
62
+
63
+ config.webpack.output_dir = 'public/webpack'
64
+ config.webpack.public_path = 'webpack'
65
+ config.webpack.manifest_filename = 'manifest.json'
66
+ ```
67
+
68
+ ## Running
69
+
70
+ It's recommended to use `foreman` or something similar to start Rails and the
71
+ webpack server during development. When using `foreman`, the `Procfile` could
72
+ look like this:
73
+
74
+ ```
75
+ rails: ./bin/rails server
76
+ webpack: ./node_modules/.bin/webpack-dev-server --hot --inline --config config/webpack.config.js
77
+ ```
78
+
79
+
80
+ ## API
81
+
82
+ The gem provides helper methods, available in views and controllers:
83
+
84
+
85
+ * Generic asset:
86
+
87
+ ```
88
+ webpack_asset_path(source, optional = false)
89
+ ```
90
+
91
+ When using this method, the full name must be specified, including the file
92
+ extension. When `optional` is set to `true`, a non-existing asset will not
93
+ raise an exception. This is useful when some assets are only available in
94
+ production mode, e.g. extracted CSS.
95
+
96
+ * Javascript tag:
97
+
98
+ ```
99
+ javascript_webpack_tag(*names, **options)
100
+ ```
101
+
102
+ This creates a javascript include tag, just like `javascript_include_tag`
103
+ would, but using an asset from webpack. The `.js` extension is added
104
+ automatically if required.
105
+
106
+
107
+ * Javascript tag:
108
+
109
+ ```
110
+ stylesheet_webpack_tag(*names, **options)
111
+ ```
112
+
113
+ This creates a stylesheet link tag, just like `stylesheet_link_tag`
114
+ would, but using an asset from webpack. The `.css` extension is added
115
+ automatically if required. CSS assets are considered optional when the
116
+ dev_server is running for reasons stated above.
117
+
118
+
119
+ # Acknowledgements
120
+
121
+ This started as a fork of <https://github.com/mipearson/webpack-rails.git>
122
+
123
+
124
+ The reason for the fork is the usage of a different webpack manifest format. The
125
+ original depends on 'stats-webpack-plugin' while this uses
126
+ 'webpack-manifest-plugin'. Since the file formats are so different, it results
127
+ in an incompatible API.
128
+
129
+ This code has lived inside an application and has been extracted back out into
130
+ this gem.
131
+
132
+
133
+ # License
134
+
135
+ This gem is released under the [MIT license](LICENSE).
@@ -0,0 +1,13 @@
1
+ module Webpack
2
+ module RailsHelper
3
+ extend self
4
+
5
+ def bootstrap
6
+ Webpack::RailsHelper::Manifest.load_manifest
7
+ end
8
+ end
9
+ end
10
+
11
+ require 'webpack/rails_helper/config'
12
+ require 'webpack/rails_helper/manifest'
13
+ require 'webpack/rails_helper/railtie'
@@ -0,0 +1,49 @@
1
+ require 'uri'
2
+
3
+ class Webpack::RailsHelper::Config
4
+ class << self
5
+ def dev_server_enabled?
6
+ ::Rails.configuration.webpack.dev_server.enabled
7
+ end
8
+
9
+ def asset_prefix
10
+ @asset_prefix ||=
11
+ if config_root.dev_server.enabled
12
+ port = config_root.dev_server.port
13
+ protocol = config_root.dev_server.https ? 'https' : 'http'
14
+
15
+ host = config_root.dev_server.host
16
+ host = instance_eval(&host) if host.respond_to?(:call)
17
+
18
+ "#{protocol}://#{host}:#{port}"
19
+ else
20
+ ''
21
+ end + "/#{config_root.public_path}/"
22
+ end
23
+
24
+ def static_manifest_path
25
+ ::Rails.root.join(config_root.output_dir, config_root.manifest_filename)
26
+ end
27
+
28
+ def manifest_uri
29
+ @manifest_uri ||= URI::Generic.build(
30
+ scheme: config_root.dev_server.https ? 'https' : 'http',
31
+ host: manifest_host,
32
+ port: config_root.dev_server.manifest_port || config_root.dev_server.port,
33
+ path: "/#{config_root.public_path}/#{config_root.manifest_filename}"
34
+ )
35
+ end
36
+
37
+ private
38
+
39
+ def config_root
40
+ ::Rails.configuration.webpack
41
+ end
42
+
43
+ def manifest_host
44
+ host = config_root.dev_server.manifest_host || config_root.dev_server.host
45
+ host = instance_eval(&host) if host.respond_to?(:call)
46
+ host
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,24 @@
1
+ require 'action_view'
2
+ require_relative 'manifest'
3
+
4
+ module Webpack
5
+ module RailsHelper::Helper
6
+ def webpack_asset_path(sources, optional = false)
7
+ return '' unless source.present?
8
+ asset_path(Webpack::RailsHelper::Manifest.asset_paths(sources, optional), skip_pipeline: true)
9
+ end
10
+
11
+ def javascript_webpack_tag(*names, **options)
12
+ js = names.map { |s| s.end_with?('.js') ? s : s + '.js' }
13
+ sources = Webpack::RailsHelper::Manifest.asset_paths(js)
14
+ javascript_include_tag(*sources, **options)
15
+ end
16
+
17
+ def stylesheet_webpack_tag(*names, **options)
18
+ css = names.map { |s| s.end_with?('.css') ? s : s + '.css' }
19
+ sources = Webpack::RailsHelper::Manifest.asset_paths(css,
20
+ Webpack::RailsHelper::Config.dev_server_enabled?)
21
+ stylesheet_link_tag(*sources, **options)
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,70 @@
1
+ require 'net/http'
2
+ require 'uri'
3
+
4
+ module Webpack
5
+ class RailsHelper::Manifest
6
+ class ManifestLoadError < StandardError; end
7
+ class EntryPointMissingError < StandardError; end
8
+
9
+ class << self
10
+ def asset_paths(source, optional = false)
11
+ manif = manifest
12
+ if source.is_a? Enumerable
13
+ source.map { |s| single_asset_path(manif, s, optional) }
14
+ else
15
+ single_asset_path(manif, source, optional)
16
+ end
17
+ end
18
+
19
+ def load_manifest
20
+ manifest unless Webpack::RailsHelper::Config.dev_server_enabled?
21
+ nil
22
+ end
23
+
24
+ private
25
+
26
+ def single_asset_path(manif, source, optional)
27
+ path = manif[source]
28
+ if path
29
+ Webpack::RailsHelper::Config.asset_prefix + path
30
+ elsif !optional
31
+ raise EntryPointMissingError, "Can't find entry point '#{source}' in webpack manifest"
32
+ else
33
+ ''
34
+ end
35
+ end
36
+
37
+
38
+ def manifest
39
+ if Webpack::RailsHelper::Config.dev_server_enabled?
40
+ # Always reload from dev_server as manifest may change
41
+ load_dev_server_manifest
42
+ else
43
+ @manifest ||= load_static_manifest
44
+ end
45
+ end
46
+
47
+ def parse_manifest(data)
48
+ JSON.parse(data)
49
+ end
50
+
51
+ def load_dev_server_manifest
52
+ uri = Webpack::RailsHelper::Config.manifest_uri
53
+ http = Net::HTTP.new(uri.host, uri.port)
54
+ http.use_ssl = uri.scheme == 'https'
55
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
56
+ parse_manifest(http.get(uri.path).body)
57
+ rescue => e
58
+ raise ManifestLoadError,
59
+ "Could not load manifest from webpack-dev-server at #{Webpack::RailsHelper::Config.manifest_uri}: #{e}"
60
+ end
61
+
62
+ def load_static_manifest
63
+ parse_manifest(File.read(Webpack::RailsHelper::Config.static_manifest_path))
64
+ rescue => e
65
+ raise ManifestLoadError,
66
+ "Could not load compiled manifest from #{Webpack::RailsHelper::Config.static_manifest_path}: #{e}"
67
+ end
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,43 @@
1
+ require 'rails/railtie'
2
+
3
+ require 'webpack/rails_helper/helper'
4
+
5
+ module Webpack
6
+ class RailsHelper::Railtie < ::Rails::Railtie
7
+ config.after_initialize do
8
+ ActiveSupport.on_load(:action_view) do
9
+ ActionController::Base.helper Webpack::RailsHelper::Helper
10
+ end
11
+
12
+ ActiveSupport.on_load :action_view do
13
+ include Webpack::RailsHelper::Helper
14
+ end
15
+
16
+ Webpack::RailsHelper.bootstrap
17
+ Spring.after_fork { Webpack::RailsHelper.bootstrap } if defined?(Spring)
18
+ end
19
+
20
+ config.webpack = ActiveSupport::OrderedOptions.new
21
+ config.webpack.dev_server = ActiveSupport::OrderedOptions.new
22
+
23
+ # Host & port to use when generating asset URLS in the manifest helpers in dev
24
+ # server mode. Defaults to the requested host rather than localhost, so
25
+ # that requests from remote hosts work.
26
+ config.webpack.dev_server.host = proc { respond_to?(:request) ? request.host : 'localhost' }
27
+ config.webpack.dev_server.port = 3808
28
+
29
+ # The host and port to use when fetching the manifest
30
+ # This is helpful for e.g. docker containers, where the host and port you
31
+ # use via the web browser is not the same as those that the containers use
32
+ # to communicate among each other. Uses 'host' and 'port' when set to nil.
33
+ config.webpack.dev_server.manifest_host = nil
34
+ config.webpack.dev_server.manifest_port = nil
35
+
36
+ config.webpack.dev_server.https = false # note - this will use OpenSSL::SSL::VERIFY_NONE
37
+ config.webpack.dev_server.enabled = ::Rails.env.development?
38
+
39
+ config.webpack.output_dir = 'public/webpack'
40
+ config.webpack.public_path = 'webpack'
41
+ config.webpack.manifest_filename = 'manifest.json'
42
+ end
43
+ end
@@ -0,0 +1,5 @@
1
+ module Webpack
2
+ module RailsHelper
3
+ VERSION = '0.1.0'.freeze
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: webpack-rails-helper
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Daniel Ritz
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-08-06 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: '4.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '4.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.12'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.12'
41
+ description:
42
+ email:
43
+ - daniel.ritz@gmx.ch
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - LICENSE
49
+ - README.md
50
+ - lib/webpack-rails-helper.rb
51
+ - lib/webpack/rails_helper/config.rb
52
+ - lib/webpack/rails_helper/helper.rb
53
+ - lib/webpack/rails_helper/manifest.rb
54
+ - lib/webpack/rails_helper/railtie.rb
55
+ - lib/webpack/rails_helper/version.rb
56
+ homepage: http://gitlab.com/shellyBits/webpack-rails-helper
57
+ licenses:
58
+ - MIT
59
+ metadata: {}
60
+ post_install_message:
61
+ rdoc_options: []
62
+ require_paths:
63
+ - lib
64
+ required_ruby_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: 2.0.0
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ requirements: []
75
+ rubyforge_project:
76
+ rubygems_version: 2.6.12
77
+ signing_key:
78
+ specification_version: 4
79
+ summary: Helper for using webpack in Rails
80
+ test_files: []