Skip to content

Commit 2492053

Browse files
committed
Define methods based on which webpacker version is loaded to make controlling support easier and more obvious
1 parent 9b7af66 commit 2492053

File tree

3 files changed

+1047
-46
lines changed

3 files changed

+1047
-46
lines changed
Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require "open-uri"
2+
require 'pry'
23

34
module React
45
module ServerRendering
@@ -7,9 +8,16 @@ module ServerRendering
78
# - webpack-dev-server
89
# - compiled pack
910
class WebpackerManifestContainer
11+
12+
MAJOR, MINOR, PATCH, _ = Bundler.locked_gems.specs.find {|gem_spec| gem_spec.name == 'webpacker'}.version.segments
13+
1014
# This pattern matches the code that initializes the dev-server client.
1115
CLIENT_REQUIRE = %r{__webpack_require__\(.*webpack-dev-server\/client\/index\.js.*\n}
1216

17+
def self.compatible?
18+
!!defined?(Webpacker)
19+
end
20+
1321
def find_asset(logical_path)
1422
# raises if not found
1523
asset_path = manifest.lookup(logical_path).to_s
@@ -26,26 +34,44 @@ def find_asset(logical_path)
2634
end
2735
end
2836

29-
def manifest
30-
Webpacker.respond_to?(:manifest) ? Webpacker.manifest : Webpacker::Manifest
37+
if MAJOR < 3
38+
def manifest
39+
Webpacker::Manifest
40+
end
41+
else
42+
def manifest
43+
Webpacker.manifest
44+
end
3145
end
3246

33-
def file_path path
34-
manifest.respond_to?(:lookup_path) ? manifest.lookup_path(path) : File.join(output_path, manifest.lookup(path).split('/')[2..-1])
47+
if MAJOR < 3
48+
def config
49+
Webpacker::Configuration
50+
end
51+
else
52+
def config
53+
Webpacker.config
54+
end
3555
end
3656

37-
def config
38-
Webpacker.respond_to?(:config) ? Webpacker.config : Webpacker::Configuration
57+
if (MAJOR == 1 && MINOR >= 2) || MAJOR == 2
58+
def file_path path
59+
manifest.lookup_path(path)
60+
end
61+
elsif MAJOR == 3
62+
def file_path path
63+
File.join(Webpacker.config.public_output_path, path)
64+
end
65+
else # 1.0 and 1.1 support
66+
def file_path path
67+
File.join(output_path, manifest.lookup(path).split('/')[2..-1])
68+
end
3969
end
4070

4171
def output_path
4272
# Webpack1 /:output/:entry, Webpack3 /public/:output
4373
config.respond_to?(:output_path) ? config.output_path : 'public'
4474
end
45-
46-
def self.compatible?
47-
!!defined?(Webpacker)
48-
end
4975
end
5076
end
5177
end

test/dummy_webpacker3/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"dependencies": {
3+
"@rails/webpacker": "^3.0.1",
34
"autoprefixer": "^7.1.1",
45
"babel-core": "^6.24.1",
56
"babel-loader": "7.x",
@@ -32,6 +33,6 @@
3233
"webpack-merge": "^4.1.0"
3334
},
3435
"devDependencies": {
35-
"webpack-dev-server": "^2.4.5"
36+
"webpack-dev-server": "^2.7.1"
3637
}
3738
}

0 commit comments

Comments
 (0)