shakapacker 6.1.1 → 6.3.0.pre.rc.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (59) hide show
  1. checksums.yaml +4 -4
  2. data/.node-version +1 -1
  3. data/CHANGELOG.md +48 -4
  4. data/CONTRIBUTING.md +2 -2
  5. data/Gemfile.lock +1 -1
  6. data/README.md +60 -26
  7. data/docs/customizing_babel_config.md +2 -0
  8. data/docs/deployment.md +2 -2
  9. data/docs/style_loader_vs_mini_css.md +48 -0
  10. data/docs/troubleshooting.md +18 -0
  11. data/docs/using_esbuild_loader.md +1 -1
  12. data/docs/using_swc_loader.md +2 -2
  13. data/docs/v6_upgrade.md +72 -74
  14. data/lib/install/config/webpacker.yml +16 -0
  15. data/lib/install/template.rb +2 -2
  16. data/lib/tasks/webpacker/clean.rake +1 -3
  17. data/lib/tasks/webpacker/clobber.rake +1 -3
  18. data/lib/tasks/webpacker/compile.rake +1 -4
  19. data/lib/webpacker/commands.rb +2 -2
  20. data/lib/webpacker/compiler.rb +12 -30
  21. data/lib/webpacker/configuration.rb +22 -4
  22. data/lib/webpacker/dev_server.rb +11 -2
  23. data/lib/webpacker/helper.rb +26 -8
  24. data/lib/webpacker/instance.rb +1 -1
  25. data/lib/webpacker/manifest.rb +4 -4
  26. data/lib/webpacker/railtie.rb +7 -0
  27. data/lib/webpacker/version.rb +1 -1
  28. data/lib/webpacker/version_checker.rb +152 -0
  29. data/package/__tests__/config.js +11 -0
  30. data/package/babel/preset.js +0 -1
  31. data/package/config.js +6 -0
  32. data/package/environments/base.js +1 -1
  33. data/package/inliningCss.js +1 -1
  34. data/package/rules/__tests__/file.js +35 -0
  35. data/package/rules/__tests__/index.js +11 -0
  36. data/package/rules/__tests__/raw.js +18 -0
  37. data/package/rules/file.js +2 -17
  38. data/package/rules/raw.js +2 -2
  39. data/package/swc/index.js +3 -3
  40. data/package.json +12 -11
  41. data/test/compiler_test.rb +27 -32
  42. data/test/configuration_test.rb +48 -2
  43. data/test/fixtures/beta_package.json +13 -0
  44. data/test/fixtures/git_url_package.json +13 -0
  45. data/test/fixtures/github_url_package.json +13 -0
  46. data/test/fixtures/normal_package.json +13 -0
  47. data/test/fixtures/relative_path_package.json +13 -0
  48. data/test/fixtures/semver_caret_package.json +13 -0
  49. data/test/fixtures/semver_tilde_package.json +13 -0
  50. data/test/fixtures/without_package.json +13 -0
  51. data/test/helper_test.rb +34 -12
  52. data/test/manifest_test.rb +3 -3
  53. data/test/test_app/config/webpacker.yml +4 -0
  54. data/test/test_app/config/webpacker_manifest_path.yml +80 -0
  55. data/test/test_app/config/webpacker_no_precompile.yml +7 -0
  56. data/test/version_checker_test.rb +271 -0
  57. data/test/webpacker_test.rb +15 -0
  58. data/yarn.lock +917 -884
  59. metadata +32 -5
data/test/helper_test.rb CHANGED
@@ -14,7 +14,6 @@ class HelperTest < ActionView::TestCase
14
14
  end.new
15
15
 
16
16
  @javascript_pack_tag_loaded = nil
17
- @stylesheet_pack_tag_loaded = nil
18
17
  end
19
18
 
20
19
  def test_asset_pack_path
@@ -116,6 +115,28 @@ class HelperTest < ActionView::TestCase
116
115
  javascript_pack_tag("application", "bootstrap", defer: false)
117
116
  end
118
117
 
118
+ def test_javascript_pack_with_append
119
+ append_javascript_pack_tag("bootstrap", defer: false)
120
+ assert_equal \
121
+ %(<script src="/packs/vendors~application~bootstrap-c20632e7baf2c81200d3.chunk.js" defer="defer"></script>\n) +
122
+ %(<script src="/packs/vendors~application-e55f2aae30c07fb6d82a.chunk.js" defer="defer"></script>\n) +
123
+ %(<script src="/packs/application-k344a6d59eef8632c9d1.js" defer="defer"></script>\n) +
124
+ %(<script src="/packs/bootstrap-300631c4f0e0f9c865bc.js"></script>),
125
+ javascript_pack_tag("application")
126
+ end
127
+
128
+ def test_append_javascript_pack_tag_raises
129
+ error = assert_raises do
130
+ javascript_pack_tag("application")
131
+ append_javascript_pack_tag("bootstrap", defer: false)
132
+ end
133
+
134
+ assert_equal \
135
+ "You can only call append_javascript_pack_tag before javascript_pack_tag helper. " +
136
+ "Please refer to https://github.com/shakacode/shakapacker/blob/master/README.md#usage for the usage guide",
137
+ error.message
138
+ end
139
+
119
140
  def test_javascript_pack_tag_splat
120
141
  assert_equal \
121
142
  %(<script src="/packs/vendors~application~bootstrap-c20632e7baf2c81200d3.chunk.js" defer="defer"></script>\n) +
@@ -149,19 +170,19 @@ class HelperTest < ActionView::TestCase
149
170
  end
150
171
 
151
172
  def hello_stimulus_stylesheet_chunks
152
- %w[/packs/hello_stimulus-k344a6d59eef8632c9d1.chunk.css]
173
+ %w[/packs/1-c20632e7baf2c81200d3.chunk.css /packs/hello_stimulus-k344a6d59eef8632c9d1.chunk.css]
153
174
  end
154
175
 
155
176
  def test_stylesheet_pack_tag
156
177
  assert_equal \
157
- (application_stylesheet_chunks + hello_stimulus_stylesheet_chunks)
178
+ (application_stylesheet_chunks + hello_stimulus_stylesheet_chunks).uniq
158
179
  .map { |chunk| stylesheet_link_tag(chunk) }.join("\n"),
159
180
  stylesheet_pack_tag("application", "hello_stimulus")
160
181
  end
161
182
 
162
183
  def test_stylesheet_pack_tag_symbol
163
184
  assert_equal \
164
- (application_stylesheet_chunks + hello_stimulus_stylesheet_chunks)
185
+ (application_stylesheet_chunks + hello_stimulus_stylesheet_chunks).uniq
165
186
  .map { |chunk| stylesheet_link_tag(chunk) }.join("\n"),
166
187
  stylesheet_pack_tag(:application, :hello_stimulus)
167
188
  end
@@ -172,15 +193,16 @@ class HelperTest < ActionView::TestCase
172
193
  stylesheet_pack_tag("application", media: "all")
173
194
  end
174
195
 
175
- def test_stylesheet_pack_tag_multiple_invocations
176
- error = assert_raises do
177
- stylesheet_pack_tag(:application)
178
- stylesheet_pack_tag(:hello_stimulus)
179
- end
196
+ def test_stylesheet_pack_tag_multiple_invocations_are_allowed
197
+ app_style = stylesheet_pack_tag(:application)
198
+ stimulus_style = stylesheet_pack_tag(:hello_stimulus)
180
199
 
181
200
  assert_equal \
182
- "To prevent duplicated chunks on the page, you should call stylesheet_pack_tag only once on the page. " +
183
- "Please refer to https://github.com/shakacode/shakapacker/blob/master/README.md#usage for the usage guide",
184
- error.message
201
+ application_stylesheet_chunks.map { |chunk| stylesheet_link_tag(chunk) }.join("\n"),
202
+ app_style
203
+
204
+ assert_equal \
205
+ hello_stimulus_stylesheet_chunks.map { |chunk| stylesheet_link_tag(chunk) }.join("\n"),
206
+ stimulus_style
185
207
  end
186
208
  end
@@ -8,7 +8,7 @@ class ManifestTest < Minitest::Test
8
8
  Webpacker.manifest.lookup!(asset_file)
9
9
  end
10
10
 
11
- assert_match "Webpacker can't find #{asset_file} in #{manifest_path}", error.message
11
+ assert_match "Shakapacker can't find #{asset_file} in #{manifest_path}", error.message
12
12
  end
13
13
 
14
14
  def test_lookup_with_type_exception!
@@ -18,7 +18,7 @@ class ManifestTest < Minitest::Test
18
18
  Webpacker.manifest.lookup!(asset_file, type: :javascript)
19
19
  end
20
20
 
21
- assert_match "Webpacker can't find #{asset_file}.js in #{manifest_path}", error.message
21
+ assert_match "Shakapacker can't find #{asset_file}.js in #{manifest_path}", error.message
22
22
  end
23
23
 
24
24
  def test_lookup_success!
@@ -60,7 +60,7 @@ class ManifestTest < Minitest::Test
60
60
  Webpacker.manifest.lookup_pack_with_chunks!(asset_file, type: :javascript)
61
61
  end
62
62
 
63
- assert_match "Webpacker can't find #{asset_file}.js in #{manifest_path}", error.message
63
+ assert_match "Shakapacker can't find #{asset_file}.js in #{manifest_path}", error.message
64
64
  end
65
65
 
66
66
  def test_lookup_entrypoint
@@ -9,6 +9,9 @@ default: &default
9
9
  webpack_compile_output: false
10
10
  webpack_loader: babel
11
11
 
12
+ # Location for manifest.json, defaults to {public_output_path}/manifest.json if unset
13
+ # manifest_path: public/packs/manifest.json
14
+
12
15
  # Additional paths webpack should look up modules
13
16
  # ['app/assets', 'engine/foo/app/assets']
14
17
  additional_paths:
@@ -36,6 +39,7 @@ default: &default
36
39
  development:
37
40
  <<: *default
38
41
  compile: true
42
+ ensure_consistent_versioning: true
39
43
 
40
44
  # Reference: https://webpack.js.org/configuration/dev-server/
41
45
  dev_server:
@@ -0,0 +1,80 @@
1
+ # Note: You must restart bin/webpack-dev-server for changes to take effect
2
+
3
+ default: &default
4
+ source_path: app/packs
5
+ source_entry_path: entrypoints
6
+ public_root_path: public
7
+ public_output_path: packs
8
+ cache_path: tmp/webpacker
9
+ webpack_compile_output: false
10
+
11
+ # Location for manifest.json, defaults to {public_output_path}/manifest.json if unset
12
+ manifest_path: app/packs/manifest.json
13
+
14
+ # Additional paths webpack should look up modules
15
+ # ['app/assets', 'engine/foo/app/assets']
16
+ additional_paths:
17
+ - app/assets
18
+ - /etc/yarn
19
+ - some.config.js
20
+ - app/elm
21
+
22
+ # Reload manifest.json on all requests so we reload latest compiled packs
23
+ cache_manifest: false
24
+
25
+ static_assets_extensions:
26
+ - .jpg
27
+ - .jpeg
28
+ - .png
29
+ - .gif
30
+ - .tiff
31
+ - .ico
32
+ - .svg
33
+
34
+ extensions:
35
+ - .mjs
36
+ - .js
37
+
38
+ development:
39
+ <<: *default
40
+ compile: true
41
+
42
+ # Reference: https://webpack.js.org/configuration/dev-server/
43
+ dev_server:
44
+ https: false
45
+ host: localhost
46
+ port: 3035
47
+ public: localhost:3035
48
+ hmr: false
49
+ overlay: true
50
+ disable_host_check: true
51
+ use_local_ip: false
52
+ pretty: false
53
+
54
+ test:
55
+ <<: *default
56
+ compile: true
57
+
58
+ # Compile test packs to a separate directory
59
+ public_output_path: packs-test
60
+
61
+ production:
62
+ <<: *default
63
+
64
+ # Production depends on precompilation of packs prior to booting for performance.
65
+ compile: false
66
+
67
+ # Cache manifest.json for performance
68
+ cache_manifest: true
69
+
70
+ staging:
71
+ <<: *default
72
+
73
+ # Production depends on precompilation of packs prior to booting for performance.
74
+ compile: false
75
+
76
+ # Cache manifest.json for performance
77
+ cache_manifest: true
78
+
79
+ # Compile staging packs to a separate directory
80
+ public_output_path: packs-staging
@@ -0,0 +1,7 @@
1
+ # Note: You must restart bin/webpacker-dev-server for changes to take effect
2
+
3
+ default: &default
4
+ webpacker_precompile: false
5
+
6
+ production:
7
+ <<: *default
@@ -0,0 +1,271 @@
1
+ require "test_helper"
2
+ require "webpacker/version"
3
+
4
+ class NodePackageVersionDouble
5
+ attr_reader :raw, :major_minor_patch
6
+
7
+ def initialize(raw: nil, major_minor_patch: nil, semver_wildcard: false, skip_processing: false)
8
+ @raw = raw
9
+ @major_minor_patch = major_minor_patch
10
+ @semver_wildcard = semver_wildcard
11
+ @skip_processing = skip_processing
12
+ end
13
+
14
+ def semver_wildcard?
15
+ @semver_wildcard
16
+ end
17
+
18
+ def skip_processing?
19
+ @skip_processing
20
+ end
21
+ end
22
+
23
+ class VersionCheckerTest < Minitest::Test
24
+ def check_version(node_package_version, stub_gem_version = Webpacker::VERSION, stub_config = true)
25
+ version_checker = Webpacker::VersionChecker.new(node_package_version)
26
+ version_checker.stub :gem_version, stub_gem_version do
27
+ Webpacker.config.stub :ensure_consistent_versioning?, stub_config do
28
+ version_checker.raise_if_gem_and_node_package_versions_differ
29
+ end
30
+ end
31
+ end
32
+
33
+ def test_message_printed_if_consistency_check_disabled_and_mismatch
34
+ node_package_version = NodePackageVersionDouble.new(raw: "6.1.0", major_minor_patch: ["6", "1", "0"])
35
+
36
+ out, err = capture_io do
37
+ check_version(node_package_version, "6.0.0", false)
38
+ end
39
+
40
+ assert_match \
41
+ "Webpacker::VersionChecker - Version mismatch detected",
42
+ err
43
+ end
44
+
45
+ def test_message_printed_if_consistency_check_disabled_and_semver
46
+ node_package_version = NodePackageVersionDouble.new(raw: "^6.1.0", major_minor_patch: ["6", "1", "0"], semver_wildcard: true)
47
+
48
+ out, err = capture_io do
49
+ check_version(node_package_version, "6.1.0", false)
50
+ end
51
+
52
+ assert_match \
53
+ "Webpacker::VersionChecker - Semver wildcard detected",
54
+ err
55
+ end
56
+
57
+ def test_raise_on_different_major_version
58
+ node_package_version = NodePackageVersionDouble.new(raw: "6.1.0", major_minor_patch: ["6", "1", "0"])
59
+
60
+ error = assert_raises do
61
+ check_version(node_package_version, "7.0.0")
62
+ end
63
+
64
+ assert_match \
65
+ "**ERROR** Webpacker: Webpacker gem and node package versions do not match",
66
+ error.message
67
+ end
68
+
69
+ def test_raise_on_different_minor_version
70
+ node_package_version = NodePackageVersionDouble.new(raw: "6.1.0", major_minor_patch: ["6", "1", "0"])
71
+
72
+ error = assert_raises do
73
+ check_version(node_package_version, "6.2.0")
74
+ end
75
+
76
+ assert_match \
77
+ "**ERROR** Webpacker: Webpacker gem and node package versions do not match",
78
+ error.message
79
+ end
80
+
81
+ def test_raise_on_different_patch_version
82
+ node_package_version = NodePackageVersionDouble.new(raw: "6.1.1", major_minor_patch: ["6", "1", "1"])
83
+
84
+ error = assert_raises do
85
+ check_version(node_package_version, "6.1.2")
86
+ end
87
+
88
+ assert_match \
89
+ "**ERROR** Webpacker: Webpacker gem and node package versions do not match",
90
+ error.message
91
+ end
92
+
93
+ def test_raise_on_semver_wildcard
94
+ node_package_version = NodePackageVersionDouble.new(raw: "^6.0.0", major_minor_patch: ["6", "0", "0"], semver_wildcard: true)
95
+
96
+ error = assert_raises do
97
+ check_version(node_package_version, "6.0.0")
98
+ end
99
+
100
+ assert_match \
101
+ "**ERROR** Webpacker: Your node package version for shakapacker contains a ^ or ~",
102
+ error.message
103
+ end
104
+
105
+ def test_no_raise_on_matching_versions
106
+ node_package_version = NodePackageVersionDouble.new(raw: "6.0.0", major_minor_patch: ["6", "0", "0"])
107
+
108
+ assert_silent do
109
+ check_version(node_package_version, "6.0.0")
110
+ end
111
+ end
112
+
113
+ def test_no_raise_on_matching_versions_beta
114
+ node_package_version = NodePackageVersionDouble.new(raw: "6.0.0-beta.1", major_minor_patch: ["6", "0", "0"])
115
+
116
+ assert_silent do
117
+ check_version(node_package_version, "6.0.0.beta.1")
118
+ end
119
+ end
120
+
121
+ def test_no_raise_on_no_package
122
+ node_package_version = NodePackageVersionDouble.new(raw: nil, skip_processing: true)
123
+
124
+ assert_silent do
125
+ check_version(node_package_version, "6.0.0")
126
+ end
127
+ end
128
+
129
+ def test_no_raise_on_skipped_path
130
+ node_package_version = NodePackageVersionDouble.new(raw: "../shakapacker", skip_processing: true)
131
+
132
+ assert_silent do
133
+ check_version(node_package_version, "6.0.0")
134
+ end
135
+ end
136
+ end
137
+
138
+ class NodePackageVersionTest < Minitest::Test
139
+ def node_package_version(fixture_version: "normal")
140
+ file_path = File.expand_path("fixtures/#{fixture_version}_package.json", __dir__)
141
+ Webpacker::VersionChecker::NodePackageVersion.new(file_path)
142
+ end
143
+
144
+ def test_normal_package_raw
145
+ assert_equal "6.0.0", node_package_version.raw
146
+ end
147
+
148
+ def test_normal_package_major_minor_patch
149
+ assert_equal ["6", "0", "0"], node_package_version.major_minor_patch
150
+ end
151
+
152
+ def test_normal_package_skip_processing
153
+ assert_equal false, node_package_version.skip_processing?
154
+ end
155
+
156
+ def test_normal_package_semver_wildcard
157
+ assert_equal false, node_package_version.semver_wildcard?
158
+ end
159
+
160
+ def test_beta_package_raw
161
+ assert_equal "6.0.0-beta.1", node_package_version(fixture_version: "beta").raw
162
+ end
163
+
164
+ def test_beta_package_major_minor_patch
165
+ assert_equal ["6", "0", "0"], node_package_version(fixture_version: "beta").major_minor_patch
166
+ end
167
+
168
+ def test_beta_package_skip_processing
169
+ assert_equal false, node_package_version(fixture_version: "beta").skip_processing?
170
+ end
171
+
172
+ def test_beta_package_semver_wildcard
173
+ assert_equal false, node_package_version(fixture_version: "beta").semver_wildcard?
174
+ end
175
+
176
+ def test_semver_caret_package_raw
177
+ assert_equal "^6.0.0", node_package_version(fixture_version: "semver_caret").raw
178
+ end
179
+
180
+ def test_semver_caret_package_major_minor_patch
181
+ assert_equal ["6", "0", "0"], node_package_version(fixture_version: "semver_caret").major_minor_patch
182
+ end
183
+
184
+ def test_semver_caret_package_skip_processing
185
+ assert_equal false, node_package_version(fixture_version: "semver_caret").skip_processing?
186
+ end
187
+
188
+ def test_semver_caret_package_semver_wildcard
189
+ assert_equal true, node_package_version(fixture_version: "semver_caret").semver_wildcard?
190
+ end
191
+
192
+ def test_semver_tilde_package_raw
193
+ assert_equal "~6.0.0", node_package_version(fixture_version: "semver_tilde").raw
194
+ end
195
+
196
+ def test_semver_tilde_package_major_minor_patch
197
+ assert_equal ["6", "0", "0"], node_package_version(fixture_version: "semver_tilde").major_minor_patch
198
+ end
199
+
200
+ def test_semver_tilde_package_skip_processing
201
+ assert_equal false, node_package_version(fixture_version: "semver_tilde").skip_processing?
202
+ end
203
+
204
+ def test_semver_tilde_package_semver_wildcard
205
+ assert_equal true, node_package_version(fixture_version: "semver_tilde").semver_wildcard?
206
+ end
207
+
208
+ def test_relative_path_package_raw
209
+ assert_equal "../shakapacker", node_package_version(fixture_version: "relative_path").raw
210
+ end
211
+
212
+ def test_relative_path_package_major_minor_patch
213
+ assert_nil node_package_version(fixture_version: "relative_path").major_minor_patch
214
+ end
215
+
216
+ def test_relative_path_package_skip_processing
217
+ assert_equal true, node_package_version(fixture_version: "relative_path").skip_processing?
218
+ end
219
+
220
+ def test_relative_path_package_semver_wildcard
221
+ assert_equal false, node_package_version(fixture_version: "relative_path").semver_wildcard?
222
+ end
223
+
224
+ def test_git_url_package_raw
225
+ assert_equal "git://github.com/shakapacker/shakapacker.git", node_package_version(fixture_version: "git_url").raw
226
+ end
227
+
228
+ def test_git_url_package_major_minor_patch
229
+ assert_nil node_package_version(fixture_version: "git_url").major_minor_patch
230
+ end
231
+
232
+ def test_git_url_package_skip_processing
233
+ assert_equal true, node_package_version(fixture_version: "git_url").skip_processing?
234
+ end
235
+
236
+ def test_git_url_package_semver_wildcard
237
+ assert_equal false, node_package_version(fixture_version: "git_url").semver_wildcard?
238
+ end
239
+
240
+ def test_github_url_package_raw
241
+ assert_equal "shakapacker/shakapacker#feature/branch", node_package_version(fixture_version: "github_url").raw
242
+ end
243
+
244
+ def test_github_url_package_major_minor_patch
245
+ assert_nil node_package_version(fixture_version: "github_url").major_minor_patch
246
+ end
247
+
248
+ def test_github_url_package_skip_processing
249
+ assert_equal true, node_package_version(fixture_version: "github_url").skip_processing?
250
+ end
251
+
252
+ def test_github_url_package_semver_wildcard
253
+ assert_equal false, node_package_version(fixture_version: "github_url").semver_wildcard?
254
+ end
255
+
256
+ def test_without_package_raw
257
+ assert_equal "", node_package_version(fixture_version: "without").raw
258
+ end
259
+
260
+ def test_without_package_major_minor_patch
261
+ assert_nil node_package_version(fixture_version: "without").major_minor_patch
262
+ end
263
+
264
+ def test_without_package_skip_processing
265
+ assert_equal true, node_package_version(fixture_version: "without").skip_processing?
266
+ end
267
+
268
+ def test_without_package_semver_wildcard
269
+ assert_equal false, node_package_version(fixture_version: "without").semver_wildcard?
270
+ end
271
+ end
@@ -23,11 +23,26 @@ class WebpackerTest < Webpacker::Test
23
23
  def dev_server.https?; true; end
24
24
  def dev_server.hmr?; true; end
25
25
  def dev_server.running?; true; end
26
+ def dev_server.inline_css?; true; end
26
27
  Webpacker.instance.stub(:dev_server, dev_server) do
27
28
  assert Webpacker.inlining_css?
28
29
  end
29
30
  end
30
31
 
32
+ def test_explicit_no_inline_css_with_hmr
33
+ dev_server = Webpacker::DevServer.new({})
34
+ def dev_server.host; "localhost"; end
35
+ def dev_server.port; "3035"; end
36
+ def dev_server.pretty?; false; end
37
+ def dev_server.https?; true; end
38
+ def dev_server.hmr?; true; end
39
+ def dev_server.running?; true; end
40
+ def dev_server.inline_css?; false; end
41
+ Webpacker.instance.stub(:dev_server, dev_server) do
42
+ assert !Webpacker.inlining_css?
43
+ end
44
+ end
45
+
31
46
  def test_app_autoload_paths_cleanup
32
47
  assert_empty $test_app_autoload_paths_in_initializer
33
48
  end