shakapacker 6.5.5 → 6.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (144) hide show
  1. checksums.yaml +4 -4
  2. data/.eslintrc.js +1 -0
  3. data/.github/FUNDING.yml +1 -0
  4. data/.github/ISSUE_TEMPLATE/bug_report.md +11 -7
  5. data/.github/ISSUE_TEMPLATE/feature_request.md +21 -0
  6. data/.github/PULL_REQUEST_TEMPLATE.md +18 -0
  7. data/.github/workflows/ruby.yml +3 -1
  8. data/.gitignore +3 -3
  9. data/.rspec +1 -0
  10. data/CHANGELOG.md +20 -2
  11. data/CONTRIBUTING.md +11 -1
  12. data/Gemfile.development_dependencies +3 -1
  13. data/README.md +41 -8
  14. data/Rakefile +4 -5
  15. data/docs/v6_upgrade.md +1 -1
  16. data/gemfiles/Gemfile-rails-edge +1 -1
  17. data/gemfiles/Gemfile-rails.5.2.x +1 -1
  18. data/gemfiles/Gemfile-rails.6.0.x +1 -1
  19. data/gemfiles/Gemfile-rails.6.1.x +1 -1
  20. data/gemfiles/Gemfile-rails.7.0.x +1 -1
  21. data/lib/install/template.rb +21 -16
  22. data/lib/tasks/webpacker/compile.rake +5 -10
  23. data/lib/webpacker/helper.rb +20 -9
  24. data/lib/webpacker/instance.rb +3 -1
  25. data/lib/webpacker/version.rb +1 -1
  26. data/package/rules/__tests__/__utils__/webpack.js +50 -0
  27. data/package/rules/__tests__/babel.js +63 -0
  28. data/package/rules/__tests__/esbuild.js +64 -0
  29. data/package/rules/__tests__/index.js +0 -4
  30. data/package/rules/__tests__/swc.js +64 -0
  31. data/package/rules/babel.js +13 -24
  32. data/package/rules/esbuild.js +2 -13
  33. data/package/rules/jscommon.js +26 -0
  34. data/package/rules/swc.js +2 -13
  35. data/package/utils/helpers.js +1 -1
  36. data/package.json +8 -2
  37. data/spec/command_spec.rb +114 -0
  38. data/spec/compiler_spec.rb +57 -0
  39. data/spec/compiler_strategy_spec.rb +20 -0
  40. data/spec/configuration_spec.rb +281 -0
  41. data/{test/dev_server_runner_test.rb → spec/dev_server_runner_spec.rb} +30 -40
  42. data/spec/dev_server_spec.rb +45 -0
  43. data/spec/digest_strategy_spec.rb +33 -0
  44. data/{test/engine_rake_tasks_test.rb → spec/engine_rake_tasks_spec.rb} +17 -9
  45. data/spec/env_spec.rb +21 -0
  46. data/spec/helper_spec.rb +241 -0
  47. data/spec/manifest_spec.rb +98 -0
  48. data/spec/mtime_strategy_spec.rb +53 -0
  49. data/spec/rake_tasks_spec.rb +32 -0
  50. data/spec/spec_helper.rb +123 -0
  51. data/{test → spec}/test_app/public/packs/manifest.json +8 -0
  52. data/spec/version_checker_spec.rb +950 -0
  53. data/{test/webpack_runner_test.rb → spec/webpack_runner_spec.rb} +12 -15
  54. data/spec/webpacker_spec.rb +61 -0
  55. data/yarn.lock +846 -844
  56. metadata +104 -187
  57. data/.github/ISSUE_TEMPLATE/feature-request.md +0 -18
  58. data/lib/tasks/yarn.rake +0 -44
  59. data/test/command_test.rb +0 -109
  60. data/test/compiler_strategy_test.rb +0 -27
  61. data/test/compiler_test.rb +0 -60
  62. data/test/configuration_test.rb +0 -186
  63. data/test/dev_server_test.rb +0 -47
  64. data/test/digest_strategy_test.rb +0 -33
  65. data/test/env_test.rb +0 -23
  66. data/test/helper_test.rb +0 -248
  67. data/test/manifest_test.rb +0 -89
  68. data/test/mtime_strategy_test.rb +0 -42
  69. data/test/rake_tasks_test.rb +0 -37
  70. data/test/test_helper.rb +0 -33
  71. data/test/version_checker_test.rb +0 -826
  72. data/test/webpacker_test.rb +0 -49
  73. /data/{test → spec}/fixtures/beta_package-lock.v1.json +0 -0
  74. /data/{test → spec}/fixtures/beta_package-lock.v2.json +0 -0
  75. /data/{test → spec}/fixtures/beta_package.json +0 -0
  76. /data/{test → spec}/fixtures/beta_yarn.v1.lock +0 -0
  77. /data/{test → spec}/fixtures/beta_yarn.v2.lock +0 -0
  78. /data/{test → spec}/fixtures/git_url_package-lock.v1.json +0 -0
  79. /data/{test → spec}/fixtures/git_url_package-lock.v2.json +0 -0
  80. /data/{test → spec}/fixtures/git_url_package.json +0 -0
  81. /data/{test → spec}/fixtures/git_url_yarn.v1.lock +0 -0
  82. /data/{test → spec}/fixtures/git_url_yarn.v2.lock +0 -0
  83. /data/{test → spec}/fixtures/github_url_package-lock.v1.json +0 -0
  84. /data/{test → spec}/fixtures/github_url_package-lock.v2.json +0 -0
  85. /data/{test → spec}/fixtures/github_url_package.json +0 -0
  86. /data/{test → spec}/fixtures/github_url_yarn.v1.lock +0 -0
  87. /data/{test → spec}/fixtures/github_url_yarn.v2.lock +0 -0
  88. /data/{test → spec}/fixtures/relative_path_package-lock.v1.json +0 -0
  89. /data/{test → spec}/fixtures/relative_path_package-lock.v2.json +0 -0
  90. /data/{test → spec}/fixtures/relative_path_package.json +0 -0
  91. /data/{test → spec}/fixtures/relative_path_yarn.v1.lock +0 -0
  92. /data/{test → spec}/fixtures/relative_path_yarn.v2.lock +0 -0
  93. /data/{test → spec}/fixtures/semver_caret_package-lock.v1.json +0 -0
  94. /data/{test → spec}/fixtures/semver_caret_package-lock.v2.json +0 -0
  95. /data/{test → spec}/fixtures/semver_caret_package.json +0 -0
  96. /data/{test → spec}/fixtures/semver_caret_yarn.v1.lock +0 -0
  97. /data/{test → spec}/fixtures/semver_caret_yarn.v2.lock +0 -0
  98. /data/{test → spec}/fixtures/semver_exact_package-lock.v1.json +0 -0
  99. /data/{test → spec}/fixtures/semver_exact_package-lock.v2.json +0 -0
  100. /data/{test → spec}/fixtures/semver_exact_package.json +0 -0
  101. /data/{test → spec}/fixtures/semver_exact_yarn.v1.lock +0 -0
  102. /data/{test → spec}/fixtures/semver_exact_yarn.v2.lock +0 -0
  103. /data/{test → spec}/fixtures/semver_tilde_package-lock.v1.json +0 -0
  104. /data/{test → spec}/fixtures/semver_tilde_package-lock.v2.json +0 -0
  105. /data/{test → spec}/fixtures/semver_tilde_package.json +0 -0
  106. /data/{test → spec}/fixtures/semver_tilde_yarn.v1.lock +0 -0
  107. /data/{test → spec}/fixtures/semver_tilde_yarn.v2.lock +0 -0
  108. /data/{test → spec}/fixtures/without_package-lock.v1.json +0 -0
  109. /data/{test → spec}/fixtures/without_package-lock.v2.json +0 -0
  110. /data/{test → spec}/fixtures/without_package.json +0 -0
  111. /data/{test → spec}/fixtures/without_yarn.v1.lock +0 -0
  112. /data/{test → spec}/fixtures/without_yarn.v2.lock +0 -0
  113. /data/{test → spec}/mounted_app/Rakefile +0 -0
  114. /data/{test → spec}/mounted_app/test/dummy/Rakefile +0 -0
  115. /data/{test → spec}/mounted_app/test/dummy/bin/rails +0 -0
  116. /data/{test → spec}/mounted_app/test/dummy/bin/rake +0 -0
  117. /data/{test → spec}/mounted_app/test/dummy/config/application.rb +0 -0
  118. /data/{test → spec}/mounted_app/test/dummy/config/environment.rb +0 -0
  119. /data/{test → spec}/mounted_app/test/dummy/config/webpacker.yml +0 -0
  120. /data/{test → spec}/mounted_app/test/dummy/config.ru +0 -0
  121. /data/{test → spec}/mounted_app/test/dummy/package.json +0 -0
  122. /data/{test → spec}/test_app/Rakefile +0 -0
  123. /data/{test → spec}/test_app/app/packs/entrypoints/application.js +0 -0
  124. /data/{test → spec}/test_app/app/packs/entrypoints/generated/something.js +0 -0
  125. /data/{test → spec}/test_app/app/packs/entrypoints/multi_entry.css +0 -0
  126. /data/{test → spec}/test_app/app/packs/entrypoints/multi_entry.js +0 -0
  127. /data/{test → spec}/test_app/bin/webpacker +0 -0
  128. /data/{test → spec}/test_app/bin/webpacker-dev-server +0 -0
  129. /data/{test → spec}/test_app/config/application.rb +0 -0
  130. /data/{test → spec}/test_app/config/environment.rb +0 -0
  131. /data/{test → spec}/test_app/config/initializers/inspect_autoload_paths.rb +0 -0
  132. /data/{test → spec}/test_app/config/webpack/webpack.config.js +0 -0
  133. /data/{test → spec}/test_app/config/webpacker.yml +0 -0
  134. /data/{test → spec}/test_app/config/webpacker_css_extract_ignore_order_warnings.yml +0 -0
  135. /data/{test → spec}/test_app/config/webpacker_defaults_fallback.yml +0 -0
  136. /data/{test → spec}/test_app/config/webpacker_manifest_path.yml +0 -0
  137. /data/{test → spec}/test_app/config/webpacker_nested_entries.yml +0 -0
  138. /data/{test → spec}/test_app/config/webpacker_no_precompile.yml +0 -0
  139. /data/{test → spec}/test_app/config/webpacker_other_location.yml +0 -0
  140. /data/{test → spec}/test_app/config/webpacker_public_root.yml +0 -0
  141. /data/{test → spec}/test_app/config.ru +0 -0
  142. /data/{test → spec}/test_app/package.json +0 -0
  143. /data/{test → spec}/test_app/some.config.js +0 -0
  144. /data/{test → spec}/test_app/yarn.lock +0 -0
@@ -0,0 +1,241 @@
1
+ module ActionView::TestCase::Behavior
2
+ attr_accessor :request
3
+
4
+ describe "Webpacker::Helper" do
5
+ let(:application_stylesheet_chunks) { %w[/packs/1-c20632e7baf2c81200d3.chunk.css /packs/application-k344a6d59eef8632c9d1.chunk.css] }
6
+ let(:hello_stimulus_stylesheet_chunks) { %w[/packs/1-c20632e7baf2c81200d3.chunk.css /packs/hello_stimulus-k344a6d59eef8632c9d1.chunk.css] }
7
+
8
+ before :each do
9
+ extend Webpacker::Helper
10
+ extend ActionView::Helpers
11
+ extend ActionView::Helpers::AssetTagHelper
12
+ extend ActionView::TestCase::Behavior
13
+
14
+ @request = Class.new do
15
+ def send_early_hints(links) end
16
+ def base_url
17
+ "https://example.com"
18
+ end
19
+ end.new
20
+ @javascript_pack_tag_loaded = nil
21
+ end
22
+
23
+ it "#asset_pack_path generates correct path" do
24
+ expect(asset_pack_path("bootstrap.js")).to eq "/packs/bootstrap-300631c4f0e0f9c865bc.js"
25
+ expect(asset_pack_path("bootstrap.css")).to eq "/packs/bootstrap-c38deda30895059837cf.css"
26
+ end
27
+
28
+ it "#asset_pack_url generates correct url" do
29
+ expect(asset_pack_url("bootstrap.js")).to eq "https://example.com/packs/bootstrap-300631c4f0e0f9c865bc.js"
30
+ expect(asset_pack_url("bootstrap.css")).to eq "https://example.com/packs/bootstrap-c38deda30895059837cf.css"
31
+ end
32
+
33
+ it "#image_pack_path generates correct path" do
34
+ expect(image_pack_path("application.png")).to eq "/packs/application-k344a6d59eef8632c9d1.png"
35
+ expect(image_pack_path("image.jpg")).to eq "/packs/static/image-c38deda30895059837cf.jpg"
36
+ expect(image_pack_path("static/image.jpg")).to eq "/packs/static/image-c38deda30895059837cf.jpg"
37
+ expect(image_pack_path("nested/image.jpg")).to eq "/packs/static/nested/image-c38deda30895059837cf.jpg"
38
+ expect(image_pack_path("static/nested/image.jpg")).to eq "/packs/static/nested/image-c38deda30895059837cf.jpg"
39
+ end
40
+
41
+ it "#image_pack_url generates correct path" do
42
+ expect(image_pack_url("application.png")).to eq "https://example.com/packs/application-k344a6d59eef8632c9d1.png"
43
+ expect(image_pack_url("image.jpg")).to eq "https://example.com/packs/static/image-c38deda30895059837cf.jpg"
44
+ expect(image_pack_url("static/image.jpg")).to eq "https://example.com/packs/static/image-c38deda30895059837cf.jpg"
45
+ expect(image_pack_url("nested/image.jpg")).to eq "https://example.com/packs/static/nested/image-c38deda30895059837cf.jpg"
46
+ expect(image_pack_url("static/nested/image.jpg")).to eq "https://example.com/packs/static/nested/image-c38deda30895059837cf.jpg"
47
+ end
48
+
49
+ it "#image_pack_tag generates correct tags" do
50
+ expect(image_pack_tag("application.png", size: "16x10", alt: "Edit Entry")).to eq "<img alt=\"Edit Entry\" src=\"/packs/application-k344a6d59eef8632c9d1.png\" width=\"16\" height=\"10\" />"
51
+ expect(image_pack_tag("image.jpg", size: "16x10", alt: "Edit Entry")).to eq "<img alt=\"Edit Entry\" src=\"/packs/static/image-c38deda30895059837cf.jpg\" width=\"16\" height=\"10\" />"
52
+ expect(image_pack_tag("static/image.jpg", size: "16x10", alt: "Edit Entry")).to eq "<img alt=\"Edit Entry\" src=\"/packs/static/image-c38deda30895059837cf.jpg\" width=\"16\" height=\"10\" />"
53
+ expect(image_pack_tag("nested/image.jpg", size: "16x10", alt: "Edit Entry")).to eq "<img alt=\"Edit Entry\" src=\"/packs/static/nested/image-c38deda30895059837cf.jpg\" width=\"16\" height=\"10\" />"
54
+ expect(image_pack_tag("static/nested/image.jpg", size: "16x10", alt: "Edit Entry")).to eq "<img alt=\"Edit Entry\" src=\"/packs/static/nested/image-c38deda30895059837cf.jpg\" width=\"16\" height=\"10\" />"
55
+ expect(image_pack_tag("static/image.jpg", srcset: { "static/image-2x.jpg" => "2x" })).to eq "<img srcset=\"/packs/static/image-2x-7cca48e6cae66ec07b8e.jpg 2x\" src=\"/packs/static/image-c38deda30895059837cf.jpg\" />"
56
+ end
57
+
58
+ it "#favicon_pack_tag generates correct tags" do
59
+ expect(favicon_pack_tag("application.png", rel: "apple-touch-icon", type: "image/png")).to eq "<link rel=\"apple-touch-icon\" type=\"image/png\" href=\"/packs/application-k344a6d59eef8632c9d1.png\" />"
60
+ expect(favicon_pack_tag("mb-icon.png", rel: "apple-touch-icon", type: "image/png")).to eq "<link rel=\"apple-touch-icon\" type=\"image/png\" href=\"/packs/static/mb-icon-c38deda30895059837cf.png\" />"
61
+ expect(favicon_pack_tag("static/mb-icon.png", rel: "apple-touch-icon", type: "image/png")).to eq "<link rel=\"apple-touch-icon\" type=\"image/png\" href=\"/packs/static/mb-icon-c38deda30895059837cf.png\" />"
62
+ expect(favicon_pack_tag("nested/mb-icon.png", rel: "apple-touch-icon", type: "image/png")).to eq "<link rel=\"apple-touch-icon\" type=\"image/png\" href=\"/packs/static/nested/mb-icon-c38deda30895059837cf.png\" />"
63
+ expect(favicon_pack_tag("static/nested/mb-icon.png", rel: "apple-touch-icon", type: "image/png")).to eq "<link rel=\"apple-touch-icon\" type=\"image/png\" href=\"/packs/static/nested/mb-icon-c38deda30895059837cf.png\" />"
64
+ end
65
+
66
+ it "#preload_pack_asset generates correct tag" do
67
+ if self.class.method_defined?(:preload_link_tag)
68
+ expect(preload_pack_asset("fonts/fa-regular-400.woff2")).to eq %(<link rel="preload" href="/packs/fonts/fa-regular-400-944fb546bd7018b07190a32244f67dc9.woff2" as="font" type="font/woff2" crossorigin="anonymous">)
69
+ else
70
+ expect { preload_pack_asset("fonts/fa-regular-400.woff2") }.to raise_error "You need Rails >= 5.2 to use this tag."
71
+ end
72
+ end
73
+
74
+ it "#javascript_pack_tag generates correct tags" do
75
+ expected = <<~HTML.chomp
76
+ <script src="/packs/vendors~application~bootstrap-c20632e7baf2c81200d3.chunk.js" defer="defer"></script>
77
+ <script src="/packs/vendors~application-e55f2aae30c07fb6d82a.chunk.js" defer="defer"></script>
78
+ <script src="/packs/application-k344a6d59eef8632c9d1.js" defer="defer"></script>
79
+ <script src="/packs/bootstrap-300631c4f0e0f9c865bc.js" defer="defer"></script>
80
+ HTML
81
+
82
+ expect(javascript_pack_tag("application", "bootstrap")).to eq expected
83
+ end
84
+
85
+ it "#javascript_pack_tag generates correct tags by passing `defer: false`" do
86
+ expected = <<~HTML.chomp
87
+ <script src="/packs/vendors~application~bootstrap-c20632e7baf2c81200d3.chunk.js"></script>
88
+ <script src="/packs/vendors~application-e55f2aae30c07fb6d82a.chunk.js"></script>
89
+ <script src="/packs/application-k344a6d59eef8632c9d1.js"></script>
90
+ <script src="/packs/bootstrap-300631c4f0e0f9c865bc.js"></script>
91
+ HTML
92
+
93
+ expect(javascript_pack_tag("application", "bootstrap", defer: false)).to eq expected
94
+ end
95
+
96
+ it "#javascript_pack_tag generates correct appended tag" do
97
+ append_javascript_pack_tag("bootstrap", defer: false)
98
+
99
+ expected = <<~HTML.chomp
100
+ <script src="/packs/vendors~application~bootstrap-c20632e7baf2c81200d3.chunk.js" defer="defer"></script>
101
+ <script src="/packs/vendors~application-e55f2aae30c07fb6d82a.chunk.js" defer="defer"></script>
102
+ <script src="/packs/application-k344a6d59eef8632c9d1.js" defer="defer"></script>
103
+ <script src="/packs/bootstrap-300631c4f0e0f9c865bc.js"></script>
104
+ HTML
105
+
106
+ expect(javascript_pack_tag("application")).to eq expected
107
+ end
108
+
109
+ it "#javascript_pack_tag generates correct prepended tag" do
110
+ append_javascript_pack_tag("bootstrap")
111
+ prepend_javascript_pack_tag("main")
112
+
113
+ expected = <<~HTML.chomp
114
+ <script src="/packs/main-e323a53c7f30f5d53cbb.js" defer="defer"></script>
115
+ <script src="/packs/bootstrap-300631c4f0e0f9c865bc.js" defer="defer"></script>
116
+ <script src="/packs/vendors~application~bootstrap-c20632e7baf2c81200d3.chunk.js" defer="defer"></script>
117
+ <script src="/packs/vendors~application-e55f2aae30c07fb6d82a.chunk.js" defer="defer"></script>
118
+ <script src="/packs/application-k344a6d59eef8632c9d1.js" defer="defer"></script>
119
+ HTML
120
+
121
+ expect(javascript_pack_tag("application")).to eq expected
122
+ end
123
+
124
+ it "#append_javascript_pack_tag raises error if called after calling #javascript_pack_tag" do
125
+ expected_error_message = \
126
+ "You can only call append_javascript_pack_tag before javascript_pack_tag helper. " +
127
+ "Please refer to https://github.com/shakacode/shakapacker/blob/master/README.md#view-helper-append_javascript_pack_tag-prepend_javascript_pack_tag-and-append_stylesheet_pack_tag for the usage guide"
128
+
129
+ expect {
130
+ javascript_pack_tag("application")
131
+ append_javascript_pack_tag("bootstrap", defer: false)
132
+ }.to raise_error(expected_error_message)
133
+ end
134
+
135
+ it "#prepend_javascript_pack_tag raises error if called after calling #javascript_pack_tag" do
136
+ expected_error_message = \
137
+ "You can only call prepend_javascript_pack_tag before javascript_pack_tag helper. " +
138
+ "Please refer to https://github.com/shakacode/shakapacker/blob/master/README.md#view-helper-append_javascript_pack_tag-prepend_javascript_pack_tag-and-append_stylesheet_pack_tag for the usage guide"
139
+
140
+ expect {
141
+ javascript_pack_tag("application")
142
+ prepend_javascript_pack_tag("bootstrap", defer: false)
143
+ }.to raise_error(expected_error_message)
144
+ end
145
+
146
+ it "#javascript_pack_tag generates correct tags by passing `defer: true`" do
147
+ expected = <<~HTML.chomp
148
+ <script src="/packs/vendors~application~bootstrap-c20632e7baf2c81200d3.chunk.js" defer="defer"></script>
149
+ <script src="/packs/vendors~application-e55f2aae30c07fb6d82a.chunk.js" defer="defer"></script>
150
+ <script src="/packs/application-k344a6d59eef8632c9d1.js" defer="defer"></script>
151
+ HTML
152
+
153
+ expect(javascript_pack_tag("application", defer: true)).to eq expected
154
+ end
155
+
156
+ it "#javascript_pack_tag generates correct tags by passing symbol" do
157
+ expected = <<~HTML.chomp
158
+ <script src="/packs/vendors~application~bootstrap-c20632e7baf2c81200d3.chunk.js" defer="defer"></script>
159
+ <script src="/packs/vendors~application-e55f2aae30c07fb6d82a.chunk.js" defer="defer"></script>
160
+ <script src="/packs/application-k344a6d59eef8632c9d1.js" defer="defer"></script>
161
+ HTML
162
+
163
+ expect(javascript_pack_tag(:application)).to eq expected
164
+ end
165
+
166
+ it "#javascript_pack_tag rases error on multiple invocations" do
167
+ expected_error_message = "To prevent duplicated chunks on the page, you should call javascript_pack_tag only once on the page. " +
168
+ "Please refer to https://github.com/shakacode/shakapacker/blob/master/README.md#view-helpers-javascript_pack_tag-and-stylesheet_pack_tag for the usage guide"
169
+
170
+ expect {
171
+ javascript_pack_tag(:application)
172
+ javascript_pack_tag(:bootstrap)
173
+ }.to raise_error(expected_error_message)
174
+ end
175
+
176
+ it "#stylesheet_pack_tag generates correct link tag with string arguments" do
177
+ expected = (application_stylesheet_chunks + hello_stimulus_stylesheet_chunks)
178
+ .uniq
179
+ .map { |chunk| stylesheet_link_tag(chunk) }
180
+ .join("\n")
181
+
182
+ expect(stylesheet_pack_tag("application", "hello_stimulus")).to eq expected
183
+ end
184
+
185
+ it "#stylesheet_pack_tag generates correct link tag with symbol arguments" do
186
+ expected = (application_stylesheet_chunks + hello_stimulus_stylesheet_chunks)
187
+ .uniq
188
+ .map { |chunk| stylesheet_link_tag(chunk) }
189
+ .join("\n")
190
+
191
+ expect(stylesheet_pack_tag(:application, :hello_stimulus)).to eq expected
192
+ end
193
+
194
+ it "#stylesheet_pack_tag generates correct link tag with mixed arguments" do
195
+ expected = (application_stylesheet_chunks)
196
+ .map { |chunk| stylesheet_link_tag(chunk, media: "all") }
197
+ .join("\n")
198
+
199
+ expect(stylesheet_pack_tag("application", media: "all")).to eq expected
200
+ end
201
+
202
+ it "#stylesheet_pack_tag allows multiple invocations" do
203
+ app_style = stylesheet_pack_tag(:application)
204
+ stimulus_style = stylesheet_pack_tag(:hello_stimulus)
205
+
206
+ expect(app_style).to eq application_stylesheet_chunks.map { |chunk| stylesheet_link_tag(chunk) }.join("\n")
207
+
208
+ expect(stimulus_style).to eq hello_stimulus_stylesheet_chunks.map { |chunk| stylesheet_link_tag(chunk) }.join("\n")
209
+
210
+ expect {
211
+ stylesheet_pack_tag(:application)
212
+ stylesheet_pack_tag(:hello_stimulus)
213
+ }.to_not raise_error
214
+ end
215
+
216
+ it "#stylesheet_pack_tag appends" do
217
+ append_stylesheet_pack_tag(:hello_stimulus)
218
+
219
+ expect(stylesheet_pack_tag(:application)).to eq \
220
+ (application_stylesheet_chunks + hello_stimulus_stylesheet_chunks).uniq.map { |chunk| stylesheet_link_tag(chunk) }.join("\n")
221
+ end
222
+
223
+ it "#stylesheet_pack_tag appends duplicate" do
224
+ append_stylesheet_pack_tag(:hello_stimulus)
225
+ append_stylesheet_pack_tag(:application)
226
+
227
+ expect(stylesheet_pack_tag(:application)).to eq \
228
+ (application_stylesheet_chunks + hello_stimulus_stylesheet_chunks).uniq.map { |chunk| stylesheet_link_tag(chunk) }.join("\n")
229
+ end
230
+
231
+ it "#stylesheet_pack_tag supports multiple invocations with different media attr" do
232
+ app_style = stylesheet_pack_tag(:application)
233
+ app_style_with_media = stylesheet_pack_tag(:application, media: "print")
234
+ hello_stimulus_style_with_media = stylesheet_pack_tag(:hello_stimulus, media: "all")
235
+
236
+ expect(app_style).to eq application_stylesheet_chunks.map { |chunk| stylesheet_link_tag(chunk) }.join("\n")
237
+ expect(app_style_with_media).to eq application_stylesheet_chunks.map { |chunk| stylesheet_link_tag(chunk, media: "print") }.join("\n")
238
+ expect(hello_stimulus_style_with_media).to eq hello_stimulus_stylesheet_chunks.map { |chunk| stylesheet_link_tag(chunk, media: "all") }.join("\n")
239
+ end
240
+ end
241
+ end
@@ -0,0 +1,98 @@
1
+ describe "Webpacker::Manifest" do
2
+ let(:manifest_path) { File.expand_path File.join(File.dirname(__FILE__), "test_app/public/packs", "manifest.json").to_s }
3
+
4
+ it "#lookup! throws exception for a non-existing asset file" do
5
+ asset_file = "calendar.js"
6
+ expected_error_message = "Shakapacker can't find #{asset_file} in #{manifest_path}"
7
+
8
+ allow(Webpacker.config).to receive(:compile?).and_return(false)
9
+
10
+ expect {
11
+ Webpacker.manifest.lookup!(asset_file)
12
+ }.to raise_error(Webpacker::Manifest::MissingEntryError, /#{expected_error_message}/)
13
+ end
14
+
15
+ it "#lookup! throws exception for a non-existing asset file with type and without extension" do
16
+ asset_file = "calendar"
17
+ expected_error_message = "Shakapacker can't find #{asset_file}.js in #{manifest_path}"
18
+
19
+ allow(Webpacker.config).to receive(:compile?).and_return(false)
20
+
21
+ expect {
22
+ Webpacker.manifest.lookup!(asset_file, type: :javascript)
23
+ }.to raise_error(Webpacker::Manifest::MissingEntryError, /#{expected_error_message}/)
24
+ end
25
+
26
+ it "#lookup! returns path to bundled bootstrap.js" do
27
+ actual = Webpacker.manifest.lookup!("bootstrap.js")
28
+ expected = "/packs/bootstrap-300631c4f0e0f9c865bc.js"
29
+
30
+ expect(actual).to eq expected
31
+ end
32
+
33
+ it "#lookup_pack_with_chunks! returns array of path to bundled bootstrap with type of javascript" do
34
+ actual = Webpacker.manifest.lookup_pack_with_chunks!("bootstrap", type: :javascript)
35
+ expected = ["/packs/bootstrap-300631c4f0e0f9c865bc.js"]
36
+
37
+ expect(actual).to eq expected
38
+ end
39
+
40
+ it "#lookup_with_chunks! returns array of path to bundled bootstrap.js with type of javascript" do
41
+ actual = Webpacker.manifest.lookup_pack_with_chunks!("bootstrap.js", type: :javascript)
42
+ expected = ["/packs/bootstrap-300631c4f0e0f9c865bc.js"]
43
+
44
+ expect(actual).to eq expected
45
+ end
46
+
47
+ it "#lookup_with_chunks! returns array of path to bundled 'print/application' without extension and in a sub-directory" do
48
+ actual = Webpacker.manifest.lookup_pack_with_chunks!("print/application", type: :css)
49
+ expected = ["/packs/print/application-983b6c164a47f7ed49cd.css"]
50
+
51
+ expect(actual).to eq expected
52
+ end
53
+
54
+ it "#lookup_with_chunks! returns array of path to bundled 'print/application.css' in a sub-directory" do
55
+ actual = Webpacker.manifest.lookup_pack_with_chunks!("print/application.css", type: :css)
56
+ expected = ["/packs/print/application-983b6c164a47f7ed49cd.css"]
57
+
58
+ expect(actual).to eq expected
59
+ end
60
+
61
+ it "#lookup returns nil for non-existing asset file" do
62
+ expect(Webpacker.manifest.lookup("foo.js")).to be nil
63
+ end
64
+
65
+ it "#lookup_pack_with_chunks returns nil for non-existing asset file" do
66
+ expect(Webpacker.manifest.lookup_pack_with_chunks("foo.js")).to be nil
67
+ end
68
+
69
+ it "#lookup returns path for bootstrap.js" do
70
+ actual = Webpacker.manifest.lookup("bootstrap.js")
71
+ expected = "/packs/bootstrap-300631c4f0e0f9c865bc.js"
72
+
73
+ expect(actual).to eq expected
74
+ end
75
+
76
+ it "#lookup_pack_with_chunks! throws exception for a non-existing asset file" do
77
+ asset_file = "calendar"
78
+
79
+ expected_error_message = "Shakapacker can't find #{asset_file}.js in #{manifest_path}"
80
+
81
+ allow(Webpacker.config).to receive(:compile?).and_return(false)
82
+
83
+ expect {
84
+ Webpacker.manifest.lookup_pack_with_chunks!(asset_file, type: :javascript)
85
+ }.to raise_error(Webpacker::Manifest::MissingEntryError, /#{expected_error_message}/)
86
+ end
87
+
88
+ it "#lookup_pack_with_chunks! returns array of paths to bundled js files with 'application' in their name" do
89
+ actual_application_entrypoints = Webpacker.manifest.lookup_pack_with_chunks!("application", type: :javascript)
90
+ expected_application_entrypoints = [
91
+ "/packs/vendors~application~bootstrap-c20632e7baf2c81200d3.chunk.js",
92
+ "/packs/vendors~application-e55f2aae30c07fb6d82a.chunk.js",
93
+ "/packs/application-k344a6d59eef8632c9d1.js"
94
+ ]
95
+
96
+ expect(actual_application_entrypoints).to eq expected_application_entrypoints
97
+ end
98
+ end
@@ -0,0 +1,53 @@
1
+ describe "Webpacker::MtimeStrategy" do
2
+ let(:mtime_strategy) { Webpacker::MtimeStrategy.new }
3
+ let(:manifest_timestamp) { Time.parse("2021-01-01 12:34:56 UTC") }
4
+
5
+ describe "#fresh?" do
6
+ it "returns false when manifest is missing" do
7
+ latest_timestamp = manifest_timestamp + 3600
8
+
9
+ with_stubs(latest_timestamp: latest_timestamp.to_i, manifest_exists: false) do
10
+ expect(mtime_strategy.fresh?).to be false
11
+ end
12
+ end
13
+
14
+ it "returns false when manifest is older" do
15
+ latest_timestamp = manifest_timestamp + 3600
16
+
17
+ with_stubs(latest_timestamp: latest_timestamp.to_i) do
18
+ expect(mtime_strategy.fresh?).to be false
19
+ end
20
+ end
21
+
22
+ it "returns true when manifest is new" do
23
+ latest_timestamp = manifest_timestamp - 3600
24
+
25
+ with_stubs(latest_timestamp: latest_timestamp.to_i) do
26
+ expect(mtime_strategy.fresh?).to be true
27
+ end
28
+ end
29
+ end
30
+
31
+ describe "#stale?" do
32
+ it "returns false when #fresh? is true" do
33
+ expect(mtime_strategy).to receive(:fresh?).and_return(true)
34
+
35
+ expect(mtime_strategy.stale?).to be false
36
+ end
37
+
38
+ it "returns true when #fresh? is false" do
39
+ expect(mtime_strategy).to receive(:fresh?).and_return(false)
40
+
41
+ expect(mtime_strategy.stale?).to be true
42
+ end
43
+ end
44
+
45
+ private
46
+
47
+ def with_stubs(latest_timestamp:, manifest_exists: true)
48
+ allow(mtime_strategy).to receive(:latest_modified_timestamp).and_return(latest_timestamp)
49
+ allow(FileTest).to receive(:exist?).and_return(manifest_exists)
50
+ allow(File).to receive(:mtime).and_return(manifest_timestamp)
51
+ yield
52
+ end
53
+ end
@@ -0,0 +1,32 @@
1
+ describe "RakeTasks" do
2
+ let(:test_app_path) { File.expand_path("test_app", __dir__) }
3
+
4
+ it "`rake -T` lists webpacker tasks" do
5
+ output = Dir.chdir(test_app_path) { `rake -T` }
6
+ expect(output).to include "webpacker"
7
+ expect(output).to include "webpacker:check_binstubs"
8
+ expect(output).to include "webpacker:check_node"
9
+ expect(output).to include "webpacker:check_yarn"
10
+ expect(output).to include "webpacker:clean"
11
+ expect(output).to include "webpacker:clobber"
12
+ expect(output).to include "webpacker:compile"
13
+ expect(output).to include "webpacker:install"
14
+ expect(output).to include "webpacker:verify_install"
15
+ end
16
+
17
+ it "`webpacker:check_binstubs` doesn't get 'webpack binstub not found' error" do
18
+ output = Dir.chdir(test_app_path) { `rake webpacker:check_binstubs 2>&1` }
19
+ expect(output).to_not include "webpack binstub not found."
20
+ end
21
+
22
+ it "`webpacker:check_node` doesn't get 'Webpacker requires Node.js' error" do
23
+ output = Dir.chdir(test_app_path) { `rake webpacker:check_node 2>&1` }
24
+ expect(output).to_not include "Webpacker requires Node.js"
25
+ end
26
+
27
+ it "`webpacker:check_yarn` doesn't get error related to yarn" do
28
+ output = Dir.chdir(test_app_path) { `rake webpacker:check_yarn 2>&1` }
29
+ expect(output).to_not include "Yarn not installed"
30
+ expect(output).to_not include "Webpacker requires Yarn"
31
+ end
32
+ end
@@ -0,0 +1,123 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # The generated `.rspec` file contains `--require spec_helper` which will cause
4
+ # this file to always be loaded, without a need to explicitly require it in any
5
+ # files.
6
+ #
7
+ # Given that it is always loaded, you are encouraged to keep this file as
8
+ # light-weight as possible. Requiring heavyweight dependencies from this file
9
+ # will add to the boot time of your test suite on EVERY test run, even for an
10
+ # individual file that may not need all of that loaded. Instead, consider making
11
+ # a separate helper file that requires the additional dependencies and performs
12
+ # the additional setup, and require it from the spec files that actually need
13
+ # it.
14
+ #
15
+ # See https://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
16
+ RSpec.configure do |config|
17
+ # rspec-expectations config goes here. You can use an alternate
18
+ # assertion/expectation library such as wrong or the stdlib/minitest
19
+ # assertions if you prefer.
20
+ config.expect_with :rspec do |expectations|
21
+ # This option will default to `true` in RSpec 4. It makes the `description`
22
+ # and `failure_message` of custom matchers include text for helper methods
23
+ # defined using `chain`, e.g.:
24
+ # be_bigger_than(2).and_smaller_than(4).description
25
+ # # => "be bigger than 2 and smaller than 4"
26
+ # ...rather than:
27
+ # # => "be bigger than 2"
28
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
29
+ end
30
+
31
+ # rspec-mocks config goes here. You can use an alternate test double
32
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
33
+ config.mock_with :rspec do |mocks|
34
+ # Prevents you from mocking or stubbing a method that does not exist on
35
+ # a real object. This is generally recommended, and will default to
36
+ # `true` in RSpec 4.
37
+ mocks.verify_partial_doubles = true
38
+ end
39
+
40
+ # This option will default to `:apply_to_host_groups` in RSpec 4 (and will
41
+ # have no way to turn it off -- the option exists only for backwards
42
+ # compatibility in RSpec 3). It causes shared context metadata to be
43
+ # inherited by the metadata hash of host groups and examples, rather than
44
+ # triggering implicit auto-inclusion in groups with matching metadata.
45
+ config.shared_context_metadata_behavior = :apply_to_host_groups
46
+
47
+ # The settings below are suggested to provide a good initial experience
48
+ # with RSpec, but feel free to customize to your heart's content.
49
+ =begin
50
+ # This allows you to limit a spec run to individual examples or groups
51
+ # you care about by tagging them with `:focus` metadata. When nothing
52
+ # is tagged with `:focus`, all examples get run. RSpec also provides
53
+ # aliases for `it`, `describe`, and `context` that include `:focus`
54
+ # metadata: `fit`, `fdescribe` and `fcontext`, respectively.
55
+ config.filter_run_when_matching :focus
56
+
57
+ # Allows RSpec to persist some state between runs in order to support
58
+ # the `--only-failures` and `--next-failure` CLI options. We recommend
59
+ # you configure your source control system to ignore this file.
60
+ config.example_status_persistence_file_path = "spec/examples.txt"
61
+
62
+ # Limits the available syntax to the non-monkey patched syntax that is
63
+ # recommended. For more details, see:
64
+ # https://relishapp.com/rspec/rspec-core/docs/configuration/zero-monkey-patching-mode
65
+ config.disable_monkey_patching!
66
+
67
+ # This setting enables warnings. It's recommended, but in some cases may
68
+ # be too noisy due to issues in dependencies.
69
+ config.warnings = true
70
+
71
+ # Many RSpec users commonly either run the entire suite or an individual
72
+ # file, and it's useful to allow more verbose output when running an
73
+ # individual spec file.
74
+ if config.files_to_run.one?
75
+ # Use the documentation formatter for detailed output,
76
+ # unless a formatter has already been configured
77
+ # (e.g. via a command-line flag).
78
+ config.default_formatter = "doc"
79
+ end
80
+
81
+ # Print the 10 slowest examples and example groups at the
82
+ # end of the spec run, to help surface which specs are running
83
+ # particularly slow.
84
+ config.profile_examples = 10
85
+
86
+ # Run specs in random order to surface order dependencies. If you find an
87
+ # order dependency and want to debug it, you can fix the order by providing
88
+ # the seed, which is printed after each run.
89
+ # --seed 1234
90
+ config.order = :random
91
+
92
+ # Seed global randomization in this process using the `--seed` CLI option.
93
+ # Setting this allows you to use `--seed` to deterministically reproduce
94
+ # test failures related to randomization by passing the same `--seed` value
95
+ # as the one that triggered the failure.
96
+ Kernel.srand config.seed
97
+ =end
98
+ end
99
+
100
+ require_relative "test_app/config/environment"
101
+
102
+ Rails.env = "production"
103
+
104
+ Webpacker.instance = ::Webpacker::Instance.new
105
+
106
+ def reloaded_config
107
+ Webpacker.instance.instance_variable_set(:@env, nil)
108
+ Webpacker.instance.instance_variable_set(:@config, nil)
109
+ Webpacker.instance.instance_variable_set(:@dev_server, nil)
110
+ Webpacker.env
111
+ Webpacker.config
112
+ Webpacker.dev_server
113
+ end
114
+
115
+ def with_rails_env(env)
116
+ original = Rails.env
117
+ Rails.env = ActiveSupport::StringInquirer.new(env)
118
+ reloaded_config
119
+ yield
120
+ ensure
121
+ Rails.env = ActiveSupport::StringInquirer.new(original)
122
+ reloaded_config
123
+ end
@@ -2,6 +2,7 @@
2
2
  "bootstrap.css": "/packs/bootstrap-c38deda30895059837cf.css",
3
3
  "application.css": "/packs/application-dd6b1cd38bfa093df600.css",
4
4
  "bootstrap.js": "/packs/bootstrap-300631c4f0e0f9c865bc.js",
5
+ "main.js": "/packs/main-e323a53c7f30f5d53cbb.js",
5
6
  "application.js": "/packs/application-k344a6d59eef8632c9d1.js",
6
7
  "application.png": "/packs/application-k344a6d59eef8632c9d1.png",
7
8
  "fonts/fa-regular-400.woff2": "/packs/fonts/fa-regular-400-944fb546bd7018b07190a32244f67dc9.woff2",
@@ -31,6 +32,13 @@
31
32
  ]
32
33
  }
33
34
  },
35
+ "main": {
36
+ "assets": {
37
+ "js": [
38
+ "/packs/main-e323a53c7f30f5d53cbb.js"
39
+ ]
40
+ }
41
+ },
34
42
  "hello_stimulus": {
35
43
  "assets": {
36
44
  "css": [