shakapacker 6.0.0.rc.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (147) hide show
  1. checksums.yaml +7 -0
  2. data/.eslintignore +4 -0
  3. data/.eslintrc.js +14 -0
  4. data/.github/ISSUE_TEMPLATE/bug_report.md +20 -0
  5. data/.github/ISSUE_TEMPLATE/feature-request.md +18 -0
  6. data/.github/workflows/jest.yml +30 -0
  7. data/.github/workflows/js-lint.yml +31 -0
  8. data/.github/workflows/rubocop.yml +39 -0
  9. data/.github/workflows/ruby.yml +48 -0
  10. data/.gitignore +13 -0
  11. data/.node-version +1 -0
  12. data/.rubocop.yml +229 -0
  13. data/CHANGELOG.md +32 -0
  14. data/CONTRIBUTING.md +62 -0
  15. data/Gemfile +13 -0
  16. data/Gemfile.lock +183 -0
  17. data/MIT-LICENSE +20 -0
  18. data/README.md +666 -0
  19. data/Rakefile +11 -0
  20. data/config/README.md +3 -0
  21. data/config/webpacker.yml +1 -0
  22. data/docs/customizing_babel_config.md +59 -0
  23. data/docs/deployment.md +116 -0
  24. data/docs/developing_webpacker.md +29 -0
  25. data/docs/troubleshooting.md +212 -0
  26. data/docs/v6_upgrade.md +158 -0
  27. data/gemfiles/Gemfile-rails-edge +12 -0
  28. data/gemfiles/Gemfile-rails.5.2.x +9 -0
  29. data/gemfiles/Gemfile-rails.6.0.x +9 -0
  30. data/gemfiles/Gemfile-rails.6.1.x +12 -0
  31. data/lib/install/application.js +15 -0
  32. data/lib/install/bin/webpacker +15 -0
  33. data/lib/install/bin/webpacker-dev-server +18 -0
  34. data/lib/install/bin/yarn +18 -0
  35. data/lib/install/binstubs.rb +4 -0
  36. data/lib/install/config/webpack/webpack.config.js +5 -0
  37. data/lib/install/config/webpacker.yml +64 -0
  38. data/lib/install/package.json +15 -0
  39. data/lib/install/template.rb +100 -0
  40. data/lib/shakapacker/utils/git_utils.rb +23 -0
  41. data/lib/shakapacker/utils/version_syntax_converter.rb +24 -0
  42. data/lib/tasks/webpacker/binstubs.rake +15 -0
  43. data/lib/tasks/webpacker/check_binstubs.rake +12 -0
  44. data/lib/tasks/webpacker/check_node.rake +31 -0
  45. data/lib/tasks/webpacker/check_yarn.rake +33 -0
  46. data/lib/tasks/webpacker/clean.rake +25 -0
  47. data/lib/tasks/webpacker/clobber.rake +20 -0
  48. data/lib/tasks/webpacker/compile.rake +45 -0
  49. data/lib/tasks/webpacker/info.rake +21 -0
  50. data/lib/tasks/webpacker/install.rake +17 -0
  51. data/lib/tasks/webpacker/verify_config.rake +14 -0
  52. data/lib/tasks/webpacker/verify_install.rake +4 -0
  53. data/lib/tasks/webpacker/yarn_install.rake +18 -0
  54. data/lib/tasks/webpacker.rake +19 -0
  55. data/lib/tasks/yarn.rake +38 -0
  56. data/lib/webpacker/commands.rb +79 -0
  57. data/lib/webpacker/compiler.rb +130 -0
  58. data/lib/webpacker/configuration.rb +111 -0
  59. data/lib/webpacker/dev_server.rb +72 -0
  60. data/lib/webpacker/dev_server_proxy.rb +33 -0
  61. data/lib/webpacker/dev_server_runner.rb +96 -0
  62. data/lib/webpacker/env.rb +43 -0
  63. data/lib/webpacker/helper.rb +161 -0
  64. data/lib/webpacker/instance.rb +41 -0
  65. data/lib/webpacker/manifest.rb +120 -0
  66. data/lib/webpacker/railtie.rb +63 -0
  67. data/lib/webpacker/runner.rb +23 -0
  68. data/lib/webpacker/version.rb +4 -0
  69. data/lib/webpacker/webpack_runner.rb +58 -0
  70. data/lib/webpacker.rb +46 -0
  71. data/package/__tests__/config.js +34 -0
  72. data/package/__tests__/dev_server.js +45 -0
  73. data/package/__tests__/development.js +35 -0
  74. data/package/__tests__/env.js +58 -0
  75. data/package/__tests__/index.js +9 -0
  76. data/package/__tests__/production.js +29 -0
  77. data/package/__tests__/staging.js +30 -0
  78. data/package/__tests__/test.js +25 -0
  79. data/package/babel/preset.js +41 -0
  80. data/package/config.js +32 -0
  81. data/package/configPath.js +3 -0
  82. data/package/dev_server.js +20 -0
  83. data/package/env.js +27 -0
  84. data/package/environments/__tests__/base.js +69 -0
  85. data/package/environments/base.js +116 -0
  86. data/package/environments/development.js +55 -0
  87. data/package/environments/production.js +79 -0
  88. data/package/environments/test.js +3 -0
  89. data/package/index.js +33 -0
  90. data/package/inliningCss.js +7 -0
  91. data/package/rules/babel.js +30 -0
  92. data/package/rules/coffee.js +6 -0
  93. data/package/rules/css.js +3 -0
  94. data/package/rules/erb.js +15 -0
  95. data/package/rules/file.js +23 -0
  96. data/package/rules/index.js +18 -0
  97. data/package/rules/less.js +22 -0
  98. data/package/rules/raw.js +5 -0
  99. data/package/rules/sass.js +16 -0
  100. data/package/rules/stylus.js +26 -0
  101. data/package/utils/get_style_rule.js +37 -0
  102. data/package/utils/helpers.js +51 -0
  103. data/package.json +71 -0
  104. data/rakelib/release.rake +57 -0
  105. data/test/command_test.rb +109 -0
  106. data/test/compiler_test.rb +68 -0
  107. data/test/configuration_test.rb +78 -0
  108. data/test/dev_server_runner_test.rb +81 -0
  109. data/test/dev_server_test.rb +47 -0
  110. data/test/engine_rake_tasks_test.rb +39 -0
  111. data/test/env_test.rb +23 -0
  112. data/test/helper_test.rb +159 -0
  113. data/test/manifest_test.rb +89 -0
  114. data/test/mounted_app/Rakefile +4 -0
  115. data/test/mounted_app/test/dummy/Rakefile +3 -0
  116. data/test/mounted_app/test/dummy/bin/rails +3 -0
  117. data/test/mounted_app/test/dummy/bin/rake +3 -0
  118. data/test/mounted_app/test/dummy/config/application.rb +10 -0
  119. data/test/mounted_app/test/dummy/config/environment.rb +3 -0
  120. data/test/mounted_app/test/dummy/config/webpacker.yml +75 -0
  121. data/test/mounted_app/test/dummy/config.ru +5 -0
  122. data/test/mounted_app/test/dummy/package.json +7 -0
  123. data/test/rake_tasks_test.rb +71 -0
  124. data/test/test_app/Rakefile +3 -0
  125. data/test/test_app/app/packs/entrypoints/application.js +10 -0
  126. data/test/test_app/app/packs/entrypoints/multi_entry.css +4 -0
  127. data/test/test_app/app/packs/entrypoints/multi_entry.js +4 -0
  128. data/test/test_app/bin/webpacker +14 -0
  129. data/test/test_app/bin/webpacker-dev-server +14 -0
  130. data/test/test_app/config/application.rb +11 -0
  131. data/test/test_app/config/environment.rb +4 -0
  132. data/test/test_app/config/initializers/inspect_autoload_paths.rb +1 -0
  133. data/test/test_app/config/webpack/webpack.config.js +0 -0
  134. data/test/test_app/config/webpacker.yml +77 -0
  135. data/test/test_app/config/webpacker_other_location.yml +79 -0
  136. data/test/test_app/config/webpacker_public_root.yml +18 -0
  137. data/test/test_app/config.ru +5 -0
  138. data/test/test_app/package.json +13 -0
  139. data/test/test_app/public/packs/manifest.json +50 -0
  140. data/test/test_app/some.config.js +0 -0
  141. data/test/test_app/yarn.lock +11 -0
  142. data/test/test_helper.rb +33 -0
  143. data/test/webpack_runner_test.rb +57 -0
  144. data/test/webpacker_test.rb +34 -0
  145. data/webpacker.gemspec +31 -0
  146. data/yarn.lock +4029 -0
  147. metadata +331 -0
@@ -0,0 +1,81 @@
1
+ require "test_helper"
2
+ require "webpacker/dev_server_runner"
3
+
4
+ class DevServerRunnerTest < Webpacker::Test
5
+ def setup
6
+ @original_node_env, ENV["NODE_ENV"] = ENV["NODE_ENV"], "development"
7
+ @original_rails_env, ENV["RAILS_ENV"] = ENV["RAILS_ENV"], "development"
8
+ @original_webpacker_config = ENV["WEBPACKER_CONFIG"]
9
+ end
10
+
11
+ def teardown
12
+ ENV["NODE_ENV"] = @original_node_env
13
+ ENV["RAILS_ENV"] = @original_rails_env
14
+ ENV["WEBPACKER_CONFIG"] = @original_webpacker_config
15
+ end
16
+
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"]
19
+
20
+ verify_command(cmd, use_node_modules: true)
21
+ end
22
+
23
+ def test_run_cmd_via_yarn
24
+ cmd = ["yarn", "webpack", "serve", "--config", "#{test_app_path}/config/webpack/webpack.config.js"]
25
+
26
+ verify_command(cmd, use_node_modules: false)
27
+ end
28
+
29
+ def test_run_cmd_argv
30
+ 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"])
33
+ end
34
+
35
+ def test_run_cmd_argv_with_https
36
+ cmd = ["#{test_app_path}/node_modules/.bin/webpack", "serve", "--config", "#{test_app_path}/config/webpack/webpack.config.js", "--https"]
37
+
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
48
+
49
+ def test_environment_variables
50
+ cmd = ["#{test_app_path}/node_modules/.bin/webpack", "serve", "--config", "#{test_app_path}/config/webpack/webpack.config.js"]
51
+ env = Webpacker::Compiler.env.dup
52
+ ENV["WEBPACKER_CONFIG"] = env["WEBPACKER_CONFIG"] = "#{test_app_path}/config/webpacker_other_location.yml"
53
+ env["WEBPACK_SERVE"] = "true"
54
+ verify_command(cmd, env: env)
55
+ end
56
+
57
+ private
58
+ def test_app_path
59
+ File.expand_path("test_app", __dir__)
60
+ end
61
+
62
+ def verify_command(cmd, use_node_modules: true, argv: [], env: Webpacker::Compiler.env)
63
+ cwd = Dir.pwd
64
+ Dir.chdir(test_app_path)
65
+
66
+ klass = Webpacker::DevServerRunner
67
+ instance = klass.new(argv)
68
+ mock = Minitest::Mock.new
69
+ mock.expect(:call, nil, [env, *cmd])
70
+
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
76
+
77
+ mock.verify
78
+ ensure
79
+ Dir.chdir(cwd)
80
+ end
81
+ end
@@ -0,0 +1,47 @@
1
+ require "test_helper"
2
+
3
+ class DevServerTest < Webpacker::Test
4
+ def test_running?
5
+ refute Webpacker.dev_server.running?
6
+ end
7
+
8
+ def test_host
9
+ with_rails_env("development") do
10
+ assert_equal Webpacker.dev_server.host, "localhost"
11
+ end
12
+ end
13
+
14
+ def test_port
15
+ with_rails_env("development") do
16
+ assert_equal Webpacker.dev_server.port, 3035
17
+ end
18
+ end
19
+
20
+ def test_https?
21
+ with_rails_env("development") do
22
+ assert_equal Webpacker.dev_server.https?, false
23
+ end
24
+ end
25
+
26
+ def test_protocol
27
+ with_rails_env("development") do
28
+ assert_equal Webpacker.dev_server.protocol, "http"
29
+ end
30
+ end
31
+
32
+ def test_host_with_port
33
+ with_rails_env("development") do
34
+ assert_equal Webpacker.dev_server.host_with_port, "localhost:3035"
35
+ end
36
+ end
37
+
38
+ def test_pretty?
39
+ with_rails_env("development") do
40
+ refute Webpacker.dev_server.pretty?
41
+ end
42
+ end
43
+
44
+ def test_default_env_prefix
45
+ assert_equal Webpacker::DevServer::DEFAULT_ENV_PREFIX, "WEBPACKER_DEV_SERVER"
46
+ end
47
+ end
@@ -0,0 +1,39 @@
1
+ require "test_helper"
2
+
3
+ class EngineRakeTasksTest < Minitest::Test
4
+ def setup
5
+ remove_webpack_binstubs
6
+ end
7
+
8
+ def teardown
9
+ remove_webpack_binstubs
10
+ end
11
+
12
+ def test_task_mounted
13
+ output = Dir.chdir(mounted_app_path) { `rake -T` }
14
+ assert_includes output, "app:webpacker"
15
+ end
16
+
17
+ def test_binstubs
18
+ Dir.chdir(mounted_app_path) { `bundle exec rake app:webpacker:binstubs` }
19
+ webpack_binstub_paths.each { |path| assert File.exist?(path) }
20
+ end
21
+
22
+ private
23
+ def mounted_app_path
24
+ File.expand_path("mounted_app", __dir__)
25
+ end
26
+
27
+ def webpack_binstub_paths
28
+ [
29
+ "#{mounted_app_path}/test/dummy/bin/webpacker",
30
+ "#{mounted_app_path}/test/dummy/bin/webpacker-dev-server",
31
+ ]
32
+ end
33
+
34
+ def remove_webpack_binstubs
35
+ webpack_binstub_paths.each do |path|
36
+ File.delete(path) if File.exist?(path)
37
+ end
38
+ end
39
+ end
data/test/env_test.rb ADDED
@@ -0,0 +1,23 @@
1
+ require "test_helper"
2
+
3
+ class EnvTest < Webpacker::Test
4
+ def test_current
5
+ assert_equal Webpacker.env, Rails.env
6
+ end
7
+
8
+ def test_custom_without_config
9
+ with_rails_env("foo") do
10
+ assert_equal Webpacker.env, "production"
11
+ end
12
+ end
13
+
14
+ def test_custom_with_config
15
+ with_rails_env("staging") do
16
+ assert_equal Webpacker.env, "staging"
17
+ end
18
+ end
19
+
20
+ def test_default
21
+ assert_equal Webpacker::Env::DEFAULT, "production"
22
+ end
23
+ end
@@ -0,0 +1,159 @@
1
+ require "test_helper"
2
+
3
+ class HelperTest < ActionView::TestCase
4
+ tests Webpacker::Helper
5
+
6
+ attr_reader :request
7
+
8
+ def setup
9
+ @request = Class.new do
10
+ def send_early_hints(links) end
11
+ def base_url
12
+ "https://example.com"
13
+ end
14
+ end.new
15
+ end
16
+
17
+ def test_asset_pack_path
18
+ assert_equal "/packs/bootstrap-300631c4f0e0f9c865bc.js", asset_pack_path("bootstrap.js")
19
+ assert_equal "/packs/bootstrap-c38deda30895059837cf.css", asset_pack_path("bootstrap.css")
20
+ end
21
+
22
+ def test_asset_pack_url
23
+ assert_equal "https://example.com/packs/bootstrap-300631c4f0e0f9c865bc.js", asset_pack_url("bootstrap.js")
24
+ assert_equal "https://example.com/packs/bootstrap-c38deda30895059837cf.css", asset_pack_url("bootstrap.css")
25
+ end
26
+
27
+ def test_image_pack_path
28
+ assert_equal "/packs/application-k344a6d59eef8632c9d1.png", image_pack_path("application.png")
29
+ assert_equal "/packs/static/image-c38deda30895059837cf.jpg", image_pack_path("image.jpg")
30
+ assert_equal "/packs/static/image-c38deda30895059837cf.jpg", image_pack_path("static/image.jpg")
31
+ assert_equal "/packs/static/nested/image-c38deda30895059837cf.jpg", image_pack_path("nested/image.jpg")
32
+ assert_equal "/packs/static/nested/image-c38deda30895059837cf.jpg", image_pack_path("static/nested/image.jpg")
33
+ end
34
+
35
+ def test_image_pack_url
36
+ assert_equal "https://example.com/packs/application-k344a6d59eef8632c9d1.png", image_pack_url("application.png")
37
+ assert_equal "https://example.com/packs/static/image-c38deda30895059837cf.jpg", image_pack_url("image.jpg")
38
+ assert_equal "https://example.com/packs/static/image-c38deda30895059837cf.jpg", image_pack_url("static/image.jpg")
39
+ assert_equal "https://example.com/packs/static/nested/image-c38deda30895059837cf.jpg", image_pack_url("nested/image.jpg")
40
+ assert_equal "https://example.com/packs/static/nested/image-c38deda30895059837cf.jpg", image_pack_url("static/nested/image.jpg")
41
+ end
42
+
43
+ def test_image_pack_tag
44
+ assert_equal \
45
+ "<img alt=\"Edit Entry\" src=\"/packs/application-k344a6d59eef8632c9d1.png\" width=\"16\" height=\"10\" />",
46
+ image_pack_tag("application.png", size: "16x10", alt: "Edit Entry")
47
+ assert_equal \
48
+ "<img alt=\"Edit Entry\" src=\"/packs/static/image-c38deda30895059837cf.jpg\" width=\"16\" height=\"10\" />",
49
+ image_pack_tag("image.jpg", size: "16x10", alt: "Edit Entry")
50
+ assert_equal \
51
+ "<img alt=\"Edit Entry\" src=\"/packs/static/image-c38deda30895059837cf.jpg\" width=\"16\" height=\"10\" />",
52
+ image_pack_tag("static/image.jpg", size: "16x10", alt: "Edit Entry")
53
+ assert_equal \
54
+ "<img alt=\"Edit Entry\" src=\"/packs/static/nested/image-c38deda30895059837cf.jpg\" width=\"16\" height=\"10\" />",
55
+ image_pack_tag("nested/image.jpg", size: "16x10", alt: "Edit Entry")
56
+ assert_equal \
57
+ "<img alt=\"Edit Entry\" src=\"/packs/static/nested/image-c38deda30895059837cf.jpg\" width=\"16\" height=\"10\" />",
58
+ image_pack_tag("static/nested/image.jpg", size: "16x10", alt: "Edit Entry")
59
+ assert_equal \
60
+ "<img srcset=\"/packs/static/image-2x-7cca48e6cae66ec07b8e.jpg 2x\" src=\"/packs/static/image-c38deda30895059837cf.jpg\" />",
61
+ image_pack_tag("static/image.jpg", srcset: { "static/image-2x.jpg" => "2x" })
62
+ end
63
+
64
+ def test_favicon_pack_tag
65
+ assert_equal \
66
+ "<link rel=\"apple-touch-icon\" type=\"image/png\" href=\"/packs/application-k344a6d59eef8632c9d1.png\" />",
67
+ favicon_pack_tag("application.png", rel: "apple-touch-icon", type: "image/png")
68
+ assert_equal \
69
+ "<link rel=\"apple-touch-icon\" type=\"image/png\" href=\"/packs/static/mb-icon-c38deda30895059837cf.png\" />",
70
+ favicon_pack_tag("mb-icon.png", rel: "apple-touch-icon", type: "image/png")
71
+ assert_equal \
72
+ "<link rel=\"apple-touch-icon\" type=\"image/png\" href=\"/packs/static/mb-icon-c38deda30895059837cf.png\" />",
73
+ favicon_pack_tag("static/mb-icon.png", rel: "apple-touch-icon", type: "image/png")
74
+ assert_equal \
75
+ "<link rel=\"apple-touch-icon\" type=\"image/png\" href=\"/packs/static/nested/mb-icon-c38deda30895059837cf.png\" />",
76
+ favicon_pack_tag("nested/mb-icon.png", rel: "apple-touch-icon", type: "image/png")
77
+ assert_equal \
78
+ "<link rel=\"apple-touch-icon\" type=\"image/png\" href=\"/packs/static/nested/mb-icon-c38deda30895059837cf.png\" />",
79
+ favicon_pack_tag("static/nested/mb-icon.png", rel: "apple-touch-icon", type: "image/png")
80
+ end
81
+
82
+ def test_preload_pack_asset
83
+ if self.class.method_defined?(:preload_link_tag)
84
+ assert_equal \
85
+ %(<link rel="preload" href="/packs/fonts/fa-regular-400-944fb546bd7018b07190a32244f67dc9.woff2" as="font" type="font/woff2" crossorigin="anonymous">),
86
+ preload_pack_asset("fonts/fa-regular-400.woff2")
87
+ else
88
+ error = assert_raises do
89
+ preload_pack_asset("fonts/fa-regular-400.woff2")
90
+ end
91
+
92
+ assert_equal \
93
+ "You need Rails >= 5.2 to use this tag.",
94
+ error.message
95
+ end
96
+ end
97
+
98
+ def test_javascript_pack_tag
99
+ assert_equal \
100
+ %(<script src="/packs/vendors~application~bootstrap-c20632e7baf2c81200d3.chunk.js" defer="defer"></script>\n) +
101
+ %(<script src="/packs/vendors~application-e55f2aae30c07fb6d82a.chunk.js" defer="defer"></script>\n) +
102
+ %(<script src="/packs/application-k344a6d59eef8632c9d1.js" defer="defer"></script>\n) +
103
+ %(<script src="/packs/bootstrap-300631c4f0e0f9c865bc.js" defer="defer"></script>),
104
+ javascript_pack_tag("application", "bootstrap")
105
+ end
106
+
107
+ def test_javascript_pack_with_no_defer_tag
108
+ assert_equal \
109
+ %(<script src="/packs/vendors~application~bootstrap-c20632e7baf2c81200d3.chunk.js"></script>\n) +
110
+ %(<script src="/packs/vendors~application-e55f2aae30c07fb6d82a.chunk.js"></script>\n) +
111
+ %(<script src="/packs/application-k344a6d59eef8632c9d1.js"></script>\n) +
112
+ %(<script src="/packs/bootstrap-300631c4f0e0f9c865bc.js"></script>),
113
+ javascript_pack_tag("application", "bootstrap", defer: false)
114
+ end
115
+
116
+ def test_javascript_pack_tag_splat
117
+ assert_equal \
118
+ %(<script src="/packs/vendors~application~bootstrap-c20632e7baf2c81200d3.chunk.js" defer="defer"></script>\n) +
119
+ %(<script src="/packs/vendors~application-e55f2aae30c07fb6d82a.chunk.js" defer="defer"></script>\n) +
120
+ %(<script src="/packs/application-k344a6d59eef8632c9d1.js" defer="defer"></script>),
121
+ javascript_pack_tag("application", defer: true)
122
+ end
123
+
124
+ def test_javascript_pack_tag_symbol
125
+ assert_equal \
126
+ %(<script src="/packs/vendors~application~bootstrap-c20632e7baf2c81200d3.chunk.js" defer="defer"></script>\n) +
127
+ %(<script src="/packs/vendors~application-e55f2aae30c07fb6d82a.chunk.js" defer="defer"></script>\n) +
128
+ %(<script src="/packs/application-k344a6d59eef8632c9d1.js" defer="defer"></script>),
129
+ javascript_pack_tag(:application)
130
+ end
131
+
132
+ def application_stylesheet_chunks
133
+ %w[/packs/1-c20632e7baf2c81200d3.chunk.css /packs/application-k344a6d59eef8632c9d1.chunk.css]
134
+ end
135
+
136
+ def hello_stimulus_stylesheet_chunks
137
+ %w[/packs/hello_stimulus-k344a6d59eef8632c9d1.chunk.css]
138
+ end
139
+
140
+ def test_stylesheet_pack_tag
141
+ assert_equal \
142
+ (application_stylesheet_chunks + hello_stimulus_stylesheet_chunks)
143
+ .map { |chunk| stylesheet_link_tag(chunk) }.join("\n"),
144
+ stylesheet_pack_tag("application", "hello_stimulus")
145
+ end
146
+
147
+ def test_stylesheet_pack_tag_symbol
148
+ assert_equal \
149
+ (application_stylesheet_chunks + hello_stimulus_stylesheet_chunks)
150
+ .map { |chunk| stylesheet_link_tag(chunk) }.join("\n"),
151
+ stylesheet_pack_tag(:application, :hello_stimulus)
152
+ end
153
+
154
+ def test_stylesheet_pack_tag_splat
155
+ assert_equal \
156
+ (application_stylesheet_chunks).map { |chunk| stylesheet_link_tag(chunk, media: "all") }.join("\n"),
157
+ stylesheet_pack_tag("application", media: "all")
158
+ end
159
+ end
@@ -0,0 +1,89 @@
1
+ require "test_helper"
2
+
3
+ class ManifestTest < Minitest::Test
4
+ def test_lookup_exception!
5
+ asset_file = "calendar.js"
6
+
7
+ error = assert_raises_manifest_missing_entry_error do
8
+ Webpacker.manifest.lookup!(asset_file)
9
+ end
10
+
11
+ assert_match "Webpacker can't find #{asset_file} in #{manifest_path}", error.message
12
+ end
13
+
14
+ def test_lookup_with_type_exception!
15
+ asset_file = "calendar"
16
+
17
+ error = assert_raises_manifest_missing_entry_error do
18
+ Webpacker.manifest.lookup!(asset_file, type: :javascript)
19
+ end
20
+
21
+ assert_match "Webpacker can't find #{asset_file}.js in #{manifest_path}", error.message
22
+ end
23
+
24
+ def test_lookup_success!
25
+ assert_equal Webpacker.manifest.lookup!("bootstrap.js"), "/packs/bootstrap-300631c4f0e0f9c865bc.js"
26
+ end
27
+
28
+ def test_lookup_with_chunks_without_extension_success!
29
+ assert_equal ["/packs/bootstrap-300631c4f0e0f9c865bc.js"], Webpacker.manifest.lookup_pack_with_chunks!("bootstrap", type: :javascript)
30
+ end
31
+
32
+ def test_lookup_with_chunks_with_extension_success!
33
+ assert_equal ["/packs/bootstrap-300631c4f0e0f9c865bc.js"], Webpacker.manifest.lookup_pack_with_chunks!("bootstrap.js", type: :javascript)
34
+ end
35
+
36
+ def test_lookup_with_chunks_without_extension_subdir_success!
37
+ assert_equal ["/packs/print/application-983b6c164a47f7ed49cd.css"], Webpacker.manifest.lookup_pack_with_chunks!("print/application", type: :css)
38
+ end
39
+
40
+ def test_lookup_with_chunks_with_extension_subdir_success!
41
+ assert_equal ["/packs/print/application-983b6c164a47f7ed49cd.css"], Webpacker.manifest.lookup_pack_with_chunks!("print/application.css", type: :css)
42
+ end
43
+
44
+ def test_lookup_nil
45
+ assert_nil Webpacker.manifest.lookup("foo.js")
46
+ end
47
+
48
+ def test_lookup_chunks_nil
49
+ assert_nil Webpacker.manifest.lookup_pack_with_chunks("foo.js")
50
+ end
51
+
52
+ def test_lookup_success
53
+ assert_equal Webpacker.manifest.lookup("bootstrap.js"), "/packs/bootstrap-300631c4f0e0f9c865bc.js"
54
+ end
55
+
56
+ def test_lookup_entrypoint_exception!
57
+ asset_file = "calendar"
58
+
59
+ error = assert_raises_manifest_missing_entry_error do
60
+ Webpacker.manifest.lookup_pack_with_chunks!(asset_file, type: :javascript)
61
+ end
62
+
63
+ assert_match "Webpacker can't find #{asset_file}.js in #{manifest_path}", error.message
64
+ end
65
+
66
+ def test_lookup_entrypoint
67
+ application_entrypoints = [
68
+ "/packs/vendors~application~bootstrap-c20632e7baf2c81200d3.chunk.js",
69
+ "/packs/vendors~application-e55f2aae30c07fb6d82a.chunk.js",
70
+ "/packs/application-k344a6d59eef8632c9d1.js"
71
+ ]
72
+
73
+ assert_equal Webpacker.manifest.lookup_pack_with_chunks!("application", type: :javascript), application_entrypoints
74
+ end
75
+
76
+ private
77
+
78
+ def assert_raises_manifest_missing_entry_error(&block)
79
+ error = nil
80
+ Webpacker.config.stub :compile?, false do
81
+ error = assert_raises Webpacker::Manifest::MissingEntryError, &block
82
+ end
83
+ error
84
+ end
85
+
86
+ def manifest_path
87
+ File.expand_path File.join(File.dirname(__FILE__), "test_app/public/packs", "manifest.json").to_s
88
+ end
89
+ end
@@ -0,0 +1,4 @@
1
+ require "bundler/setup"
2
+
3
+ APP_RAKEFILE = File.expand_path("test/dummy/Rakefile", __dir__)
4
+ load "rails/tasks/engine.rake"
@@ -0,0 +1,3 @@
1
+ require_relative "config/application"
2
+
3
+ Rails.application.load_tasks
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+ APP_PATH = File.expand_path("../config/application", __dir__)
3
+ require "rails/commands"
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+ require "rake"
3
+ Rake.application.run
@@ -0,0 +1,10 @@
1
+ require "action_controller/railtie"
2
+ require "action_view/railtie"
3
+ require "webpacker"
4
+
5
+ module TestDummyApp
6
+ class Application < Rails::Application
7
+ config.secret_key_base = "abcdef"
8
+ config.eager_load = true
9
+ end
10
+ end
@@ -0,0 +1,3 @@
1
+ require_relative "application"
2
+
3
+ Rails.application.initialize!
@@ -0,0 +1,75 @@
1
+ # Note: You must restart bin/webpacker-dev-server for changes to take effect
2
+
3
+ default: &default
4
+ source_path: app/packs
5
+ source_entry_path: entrypoints
6
+ public_output_path: packs
7
+ cache_path: tmp/webpacker
8
+
9
+ # Additional paths webpack should look up modules
10
+ # ['app/assets', 'engine/foo/app/assets']
11
+ additional_paths:
12
+ - app/assets
13
+ - /etc/yarn
14
+
15
+ # Reload manifest.json on all requests so we reload latest compiled packs
16
+ cache_manifest: false
17
+
18
+ extensions:
19
+ - .js
20
+ - .sass
21
+ - .scss
22
+ - .css
23
+ - .module.sass
24
+ - .module.scss
25
+ - .module.css
26
+ - .png
27
+ - .svg
28
+ - .gif
29
+ - .jpeg
30
+ - .jpg
31
+
32
+ development:
33
+ <<: *default
34
+ compile: true
35
+
36
+ # Reference: https://webpack.js.org/configuration/dev-server/
37
+ dev_server:
38
+ https: false
39
+ host: localhost
40
+ port: 3035
41
+ public: localhost:3035
42
+ hmr: false
43
+ # Inline should be set to true if using HMR
44
+ inline: true
45
+ overlay: true
46
+ disable_host_check: true
47
+ use_local_ip: false
48
+
49
+ test:
50
+ <<: *default
51
+ compile: true
52
+
53
+ # Compile test packs to a separate directory
54
+ public_output_path: packs-test
55
+
56
+ production:
57
+ <<: *default
58
+
59
+ # Production depends on precompilation of packs prior to booting for performance.
60
+ compile: false
61
+
62
+ # Cache manifest.json for performance
63
+ cache_manifest: true
64
+
65
+ staging:
66
+ <<: *default
67
+
68
+ # Production depends on precompilation of packs prior to booting for performance.
69
+ compile: false
70
+
71
+ # Cache manifest.json for performance
72
+ cache_manifest: true
73
+
74
+ # Compile staging packs to a separate directory
75
+ public_output_path: packs-staging
@@ -0,0 +1,5 @@
1
+ # This file allows the `Rails.root` to be correctly determined.
2
+
3
+ require_relative "config/environment"
4
+
5
+ run Rails.application
@@ -0,0 +1,7 @@
1
+ {
2
+ "private": true,
3
+ "dependencies": {
4
+ "@shakacode/shakapacker": "file:../../../../"
5
+ },
6
+ "license": "MIT"
7
+ }
@@ -0,0 +1,71 @@
1
+ require "test_helper"
2
+
3
+ class RakeTasksTest < Minitest::Test
4
+ def test_rake_tasks
5
+ output = Dir.chdir(test_app_path) { `rake -T` }
6
+ assert_includes output, "webpacker"
7
+ assert_includes output, "webpacker:check_binstubs"
8
+ assert_includes output, "webpacker:check_node"
9
+ assert_includes output, "webpacker:check_yarn"
10
+ assert_includes output, "webpacker:clean"
11
+ assert_includes output, "webpacker:clobber"
12
+ assert_includes output, "webpacker:compile"
13
+ assert_includes output, "webpacker:install"
14
+ assert_includes output, "webpacker:verify_install"
15
+ end
16
+
17
+ def test_rake_task_webpacker_check_binstubs
18
+ output = Dir.chdir(test_app_path) { `rake webpacker:check_binstubs 2>&1` }
19
+ refute_includes output, "webpack binstub not found."
20
+ end
21
+
22
+ def test_check_node_version
23
+ output = Dir.chdir(test_app_path) { `rake webpacker:check_node 2>&1` }
24
+ refute_includes output, "Webpacker requires Node.js"
25
+ end
26
+
27
+ def test_check_yarn_version
28
+ output = Dir.chdir(test_app_path) { `rake webpacker:check_yarn 2>&1` }
29
+ refute_includes output, "Yarn not installed"
30
+ refute_includes output, "Webpacker requires Yarn"
31
+ end
32
+
33
+ def test_rake_webpacker_yarn_install_in_non_production_environments
34
+ assert_includes test_app_dev_dependencies, "right-pad"
35
+
36
+ Webpacker.with_node_env("test") do
37
+ Dir.chdir(test_app_path) do
38
+ `bundle exec rake webpacker:yarn_install`
39
+ end
40
+ end
41
+
42
+ assert_includes installed_node_module_names, "right-pad",
43
+ "Expected dev dependencies to be installed"
44
+ end
45
+
46
+ def test_rake_webpacker_yarn_install_in_production_environment
47
+ Webpacker.with_node_env("production") do
48
+ Dir.chdir(test_app_path) do
49
+ `bundle exec rake webpacker:yarn_install`
50
+ end
51
+ end
52
+
53
+ refute_includes installed_node_module_names, "right-pad",
54
+ "Expected only production dependencies to be installed"
55
+ end
56
+
57
+ private
58
+ def test_app_path
59
+ File.expand_path("test_app", __dir__)
60
+ end
61
+
62
+ def test_app_dev_dependencies
63
+ package_json = File.expand_path("package.json", test_app_path)
64
+ JSON.parse(File.read(package_json))["devDependencies"]
65
+ end
66
+
67
+ def installed_node_module_names
68
+ node_modules_path = File.expand_path("node_modules", test_app_path)
69
+ Dir.chdir(node_modules_path) { Dir.glob("*") }
70
+ end
71
+ end
@@ -0,0 +1,3 @@
1
+ require_relative "config/application"
2
+
3
+ Rails.application.load_tasks
@@ -0,0 +1,10 @@
1
+ /* eslint no-console:0 */
2
+ // This file is automatically compiled by Webpack, along with any other files
3
+ // present in this directory. You're encouraged to place your actual application logic in
4
+ // a relevant structure within app/packs and only use these pack files to reference
5
+ // that code so it'll be compiled.
6
+ //
7
+ // To reference this file, add <%= javascript_pack_tag 'application' %> to the appropriate
8
+ // layout file, like app/views/layouts/application.html.erb
9
+
10
+ console.log('Hello World from Webpacker')
@@ -0,0 +1,4 @@
1
+ /*
2
+ * Dummy file #1 for Multi File Entry points: https://webpack.js.org/guides/entry-advanced/
3
+ * This file must be named the same
4
+ */
@@ -0,0 +1,4 @@
1
+ /*
2
+ * Dummy file #2 for Multi File Entry points: https://webpack.js.org/guides/entry-advanced/
3
+ * This file must be named the same
4
+ */