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,281 @@
1
+ describe "Webpacker::Configuration" do
2
+ ROOT_PATH = Pathname.new(File.expand_path("test_app", __dir__))
3
+
4
+ context "with standard webpacker.yml" do
5
+ let(:config) do
6
+ Webpacker::Configuration.new(
7
+ root_path: ROOT_PATH,
8
+ config_path: Pathname.new(File.expand_path("./test_app/config/webpacker.yml", __dir__)),
9
+ env: "production"
10
+ )
11
+ end
12
+
13
+ it "#source_path returns correct path" do
14
+ source_path = File.expand_path File.join(File.dirname(__FILE__), "test_app/app/packs").to_s
15
+ expect(config.source_path.to_s).to eq source_path
16
+ end
17
+
18
+ it "#source_entry_path returns correct path" do
19
+ source_entry_path = File.expand_path File.join(File.dirname(__FILE__), "test_app/app/packs", "entrypoints").to_s
20
+ expect(config.source_entry_path.to_s).to eq source_entry_path
21
+ end
22
+
23
+ it "#public_root_path returns correct path" do
24
+ public_root_path = File.expand_path File.join(File.dirname(__FILE__), "test_app/public").to_s
25
+ expect(config.public_path.to_s).to eq public_root_path
26
+ end
27
+
28
+ it "#public_output_path returns correct path" do
29
+ public_output_path = File.expand_path File.join(File.dirname(__FILE__), "test_app/public/packs").to_s
30
+ expect(config.public_output_path.to_s).to eq public_output_path
31
+ end
32
+
33
+ it "#public_manifest_path returns correct path" do
34
+ public_manifest_path = File.expand_path File.join(File.dirname(__FILE__), "test_app/public/packs", "manifest.json").to_s
35
+ expect(config.public_manifest_path.to_s).to eq public_manifest_path
36
+ end
37
+
38
+ it "#manifest_path returns correct path" do
39
+ manifest_path = File.expand_path File.join(File.dirname(__FILE__), "test_app/public/packs", "manifest.json").to_s
40
+ expect(config.manifest_path.to_s).to eq manifest_path
41
+ end
42
+
43
+ it "#cache_path returns correct path" do
44
+ cache_path = File.expand_path File.join(File.dirname(__FILE__), "test_app/tmp/webpacker").to_s
45
+ expect(config.cache_path.to_s).to eq cache_path
46
+ end
47
+
48
+ it "#additional_paths returns correct path" do
49
+ expect(config.additional_paths).to eq ["app/assets", "/etc/yarn", "some.config.js", "app/elm"]
50
+ end
51
+
52
+ describe "#cache_manifest?" do
53
+ it "returns true in production environment" do
54
+ expect(config.cache_manifest?).to be true
55
+ end
56
+
57
+ it "returns false in developemnt environemnt" do
58
+ with_rails_env("development") do
59
+ expect(Webpacker.config.cache_manifest?).to be false
60
+ end
61
+ end
62
+
63
+ it "returns false in test environment" do
64
+ with_rails_env("test") do
65
+ expect(Webpacker.config.cache_manifest?).to be false
66
+ end
67
+ end
68
+ end
69
+
70
+ describe "#compile?" do
71
+ it "returns false in production environment" do
72
+ expect(config.compile?).to be false
73
+ end
74
+
75
+ it "returns true in developemnt environemnt" do
76
+ with_rails_env("development") do
77
+ expect(Webpacker.config.compile?).to be true
78
+ end
79
+ end
80
+
81
+ it "returns true in test environemnt" do
82
+ with_rails_env("test") do
83
+ expect(Webpacker.config.compile?).to be true
84
+ end
85
+ end
86
+ end
87
+
88
+ describe "#nested_entries?" do
89
+ it "returns false in production environment" do
90
+ expect(config.nested_entries?).to be false
91
+ end
92
+
93
+ it "returns false in development environment" do
94
+ with_rails_env("development") do
95
+ expect(Webpacker.config.nested_entries?).to be false
96
+ end
97
+ end
98
+
99
+ it "returns false in test environment" do
100
+ with_rails_env("test") do
101
+ expect(Webpacker.config.nested_entries?).to be false
102
+ end
103
+ end
104
+ end
105
+
106
+ describe "#ensure_consistent_versioning?" do
107
+ it "returns false in production environment" do
108
+ expect(config.ensure_consistent_versioning?).to be false
109
+ end
110
+
111
+ it "returns true in development environment" do
112
+ with_rails_env("development") do
113
+ expect(Webpacker.config.ensure_consistent_versioning?).to be true
114
+ end
115
+ end
116
+
117
+ it "returns false in test environment" do
118
+ with_rails_env("test") do
119
+ expect(Webpacker.config.ensure_consistent_versioning?).to be false
120
+ end
121
+ end
122
+ end
123
+
124
+ describe "#webpacker_precompile?" do
125
+ before :each do
126
+ ENV["WEBPACKER_PRECOMPILE"] = nil
127
+ end
128
+
129
+ subject { config.webpacker_precompile? }
130
+
131
+ it "returns true when WEBPACKER_PRECOMPILE is unset" do
132
+ is_expected.to be true
133
+ end
134
+
135
+ it "returns false when WEBPACKER_PRECOMPILE sets to no" do
136
+ ENV["WEBPACKER_PRECOMPILE"] = "no"
137
+ is_expected.to be false
138
+ end
139
+
140
+ it "returns true when WEBPACKER_PRECOMPILE sets to yes" do
141
+ ENV["WEBPACKER_PRECOMPILE"] = "yes"
142
+ is_expected.to be true
143
+ end
144
+
145
+ it "returns false when WEBPACKER_PRECOMPILE sets to false" do
146
+ ENV["WEBPACKER_PRECOMPILE"] = "false"
147
+ is_expected.to be false
148
+ end
149
+
150
+ it "returns true when WEBPACKER_PRECOMPILE sets to true" do
151
+ ENV["WEBPACKER_PRECOMPILE"] = "true"
152
+ is_expected.to be true
153
+ end
154
+
155
+ it "returns false when WEBPACKER_PRECOMPILE sets to n" do
156
+ ENV["WEBPACKER_PRECOMPILE"] = "n"
157
+ is_expected.to be false
158
+ end
159
+
160
+ it "returns true when WEBPACKER_PRECOMPILE sets to y" do
161
+ ENV["WEBPACKER_PRECOMPILE"] = "y"
162
+ is_expected.to be true
163
+ end
164
+
165
+ it "returns false when WEBPACKER_PRECOMPILE sets to f" do
166
+ ENV["WEBPACKER_PRECOMPILE"] = "f"
167
+ is_expected.to be false
168
+ end
169
+
170
+ it "returns true when WEBPACKER_PRECOMPILE sets to t" do
171
+ ENV["WEBPACKER_PRECOMPILE"] = "t"
172
+ is_expected.to be true
173
+ end
174
+ end
175
+ end
176
+
177
+ context "with webpacker config file containing public_output_path entry" do
178
+ config = Webpacker::Configuration.new(
179
+ root_path: ROOT_PATH,
180
+ config_path: Pathname.new(File.expand_path("./test_app/config/webpacker_public_root.yml", __dir__)),
181
+ env: "production"
182
+ )
183
+
184
+ it "#public_output_path returns correct path" do
185
+ expected_public_output_path = File.expand_path File.join(File.dirname(__FILE__), "public/packs").to_s
186
+ expect(config.public_output_path.to_s).to eq expected_public_output_path
187
+ end
188
+ end
189
+
190
+ context "with webpacker config file containing manifext_path entry" do
191
+ config = Webpacker::Configuration.new(
192
+ root_path: ROOT_PATH,
193
+ config_path: Pathname.new(File.expand_path("./test_app/config/webpacker_manifest_path.yml", __dir__)),
194
+ env: "production"
195
+ )
196
+
197
+ it "#manifest_path returns correct expected value" do
198
+ expected_manifest_path = File.expand_path File.join(File.dirname(__FILE__), "test_app/app/packs", "manifest.json").to_s
199
+ expect(config.manifest_path.to_s).to eq expected_manifest_path
200
+ end
201
+ end
202
+
203
+ context "with webpacker_precompile entry set to false" do
204
+ describe "#webpacker_precompile?" do
205
+ before :each do
206
+ ENV["WEBPACKER_PRECOMPILE"] = nil
207
+ end
208
+
209
+ let(:config) {
210
+ Webpacker::Configuration.new(
211
+ root_path: ROOT_PATH,
212
+ config_path: Pathname.new(File.expand_path("./test_app/config/webpacker_no_precompile.yml", __dir__)),
213
+ env: "production"
214
+ )
215
+ }
216
+
217
+ subject { config.webpacker_precompile? }
218
+
219
+ it "returns false with unset WEBPACKER_PRECOMPILE" do
220
+ expect(subject).to be false
221
+ end
222
+
223
+ it "returns true with WEBPACKER_PRECOMPILE set to true" do
224
+ ENV["WEBPACKER_PRECOMPILE"] = "true"
225
+ expect(subject).to be true
226
+ end
227
+
228
+ it "returns false with WEBPACKER_PRECOMPILE set to nil" do
229
+ ENV["WEBPACKER_PRECOMPILE"] = nil
230
+ expect(subject).to be false
231
+ end
232
+ end
233
+ end
234
+
235
+ context "with webpacker config file containing invalid path" do
236
+ config = Webpacker::Configuration.new(
237
+ root_path: ROOT_PATH,
238
+ config_path: Pathname.new(File.expand_path("./test_app/config/invalid_path.yml", __dir__)),
239
+ env: "default"
240
+ )
241
+
242
+ it "#webpacker_precompile? returns false" do
243
+ expect(config.webpacker_precompile?).to be false
244
+ end
245
+ end
246
+
247
+ context "with webpacker config file with defaults fallback" do
248
+ let(:config) do
249
+ Webpacker::Configuration.new(
250
+ root_path: ROOT_PATH,
251
+ config_path: Pathname.new(File.expand_path("./test_app/config/webpacker_defaults_fallback.yml", __dir__)),
252
+ env: "default"
253
+ )
254
+ end
255
+
256
+ it "#cache_manifest? falls back to 'default' config from bundled file" do
257
+ expect(config.cache_manifest?).to be false
258
+ end
259
+
260
+ it "#webpacker_precompile? uses 'default' config from custom file" do
261
+ expect(config.webpacker_precompile?).to be false
262
+ end
263
+ end
264
+
265
+ context "falls back to bundled production config for custom environments" do
266
+ let(:config) do
267
+ Webpacker::Configuration.new(
268
+ root_path: ROOT_PATH,
269
+ config_path: Pathname.new(File.expand_path("./test_app/config/webpacker_defaults_fallback.yml", __dir__)),
270
+ env: "staging"
271
+ )
272
+ end
273
+
274
+ it "#cache_manifest? fall back to 'production' config from bundled file" do
275
+ expect(config.cache_manifest?).to be true
276
+ end
277
+ it "#webpacker_precompile? use 'staging' config from custom file" do
278
+ expect(config.webpacker_precompile?).to be false
279
+ end
280
+ end
281
+ end
@@ -1,52 +1,47 @@
1
- require "test_helper"
2
1
  require "webpacker/dev_server_runner"
3
2
 
4
- class DevServerRunnerTest < Webpacker::Test
5
- def setup
3
+ describe "DevServerRunner" do
4
+ before do
6
5
  @original_node_env, ENV["NODE_ENV"] = ENV["NODE_ENV"], "development"
7
6
  @original_rails_env, ENV["RAILS_ENV"] = ENV["RAILS_ENV"], "development"
8
7
  @original_webpacker_config = ENV["WEBPACKER_CONFIG"]
9
8
  end
10
9
 
11
- def teardown
10
+ after do
12
11
  ENV["NODE_ENV"] = @original_node_env
13
12
  ENV["RAILS_ENV"] = @original_rails_env
14
13
  ENV["WEBPACKER_CONFIG"] = @original_webpacker_config
15
14
  end
16
15
 
17
- def test_run_cmd_via_node_modules
18
- cmd = ["#{test_app_path}/node_modules/.bin/webpack", "serve", "--config", "#{test_app_path}/config/webpack/webpack.config.js"]
16
+ let(:test_app_path) { File.expand_path("test_app", __dir__) }
19
17
 
18
+ it "run cmd via node modules" do
19
+ cmd = ["#{test_app_path}/node_modules/.bin/webpack", "serve", "--config", "#{test_app_path}/config/webpack/webpack.config.js"]
20
20
  verify_command(cmd, use_node_modules: true)
21
21
  end
22
-
23
- def test_run_cmd_via_yarn
22
+ it "run cmd via yarn" do
24
23
  cmd = ["yarn", "webpack", "serve", "--config", "#{test_app_path}/config/webpack/webpack.config.js"]
25
-
26
24
  verify_command(cmd, use_node_modules: false)
27
25
  end
28
-
29
- def test_run_cmd_argv
26
+ it "run cmd argv" do
30
27
  cmd = ["#{test_app_path}/node_modules/.bin/webpack", "serve", "--config", "#{test_app_path}/config/webpack/webpack.config.js", "--quiet"]
31
-
32
- verify_command(cmd, argv: ["--quiet"])
28
+ verify_command(cmd, argv: (["--quiet"]))
33
29
  end
34
-
35
- def test_run_cmd_argv_with_https
30
+ it "run cmd argv with https" do
36
31
  cmd = ["#{test_app_path}/node_modules/.bin/webpack", "serve", "--config", "#{test_app_path}/config/webpack/webpack.config.js", "--https"]
37
32
 
38
- dev_server = Webpacker::DevServer.new({})
39
- def dev_server.host; "localhost"; end
40
- def dev_server.port; "3035"; end
41
- def dev_server.pretty?; false; end
42
- def dev_server.https?; true; end
43
- def dev_server.hmr?; false; end
44
- Webpacker::DevServer.stub(:new, dev_server) do
45
- verify_command(cmd, argv: ["--https"])
46
- end
47
- end
33
+ dev_server = double()
34
+ allow(dev_server).to receive(:host).and_return("localhost")
35
+ allow(dev_server).to receive(:port).and_return("3035")
36
+ allow(dev_server).to receive(:pretty?).and_return(false)
37
+ allow(dev_server).to receive(:https?).and_return(true)
38
+ allow(dev_server).to receive(:hmr?).and_return(false)
48
39
 
49
- def test_environment_variables
40
+ allow(Webpacker::DevServer).to receive(:new) do
41
+ verify_command(cmd, argv: (["--https"]))
42
+ end.and_return(dev_server)
43
+ end
44
+ it "accepts environment variables" do
50
45
  cmd = ["#{test_app_path}/node_modules/.bin/webpack", "serve", "--config", "#{test_app_path}/config/webpack/webpack.config.js"]
51
46
  env = Webpacker::Compiler.env.dup
52
47
  ENV["WEBPACKER_CONFIG"] = env["WEBPACKER_CONFIG"] = "#{test_app_path}/config/webpacker_other_location.yml"
@@ -55,27 +50,22 @@ class DevServerRunnerTest < Webpacker::Test
55
50
  end
56
51
 
57
52
  private
58
- def test_app_path
59
- File.expand_path("test_app", __dir__)
60
- end
61
53
 
62
54
  def verify_command(cmd, use_node_modules: true, argv: [], env: Webpacker::Compiler.env)
63
55
  cwd = Dir.pwd
64
56
  Dir.chdir(test_app_path)
65
-
66
57
  klass = Webpacker::DevServerRunner
67
58
  instance = klass.new(argv)
68
- mock = Minitest::Mock.new
69
- mock.expect(:call, nil, [env, *cmd])
70
59
 
71
- klass.stub(:new, instance) do
72
- instance.stub(:node_modules_bin_exist?, use_node_modules) do
73
- Kernel.stub(:exec, mock) { klass.run(argv) }
74
- end
75
- end
60
+ allow(klass).to receive(:new).and_return(instance)
61
+ allow(instance).to receive(:node_modules_bin_exist?).and_return(use_node_modules)
62
+ allow(Kernel).to receive(:exec).with(env, *cmd)
63
+
64
+ klass.run(argv)
65
+
66
+ expect(Kernel).to have_received(:exec).with(env, *cmd)
76
67
 
77
- mock.verify
78
- ensure
79
- Dir.chdir(cwd)
68
+ ensure
69
+ Dir.chdir(cwd)
80
70
  end
81
71
  end
@@ -0,0 +1,45 @@
1
+ describe "DevServer" do
2
+ it "doesn't run by default" do
3
+ expect(Webpacker.dev_server.running?).to be_falsy
4
+ end
5
+
6
+ it "uses localhost as host in development environment" do
7
+ with_rails_env("development") do
8
+ expect(Webpacker.dev_server.host).to eq "localhost"
9
+ end
10
+ end
11
+
12
+ it "uses port 3035 in development environment" do
13
+ with_rails_env("development") do
14
+ expect(Webpacker.dev_server.port).to eq 3035
15
+ end
16
+ end
17
+
18
+ it "doesn't use https in development environment" do
19
+ with_rails_env("development") do
20
+ expect(Webpacker.dev_server.https?).to be false
21
+ end
22
+ end
23
+
24
+ it "uses http protocol in development environment" do
25
+ with_rails_env("development") do
26
+ expect(Webpacker.dev_server.protocol).to eq "http"
27
+ end
28
+ end
29
+
30
+ it "sets host_with_port to localhost:3035 in development environment" do
31
+ with_rails_env("development") do
32
+ expect(Webpacker.dev_server.host_with_port).to eq "localhost:3035"
33
+ end
34
+ end
35
+
36
+ it "doesn't use pretty in development environment" do
37
+ with_rails_env("development") do
38
+ expect(Webpacker.dev_server.pretty?).to be false
39
+ end
40
+ end
41
+
42
+ it "users WEBPACKER_DEV_SERVER for DEFAULT_ENV_PREFIX" do
43
+ expect(Webpacker::DevServer::DEFAULT_ENV_PREFIX).to eq "WEBPACKER_DEV_SERVER"
44
+ end
45
+ end
@@ -0,0 +1,33 @@
1
+ describe "DigestStrategy" do
2
+ def remove_compilation_digest_path
3
+ @digest_strategy.send(:compilation_digest_path).tap do |path|
4
+ path.delete if path.exist?
5
+ end
6
+ end
7
+
8
+ before :all do
9
+ @digest_strategy = Webpacker::DigestStrategy.new
10
+ remove_compilation_digest_path
11
+ end
12
+
13
+ after :all do
14
+ remove_compilation_digest_path
15
+ end
16
+
17
+ it "is not fresh before compilation" do
18
+ expect(@digest_strategy.stale?).to be true
19
+ expect(@digest_strategy.fresh?).to be_falsy
20
+ end
21
+
22
+ it "is fresh after compilation" do
23
+ @digest_strategy.after_compile_hook
24
+ expect(@digest_strategy.stale?).to be false
25
+ expect(@digest_strategy.fresh?).to be true
26
+ end
27
+
28
+ it "generates correct compilation_digest_path" do
29
+ actual_path = @digest_strategy.send(:compilation_digest_path).basename.to_s
30
+ expected_path = "last-compilation-digest-#{Webpacker.env}"
31
+ expect(actual_path).to eq expected_path
32
+ end
33
+ end
@@ -1,22 +1,25 @@
1
- require "test_helper"
2
-
3
- class EngineRakeTasksTest < Minitest::Test
4
- def setup
1
+ describe "EngineRakeTasks" do
2
+ before :context do
5
3
  remove_webpack_binstubs
6
4
  end
7
5
 
8
- def teardown
6
+ after :context do
9
7
  remove_webpack_binstubs
10
8
  end
11
9
 
12
- def test_task_mounted
10
+ it "mounts app:webpacker task successfully" do
13
11
  output = Dir.chdir(mounted_app_path) { `rake -T` }
14
- assert_includes output, "app:webpacker"
12
+ expect(output).to include "app:webpacker"
15
13
  end
16
14
 
17
- def test_binstubs
15
+ it "binstubs adds only expected files to bin directory" do
16
+ original_files_in_bin = current_files_in_bin
17
+
18
18
  Dir.chdir(mounted_app_path) { `bundle exec rake app:webpacker:binstubs` }
19
- webpack_binstub_paths.each { |path| assert File.exist?(path) }
19
+ webpack_binstub_paths.each { |path| expect(File.exist?(path)).to be true }
20
+
21
+ # and no other files are added
22
+ expect(current_files_in_bin - webpack_binstub_paths).to match_array original_files_in_bin
20
23
  end
21
24
 
22
25
  private
@@ -24,8 +27,13 @@ class EngineRakeTasksTest < Minitest::Test
24
27
  File.expand_path("mounted_app", __dir__)
25
28
  end
26
29
 
30
+ def current_files_in_bin
31
+ Dir.glob("#{mounted_app_path}/test/dummy/bin/*")
32
+ end
33
+
27
34
  def webpack_binstub_paths
28
35
  [
36
+ "#{mounted_app_path}/test/dummy/bin/yarn",
29
37
  "#{mounted_app_path}/test/dummy/bin/webpacker",
30
38
  "#{mounted_app_path}/test/dummy/bin/webpacker-dev-server",
31
39
  ]
data/spec/env_spec.rb ADDED
@@ -0,0 +1,21 @@
1
+ RSpec.describe "Env" do
2
+ it "uses the same env for Rails and Webpacker" do
3
+ expect(Webpacker.env).to eq Rails.env
4
+ end
5
+
6
+ it "uses production env without config" do
7
+ with_rails_env("foo") do
8
+ expect(Webpacker.env).to eq "production"
9
+ end
10
+ end
11
+
12
+ it "uses the given env in custom config" do
13
+ with_rails_env("staging") do
14
+ expect(Webpacker.env).to eq "staging"
15
+ end
16
+ end
17
+
18
+ it "uses 'production' as default env" do
19
+ expect(Webpacker::DEFAULT_ENV).to eq "production"
20
+ end
21
+ end