webpacker_helpers 3.0.0.beta.1

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,67 @@
1
+ require "webpacker_helpers/manifest"
2
+ require "webpacker_helpers/env"
3
+
4
+ module WebpackerHelpers::Helper
5
+ # Computes the full path for a given webpacker asset.
6
+ # Return relative path using manifest.json and passes it to asset_url helper
7
+ # This will use asset_path internally, so most of their behaviors will be the same.
8
+ # Examples:
9
+ #
10
+ # In development mode:
11
+ # <%= asset_pack_path 'calendar.js' %> # => "/public/webpack/development/calendar-1016838bab065ae1e122.js"
12
+ # In production mode:
13
+ # <%= asset_pack_path 'calendar.css' %> # => "/public/webpack/production/calendar-1016838bab065ae1e122.css"
14
+ def asset_pack_path(name, **options)
15
+ path = WebpackerHelpers::Configuration.base_path
16
+ file = WebpackerHelpers::Manifest.lookup(name)
17
+ asset_path("#{path}/#{file}", **options)
18
+ end
19
+
20
+ # Creates a script tag that references the named pack file, as compiled by Webpack.
21
+ #
22
+ # Examples:
23
+ #
24
+ # In development mode:
25
+ # <%= javascript_pack_tag 'calendar', 'data-turbolinks-track': 'reload' %> # =>
26
+ # <script src="/public/webpack/development/calendar-1016838bab065ae1e314.js" data-turbolinks-track="reload"></script>
27
+ #
28
+ # # In production mode:
29
+ # <%= javascript_pack_tag 'calendar', 'data-turbolinks-track': 'reload' %> # =>
30
+ # <script src="/public/webpack/production/calendar-1016838bab065ae1e314.js" data-turbolinks-track="reload"></script>
31
+ def javascript_pack_tag(name, **options)
32
+ javascript_include_tag(asset_source(name, :javascript), **options)
33
+ end
34
+
35
+ # Creates a link tag that references the named pack file(s), as compiled by Webpack per the entries list
36
+ # in client/webpack.client.base.config.js.
37
+ #
38
+ # Examples:
39
+ #
40
+ # # In production mode:
41
+ # <%= stylesheet_pack_tag 'calendar', 'data-turbolinks-track': 'reload' %> # =>
42
+ # <link rel="stylesheet" media="screen" href="/public/webpack/production/calendar-1016838bab065ae1e122.css" data-turbolinks-track="reload" />
43
+ #
44
+ # # In development mode:
45
+ # <%= stylesheet_pack_tag 'calendar', 'data-turbolinks-track': 'reload' %> # =>
46
+ # <link rel="stylesheet" media="screen" href="/public/webpack/development/calendar-1016838bab065ae1e122.css" data-turbolinks-track="reload" />
47
+ #
48
+ # # In development mode with hot-reloading
49
+ # <%= stylesheet_pack_tag('main') %> <% # Default is false for enabled_when_hot_loading%>
50
+ # # No output
51
+ #
52
+ # # In development mode with hot-reloading and enabled_when_hot_loading
53
+ # # <%= stylesheet_pack_tag('main', enabled_when_hot_loading: true) %>
54
+ # <link rel="stylesheet" media="screen" href="/public/webpack/development/calendar-1016838bab065ae1e122.css" />
55
+ #
56
+ def stylesheet_pack_tag(name, **options)
57
+ return "" if WebpackerHelpers::Env.hot_loading? && !options[:enabled_when_hot_loading].presence
58
+ stylesheet_link_tag(asset_source(name, :stylesheet), **options)
59
+ end
60
+
61
+ private
62
+ def asset_source(name, type)
63
+ url = WebpackerHelpers::Configuration.base_url
64
+ path = WebpackerHelpers::Manifest.lookup("#{name}#{compute_asset_extname(name, type: type)}")
65
+ "#{url}/#{path}"
66
+ end
67
+ end
@@ -0,0 +1,72 @@
1
+ # Singleton registry for accessing the output path using generated manifest.
2
+ # This allows javascript_pack_tag, stylesheet_pack_tag, asset_pack_path to take a reference to,
3
+ # say, "calendar.js" or "calendar.css" and turn it into "/public/webpack/calendar.js" or
4
+ # "/public/webpack/calendar.css" in development. In production mode, it returns compiles
5
+ # files, # "/public/webpack/calendar-1016838bab065ae1e314.js" and
6
+ # "/webpack/calendar-1016838bab065ae1e314.css" for long-term caching
7
+
8
+ require "webpacker_helpers/file_loader"
9
+ require "webpacker_helpers/env"
10
+ require "webpacker_helpers/configuration"
11
+
12
+ class WebpackerHelpers::Manifest < WebpackerHelpers::FileLoader
13
+ class << self
14
+ # Helper method to determine if the manifest file exists.
15
+ def exist?
16
+ path_object = WebpackerHelpers::Configuration.manifest_path
17
+ path_object.exist?
18
+ end
19
+
20
+ def file_path
21
+ WebpackerHelpers::Configuration.manifest_path
22
+ end
23
+
24
+ def missing_file_from_manifest_error(bundle_name)
25
+ msg = <<-MSG
26
+ WebpackerHelpers can't find #{bundle_name} in your manifest #{file_path}. Possible causes:
27
+ 1. You are hot reloading.
28
+ 2. Webpack has not re-run to reflect updates.
29
+ 3. You have misconfigured WebpackerHelpers's config/webpacker_helpers.yml file.
30
+ 4. Your Webpack configuration is not creating a manifest.
31
+ MSG
32
+ raise(WebpackerHelpers::FileLoader::NotFoundError.new(msg))
33
+ end
34
+
35
+ # Same as lookup, but raises an error.
36
+ def lookup!(name)
37
+ lookup(name) || missing_file_from_manifest_error(name)
38
+ end
39
+
40
+ # Find the real file name from the manifest key.
41
+ def lookup(name)
42
+ instance.confirm_manifest_exists
43
+
44
+ load_instance
45
+ raise WebpackerHelpers::FileLoader::FileLoaderError.new("WebpackerHelpers::Manifest.load must be called first") unless instance
46
+ instance.data[name.to_s]
47
+ end
48
+ end
49
+
50
+ def confirm_manifest_exists
51
+ raise missing_manifest_file_error(@path) unless File.exist?(@path)
52
+ end
53
+
54
+ private
55
+
56
+ def missing_manifest_file_error(path_object)
57
+ msg = <<-MSG
58
+
59
+ WebpackerHelpers can't find the manifest file: #{path_object}
60
+ Possible causes:
61
+ 1. You have not invoked webpack.
62
+ 2. You have misconfigured WebpackerHelpers's config/webpacker_helpers.yml file.
63
+ 3. Your Webpack configuration is not creating a manifest.
64
+ MSG
65
+ raise(WebpackerHelpers::FileLoader::NotFoundError.new(msg))
66
+ end
67
+
68
+ def load_data
69
+ return super unless File.exist?(@path)
70
+ JSON.parse(File.read(@path))
71
+ end
72
+ end
@@ -0,0 +1,18 @@
1
+ require "rails/railtie"
2
+
3
+ require "webpacker_helpers/helper"
4
+
5
+ class WebpackerHelpers::Engine < ::Rails::Engine
6
+ initializer :webpacker_helpers do |app|
7
+ ActiveSupport.on_load :action_controller do
8
+ ActionController::Base.helper WebpackerHelpers::Helper
9
+ end
10
+
11
+ ActiveSupport.on_load :action_view do
12
+ include WebpackerHelpers::Helper
13
+ end
14
+
15
+ WebpackerHelpers.bootstrap
16
+ Spring.after_fork { WebpackerHelpers.bootstrap } if defined?(Spring)
17
+ end
18
+ end
@@ -0,0 +1,3 @@
1
+ module WebpackerHelpers
2
+ VERSION = "3.0.0.beta.1".freeze
3
+ end
@@ -0,0 +1,13 @@
1
+ require "webpacker_test"
2
+
3
+ class ConfigurationTest < Minitest::Test
4
+ def test_manifest_path
5
+ manifest_path = File.join(File.dirname(__FILE__), "test_app/public/webpack/test", "manifest.json").to_s
6
+ assert_equal manifest_path, WebpackerHelpers::Configuration.manifest_path.to_s
7
+ end
8
+
9
+ def test_output_path
10
+ output_path = File.join(File.dirname(__FILE__), "test_app/public/webpack/test").to_s
11
+ assert_equal output_path, WebpackerHelpers::Configuration.webpack_public_output_dir.to_s
12
+ end
13
+ end
data/test/env_test.rb ADDED
@@ -0,0 +1,12 @@
1
+ require "webpacker_test"
2
+
3
+ class EnvTest < Minitest::Test
4
+ def test_current_env
5
+ assert_equal Rails.env, WebpackerHelpers::Env.current
6
+ end
7
+
8
+ def test_file_path
9
+ correct_path = File.join(File.dirname(__FILE__), "test_app", "config", "webpacker_helpers.yml").to_s
10
+ assert_equal correct_path, WebpackerHelpers::Env.file_path.to_s
11
+ end
12
+ end
@@ -0,0 +1,23 @@
1
+ require "webpacker_test"
2
+
3
+ class HelperTest < ActionView::TestCase
4
+ def setup
5
+ @view = ActionView::Base.new
6
+ @view.extend WebpackerHelpers::Helper
7
+ end
8
+
9
+ def test_asset_pack_path
10
+ assert_equal @view.asset_pack_path("bootstrap.js"), "/webpack/test/bootstrap-300631c4f0e0f9c865bc.js"
11
+ assert_equal @view.asset_pack_path("bootstrap.css"), "/webpack/test/bootstrap-c38deda30895059837cf.css"
12
+ end
13
+
14
+ def test_javascript_pack_tag
15
+ script = %(<script src="/webpack/test/bootstrap-300631c4f0e0f9c865bc.js"></script>)
16
+ assert_equal @view.javascript_pack_tag("bootstrap.js"), script
17
+ end
18
+
19
+ def test_stylesheet_pack_tag
20
+ expected_style = %(<link rel="stylesheet" media="screen" href="/webpack/test/bootstrap-c38deda30895059837cf.css" />)
21
+ assert_equal @view.stylesheet_pack_tag("bootstrap.css"), expected_style
22
+ end
23
+ end
@@ -0,0 +1,44 @@
1
+ require "webpacker_test"
2
+
3
+ class ManifestTest < Minitest::Test
4
+ def test_file_path
5
+ file_path = File.join(File.dirname(__FILE__), "test_app/public/webpack/test", "manifest.json").to_s
6
+ assert_equal WebpackerHelpers::Manifest.file_path.to_s, file_path
7
+ end
8
+
9
+ def test_file_not_existing
10
+ begin
11
+ file_path = File.join(File.dirname(__FILE__), "test_app/public/webpack/test", "manifest.json")
12
+ temp_path = "#{file_path}.backup"
13
+ FileUtils.mv(file_path, temp_path)
14
+ # Point of this test is to ensure no crash
15
+ WebpackerHelpers::Manifest.load_instance
16
+ assert_equal({}, WebpackerHelpers::Manifest.instance.data)
17
+ ensure
18
+ FileUtils.mv(temp_path, file_path)
19
+ end
20
+ end
21
+
22
+ def test_lookup_exception
23
+ manifest_path = File.join(File.dirname(__FILE__), "test_app/public/webpack/test", "manifest.json").to_s
24
+ asset_file = "calendar.js"
25
+ msg = <<-MSG
26
+ WebpackerHelpers can't find #{asset_file} in your manifest #{manifest_path}. Possible causes:
27
+ 1. You are hot reloading.
28
+ 2. Webpack has not re-run to reflect updates.
29
+ 3. You have misconfigured WebpackerHelpers's config/webpacker_helpers.yml file.
30
+ 4. Your Webpack configuration is not creating a manifest.
31
+ MSG
32
+
33
+ error = assert_raises WebpackerHelpers::FileLoader::NotFoundError do
34
+ WebpackerHelpers::Manifest.lookup!(asset_file)
35
+ end
36
+
37
+ assert_equal error.message, msg
38
+ end
39
+
40
+ def test_lookup_success
41
+ asset_file = "bootstrap.js"
42
+ assert_equal WebpackerHelpers::Manifest.lookup!(asset_file), "bootstrap-300631c4f0e0f9c865bc.js"
43
+ end
44
+ end
@@ -0,0 +1,5 @@
1
+ development:
2
+ secret_key_base: 'abcdef'
3
+
4
+ test:
5
+ secret_key_base: 'abcdef'
@@ -0,0 +1,27 @@
1
+ # Note: Base output directory of /public is assumed for static files
2
+ default: &default
3
+ manifest: manifest.json
4
+ # Used in your webpack configuration. Must be created in the
5
+ # webpack_public_output_dir folder
6
+
7
+ development:
8
+ <<: *default
9
+ # generated files for development, in /public/webpack/development
10
+ webpack_public_output_dir: webpack/development
11
+
12
+ # Default is localhost:3500
13
+ hot_reloading_host: localhost:3500
14
+
15
+ # Developer note: considering removing this option so it can ONLY be turned by using an ENV value.
16
+ # Default is false, ENV 'HOT_RELOAD' will always override
17
+ hot_reloading_enabled_by_default: false
18
+
19
+ test:
20
+ <<: *default
21
+ # generated files for tests, in /public/webpack/test
22
+ webpack_public_output_dir: webpack/test
23
+
24
+ production:
25
+ <<: *default
26
+ # generated files for tests, in /public/webpack/production
27
+ webpack_public_output_dir: webpack/production
@@ -0,0 +1,4 @@
1
+ {
2
+ "bootstrap.css": "bootstrap-c38deda30895059837cf.css",
3
+ "bootstrap.js": "bootstrap-300631c4f0e0f9c865bc.js"
4
+ }
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ if ENV["USE_PRY"]
4
+ require "pry"
5
+ end
6
+
7
+ require "minitest/autorun"
8
+ require "rails"
9
+ require "rails/test_help"
10
+ require "webpacker_helpers"
11
+
12
+ Rails.env = "test"
13
+
14
+ module TestApp
15
+ class Application < ::Rails::Application
16
+ config.root = File.join(File.dirname(__FILE__), "test_app")
17
+ end
18
+ end
19
+
20
+ TestApp::Application.initialize!
data/webpacker.gemspec ADDED
@@ -0,0 +1,23 @@
1
+ $:.push File.expand_path("../lib", __FILE__)
2
+ require "webpacker_helpers/version"
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "webpacker_helpers"
6
+ s.version = WebpackerHelpers::VERSION
7
+ s.authors = "David Heinemeier Hansson, Justin Gordon"
8
+ s.email = "justin@shakacode.com"
9
+ s.summary = "Asset Helpers for Webpack"
10
+ s.homepage = "https://github.com/shakacode/webpacker_helpers"
11
+ s.license = "MIT"
12
+
13
+ s.required_ruby_version = ">= 1.9.3"
14
+
15
+ s.add_dependency "activesupport", ">= 4.2"
16
+ s.add_dependency "multi_json", "~> 1.2"
17
+ s.add_dependency "railties", ">= 4.2"
18
+
19
+ s.add_development_dependency "bundler", "~> 1.12"
20
+
21
+ s.files = `git ls-files`.split("\n")
22
+ s.test_files = `git ls-files -- test/*`.split("\n")
23
+ end
data/yarn.lock ADDED
@@ -0,0 +1,4 @@
1
+ # THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
2
+ # yarn lockfile v1
3
+
4
+
metadata ADDED
@@ -0,0 +1,140 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: webpacker_helpers
3
+ version: !ruby/object:Gem::Version
4
+ version: 3.0.0.beta.1
5
+ platform: ruby
6
+ authors:
7
+ - David Heinemeier Hansson, Justin Gordon
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-06-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
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: multi_json
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.2'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.2'
41
+ - !ruby/object:Gem::Dependency
42
+ name: railties
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '4.2'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '4.2'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.12'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.12'
69
+ description:
70
+ email: justin@shakacode.com
71
+ executables: []
72
+ extensions: []
73
+ extra_rdoc_files: []
74
+ files:
75
+ - ".gitignore"
76
+ - ".rubocop.yml"
77
+ - ".travis.yml"
78
+ - CHANGELOG.md
79
+ - CONTRIBUTING.md
80
+ - Gemfile
81
+ - Gemfile.lock
82
+ - MIT-LICENSE
83
+ - README.md
84
+ - Rakefile
85
+ - lib/tasks/webpacker.rake
86
+ - lib/tasks/webpacker_lite/check_node.rake
87
+ - lib/tasks/webpacker_lite/check_yarn.rake
88
+ - lib/tasks/webpacker_lite/clobber.rake
89
+ - lib/tasks/webpacker_lite/verify_install.rake
90
+ - lib/webpacker_helpers.rb
91
+ - lib/webpacker_helpers/configuration.rb
92
+ - lib/webpacker_helpers/env.rb
93
+ - lib/webpacker_helpers/file_loader.rb
94
+ - lib/webpacker_helpers/helper.rb
95
+ - lib/webpacker_helpers/manifest.rb
96
+ - lib/webpacker_helpers/railtie.rb
97
+ - lib/webpacker_helpers/version.rb
98
+ - test/configuration_test.rb
99
+ - test/env_test.rb
100
+ - test/helper_test.rb
101
+ - test/manifest_test.rb
102
+ - test/test_app/config/secrets.yml
103
+ - test/test_app/config/webpacker_lite.yml
104
+ - test/test_app/public/webpack/test/manifest.json
105
+ - test/webpacker_test.rb
106
+ - webpacker.gemspec
107
+ - yarn.lock
108
+ homepage: https://github.com/shakacode/webpacker_helpers
109
+ licenses:
110
+ - MIT
111
+ metadata: {}
112
+ post_install_message:
113
+ rdoc_options: []
114
+ require_paths:
115
+ - lib
116
+ required_ruby_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: 1.9.3
121
+ required_rubygems_version: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ">"
124
+ - !ruby/object:Gem::Version
125
+ version: 1.3.1
126
+ requirements: []
127
+ rubyforge_project:
128
+ rubygems_version: 2.6.11
129
+ signing_key:
130
+ specification_version: 4
131
+ summary: Asset Helpers for Webpack
132
+ test_files:
133
+ - test/configuration_test.rb
134
+ - test/env_test.rb
135
+ - test/helper_test.rb
136
+ - test/manifest_test.rb
137
+ - test/test_app/config/secrets.yml
138
+ - test/test_app/config/webpacker_lite.yml
139
+ - test/test_app/public/webpack/test/manifest.json
140
+ - test/webpacker_test.rb