better_vite_helper 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +38 -1
- data/lib/better_vite_helper/configuration.rb +2 -1
- data/lib/better_vite_helper/version.rb +1 -1
- data/lib/better_vite_helper/view_helpers.rb +17 -2
- data/lib/generators/better_vite_helper/install/install_generator.rb +104 -6
- data/lib/generators/better_vite_helper/install/templates/application.css +1 -0
- data/lib/generators/better_vite_helper/install/templates/application.js +10 -0
- data/lib/generators/better_vite_helper/install/templates/vite.config.js +1 -2
- metadata +3 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 3eb6dfcbfe19c34dd9712d350b6c3e8af4325ec07641a5dfe971c7fd49dc2989
|
|
4
|
+
data.tar.gz: 9c17d2898214eced091ab3f89ebb55cff85858f75ce2b5656016a6d4604a4546
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: f760f9cbfda00fd1a57630e9014b5199aa2e6f0a6a5fcc6b5d0e4900a64f328b4cb27aedd39bec9c444f2185d3c21ee20cbfe429f54efa616b7712bec1a7bfbd
|
|
7
|
+
data.tar.gz: d76dcf6b385f9e74f710b6a7b310ae51b050164645df5dcbda83c58f6e9f446e31c617534e44deb7b9066704bb6b011d65199f678e22961eb9806ac10ad278af
|
data/README.md
CHANGED
|
@@ -22,7 +22,26 @@ bundle install
|
|
|
22
22
|
bin/rails generate better_vite_helper:install
|
|
23
23
|
```
|
|
24
24
|
|
|
25
|
-
|
|
25
|
+
The generator automatically:
|
|
26
|
+
- Creates `package.json` if missing (with `dev` and `build` scripts)
|
|
27
|
+
- Installs Vite and Tailwind CSS v4 via yarn (or npm)
|
|
28
|
+
- Copies `vite.config.js` and `postcss.config.js`
|
|
29
|
+
- Copies `app/javascript/application.js` with image glob imports
|
|
30
|
+
- Copies `app/assets/stylesheets/application.css` with Tailwind CSS import
|
|
31
|
+
- Updates `app/views/layouts/application.html.erb` with Vite helpers
|
|
32
|
+
- Updates `.gitignore` with Node/Yarn/Vite entries
|
|
33
|
+
|
|
34
|
+
### Generator Options
|
|
35
|
+
|
|
36
|
+
```bash
|
|
37
|
+
bin/rails generate better_vite_helper:install --skip-install # Skip npm dependencies
|
|
38
|
+
bin/rails generate better_vite_helper:install --skip-layout # Skip layout modification
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
| Option | Description |
|
|
42
|
+
|--------|-------------|
|
|
43
|
+
| `--skip-install` | Skip installing Vite via yarn/npm |
|
|
44
|
+
| `--skip-layout` | Skip updating application layout with Vite helpers |
|
|
26
45
|
|
|
27
46
|
## Usage
|
|
28
47
|
|
|
@@ -44,6 +63,23 @@ This copies `vite.config.js` and `postcss.config.js` to your application.
|
|
|
44
63
|
<% end %>
|
|
45
64
|
```
|
|
46
65
|
|
|
66
|
+
### Image Helpers
|
|
67
|
+
|
|
68
|
+
```erb
|
|
69
|
+
<%# Generate img tag with Vite-resolved src %>
|
|
70
|
+
<%= vite_image_tag "logo.png", alt: "Logo", class: "logo" %>
|
|
71
|
+
|
|
72
|
+
<%# Subdirectory images %>
|
|
73
|
+
<%= vite_image_tag "icons/arrow.svg", alt: "Arrow" %>
|
|
74
|
+
|
|
75
|
+
<%# Get image path directly %>
|
|
76
|
+
<%= vite_image_path "logo.png" %>
|
|
77
|
+
<%# Dev: http://localhost:5173/app/assets/images/logo.png %>
|
|
78
|
+
<%# Prod: /assets/logo-abc123.png (with CDN if configured) %>
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
Images in `app/assets/images/` are automatically included in the Vite build via `import.meta.glob`.
|
|
82
|
+
|
|
47
83
|
### Configuration
|
|
48
84
|
|
|
49
85
|
```ruby
|
|
@@ -52,6 +88,7 @@ BetterViteHelper.configure do |config|
|
|
|
52
88
|
config.dev_server_url = "http://localhost:5173" # default, or ENV["VITE_DEV_SERVER_URL"]
|
|
53
89
|
config.manifest_path = Rails.root.join("public/assets/.vite/manifest.json") # default
|
|
54
90
|
config.asset_host = "https://cdn.example.com" # optional, falls back to Rails.application.config.asset_host
|
|
91
|
+
config.images_path = "app/assets/images" # default
|
|
55
92
|
end
|
|
56
93
|
```
|
|
57
94
|
|
|
@@ -2,13 +2,14 @@
|
|
|
2
2
|
|
|
3
3
|
module BetterViteHelper
|
|
4
4
|
class Configuration
|
|
5
|
-
attr_accessor :asset_host
|
|
5
|
+
attr_accessor :asset_host, :images_path
|
|
6
6
|
attr_writer :manifest_path, :dev_server_url
|
|
7
7
|
|
|
8
8
|
def initialize
|
|
9
9
|
@manifest_path = nil
|
|
10
10
|
@dev_server_url = nil
|
|
11
11
|
@asset_host = nil
|
|
12
|
+
@images_path = "app/assets/images"
|
|
12
13
|
end
|
|
13
14
|
|
|
14
15
|
def manifest_path
|
|
@@ -65,6 +65,15 @@ module BetterViteHelper
|
|
|
65
65
|
end
|
|
66
66
|
end
|
|
67
67
|
|
|
68
|
+
def vite_image_path(source)
|
|
69
|
+
full_path = resolve_image_path(source)
|
|
70
|
+
vite_asset_path(full_path)
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def vite_image_tag(source, **options)
|
|
74
|
+
image_tag(vite_image_path(source), **options)
|
|
75
|
+
end
|
|
76
|
+
|
|
68
77
|
def reset_vite_manifest_cache!
|
|
69
78
|
@vite_manifest = nil
|
|
70
79
|
end
|
|
@@ -79,7 +88,7 @@ module BetterViteHelper
|
|
|
79
88
|
base_url = resolved_asset_host || BetterViteHelper.configuration.dev_server_url
|
|
80
89
|
case entry_name
|
|
81
90
|
when "application.js"
|
|
82
|
-
"#{base_url}/app/
|
|
91
|
+
"#{base_url}/app/javascript/application.js"
|
|
83
92
|
when "application.css"
|
|
84
93
|
"#{base_url}/app/assets/stylesheets/application.css"
|
|
85
94
|
else
|
|
@@ -96,7 +105,7 @@ module BetterViteHelper
|
|
|
96
105
|
def resolve_manifest_key(entry_name)
|
|
97
106
|
case entry_name
|
|
98
107
|
when "application.js"
|
|
99
|
-
"app/
|
|
108
|
+
"app/javascript/application.js"
|
|
100
109
|
when "application.css"
|
|
101
110
|
"app/assets/stylesheets/application.css"
|
|
102
111
|
else
|
|
@@ -115,5 +124,11 @@ module BetterViteHelper
|
|
|
115
124
|
def resolved_asset_host
|
|
116
125
|
BetterViteHelper.configuration.asset_host || Rails.application.config.asset_host
|
|
117
126
|
end
|
|
127
|
+
|
|
128
|
+
def resolve_image_path(source)
|
|
129
|
+
return source if source.start_with?("app/", "/", "http://", "https://")
|
|
130
|
+
|
|
131
|
+
"#{BetterViteHelper.configuration.images_path}/#{source}"
|
|
132
|
+
end
|
|
118
133
|
end
|
|
119
134
|
end
|
|
@@ -1,13 +1,90 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require "rails/generators"
|
|
4
|
+
require "json"
|
|
4
5
|
|
|
5
6
|
module BetterViteHelper
|
|
6
7
|
module Generators
|
|
7
8
|
class InstallGenerator < Rails::Generators::Base
|
|
8
9
|
source_root File.expand_path("templates", __dir__)
|
|
9
10
|
|
|
10
|
-
desc "
|
|
11
|
+
desc "Installs Vite and configures your Rails application for BetterViteHelper"
|
|
12
|
+
|
|
13
|
+
class_option :skip_install, type: :boolean, default: false,
|
|
14
|
+
desc: "Skip installing npm dependencies"
|
|
15
|
+
class_option :skip_layout, type: :boolean, default: false,
|
|
16
|
+
desc: "Skip updating application layout"
|
|
17
|
+
|
|
18
|
+
def create_package_json_if_missing
|
|
19
|
+
return if File.exist?(File.join(destination_root, "package.json"))
|
|
20
|
+
|
|
21
|
+
content = {
|
|
22
|
+
"name" => File.basename(destination_root),
|
|
23
|
+
"private" => true,
|
|
24
|
+
"type" => "module",
|
|
25
|
+
"scripts" => {
|
|
26
|
+
"dev" => "vite --host 0.0.0.0 --port 5173",
|
|
27
|
+
"build" => "vite build"
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
create_file "package.json", JSON.pretty_generate(content) + "\n"
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def update_gitignore
|
|
34
|
+
gitignore_path = ".gitignore"
|
|
35
|
+
|
|
36
|
+
entries = <<~GITIGNORE
|
|
37
|
+
|
|
38
|
+
# Node/Yarn
|
|
39
|
+
/node_modules
|
|
40
|
+
|
|
41
|
+
# Logs
|
|
42
|
+
*.log
|
|
43
|
+
npm-debug.log*
|
|
44
|
+
yarn-debug.log*
|
|
45
|
+
yarn-error.log*
|
|
46
|
+
|
|
47
|
+
# Vite
|
|
48
|
+
/.vite
|
|
49
|
+
|
|
50
|
+
# Cache
|
|
51
|
+
.cache
|
|
52
|
+
.eslintcache
|
|
53
|
+
.stylelintcache
|
|
54
|
+
|
|
55
|
+
# TypeScript
|
|
56
|
+
*.tsbuildinfo
|
|
57
|
+
|
|
58
|
+
# Yarn PnP
|
|
59
|
+
/.pnp.*
|
|
60
|
+
/.yarn/*
|
|
61
|
+
!/.yarn/patches
|
|
62
|
+
!/.yarn/plugins
|
|
63
|
+
!/.yarn/releases
|
|
64
|
+
!/.yarn/sdks
|
|
65
|
+
!/.yarn/versions
|
|
66
|
+
GITIGNORE
|
|
67
|
+
|
|
68
|
+
if File.exist?(File.join(destination_root, gitignore_path))
|
|
69
|
+
append_to_file gitignore_path, entries
|
|
70
|
+
else
|
|
71
|
+
create_file gitignore_path, entries.strip + "\n"
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def install_vite_dependencies
|
|
76
|
+
return if options[:skip_install]
|
|
77
|
+
|
|
78
|
+
say "Installing Vite and PostCSS dependencies...", :green
|
|
79
|
+
|
|
80
|
+
deps = %w[vite @tailwindcss/postcss postcss tailwindcss autoprefixer]
|
|
81
|
+
|
|
82
|
+
if File.exist?(File.join(destination_root, "yarn.lock")) || !File.exist?(File.join(destination_root, "package-lock.json"))
|
|
83
|
+
run "yarn add -D #{deps.join(' ')}"
|
|
84
|
+
else
|
|
85
|
+
run "npm install --save-dev #{deps.join(' ')}"
|
|
86
|
+
end
|
|
87
|
+
end
|
|
11
88
|
|
|
12
89
|
def copy_vite_config
|
|
13
90
|
copy_file "vite.config.js", "vite.config.js"
|
|
@@ -17,15 +94,36 @@ module BetterViteHelper
|
|
|
17
94
|
copy_file "postcss.config.js", "postcss.config.js"
|
|
18
95
|
end
|
|
19
96
|
|
|
97
|
+
def copy_application_js
|
|
98
|
+
copy_file "application.js", "app/javascript/application.js"
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
def copy_application_css
|
|
102
|
+
copy_file "application.css", "app/assets/stylesheets/application.css"
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
def update_application_layout
|
|
106
|
+
return if options[:skip_layout]
|
|
107
|
+
|
|
108
|
+
layout_path = "app/views/layouts/application.html.erb"
|
|
109
|
+
return unless File.exist?(File.join(destination_root, layout_path))
|
|
110
|
+
|
|
111
|
+
gsub_file layout_path,
|
|
112
|
+
/<%=\s*stylesheet_link_tag\s+["']application["'].*%>/,
|
|
113
|
+
'<%= vite_stylesheet_link_tag "application.css" %>'
|
|
114
|
+
|
|
115
|
+
inject_into_file layout_path, before: "</body>" do
|
|
116
|
+
" <%= vite_javascript_include_tag \"application.js\" %>\n "
|
|
117
|
+
end
|
|
118
|
+
end
|
|
119
|
+
|
|
20
120
|
def show_post_install_message
|
|
21
121
|
say ""
|
|
22
122
|
say "BetterViteHelper installed successfully!", :green
|
|
23
123
|
say ""
|
|
24
|
-
say "
|
|
25
|
-
say "
|
|
26
|
-
say "
|
|
27
|
-
say " 3. Add your CSS entry point at app/assets/stylesheets/application.css"
|
|
28
|
-
say " 4. Run 'yarn vite' for development or 'yarn vite build' for production"
|
|
124
|
+
say "To start development:"
|
|
125
|
+
say " yarn dev # Start Vite dev server"
|
|
126
|
+
say " bin/rails server # Start Rails (in another terminal)"
|
|
29
127
|
say ""
|
|
30
128
|
end
|
|
31
129
|
end
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
@import "tailwindcss";
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
// Entry point per Vite
|
|
2
|
+
import "../assets/stylesheets/application.css";
|
|
3
|
+
|
|
4
|
+
// Import all images to include them in Vite manifest
|
|
5
|
+
// Images will be available via vite_image_tag helper
|
|
6
|
+
import.meta.glob("../assets/images/**/*.{png,jpg,jpeg,gif,svg,webp,avif,ico}", {
|
|
7
|
+
eager: true,
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
// Il tuo codice JavaScript qui
|
|
@@ -30,9 +30,8 @@ export default defineConfig({
|
|
|
30
30
|
input: {
|
|
31
31
|
application: resolve(
|
|
32
32
|
__dirname,
|
|
33
|
-
"app/
|
|
33
|
+
"app/javascript/application.js"
|
|
34
34
|
),
|
|
35
|
-
styles: resolve(__dirname, "app/assets/stylesheets/application.css"),
|
|
36
35
|
},
|
|
37
36
|
output: {
|
|
38
37
|
entryFileNames: "application-[hash].js",
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: better_vite_helper
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.2.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Umberto Peserico
|
|
@@ -48,6 +48,8 @@ files:
|
|
|
48
48
|
- lib/better_vite_helper/version.rb
|
|
49
49
|
- lib/better_vite_helper/view_helpers.rb
|
|
50
50
|
- lib/generators/better_vite_helper/install/install_generator.rb
|
|
51
|
+
- lib/generators/better_vite_helper/install/templates/application.css
|
|
52
|
+
- lib/generators/better_vite_helper/install/templates/application.js
|
|
51
53
|
- lib/generators/better_vite_helper/install/templates/postcss.config.js
|
|
52
54
|
- lib/generators/better_vite_helper/install/templates/vite.config.js
|
|
53
55
|
- lib/tasks/better_vite_helper_tasks.rake
|