vite_rails 1.0.0 → 1.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +9 -1
- data/README.md +69 -2
- data/lib/install/bin/vite +17 -0
- data/lib/install/config/vite.config.ts +11 -0
- data/lib/install/config/vite.json +14 -0
- data/lib/install/javascript/entrypoints/application.js +18 -0
- data/lib/install/template.rb +7 -29
- data/lib/tasks/vite/binstubs.rake +12 -0
- data/lib/tasks/vite/build.rake +33 -0
- data/lib/tasks/vite/clean.rake +25 -0
- data/lib/tasks/vite/clobber.rake +20 -0
- data/lib/tasks/vite/info.rake +21 -0
- data/lib/tasks/vite/install.rake +12 -0
- data/lib/tasks/vite/install_dependencies.rake +20 -0
- data/lib/tasks/vite/verify_install.rake +23 -0
- data/lib/vite_rails.rb +6 -1
- data/lib/vite_rails/config.rb +0 -2
- data/lib/vite_rails/dev_server_proxy.rb +6 -4
- data/lib/vite_rails/helper.rb +29 -8
- data/lib/vite_rails/manifest.rb +16 -12
- data/lib/vite_rails/version.rb +1 -1
- data/package.json +4 -20
- metadata +16 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c1036b7b19ba86a9e76a58b29bac36a0e919bdadeac59846a37a13f5f5d3690b
|
4
|
+
data.tar.gz: 51fc54fa2138c581ca9f92182e14faa2108cba30084656718f7792c3bf5947b4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 13857bbd2f3b308357b82663e140a1630699259274a5bab6c738c84444ab02aba67ef0b361a9598528f60186ec8a3748584885492026a36c928db77976ec7e5b
|
7
|
+
data.tar.gz: e15766ff9f599d4f0e18b2e1a30a3cd38182dc36efc479371c474f54ed0866c8451469952afc267f4247351592a3e6651010b2b5db28ac58e99e3bc60deef3f0
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,11 @@
|
|
1
|
-
## Vite Rails 1.0.
|
1
|
+
## Vite Rails 1.0.5 (2020-01-20)
|
2
|
+
|
3
|
+
- Automatically add `<link rel="modulepreload">` and `<link rel="stylesheet">` when using `vite_javascript_tag`, which simplifies usage.
|
4
|
+
|
5
|
+
## Vite Rails 1.0.4 (2020-01-19)
|
6
|
+
|
7
|
+
- Remove Vue specific examples from installation templates, to ensure they always run.
|
8
|
+
|
9
|
+
## Vite Rails 1.0.0 (2020-01-18)
|
2
10
|
|
3
11
|
Initial Version
|
data/README.md
CHANGED
@@ -22,6 +22,7 @@
|
|
22
22
|
[vite_rails]: https://github.com/ElMassimo/vite_rails
|
23
23
|
[webpacker]: https://github.com/rails/webpacker
|
24
24
|
[vite]: http://vitejs.dev/
|
25
|
+
[config file]: https://github.com/ElMassimo/vite_rails/blob/main/package/default.vite.json
|
25
26
|
|
26
27
|
[__Vite Rails__][vite_rails] allows you to use [Vite] to power the frontend.
|
27
28
|
|
@@ -32,11 +33,12 @@
|
|
32
33
|
- 🤖 Automatic Entrypoint Detection
|
33
34
|
- ⚡️ Hot Reload
|
34
35
|
- ⚙️ Rake Tasks
|
35
|
-
-
|
36
|
+
- 🤝 Integrated with <kbd>assets:precompile</kbd> and friends
|
37
|
+
- And more! (detects changes, and builds automatically if Vite is not running)
|
36
38
|
|
37
39
|
## Documentation 📖
|
38
40
|
|
39
|
-
|
41
|
+
A documentation website is coming soon!
|
40
42
|
|
41
43
|
## Installation 💿
|
42
44
|
|
@@ -55,6 +57,71 @@ bin/rake vite:install
|
|
55
57
|
|
56
58
|
This will generate configuration files and a sample setup.
|
57
59
|
|
60
|
+
## Usage 🚀
|
61
|
+
|
62
|
+
Drawing inspiration from [webpacker], any files in `app/javascript/entrypoints`
|
63
|
+
will be considered entries to your application (SPAs or pages).
|
64
|
+
|
65
|
+
These files will be detected, and passed on to Vite, all configuration is done
|
66
|
+
for you.
|
67
|
+
|
68
|
+
### Imports ⤵️
|
69
|
+
|
70
|
+
For convenience, a `~/` import alias is configured to `app/javascript`, allowing
|
71
|
+
you to use absolute paths:
|
72
|
+
|
73
|
+
```js
|
74
|
+
import { createApp } from 'vue'
|
75
|
+
import App from '~/App.vue'
|
76
|
+
import '~/channels'
|
77
|
+
|
78
|
+
createApp(App).mount('#app')
|
79
|
+
```
|
80
|
+
|
81
|
+
### Tags 🏷
|
82
|
+
|
83
|
+
`vite_typescript_tag`, `vite_javascript_tag`, and `vite_stylesheet_tag` can be
|
84
|
+
used to output `<script>` and `<link>` tags in your Rails layouts or templates.
|
85
|
+
|
86
|
+
```html
|
87
|
+
<head>
|
88
|
+
<title>Joie</title>
|
89
|
+
<%= csrf_meta_tags %>
|
90
|
+
<%= csp_meta_tag %>
|
91
|
+
|
92
|
+
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
|
93
|
+
<%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %>
|
94
|
+
|
95
|
+
<%= vite_stylesheet_tag 'strange' %>
|
96
|
+
<%= vite_typescript_tag 'application' %>
|
97
|
+
</head>
|
98
|
+
```
|
99
|
+
|
100
|
+
For other types of assets, you can use `vite_asset_path` and pass that to the appropriate tag helper.
|
101
|
+
|
102
|
+
## Configuration ⚙️
|
103
|
+
|
104
|
+
This is what your `config/vite.json` might look like:
|
105
|
+
|
106
|
+
```json
|
107
|
+
{
|
108
|
+
"all": {
|
109
|
+
"watchAdditionalPaths": []
|
110
|
+
},
|
111
|
+
"development": {
|
112
|
+
"autoBuild": true,
|
113
|
+
"publicOutputDir": "vite-dev",
|
114
|
+
"port": 3036
|
115
|
+
},
|
116
|
+
"test": {
|
117
|
+
"autoBuild": true,
|
118
|
+
"publicOutputDir": "vite-test"
|
119
|
+
}
|
120
|
+
}
|
121
|
+
```
|
122
|
+
|
123
|
+
Check [this file][config file] to see all config options, documentation is coming soon.
|
124
|
+
|
58
125
|
## Inspiration 💡
|
59
126
|
|
60
127
|
- [webpacker]
|
@@ -0,0 +1,17 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
ENV['RAILS_ENV'] ||= ENV['RACK_ENV'] || 'development'
|
5
|
+
ENV['NODE_ENV'] ||= ENV['RAILS_ENV'] == 'development' ? 'development' : 'production'
|
6
|
+
|
7
|
+
require 'pathname'
|
8
|
+
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', Pathname.new(__FILE__).realpath)
|
9
|
+
|
10
|
+
require 'bundler/setup'
|
11
|
+
|
12
|
+
require 'vite_rails'
|
13
|
+
|
14
|
+
APP_ROOT = File.expand_path('..', __dir__)
|
15
|
+
Dir.chdir(APP_ROOT) do
|
16
|
+
ViteRails.run(ARGV)
|
17
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
// Example: Load Rails libraries in Vite.
|
2
|
+
//
|
3
|
+
// import '@rails/ujs'
|
4
|
+
//
|
5
|
+
// import Turbolinks from 'turbolinks'
|
6
|
+
// import ActiveStorage from '@rails/activestorage'
|
7
|
+
//
|
8
|
+
// // Import all channels.
|
9
|
+
// import.meta.globEager('./**/*_channel.js')
|
10
|
+
//
|
11
|
+
// Turbolinks.start()
|
12
|
+
// ActiveStorage.start()
|
13
|
+
|
14
|
+
// Example: Import a stylesheet in app/javascript/index.css
|
15
|
+
// import '~/index.css'
|
16
|
+
|
17
|
+
console.log('Vite ⚡️ Rails')
|
18
|
+
|
data/lib/install/template.rb
CHANGED
@@ -1,14 +1,12 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
# Install
|
3
|
+
# Install Vite Rails
|
4
|
+
say 'Creating configuration files'
|
4
5
|
copy_file "#{ __dir__ }/config/vite.json", ViteRails.config.config_path
|
6
|
+
copy_file "#{ __dir__ }/config/vite.config.ts", Rails.root.join('vite.config.ts')
|
5
7
|
|
6
|
-
|
7
|
-
|
8
|
-
else
|
9
|
-
say 'Creating JavaScript app source directory'
|
10
|
-
directory "#{ __dir__ }/javascript", ViteRails.config.source_code_dir
|
11
|
-
end
|
8
|
+
say 'Creating entrypoints directory'
|
9
|
+
directory "#{ __dir__ }/javascript/entrypoints", ViteRails.config.source_code_dir.join(ViteRails.config.entrypoints_dir)
|
12
10
|
|
13
11
|
apply "#{ __dir__ }/binstubs.rb"
|
14
12
|
|
@@ -28,14 +26,6 @@ if git_ignore_path.exist?
|
|
28
26
|
}
|
29
27
|
end
|
30
28
|
|
31
|
-
install = if Rails.root.join('yarn.lock').exist?
|
32
|
-
'yarn add'
|
33
|
-
elsif Rails.root.join('pnpm-lock.yaml').exist?
|
34
|
-
'pnpm install'
|
35
|
-
else
|
36
|
-
'npm install'
|
37
|
-
end
|
38
|
-
|
39
29
|
Dir.chdir(Rails.root) do
|
40
30
|
say 'Installing JavaScript dependencies for Vite Rails'
|
41
31
|
package_json = File.read("#{ __dir__ }/../../package.json")
|
@@ -44,19 +34,7 @@ Dir.chdir(Rails.root) do
|
|
44
34
|
plugin_version = package_json.match(/"vite-plugin-ruby": "(.*)"/)[1]
|
45
35
|
|
46
36
|
say 'Installing vite as direct dependencies'
|
47
|
-
run "
|
48
|
-
end
|
49
|
-
|
50
|
-
if Rails::VERSION::MAJOR == 5 && Rails::VERSION::MINOR > 1
|
51
|
-
src = begin
|
52
|
-
"#{ ViteRails.config.protocol }://#{ ViteRails.config.host_with_port }"
|
53
|
-
rescue StandardError
|
54
|
-
'http://localhost:3036'
|
55
|
-
end
|
56
|
-
say 'You need to allow vite-dev-server host as allowed origin for connect-src.', :yellow
|
57
|
-
say 'This can be done in Rails 5.2+ for development environment in the CSP initializer', :yellow
|
58
|
-
say 'config/initializers/content_security_policy.rb with a snippet like this:', :yellow
|
59
|
-
say %(policy.connect_src :self, :https, "http://#{ src }", "ws://#{ src }" if Rails.env.development?), :yellow
|
37
|
+
run "yarn add vite@#{ vite_version } vite-plugin-ruby@#{ plugin_version }"
|
60
38
|
end
|
61
39
|
|
62
|
-
say '
|
40
|
+
say 'Vite ⚡️ Rails successfully installed! 🎉', :green
|
@@ -0,0 +1,12 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
binstubs_template_path = File.expand_path('../../install/binstubs.rb', __dir__).freeze
|
4
|
+
bin_path = ENV['BUNDLE_BIN'] || Rails.root.join('bin')
|
5
|
+
|
6
|
+
namespace :vite do
|
7
|
+
desc 'Installs Vite binstubs in this application'
|
8
|
+
task :binstubs do |task|
|
9
|
+
prefix = task.name.split(/#|vite:binstubs/).first
|
10
|
+
exec "#{ RbConfig.ruby } #{ bin_path }/rails #{ prefix }app:template LOCATION=#{ binstubs_template_path }"
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
$stdout.sync = true
|
4
|
+
|
5
|
+
def enhance_assets_precompile
|
6
|
+
Rake::Task['assets:precompile'] do |task|
|
7
|
+
prefix = task.name.split(/#|assets:precompile/).first
|
8
|
+
|
9
|
+
Rake::Task["#{ prefix }vite:build"].invoke
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
namespace :vite do
|
14
|
+
desc 'Compile JavaScript packs using vite for production with digests'
|
15
|
+
task build: [:'vite:verify_install', :environment] do
|
16
|
+
ViteRails.with_node_env(ENV.fetch('NODE_ENV', 'production')) do
|
17
|
+
ViteRails.ensure_log_goes_to_stdout do
|
18
|
+
ViteRails.build || exit!
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
# Compile packs after we've compiled all other assets during precompilation
|
25
|
+
skip_vite_precompile = %w[no false n f].include?(ENV['VITE_RUBY_PRECOMPILE'])
|
26
|
+
|
27
|
+
unless skip_vite_precompile
|
28
|
+
if Rake::Task.task_defined?('assets:precompile')
|
29
|
+
enhance_assets_precompile
|
30
|
+
else
|
31
|
+
Rake::Task.define_task('assets:precompile' => [:'vite:install_dependencies', :'vite:build'])
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
$stdout.sync = true
|
4
|
+
|
5
|
+
namespace :vite do
|
6
|
+
desc 'Remove old compiled vites'
|
7
|
+
task :clean, [:keep, :age] => [:'vite:verify_install', :environment] do |_, args|
|
8
|
+
ViteRails.ensure_log_goes_to_stdout do
|
9
|
+
ViteRails.clean(keep_up_to: Integer(args.keep || 2), age_in_seconds: Integer(args.age || 3600))
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
skip_vite_clean = %w[no false n f].include?(ENV['VITE_RUBY_PRECOMPILE'])
|
15
|
+
|
16
|
+
unless skip_vite_clean
|
17
|
+
# Run clean if the assets:clean is run
|
18
|
+
if Rake::Task.task_defined?('assets:clean')
|
19
|
+
Rake::Task['assets:clean'].enhance do
|
20
|
+
Rake::Task['vite:clean'].invoke
|
21
|
+
end
|
22
|
+
else
|
23
|
+
Rake::Task.define_task('assets:clean' => 'vite:clean')
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
namespace :vite do
|
4
|
+
desc 'Remove the vite build output directory'
|
5
|
+
task clobber: [:'vite:verify_install', :environment] do
|
6
|
+
ViteRails.clobber
|
7
|
+
$stdout.puts "Removed vite build output directory #{ ViteRails.config.build_output_dir }"
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
skip_vite_clobber = %w[no false n f].include?(ENV['VITE_RUBY_PRECOMPILE'])
|
12
|
+
|
13
|
+
unless skip_vite_clobber
|
14
|
+
# Run clobber if the assets:clobber is run
|
15
|
+
if Rake::Task.task_defined?('assets:clobber')
|
16
|
+
Rake::Task['assets:clobber'].enhance do
|
17
|
+
Rake::Task['vite:clobber'].invoke
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
namespace :vite do
|
4
|
+
desc "Provide information on ViteRails's environment"
|
5
|
+
task :info do
|
6
|
+
Dir.chdir(Rails.root) do
|
7
|
+
$stdout.puts "Ruby: #{ `ruby --version` }"
|
8
|
+
$stdout.puts "Rails: #{ Rails.version }"
|
9
|
+
$stdout.puts "ViteRails: #{ ViteRails::VERSION }"
|
10
|
+
$stdout.puts "Node: #{ `node --version` }"
|
11
|
+
$stdout.puts "Yarn: #{ `yarn --version` }"
|
12
|
+
|
13
|
+
$stdout.puts "\n"
|
14
|
+
$stdout.puts "vite-plugin-ruby: \n#{ `npm list vite-plugin-ruby version` }"
|
15
|
+
|
16
|
+
$stdout.puts "Is bin/vite present?: #{ File.exist? 'bin/vite' }"
|
17
|
+
$stdout.puts "Is bin/vite-dev-server present?: #{ File.exist? 'bin/vite-dev-server' }"
|
18
|
+
$stdout.puts "Is bin/yarn present?: #{ File.exist? 'bin/yarn' }"
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
install_template_path = File.expand_path('../../install/template.rb', __dir__).freeze
|
4
|
+
bin_path = ENV['BUNDLE_BIN'] || Rails.root.join('bin')
|
5
|
+
|
6
|
+
namespace :vite do
|
7
|
+
desc 'Install ViteRails in this application'
|
8
|
+
task :install do |task|
|
9
|
+
prefix = task.name.split(/#|vite:install/).first
|
10
|
+
exec "#{ RbConfig.ruby } #{ bin_path }/rails #{ prefix }app:template LOCATION=#{ install_template_path }"
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
namespace :vite do
|
4
|
+
desc 'Support for older Rails versions. Install all JavaScript dependencies as specified via Yarn'
|
5
|
+
task :install_dependencies do
|
6
|
+
valid_node_envs = %w[test development production]
|
7
|
+
node_env = ENV.fetch('NODE_ENV') { valid_node_envs.include?(Rails.env) ? Rails.env : 'production' }
|
8
|
+
Dir.chdir(Rails.root) do
|
9
|
+
install_command = if Rails.root.join('yarn.lock').exist?
|
10
|
+
v1 = `yarn --version`.start_with?('1')
|
11
|
+
"yarn install #{ v1 ? '--no-progress --frozen-lockfile' : '--immutable' }"
|
12
|
+
elsif Rails.root.join('pnpm-lock.yaml').exist?
|
13
|
+
'pnpm install'
|
14
|
+
else
|
15
|
+
'npm ci'
|
16
|
+
end
|
17
|
+
system({ 'NODE_ENV' => node_env }, install_command)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
namespace :vite do
|
4
|
+
desc 'Verifies if Vite Rails is installed'
|
5
|
+
task :verify_install do
|
6
|
+
unless File.exist?(Rails.root.join('bin/vite'))
|
7
|
+
warn <<~WARN
|
8
|
+
vite binstub not found.
|
9
|
+
Have you run rails vite:install?
|
10
|
+
Make sure the bin directory and bin/vite are not included in .gitignore
|
11
|
+
WARN
|
12
|
+
exit!
|
13
|
+
end
|
14
|
+
unless ViteRails.config.config_path.exist?
|
15
|
+
path = ViteRails.config.config_path.relative_path_from(Pathname.new(pwd)).to_s
|
16
|
+
warn <<~WARN
|
17
|
+
Configuration #{ path } file for vite-plugin-ruby not found.
|
18
|
+
Make sure vite:install has run successfully before running dependent tasks.
|
19
|
+
WARN
|
20
|
+
exit!
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
data/lib/vite_rails.rb
CHANGED
@@ -22,7 +22,7 @@ class ViteRails
|
|
22
22
|
cattr_accessor(:logger) { ActiveSupport::TaggedLogging.new(ActiveSupport::Logger.new(STDOUT)) }
|
23
23
|
|
24
24
|
class << self
|
25
|
-
delegate :config, :builder, :manifest, :commands, :dev_server, to: :instance
|
25
|
+
delegate :config, :builder, :manifest, :commands, :dev_server, :dev_server_running?, to: :instance
|
26
26
|
delegate :mode, to: :config
|
27
27
|
delegate :bootstrap, :clean, :clobber, :build, to: :commands
|
28
28
|
|
@@ -62,6 +62,11 @@ class ViteRails
|
|
62
62
|
end
|
63
63
|
end
|
64
64
|
|
65
|
+
# Public: Returns true if the Vite development server is running.
|
66
|
+
def dev_server_running?
|
67
|
+
ViteRails.run_proxy? && dev_server.running?
|
68
|
+
end
|
69
|
+
|
65
70
|
# Public: Current instance configuration for Vite.
|
66
71
|
def config
|
67
72
|
@config ||= ViteRails::Config.resolve_config
|
data/lib/vite_rails/config.rb
CHANGED
@@ -66,8 +66,6 @@ private
|
|
66
66
|
# Internal: Used to load a JSON file from the specified path.
|
67
67
|
def load_json(path)
|
68
68
|
JSON.parse(File.read(File.expand_path(path))).deep_transform_keys(&:underscore)
|
69
|
-
rescue => error
|
70
|
-
(require 'pry-byebug';binding.pry;);
|
71
69
|
end
|
72
70
|
|
73
71
|
# Internal: Retrieves a configuration option from environment variables.
|
@@ -14,7 +14,7 @@ class ViteRails::DevServerProxy < Rack::Proxy
|
|
14
14
|
|
15
15
|
# Rack: Intercept asset requests and send them to the Vite server.
|
16
16
|
def perform_request(env)
|
17
|
-
if vite_should_handle?(env['REQUEST_URI']) && dev_server.running?
|
17
|
+
if vite_should_handle?(env['REQUEST_URI'], env['HTTP_REFERER']) && dev_server.running?
|
18
18
|
env['REQUEST_URI'] = env['REQUEST_URI']
|
19
19
|
.sub(vite_asset_url_prefix, '/')
|
20
20
|
.sub('.ts.js', '.ts') # Patch: Rails helpers always append the extension.
|
@@ -36,9 +36,11 @@ private
|
|
36
36
|
|
37
37
|
delegate :config, :dev_server, to: :@vite_rails
|
38
38
|
|
39
|
-
def vite_should_handle?(url)
|
40
|
-
url.start_with?(vite_asset_url_prefix)
|
41
|
-
|
39
|
+
def vite_should_handle?(url, referer)
|
40
|
+
return true if url.start_with?(vite_asset_url_prefix) # Vite Asset
|
41
|
+
return true if url.start_with?(VITE_DEPENDENCY_PREFIX) # Vite Package Asset
|
42
|
+
return true if url.include?('?t=') # Hot Reload
|
43
|
+
return true if referer && URI.parse(referer).path.start_with?(vite_asset_url_prefix) # Entry Imported from another Entry.
|
42
44
|
end
|
43
45
|
|
44
46
|
def vite_asset_url_prefix
|
data/lib/vite_rails/helper.rb
CHANGED
@@ -2,6 +2,8 @@
|
|
2
2
|
|
3
3
|
# Public: Allows to render HTML tags for scripts and styles processed by Vite.
|
4
4
|
module ViteRails::Helper
|
5
|
+
DEFAULT_VITE_SKIP_PRELOAD_TAGS = Rails::VERSION::MAJOR <= 5 && Rails::VERSION::MINOR < 2
|
6
|
+
|
5
7
|
# Public: Returns the current Vite Rails instance.
|
6
8
|
def current_vite_instance
|
7
9
|
ViteRails.instance
|
@@ -12,30 +14,49 @@ module ViteRails::Helper
|
|
12
14
|
# Example:
|
13
15
|
# <%= vite_asset_path 'calendar.css' %> # => "/vite/assets/calendar-1016838bab065ae1e122.css"
|
14
16
|
def vite_asset_path(name, **options)
|
15
|
-
current_vite_instance.manifest.lookup!(name, **options)
|
17
|
+
current_vite_instance.manifest.lookup!(name, **options).fetch('file')
|
16
18
|
end
|
17
19
|
|
18
20
|
# Public: Renders a <script> tag for the specified Vite entrypoints.
|
19
|
-
def vite_javascript_tag(*names,
|
20
|
-
|
21
|
+
def vite_javascript_tag(*names,
|
22
|
+
type: 'module',
|
23
|
+
asset_type: :javascript,
|
24
|
+
skip_preload_tags: DEFAULT_VITE_SKIP_PRELOAD_TAGS,
|
25
|
+
skip_style_tags: false,
|
26
|
+
crossorigin: 'anonymous',
|
27
|
+
**options)
|
28
|
+
js_entries = names.map { |name| current_vite_instance.manifest.lookup!(name, type: asset_type) }
|
29
|
+
js_tags = javascript_include_tag(*js_entries.map { |entry| entry['file'] }, type: type, crossorigin: crossorigin, **options)
|
30
|
+
|
31
|
+
unless skip_preload_tags || ViteRails.dev_server.running?
|
32
|
+
preload_paths = js_entries.flat_map { |entry| entry['imports'] }.compact.uniq
|
33
|
+
preload_tags = preload_paths.map { |path| preload_link_tag(path, crossorigin: crossorigin) }
|
34
|
+
end
|
35
|
+
|
36
|
+
unless skip_style_tags || ViteRails.dev_server.running?
|
37
|
+
style_paths = names.map { |name| current_vite_instance.manifest.lookup(name, type: :stylesheet)&.fetch('file') }.compact
|
38
|
+
style_tags = stylesheet_link_tag(*style_paths)
|
39
|
+
end
|
40
|
+
|
41
|
+
safe_join [js_tags, preload_tags, style_tags]
|
21
42
|
end
|
22
43
|
|
23
44
|
# Public: Renders a <script> tag for the specified Vite entrypoints.
|
24
45
|
#
|
25
46
|
# NOTE: Because TypeScript is not a valid target in browsers, we only specify
|
26
47
|
# the ts file when running the Vite development server.
|
27
|
-
def vite_typescript_tag(*names,
|
28
|
-
|
48
|
+
def vite_typescript_tag(*names, **options)
|
49
|
+
vite_javascript_tag(*names, asset_type: :typescript, **options)
|
29
50
|
end
|
30
51
|
|
31
52
|
# Public: Renders a <link> tag for the specified Vite entrypoints.
|
32
53
|
def vite_stylesheet_tag(*names, **options)
|
33
|
-
stylesheet_link_tag(*
|
54
|
+
stylesheet_link_tag(*sources_from_vite_manifest(names, type: :stylesheet), **options)
|
34
55
|
end
|
35
56
|
|
36
57
|
private
|
37
58
|
|
38
|
-
def
|
39
|
-
names.
|
59
|
+
def sources_from_vite_manifest(names, type:)
|
60
|
+
names.map { |name| vite_asset_path(name, type: type) }
|
40
61
|
end
|
41
62
|
end
|
data/lib/vite_rails/manifest.rb
CHANGED
@@ -5,7 +5,7 @@
|
|
5
5
|
#
|
6
6
|
# Example:
|
7
7
|
# lookup_entrypoint('calendar', type: :javascript)
|
8
|
-
# => "/vite/assets/calendar-1016838bab065ae1e314.js"
|
8
|
+
# => { "file" => "/vite/assets/calendar-1016838bab065ae1e314.js", "imports" => [] }
|
9
9
|
#
|
10
10
|
# NOTE: Using "autoBuild": true` in `config/vite.json` file will trigger a build
|
11
11
|
# on demand as needed, before performing any lookup.
|
@@ -29,7 +29,8 @@ class ViteRails::Manifest
|
|
29
29
|
# Returns a relative path, or nil if the asset is not found.
|
30
30
|
#
|
31
31
|
# Example:
|
32
|
-
# ViteRails.manifest.lookup('calendar.js')
|
32
|
+
# ViteRails.manifest.lookup('calendar.js')
|
33
|
+
# # { "file" => "/vite/assets/calendar-1016838bab065ae1e122.js", "imports" => [] }
|
33
34
|
def lookup(name, type:)
|
34
35
|
build if should_build?
|
35
36
|
|
@@ -43,12 +44,7 @@ class ViteRails::Manifest
|
|
43
44
|
|
44
45
|
private
|
45
46
|
|
46
|
-
delegate :config, :builder, :
|
47
|
-
|
48
|
-
# Public: Returns true if the Vite development server is running.
|
49
|
-
def dev_server_running?
|
50
|
-
ViteRails.run_proxy? && dev_server.running?
|
51
|
-
end
|
47
|
+
delegate :config, :builder, :dev_server_running?, to: :@vite_rails
|
52
48
|
|
53
49
|
# NOTE: Auto compilation is convenient when running tests, when the developer
|
54
50
|
# won't focus on the frontend, or when running the Vite server is not desired.
|
@@ -59,9 +55,9 @@ private
|
|
59
55
|
# Internal: Finds the specified entry in the manifest.
|
60
56
|
def find_manifest_entry(name)
|
61
57
|
if dev_server_running?
|
62
|
-
"/#{ config.public_output_dir.join(name.to_s) }"
|
63
|
-
|
64
|
-
|
58
|
+
{ 'file' => "/#{ config.public_output_dir.join(name.to_s) }" }
|
59
|
+
else
|
60
|
+
manifest[name.to_s]
|
65
61
|
end
|
66
62
|
end
|
67
63
|
|
@@ -83,7 +79,10 @@ private
|
|
83
79
|
# Internal: Returns a Hash with the entries in the manifest.json.
|
84
80
|
def load_manifest
|
85
81
|
if config.manifest_path.exist?
|
86
|
-
JSON.parse(config.manifest_path.read)
|
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
|
87
86
|
else
|
88
87
|
{}
|
89
88
|
end
|
@@ -96,6 +95,11 @@ private
|
|
96
95
|
"#{ name }.#{ extension_for_type(entry_type) }"
|
97
96
|
end
|
98
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
|
+
|
99
103
|
# Internal: Allows to receive :javascript and :stylesheet as :type in helpers.
|
100
104
|
def extension_for_type(entry_type)
|
101
105
|
case entry_type
|
data/lib/vite_rails/version.rb
CHANGED
data/package.json
CHANGED
@@ -2,27 +2,11 @@
|
|
2
2
|
"name": "only-for-workflows",
|
3
3
|
"version": "unknown",
|
4
4
|
"scripts": {
|
5
|
-
"
|
6
|
-
"
|
5
|
+
"lint": "npm -C package run lint",
|
6
|
+
"test": "npm -C package run test"
|
7
7
|
},
|
8
8
|
"dependencies": {
|
9
|
-
"vite": "^2.0.0-beta.
|
10
|
-
"vite-plugin-ruby": "1.0.
|
11
|
-
},
|
12
|
-
"devDependencies": {
|
13
|
-
"@vitejs/plugin-vue": "^1.0.6",
|
14
|
-
"eslint": "^7.16.0",
|
15
|
-
"eslint-config-airbnb": "^18.2.0",
|
16
|
-
"eslint-config-prettier": "^7.1.0",
|
17
|
-
"eslint-plugin-import": "^2.22.1",
|
18
|
-
"eslint-plugin-jsx-a11y": "^6.3.1",
|
19
|
-
"eslint-plugin-react": "^7.21.4",
|
20
|
-
"jest": "^26.5.3"
|
21
|
-
},
|
22
|
-
"jest": {
|
23
|
-
"testRegex": "(/__tests__/.*|(\\.|/))\\.jsx?$",
|
24
|
-
"roots": [
|
25
|
-
"<rootDir>/package"
|
26
|
-
]
|
9
|
+
"vite": "^2.0.0-beta.34",
|
10
|
+
"vite-plugin-ruby": "^1.0.1"
|
27
11
|
}
|
28
12
|
}
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: vite_rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Máximo Mussini
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-01-
|
11
|
+
date: 2021-01-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -119,8 +119,20 @@ files:
|
|
119
119
|
- CONTRIBUTING.md
|
120
120
|
- LICENSE.txt
|
121
121
|
- README.md
|
122
|
+
- lib/install/bin/vite
|
122
123
|
- lib/install/binstubs.rb
|
124
|
+
- lib/install/config/vite.config.ts
|
125
|
+
- lib/install/config/vite.json
|
126
|
+
- lib/install/javascript/entrypoints/application.js
|
123
127
|
- lib/install/template.rb
|
128
|
+
- lib/tasks/vite/binstubs.rake
|
129
|
+
- lib/tasks/vite/build.rake
|
130
|
+
- lib/tasks/vite/clean.rake
|
131
|
+
- lib/tasks/vite/clobber.rake
|
132
|
+
- lib/tasks/vite/info.rake
|
133
|
+
- lib/tasks/vite/install.rake
|
134
|
+
- lib/tasks/vite/install_dependencies.rake
|
135
|
+
- lib/tasks/vite/verify_install.rake
|
124
136
|
- lib/vite_rails.rb
|
125
137
|
- lib/vite_rails/builder.rb
|
126
138
|
- lib/vite_rails/commands.rb
|
@@ -174,8 +186,8 @@ homepage: https://github.com/ElMassimo/vite_rails
|
|
174
186
|
licenses:
|
175
187
|
- MIT
|
176
188
|
metadata:
|
177
|
-
source_code_uri: https://github.com/ElMassimo/vite_rails/tree/v1.0.
|
178
|
-
changelog_uri: https://github.com/ElMassimo/vite_rails/blob/v1.0.
|
189
|
+
source_code_uri: https://github.com/ElMassimo/vite_rails/tree/v1.0.5
|
190
|
+
changelog_uri: https://github.com/ElMassimo/vite_rails/blob/v1.0.5/CHANGELOG.md
|
179
191
|
post_install_message:
|
180
192
|
rdoc_options: []
|
181
193
|
require_paths:
|