vite_rails 1.0.10 → 2.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (69) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +25 -0
  3. data/README.md +55 -30
  4. data/lib/tasks/vite.rake +17 -0
  5. data/lib/vite_rails.rb +5 -82
  6. data/lib/vite_rails/config.rb +11 -135
  7. data/lib/vite_rails/engine.rb +7 -11
  8. data/lib/vite_rails/installation.rb +52 -0
  9. data/lib/vite_rails/tag_helpers.rb +61 -0
  10. data/lib/vite_rails/version.rb +2 -2
  11. data/{test/mounted_app/test/dummy/config/vite.json → templates/config/rails-vite.json} +1 -0
  12. data/{lib/install/javascript → templates}/entrypoints/application.js +0 -0
  13. metadata +25 -127
  14. data/lib/install/bin/vite +0 -17
  15. data/lib/install/binstubs.rb +0 -6
  16. data/lib/install/config/vite.config.ts +0 -11
  17. data/lib/install/config/vite.json +0 -14
  18. data/lib/install/template.rb +0 -40
  19. data/lib/tasks/vite/binstubs.rake +0 -12
  20. data/lib/tasks/vite/build.rake +0 -29
  21. data/lib/tasks/vite/clean.rake +0 -23
  22. data/lib/tasks/vite/clobber.rake +0 -20
  23. data/lib/tasks/vite/info.rake +0 -20
  24. data/lib/tasks/vite/install.rake +0 -12
  25. data/lib/tasks/vite/install_dependencies.rake +0 -20
  26. data/lib/tasks/vite/verify_install.rake +0 -23
  27. data/lib/vite_rails/builder.rb +0 -111
  28. data/lib/vite_rails/commands.rb +0 -109
  29. data/lib/vite_rails/dev_server.rb +0 -23
  30. data/lib/vite_rails/dev_server_proxy.rb +0 -57
  31. data/lib/vite_rails/helper.rb +0 -69
  32. data/lib/vite_rails/manifest.rb +0 -140
  33. data/lib/vite_rails/runner.rb +0 -53
  34. data/package.json +0 -16
  35. data/package/default.vite.json +0 -16
  36. data/test/builder_test.rb +0 -77
  37. data/test/commands_test.rb +0 -67
  38. data/test/config_test.rb +0 -133
  39. data/test/dev_server_proxy_test.rb +0 -101
  40. data/test/dev_server_test.rb +0 -9
  41. data/test/engine_rake_tasks_test.rb +0 -80
  42. data/test/helper_test.rb +0 -70
  43. data/test/manifest_test.rb +0 -79
  44. data/test/mode_test.rb +0 -16
  45. data/test/mounted_app/Rakefile +0 -6
  46. data/test/mounted_app/test/dummy/Rakefile +0 -5
  47. data/test/mounted_app/test/dummy/bin/rails +0 -5
  48. data/test/mounted_app/test/dummy/bin/rake +0 -5
  49. data/test/mounted_app/test/dummy/config.ru +0 -7
  50. data/test/mounted_app/test/dummy/config/application.rb +0 -12
  51. data/test/mounted_app/test/dummy/config/environment.rb +0 -5
  52. data/test/mounted_app/test/dummy/package.json +0 -8
  53. data/test/mounted_app/test/dummy/yarn.lock +0 -208
  54. data/test/rake_tasks_test.rb +0 -60
  55. data/test/runner_test.rb +0 -31
  56. data/test/test_app/Rakefile +0 -5
  57. data/test/test_app/app/frontend/entrypoints/application.js +0 -2
  58. data/test/test_app/bin/vite +0 -17
  59. data/test/test_app/config.ru +0 -7
  60. data/test/test_app/config/application.rb +0 -13
  61. data/test/test_app/config/environment.rb +0 -6
  62. data/test/test_app/config/vite.json +0 -18
  63. data/test/test_app/config/vite_additional_paths.json +0 -5
  64. data/test/test_app/config/vite_public_dir.json +0 -5
  65. data/test/test_app/package.json +0 -13
  66. data/test/test_app/public/vite-production/manifest.json +0 -22
  67. data/test/test_app/some.config.js +0 -0
  68. data/test/test_app/yarn.lock +0 -11
  69. data/test/test_helper.rb +0 -68
data/test/config_test.rb DELETED
@@ -1,133 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'test_helper'
4
-
5
- class ConfigTest < ViteRails::Test
6
- def expand_path(path)
7
- File.expand_path(Pathname.new(__dir__).join(path).to_s)
8
- end
9
-
10
- def assert_path(expected, actual)
11
- assert_equal expand_path(expected), actual.to_s
12
- end
13
-
14
- def assert_pathname(expected, actual)
15
- assert_equal Pathname.new(expand_path("test_app/#{ expected }")), actual
16
- end
17
-
18
- def resolve_config(mode: 'production', root: test_app_path, **attrs)
19
- ViteRails::Config.resolve_config(mode: mode, root: root, **attrs)
20
- end
21
-
22
- def setup
23
- @config = resolve_config
24
- end
25
-
26
- def test_source_code_dir
27
- assert_equal 'app/frontend', @config.source_code_dir
28
- end
29
-
30
- def test_entrypoints_dir
31
- assert_path 'test_app/app/frontend/entrypoints', @config.resolved_entrypoints_dir
32
- end
33
-
34
- def test_public_dir
35
- assert_equal 'public', @config.public_dir
36
- end
37
-
38
- def test_build_output_dir
39
- assert_path 'test_app/public/vite-production', @config.build_output_dir
40
-
41
- @config = resolve_config(config_path: 'config/vite_public_dir.json')
42
- assert_path 'public/vite', @config.build_output_dir
43
- end
44
-
45
- def test_manifest_path
46
- assert_path 'test_app/public/vite-production/manifest.json', @config.manifest_path
47
- end
48
-
49
- def test_build_cache_dir
50
- assert_path 'test_app/tmp/cache/vite', @config.build_cache_dir
51
- end
52
-
53
- def test_watch_additional_paths
54
- assert_equal [], @config.watch_additional_paths
55
- @config = resolve_config(config_path: 'config/vite_additional_paths.json')
56
- assert_equal ['config/*'], @config.watch_additional_paths
57
- end
58
-
59
- def test_auto_build
60
- refute @config.auto_build
61
-
62
- with_rails_env('development') do |config|
63
- assert config.auto_build
64
- end
65
-
66
- with_rails_env('test') do |config|
67
- assert config.auto_build
68
- end
69
-
70
- with_rails_env('staging') do |config|
71
- refute config.auto_build
72
- end
73
- end
74
-
75
- def test_protocol
76
- assert_equal 'http', @config.protocol
77
- end
78
-
79
- def test_host_with_port
80
- assert_equal 3036, @config.port
81
-
82
- with_rails_env('development') do |config|
83
- assert_equal 3535, config.port
84
- assert_equal 'localhost:3535', config.host_with_port
85
- end
86
- end
87
-
88
- def test_to_env
89
- env = @config.to_env
90
- assert_nil env['VITE_RUBY_ASSET_HOST']
91
-
92
- Rails.application.config.action_controller.asset_host = 'assets-cdn.com'
93
- env = refresh_config.to_env
94
- assert_equal env['VITE_RUBY_ASSET_HOST'], 'assets-cdn.com'
95
- ensure
96
- Rails.application.config.action_controller.asset_host = nil
97
- end
98
-
99
- def test_environment_vars
100
- ViteRails.env = {
101
- 'VITE_RUBY_AUTO_BUILD' => 'true',
102
- 'VITE_RUBY_HOST' => 'example.com',
103
- 'VITE_RUBY_PORT' => '1920',
104
- 'VITE_RUBY_HTTPS' => 'true',
105
- 'VITE_RUBY_CONFIG_PATH' => 'config/vite_additional_paths.json',
106
- 'VITE_RUBY_BUILD_CACHE_DIR' => 'tmp/vitebuild',
107
- 'VITE_RUBY_PUBLIC_DIR' => 'pb',
108
- 'VITE_RUBY_PUBLIC_OUTPUT_DIR' => 'ft',
109
- 'VITE_RUBY_ASSETS_DIR' => 'as',
110
- 'VITE_RUBY_SOURCE_CODE_DIR' => 'app',
111
- 'VITE_RUBY_ENTRYPOINTS_DIR' => 'frontend/entrypoints',
112
- 'VITE_RUBY_HIDE_BUILD_CONSOLE_OUTPUT' => 'true',
113
- }
114
- @config = resolve_config
115
- assert_equal true, @config.auto_build
116
- assert_equal 'example.com', @config.host
117
- assert_equal 1920, @config.port
118
- assert_equal true, @config.https
119
- assert_equal 'https', @config.protocol
120
- assert_equal 'config/vite_additional_paths.json', @config.config_path
121
- assert_pathname 'tmp/vitebuild', @config.build_cache_dir
122
- assert_equal 'pb', @config.public_dir
123
- assert_equal Pathname.new('ft'), @config.public_output_dir
124
- assert_pathname 'pb/ft', @config.build_output_dir
125
- assert_equal 'as', @config.assets_dir
126
- assert_equal 'app', @config.source_code_dir
127
- assert_equal 'frontend/entrypoints', @config.entrypoints_dir
128
- assert_pathname 'app/frontend/entrypoints', @config.resolved_entrypoints_dir
129
- assert_equal true, @config.hide_build_console_output
130
- ensure
131
- ViteRails.env = {}
132
- end
133
- end
@@ -1,101 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'test_helper'
4
-
5
- class DevServerProxyTest < ViteRails::Test
6
- include Rack::Test::Methods
7
-
8
- def app
9
- # Capture all changes to the env made by the proxy.
10
- capture_app = Rack::Builder.new.run(->(env) {
11
- [200, { 'Content-Type' => 'application/json' }, env.to_json]
12
- })
13
- # Avoid actually using the proxy.
14
- Rack::Proxy.define_method(:perform_request) { |env| capture_app.call(env) }
15
-
16
- ViteRails::DevServerProxy.new(capture_app)
17
- end
18
-
19
- def test_non_asset
20
- get_with_dev_server_running '/'
21
- assert_not_forwarded
22
- end
23
-
24
- def test_non_vite_asset
25
- get_with_dev_server_running '/example_import.js'
26
- assert_not_forwarded
27
- end
28
-
29
- def test_vite_asset
30
- get_with_dev_server_running '/vite-production/application.js'
31
- assert_forwarded to: '/application.js'
32
- end
33
-
34
- def test_vite_client
35
- get_with_dev_server_running '/@vite/client'
36
- assert_forwarded to: '/@vite/client'
37
- end
38
-
39
- def test_vite_import
40
- get_with_dev_server_running '/@fs//package/example/app/frontend/App.vue?import&t=1611322300214&vue&type=style&index=0&lang.css'
41
- assert_forwarded to: '/@fs//package/example/app/frontend/App.vue?import&t=1611322300214&vue&type=style&index=0&lang.css'
42
- end
43
-
44
- def test_hmr_for_stylesheet
45
- get_with_dev_server_running '/colored.css?t=1611322562923'
46
- assert_forwarded to: '/colored.css?t=1611322562923'
47
- end
48
-
49
- def test_hmr_for_imported_entrypoint
50
- get_with_dev_server_running '/colored.css?import&t=1611322562923'
51
- assert_forwarded to: '/colored.css?import&t=1611322562923'
52
- end
53
-
54
- def test_entrypoint_imported_from_entrypoint
55
- header 'Referer', 'http://localhost:3000/vite-production/application.js'
56
- get_with_dev_server_running '/example_import.js'
57
- assert_forwarded to: '/example_import.js'
58
- end
59
-
60
- def test_without_dev_server_running
61
- get '/vite-production/application.js'
62
- assert_not_forwarded
63
-
64
- get '/colored.css?import&t=1611322562923'
65
- assert_not_forwarded
66
-
67
- header 'Referer', 'http://localhost:3000/vite-production/application.js'
68
- get '/example_import.js'
69
- assert_not_forwarded
70
- end
71
-
72
- private
73
-
74
- def get_with_dev_server_running(*args)
75
- with_dev_server_running {
76
- get(*args)
77
- }
78
- end
79
-
80
- def assert_not_forwarded
81
- assert last_response.ok?
82
- env = JSON.parse(last_response.body)
83
- assert_nil env['HTTP_X_FORWARDED_HOST']
84
- assert_nil env['HTTP_X_FORWARDED_PORT']
85
- end
86
-
87
- def assert_forwarded(to: nil)
88
- assert last_response.ok?
89
- env = JSON.parse(last_response.body)
90
-
91
- assert_equal ViteRails.config.host, env['HTTP_X_FORWARDED_HOST']
92
- assert_equal ViteRails.config.port, Integer(env['HTTP_X_FORWARDED_PORT'])
93
-
94
- return unless to
95
-
96
- path, query = to.split('?')
97
- assert_equal path, env['PATH_INFO']
98
- assert_equal query, env['QUERY_STRING']
99
- assert_equal to, env['REQUEST_URI']
100
- end
101
- end
@@ -1,9 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'test_helper'
4
-
5
- class DevServerTest < ViteRails::Test
6
- def test_running?
7
- refute ViteRails.dev_server.running?
8
- end
9
- end
@@ -1,80 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'test_helper'
4
-
5
- class EngineRakeTasksTest < Minitest::Test
6
- def setup
7
- remove_vite_files
8
- end
9
-
10
- def teardown
11
- remove_vite_files
12
- end
13
-
14
- def test_tasks_mounted
15
- output = within_mounted_app { `bundle exec rake -T` }
16
- assert_includes output, 'app:vite'
17
- end
18
-
19
- def test_rake_tasks
20
- within_mounted_app { `bundle exec rake app:vite:binstubs` }
21
- assert vite_binstub_path.exist?
22
-
23
- within_mounted_app { `bundle exec rake app:vite:install` }
24
- assert vite_config_ts_path.exist?
25
- assert app_frontend_dir.exist?
26
-
27
- within_mounted_app { `bundle exec rake app:vite:build` }
28
- assert app_public_dir.exist?
29
- assert app_public_dir.join('manifest.json').exist?
30
- assert app_public_dir.join('assets').exist?
31
-
32
- within_mounted_app { `bundle exec rake app:vite:clean` }
33
- assert app_public_dir.join('manifest.json').exist? # Still fresh
34
-
35
- within_mounted_app { `bundle exec rake app:vite:clean[0,0]` }
36
- refute app_public_dir.join('manifest.json').exist?
37
-
38
- within_mounted_app { `bundle exec rake app:vite:clobber` }
39
- refute app_public_dir.exist?
40
- rescue Minitest::Assertion => error
41
- raise error unless Rails.gem_version >= Gem::Version.new('6.1.0')
42
- end
43
-
44
- private
45
-
46
- def within_mounted_app
47
- Dir.chdir(mounted_app_path) { yield }
48
- end
49
-
50
- def mounted_app_path
51
- Pathname.new(File.expand_path(__dir__)).join('mounted_app')
52
- end
53
-
54
- def root_dir
55
- mounted_app_path.join('test/dummy')
56
- end
57
-
58
- def vite_binstub_path
59
- root_dir.join('bin/vite')
60
- end
61
-
62
- def vite_config_ts_path
63
- root_dir.join('vite.config.ts')
64
- end
65
-
66
- def app_frontend_dir
67
- root_dir.join('app/frontend')
68
- end
69
-
70
- def app_public_dir
71
- root_dir.join('public/vite-dev')
72
- end
73
-
74
- def remove_vite_files
75
- vite_binstub_path.delete if vite_binstub_path.exist?
76
- vite_config_ts_path.delete if vite_config_ts_path.exist?
77
- app_frontend_dir.rmtree if app_frontend_dir.exist?
78
- app_public_dir.rmtree if app_public_dir.exist?
79
- end
80
- end
data/test/helper_test.rb DELETED
@@ -1,70 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'test_helper'
4
-
5
- class HelperTest < ActionView::TestCase
6
- include ViteRailsTestHelpers
7
-
8
- tests ViteRails::Helper
9
-
10
- attr_reader :request
11
-
12
- def setup
13
- @request = Class.new do
14
- def send_early_hints(links) end
15
-
16
- def base_url
17
- 'https://example.com'
18
- end
19
- end.new
20
- end
21
-
22
- def test_vite_client_tag
23
- assert_nil vite_client_tag
24
- with_dev_server_running {
25
- assert_equal '<script src="/@vite/client" type="module"></script>', vite_client_tag
26
- }
27
- end
28
-
29
- def test_vite_asset_path
30
- assert_equal '/vite-production/assets/colored.1173bfe0.js', vite_asset_path('colored.js')
31
- assert_equal '/vite-production/assets/colored.84277fd6.css', vite_asset_path('colored.css')
32
- with_dev_server_running {
33
- assert_equal '/vite-production/colored.js', vite_asset_path('colored.js')
34
- assert_equal '/vite-production/colored.css', vite_asset_path('colored.css')
35
- }
36
- end
37
-
38
- def test_vite_stylesheet_tag
39
- assert_equal %(<link rel="stylesheet" media="screen" href="/vite-production/assets/colored.84277fd6.css" />),
40
- vite_stylesheet_tag('colored')
41
-
42
- assert_equal vite_stylesheet_tag('colored'), vite_stylesheet_tag('colored.css')
43
-
44
- with_dev_server_running {
45
- assert_equal %(<link rel="stylesheet" media="screen" href="/vite-production/colored.css" />),
46
- vite_stylesheet_tag('colored')
47
-
48
- assert_equal vite_stylesheet_tag('colored'), vite_stylesheet_tag('colored.css')
49
- }
50
- end
51
-
52
- def test_vite_javascript_tag
53
- assert_equal [
54
- %(<script src="/vite-production/assets/application.a0ba047e.js" crossorigin="anonymous" type="module"></script>),
55
- %(<link rel="preload" href="/vite-production/assets/example_import.8e1fddc0.js" as="script" type="text/javascript" crossorigin="anonymous">),
56
- %(<link rel="stylesheet" media="screen" href="/vite-production/assets/application.cccfef34.css" />),
57
- ].join, vite_javascript_tag('application')
58
-
59
- assert_equal vite_javascript_tag('application'), vite_javascript_tag('application.js')
60
- assert_equal vite_javascript_tag('application'), vite_typescript_tag('application')
61
-
62
- with_dev_server_running {
63
- assert_equal %(<script src="/vite-production/application.js" crossorigin="anonymous" type="module"></script>),
64
- vite_javascript_tag('application')
65
-
66
- assert_equal %(<script src="/vite-production/application.ts" crossorigin="anonymous" type="module"></script>),
67
- vite_typescript_tag('application')
68
- }
69
- end
70
- end
@@ -1,79 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'test_helper'
4
-
5
- class ManifestTest < ViteRails::Test
6
- def test_lookup_exception!
7
- asset_file = 'calendar.js'
8
-
9
- error = assert_raises_manifest_missing_entry_error do
10
- ViteRails.manifest.lookup!(asset_file)
11
- end
12
-
13
- assert_match "Vite Rails can't find #{ asset_file } in #{ manifest_path }", error.message
14
- end
15
-
16
- def test_lookup_with_type_exception!
17
- asset_file = 'calendar'
18
-
19
- error = assert_raises_manifest_missing_entry_error do
20
- ViteRails.manifest.lookup!(asset_file, type: :javascript)
21
- end
22
-
23
- assert_match "Vite Rails can't find #{ asset_file }.js in #{ manifest_path }", error.message
24
- end
25
-
26
- def test_lookup_success!
27
- entry = {
28
- 'file' => '/vite-production/assets/application.a0ba047e.js',
29
- 'imports' => [
30
- '/vite-production/assets/example_import.8e1fddc0.js',
31
- ],
32
- }
33
- assert_equal entry, ViteRails.manifest.lookup!('application.js', type: :javascript)
34
- assert_equal entry, ViteRails.manifest.lookup!('application', type: :typescript)
35
- end
36
-
37
- def test_lookup_success_with_dev_server_running!
38
- entry = { 'file' => '/vite-production/application.js' }
39
- with_dev_server_running {
40
- assert_equal entry, ViteRails.manifest.lookup!('application.js')
41
- }
42
- entry = { 'file' => '/vite-production/application.ts' }
43
- with_dev_server_running {
44
- assert_equal entry, ViteRails.manifest.lookup!('application', type: :typescript)
45
- }
46
- end
47
-
48
- def test_lookup_nil
49
- assert_nil ViteRails.manifest.lookup('foo.js')
50
- end
51
-
52
- def test_lookup_success
53
- entry = { 'file' => '/vite-production/assets/application.cccfef34.css', 'imports' => nil }
54
- assert_equal entry, ViteRails.manifest.lookup('application.css')
55
- assert_equal entry, ViteRails.manifest.lookup('application.css', type: :stylesheet)
56
- assert_equal entry, ViteRails.manifest.lookup('application', type: :stylesheet)
57
- end
58
-
59
- def test_lookup_success_with_dev_server_running
60
- entry = { 'file' => '/vite-production/application.css' }
61
- with_dev_server_running {
62
- assert_equal entry, ViteRails.manifest.lookup('application', type: :stylesheet)
63
- }
64
- end
65
-
66
- private
67
-
68
- def assert_raises_manifest_missing_entry_error(&block)
69
- error = nil
70
- ViteRails.config.stub :auto_build, false do
71
- error = assert_raises ViteRails::Manifest::MissingEntryError, &block
72
- end
73
- error
74
- end
75
-
76
- def manifest_path
77
- File.expand_path File.join(File.dirname(__FILE__), 'test_app/public/vite-production', 'manifest.json').to_s
78
- end
79
- end