vite_rails 1.0.8 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (71) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +27 -1
  3. data/CONTRIBUTING.md +0 -1
  4. data/README.md +57 -32
  5. data/lib/tasks/vite.rake +17 -0
  6. data/lib/vite_rails.rb +5 -93
  7. data/lib/vite_rails/config.rb +11 -100
  8. data/lib/vite_rails/engine.rb +7 -11
  9. data/lib/vite_rails/installation.rb +47 -0
  10. data/lib/vite_rails/tag_helpers.rb +61 -0
  11. data/lib/vite_rails/version.rb +2 -2
  12. data/{lib/install/config/vite.json → templates/config/rails-vite.json} +1 -0
  13. data/{lib/install/javascript → templates}/entrypoints/application.js +0 -0
  14. metadata +25 -129
  15. data/lib/install/bin/vite +0 -17
  16. data/lib/install/binstubs.rb +0 -6
  17. data/lib/install/config/vite.config.ts +0 -11
  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 -33
  21. data/lib/tasks/vite/clean.rake +0 -25
  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 -113
  28. data/lib/vite_rails/commands.rb +0 -68
  29. data/lib/vite_rails/dev_server.rb +0 -23
  30. data/lib/vite_rails/dev_server_proxy.rb +0 -49
  31. data/lib/vite_rails/helper.rb +0 -67
  32. data/lib/vite_rails/manifest.rb +0 -138
  33. data/lib/vite_rails/runner.rb +0 -56
  34. data/package.json +0 -16
  35. data/package/default.vite.json +0 -15
  36. data/test/builder_test.rb +0 -72
  37. data/test/command_test.rb +0 -35
  38. data/test/configuration_test.rb +0 -80
  39. data/test/dev_server_runner_test.rb +0 -83
  40. data/test/dev_server_test.rb +0 -39
  41. data/test/engine_rake_tasks_test.rb +0 -42
  42. data/test/helper_test.rb +0 -138
  43. data/test/manifest_test.rb +0 -75
  44. data/test/mode_test.rb +0 -21
  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/config/vite.json +0 -20
  53. data/test/mounted_app/test/dummy/package.json +0 -7
  54. data/test/rake_tasks_test.rb +0 -74
  55. data/test/test_app/Rakefile +0 -5
  56. data/test/test_app/app/javascript/entrypoints/application.js +0 -2
  57. data/test/test_app/app/javascript/entrypoints/multi_entry.css +0 -4
  58. data/test/test_app/app/javascript/entrypoints/multi_entry.js +0 -4
  59. data/test/test_app/bin/vite +0 -17
  60. data/test/test_app/config.ru +0 -7
  61. data/test/test_app/config/application.rb +0 -13
  62. data/test/test_app/config/environment.rb +0 -6
  63. data/test/test_app/config/vite.json +0 -20
  64. data/test/test_app/config/vite_public_root.yml +0 -20
  65. data/test/test_app/package.json +0 -13
  66. data/test/test_app/public/vite/manifest.json +0 -36
  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 -34
  70. data/test/vite_runner_test.rb +0 -59
  71. data/test/webpacker_test.rb +0 -15
@@ -1,138 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # Public: Registry for accessing resources managed by Vite, using a generated
4
- # manifest file which maps entrypoint names to file paths.
5
- #
6
- # Example:
7
- # lookup_entrypoint('calendar', type: :javascript)
8
- # => { "file" => "/vite/assets/calendar-1016838bab065ae1e314.js", "imports" => [] }
9
- #
10
- # NOTE: Using "autoBuild": true` in `config/vite.json` file will trigger a build
11
- # on demand as needed, before performing any lookup.
12
- class ViteRails::Manifest
13
- class MissingEntryError < StandardError
14
- end
15
-
16
- def initialize(vite_rails)
17
- @vite_rails = vite_rails
18
- end
19
-
20
- # Public: Strict version of lookup.
21
- #
22
- # Returns a relative path for the asset, or raises an error if not found.
23
- def lookup!(*args, **options)
24
- lookup(*args, **options) || missing_entry_error(*args, **options)
25
- end
26
-
27
- # Public: Computes the path for a given Vite asset using manifest.json.
28
- #
29
- # Returns a relative path, or nil if the asset is not found.
30
- #
31
- # Example:
32
- # ViteRails.manifest.lookup('calendar.js')
33
- # # { "file" => "/vite/assets/calendar-1016838bab065ae1e122.js", "imports" => [] }
34
- def lookup(name, type:)
35
- build if should_build?
36
-
37
- find_manifest_entry(with_file_extension(name, type))
38
- end
39
-
40
- # Public: Refreshes the cached mappings by reading the updated manifest.
41
- def refresh
42
- @manifest = load_manifest
43
- end
44
-
45
- private
46
-
47
- delegate :config, :builder, :dev_server_running?, to: :@vite_rails
48
-
49
- # NOTE: Auto compilation is convenient when running tests, when the developer
50
- # won't focus on the frontend, or when running the Vite server is not desired.
51
- def should_build?
52
- config.auto_build && !dev_server_running?
53
- end
54
-
55
- # Internal: Finds the specified entry in the manifest.
56
- def find_manifest_entry(name)
57
- if dev_server_running?
58
- { 'file' => "/#{ config.public_output_dir.join(name.to_s) }" }
59
- else
60
- manifest[name.to_s]
61
- end
62
- end
63
-
64
- # Internal: Performs a Vite build.
65
- def build
66
- ViteRails.logger.tagged('Vite') { builder.build }
67
- end
68
-
69
- # Internal: The parsed data from manifest.json.
70
- #
71
- # NOTE: When using build-on-demand in development and testing, the manifest
72
- # is reloaded automatically before each lookup, to ensure it's always fresh.
73
- def manifest
74
- return refresh if config.auto_build
75
-
76
- @manifest ||= load_manifest
77
- end
78
-
79
- # Internal: Returns a Hash with the entries in the manifest.json.
80
- def load_manifest
81
- if config.manifest_path.exist?
82
- JSON.parse(config.manifest_path.read).each do |_, entry|
83
- entry['file'] = within_public_output_dir(entry['file'])
84
- entry['imports'] = entry['imports']&.map { |path| within_public_output_dir(path) }
85
- end
86
- else
87
- {}
88
- end
89
- end
90
-
91
- # Internal: Adds a file extension to the file name, unless it already has one.
92
- def with_file_extension(name, entry_type)
93
- return name unless File.extname(name.to_s).empty?
94
-
95
- "#{ name }.#{ extension_for_type(entry_type) }"
96
- end
97
-
98
- # Internal: Scopes the paths in the manifest to the output folder in public.
99
- def within_public_output_dir(path)
100
- "/#{ config.public_output_dir.join(path) }"
101
- end
102
-
103
- # Internal: Allows to receive :javascript and :stylesheet as :type in helpers.
104
- def extension_for_type(entry_type)
105
- case entry_type
106
- when :javascript then 'js'
107
- when :stylesheet then 'css'
108
- when :typescript then dev_server_running? ? 'ts' : 'js'
109
- else entry_type.to_s
110
- end
111
- end
112
-
113
- # Internal: Raises a detailed message when an entry is missing in the manifest.
114
- def missing_entry_error(name, type: nil, **_options)
115
- file_name = with_file_extension(name, type)
116
- raise ViteRails::Manifest::MissingEntryError, <<~MSG
117
- Vite Rails can't find #{ file_name } in #{ config.manifest_path }.
118
-
119
- Possible causes:
120
- #{ missing_entry_causes.map { |cause| "\t- #{ cause }" }.join("\n") }
121
-
122
- Your manifest contains:
123
- #{ JSON.pretty_generate(@manifest) }
124
- MSG
125
- end
126
-
127
- def missing_entry_causes
128
- local = config.auto_build
129
- [
130
- (dev_server_running? && 'Vite has not yet re-built your latest changes.'),
131
- (local && !dev_server_running? && "\"autoBuild\": false in your #{ config.mode } configuration."),
132
- 'You have misconfigured config/vite.json file.',
133
- (!local && 'Assets have not been precompiled'),
134
- ].select(&:itself)
135
- rescue StandardError
136
- []
137
- end
138
- end
@@ -1,56 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # Public: Executes Vite commands, providing conveniences for debugging.
4
- class ViteRails::Runner
5
- def initialize(argv)
6
- detect_unsupported_switches!(argv)
7
- @argv = argv
8
- end
9
-
10
- # Public: Executes Vite with the specified arguments.
11
- def run
12
- execute_command(@argv.clone)
13
- end
14
-
15
- private
16
-
17
- UNSUPPORTED_SWITCHES = %w[--host --port --https --root -r --config -c]
18
- private_constant :UNSUPPORTED_SWITCHES
19
-
20
- # Internal: Allows to prevent configuration mistakes by ensuring the Rails app
21
- # and vite-plugin-ruby are using the same configuration for the dev server.
22
- def detect_unsupported_switches!(args)
23
- return unless (unsupported = UNSUPPORTED_SWITCHES & args).any?
24
-
25
- $stdout.puts "Please set the following switches in your vite.json instead: #{ unsupported }."
26
- exit!
27
- end
28
-
29
- # Internal: Executes the command with the specified arguments.
30
- def execute_command(args)
31
- cmd = vite_executable
32
- cmd.prepend('node', '--inspect-brk') if args.include?('--debug')
33
- cmd.prepend('node', '--trace-deprecation') if args.delete('--trace-deprecation')
34
- args.append('--mode', ENV['RAILS_ENV']) unless args.include?('--mode') || args.include?('-m')
35
- cmd += args
36
- puts cmd.join(' ')
37
- Dir.chdir(File.expand_path('.', Dir.pwd)) { Kernel.exec(ViteRails.env, *cmd) }
38
- end
39
-
40
- # Internal: Resolves to an executable for Vite.
41
- def vite_executable
42
- executable_exists?(path = vite_bin_path) ? [path] : %w[yarn vite]
43
- end
44
-
45
- # Internal: Only so that we can easily cover both paths in tests
46
- def executable_exists?(path)
47
- File.exist?(path)
48
- end
49
-
50
- # Internal: Returns a path where a Vite executable should be found.
51
- def vite_bin_path
52
- ENV["#{ ViteRails::ENV_PREFIX }_VITE_BIN_PATH"] || `yarn bin vite`.chomp
53
- rescue StandardError
54
- "#{ `npm bin`.chomp }/vite"
55
- end
56
- end
data/package.json DELETED
@@ -1,16 +0,0 @@
1
- {
2
- "name": "only-for-workflows",
3
- "version": "unknown",
4
- "scripts": {
5
- "docs": "npm -C docs run docs",
6
- "docs:build": "npm -C docs run docs:build",
7
- "docs:search": "npm -C docs run docs:search",
8
- "docs:lint": "npm -C docs run lint",
9
- "lint": "npm -C package run lint",
10
- "test": "npm -C package run test"
11
- },
12
- "dependencies": {
13
- "vite": "^2.0.0-beta.34",
14
- "vite-plugin-ruby": "^1.0.2"
15
- }
16
- }
@@ -1,15 +0,0 @@
1
- {
2
- "assetsDir": "assets",
3
- "autoBuild": false,
4
- "buildCacheDir": "tmp/cache/vite",
5
- "publicOutputDir": "vite",
6
- "configPath": "config/vite.json",
7
- "publicDir": "public",
8
- "entrypointsDir": "entrypoints",
9
- "sourceCodeDir": "app/frontend",
10
- "host": "localhost",
11
- "https": null,
12
- "port": 3036,
13
- "hideBuildConsoleOutput": false,
14
- "watchAdditionalPaths": []
15
- }
data/test/builder_test.rb DELETED
@@ -1,72 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'test_helper'
4
-
5
- class BuilderTest < Minitest::Test
6
- def remove_files_digest_path
7
- ViteRails.builder.send(:files_digest_path).tap do |path|
8
- path.delete if path.exist?
9
- end
10
- end
11
-
12
- def setup
13
- remove_files_digest_path
14
- end
15
-
16
- def teardown
17
- remove_files_digest_path
18
- end
19
-
20
- def test_custom_environment_variables
21
- assert_nil ViteRails.builder.send(:vite_env)['FOO']
22
- ViteRails.env['FOO'] = 'BAR'
23
- assert ViteRails.builder.send(:vite_env)['FOO'] == 'BAR'
24
- ensure
25
- ViteRails.env = {}
26
- end
27
-
28
- def test_freshness
29
- assert ViteRails.builder.stale?
30
- assert !ViteRails.builder.fresh?
31
- end
32
-
33
- def test_build
34
- assert !ViteRails.builder.build
35
- end
36
-
37
- def test_freshness_on_build_success
38
- status = OpenStruct.new(success?: true)
39
-
40
- assert ViteRails.builder.stale?
41
- Open3.stub :capture3, [:sterr, :stdout, status] do
42
- ViteRails.builder.build
43
- assert ViteRails.builder.fresh?
44
- end
45
- end
46
-
47
- def test_freshness_on_build_fail
48
- status = OpenStruct.new(success?: false)
49
-
50
- assert ViteRails.builder.stale?
51
- Open3.stub :capture3, [:sterr, :stdout, status] do
52
- ViteRails.builder.build
53
- assert ViteRails.builder.fresh?
54
- end
55
- end
56
-
57
- def test_files_digest_path
58
- assert_equal ViteRails.builder.send(:files_digest_path).basename.to_s, "last-compilation-digest-#{ ViteRails.config.mode }"
59
- end
60
-
61
- def test_external_env_variables
62
- ViteRails.env = {}
63
- assert_equal ViteRails.builder.send(:vite_env)['VITE_RUBY_MODE'], 'production'
64
- assert_equal ViteRails.builder.send(:vite_env)['VITE_RUBY_ROOT'], Rails.root.to_s
65
-
66
- ENV['VITE_RUBY_MODE'] = 'foo.bar'
67
- ENV['VITE_RUBY_ROOT'] = '/baz'
68
-
69
- assert_equal ViteRails.builder.send(:vite_env)['VITE_RUBY_MODE'], 'foo.bar'
70
- assert_equal ViteRails.builder.send(:vite_env)['VITE_RUBY_ROOT'], '/baz'
71
- end
72
- end
data/test/command_test.rb DELETED
@@ -1,35 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'test_helper'
4
-
5
- class CommandTest < Minitest::Test
6
- def test_compile_command_returns_success_status_when_stale
7
- ViteRails.builder.stub :stale?, true do
8
- ViteRails.builder.stub :run_vite, true do
9
- assert_equal true, ViteRails.commands.compile
10
- end
11
- end
12
- end
13
-
14
- def test_compile_command_returns_success_status_when_fresh
15
- ViteRails.builder.stub :stale?, false do
16
- ViteRails.builder.stub :run_vite, true do
17
- assert_equal true, ViteRails.commands.compile
18
- end
19
- end
20
- end
21
-
22
- def test_compile_command_returns_failure_status_when_stale
23
- ViteRails.builder.stub :stale?, true do
24
- ViteRails.builder.stub :run_vite, false do
25
- assert_equal false, ViteRails.commands.compile
26
- end
27
- end
28
- end
29
-
30
- def test_clean_command_works_with_nested_hashes_and_without_any_compiled_files
31
- File.stub :delete, true do
32
- assert ViteRails.commands.clean
33
- end
34
- end
35
- end
@@ -1,80 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'test_helper'
4
-
5
- class ConfigurationTest < ViteRails::Test
6
- def setup
7
- @config = ViteRails::Configuration.new(
8
- root_path: Pathname.new(File.expand_path('test_app', __dir__)),
9
- config_path: Pathname.new(File.expand_path('./test_app/config/vite.yml', __dir__)),
10
- env: 'production',
11
- )
12
- end
13
-
14
- def test_source_path
15
- source_path = File.expand_path File.join(File.dirname(__FILE__), 'test_app/app/javascript').to_s
16
- assert_equal source_path, @config.source_path.to_s
17
- end
18
-
19
- def test_source_entry_path
20
- source_entry_path = File.expand_path File.join(File.dirname(__FILE__), 'test_app/app/javascript', 'packs').to_s
21
- assert_equal @config.source_entry_path.to_s, source_entry_path
22
- end
23
-
24
- def test_public_root_path
25
- public_root_path = File.expand_path File.join(File.dirname(__FILE__), 'test_app/public').to_s
26
- assert_equal @config.public_path.to_s, public_root_path
27
- end
28
-
29
- def test_public_output_path
30
- public_output_path = File.expand_path File.join(File.dirname(__FILE__), 'test_app/public/packs').to_s
31
- assert_equal @config.public_output_path.to_s, public_output_path
32
-
33
- @config = ViteRails::Configuration.new(
34
- root_path: @config.root_path,
35
- config_path: Pathname.new(File.expand_path('./test_app/config/vite_public_root.yml', __dir__)),
36
- env: 'production',
37
- )
38
-
39
- public_output_path = File.expand_path File.join(File.dirname(__FILE__), 'public/packs').to_s
40
- assert_equal @config.public_output_path.to_s, public_output_path
41
- end
42
-
43
- def test_public_manifest_path
44
- public_manifest_path = File.expand_path File.join(File.dirname(__FILE__), 'test_app/public/packs', 'manifest.json').to_s
45
- assert_equal @config.public_manifest_path.to_s, public_manifest_path
46
- end
47
-
48
- def test_cache_path
49
- cache_path = File.expand_path File.join(File.dirname(__FILE__), 'test_app/tmp/cache/vite').to_s
50
- assert_equal @config.cache_path.to_s, cache_path
51
- end
52
-
53
- def test_additional_paths
54
- assert_equal @config.additional_paths, ['app/assets', '/etc/yarn', 'some.config.js', 'app/elm']
55
- end
56
-
57
- def test_cache_manifest?
58
- assert @config.cache_manifest?
59
-
60
- with_rails_env('development') do
61
- refute ViteRails.config.cache_manifest?
62
- end
63
-
64
- with_rails_env('test') do
65
- refute ViteRails.config.cache_manifest?
66
- end
67
- end
68
-
69
- def test_compile?
70
- refute @config.compile?
71
-
72
- with_rails_env('development') do
73
- assert ViteRails.config.compile?
74
- end
75
-
76
- with_rails_env('test') do
77
- assert ViteRails.config.compile?
78
- end
79
- end
80
- end
@@ -1,83 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'test_helper'
4
-
5
- class DevServerRunnerTest < ViteRails::Test
6
- def setup
7
- @original_node_env, ENV['NODE_ENV'] = ENV['NODE_ENV'], 'development'
8
- @original_rails_env, ENV['RAILS_ENV'] = ENV['RAILS_ENV'], 'development'
9
- end
10
-
11
- def teardown
12
- ENV['NODE_ENV'] = @original_node_env
13
- ENV['RAILS_ENV'] = @original_rails_env
14
- end
15
-
16
- def test_run_cmd_via_node_modules
17
- cmd = ["#{ test_app_path }/node_modules/.bin/vite", '--mode', 'development']
18
-
19
- verify_command(cmd, use_node_modules: true)
20
- end
21
-
22
- def test_run_cmd_via_yarn
23
- cmd = ['yarn', 'vite', '--mode', 'development']
24
-
25
- verify_command(cmd, use_node_modules: false)
26
- end
27
-
28
- def test_run_cmd_argv
29
- cmd = ["#{ test_app_path }/node_modules/.bin/vite", '--quiet', '--mode', 'development']
30
-
31
- verify_command(cmd, argv: ['--quiet'])
32
- end
33
-
34
- def test_run_cmd_argv_with_https
35
- cmd = ["#{ test_app_path }/node_modules/.bin/vite", '--https', '--mode', 'development']
36
-
37
- dev_server = ViteRails::DevServer.new({})
38
- def dev_server.host
39
- 'localhost'
40
- end
41
-
42
- def dev_server.port
43
- '3035'
44
- end
45
-
46
- def dev_server.pretty?
47
- false
48
- end
49
-
50
- def dev_server.https?
51
- true
52
- end
53
- ViteRails::DevServer.stub(:new, dev_server) do
54
- verify_command(cmd, argv: ['--https'])
55
- end
56
- end
57
-
58
- private
59
-
60
- def test_app_path
61
- File.expand_path('test_app', __dir__)
62
- end
63
-
64
- def verify_command(cmd, use_node_modules: true, argv: [])
65
- cwd = Dir.pwd
66
- Dir.chdir(test_app_path)
67
-
68
- klass = ViteRails::Runner
69
- instance = klass.new(argv)
70
- mock = Minitest::Mock.new
71
- mock.expect(:call, nil, [ViteRails.env, *cmd])
72
-
73
- klass.stub(:new, instance) do
74
- instance.stub(:executable_exists?, use_node_modules) do
75
- Kernel.stub(:exec, mock) { ViteRails.run(argv) }
76
- end
77
- end
78
-
79
- mock.verify
80
- ensure
81
- Dir.chdir(cwd)
82
- end
83
- end