Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

== Unreleased

- NEW #2334 Vite script component: `padrino g project --script=vite` (@osbre)
- NEW #2331 Faster boot outside development: files are no longer tracked for reloading unless `Padrino::Reloader.enabled = true` (@osbre)
- WARN #2331 Reloading outside development (`enable :reload`, `Padrino.reload!` or `reload!` in the console) needs `Padrino::Reloader.enabled = true` before `Padrino.load!`. Without it, app files and controllers still reload, but changes to lib, models, shared and app prerequisites (e.g. app/models) are skipped with a warning
- FIX #2331 Excluding a constant from reloading no longer also excludes constants whose names merely start with it (e.g. excluding `F` excluded `Foo`); only the constant and its namespace are excluded, and symbols and classes are accepted too
Expand Down
2 changes: 1 addition & 1 deletion padrino-gen/README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ test:: none (default), bacon, shoulda, cucumber, rspec, minitest, testuni
renderer:: none (default), slim, erb, erubis, liquid, haml
stylesheet:: none (default), less, sass, scss
mock:: none (default), mocha, rr
script:: none (default), jquery, prototype, mootools, dojo
script:: none (default), jquery, prototype, mootools, dojo, vite
orm:: none (default), mongoid, activerecord, minirecord, sequel, ohm, dynamoid

In addition, you can generate projects based on existing templates:
Expand Down
78 changes: 78 additions & 0 deletions padrino-gen/lib/padrino-gen/generators/components/scripts/vite.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
VITE_JSON = <<~JSON unless defined?(VITE_JSON)
{
"development": {
"autoBuild": true,
"publicOutputDir": "vite-dev"
},
"test": {
"autoBuild": true,
"publicOutputDir": "vite-test"
}
}
JSON

VITE_CONFIG = <<~TS unless defined?(VITE_CONFIG)
import { defineConfig } from 'vite'
import RubyPlugin from 'vite-plugin-ruby'

export default defineConfig({
plugins: [
RubyPlugin(),
],
})
TS

VITE_PACKAGE_JSON = <<~JSON unless defined?(VITE_PACKAGE_JSON)
{
"private": true,
"type": "module",
"devDependencies": {
"vite": "^8.0.0",
"vite-plugin-ruby": "^5.2.5"
}
}
JSON

VITE_ENTRYPOINT = <<~JS unless defined?(VITE_ENTRYPOINT)
// Vite entrypoint, load it in your layout with `vite_javascript_tag 'application'`.
// Import other scripts and stylesheets from app/frontend here, e.g.:
// import '~/stylesheets/application.css'
console.log('Vite ⚡️ Padrino')
JS

VITE_RAKE = <<~RUBY unless defined?(VITE_RAKE)

require 'vite_padrino'
ViteRuby.install_tasks
RUBY

VITE_GITIGNORE = <<~GITIGNORE unless defined?(VITE_GITIGNORE)
/public/vite*
node_modules
*.local
GITIGNORE

VITE_GEMS = <<~RUBY unless defined?(VITE_GEMS)
gem 'vite_padrino', '~> 3.0'
# vite_padrino 3.0.0 calls ActiveSupport's `try`, the fix in https://github.com/ElMassimo/vite_ruby/pull/635 is pending
gem 'activesupport', '>= 3.1', require: 'active_support/core_ext/object/try'
RUBY

def setup_script
inject_into_file 'Gemfile', VITE_GEMS, after: "Component requirements\n"
initializer :VitePadrino
create_file destination_root('config/vite.json'), VITE_JSON
create_file destination_root('vite.config.ts'), VITE_CONFIG
package_json_exists = File.exist?(destination_root('package.json'))
create_file destination_root('package.json'), VITE_PACKAGE_JSON, skip: true
create_file destination_root('app/frontend/entrypoints/application.js'), VITE_ENTRYPOINT
create_file destination_root('Procfile.dev'), "web: bundle exec padrino start\nvite: bundle exec vite dev\n", skip: true
append_file destination_root('Rakefile'), VITE_RAKE
append_file destination_root('.gitignore'), VITE_GITIGNORE if File.exist?(destination_root('.gitignore'))
renderer = fetch_component_choice(:renderer).to_s
renderer = 'erb' unless %w[haml slim].include?(renderer)
tags = renderer == 'erb' ? ['<%= vite_client_tag %>', "<%= vite_javascript_tag 'application' %>"] : ['= vite_client_tag', "= vite_javascript_tag 'application'"]
install = package_json_exists ? 'npm install -D vite@^8.0.0 vite-plugin-ruby@^5.2.5' : 'npm install'
say "Run `#{install}` to install Vite, then add to the <head> of your layout (app/views/layouts/application.#{renderer}):", :yellow
say tags.map { |tag| " #{tag}" }.join("\n"), :yellow
end
21 changes: 21 additions & 0 deletions padrino-gen/test/test_component_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,27 @@ def teardown
end
end

describe 'vite script component' do
it 'should add vite to an existing project and keep its package.json' do
capture_io { generate(:project, 'sample_project', '--renderer=haml', "--root=#{@apptmp}") }
File.write("#{@apptmp}/sample_project/package.json", "{}\n")
out, = capture_io { generate(:component, '--script=vite', "-r=#{@apptmp}/sample_project") }
assert_match(/applying.*?vite.*?script/, out)
assert_match(/npm install -D vite@\^8\.0\.0 vite-plugin-ruby@\^5\.2\.5/, out)
assert_match(/application\.haml/, out)
assert_match(/^\s+= vite_javascript_tag 'application'/, out)
assert_equal "{}\n", File.read("#{@apptmp}/sample_project/package.json")
assert_match_in_file(/gem 'vite_padrino', '~> 3.0'/, "#{@apptmp}/sample_project/Gemfile")
assert_match_in_file(/register VitePadrino/, "#{@apptmp}/sample_project/app/app.rb")
assert_file_exists("#{@apptmp}/sample_project/app/frontend/entrypoints/application.js")
end

it 'should declare activesupport like padrino-admin does' do
capture_io { generate(:project, 'sample_project', '--script=vite', "--root=#{@apptmp}") }
assert_match_in_file(/gem 'activesupport', '>= 3.1'/, "#{@apptmp}/sample_project/Gemfile")
end
end

describe 'specified of same the component' do
it 'should does not change' do
capture_io { generate(:project, 'sample_project', '--script=jquery', "--root=#{@apptmp}") }
Expand Down
18 changes: 18 additions & 0 deletions padrino-gen/test/test_project_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,24 @@ def teardown
assert_file_exists("#{@apptmp}/sample_project/public/javascripts/dojo-ujs.js")
assert_file_exists("#{@apptmp}/sample_project/public/javascripts/application.js")
end

it 'should properly generate for vite' do
out, = capture_io { generate(:project, 'sample_project', "--root=#{@apptmp}", '--script=vite') }
assert_match(/applying.*?vite.*?script/, out)
assert_match(/vite_javascript_tag 'application'/, out)
assert_match(/Run `npm install`/, out)
assert_match(/<%= vite_client_tag %>/, out)
assert_match_in_file(/gem 'vite_padrino', '~> 3.0'/, "#{@apptmp}/sample_project/Gemfile")
assert_match_in_file(%r{gem 'activesupport', '>= 3.1', require: 'active_support/core_ext/object/try'}, "#{@apptmp}/sample_project/Gemfile")
assert_match_in_file(/register VitePadrino/, "#{@apptmp}/sample_project/app/app.rb")
assert_match_in_file(/"publicOutputDir": "vite-dev"/, "#{@apptmp}/sample_project/config/vite.json")
assert_match_in_file(/RubyPlugin\(\)/, "#{@apptmp}/sample_project/vite.config.ts")
assert_match_in_file(/"vite-plugin-ruby"/, "#{@apptmp}/sample_project/package.json")
assert_match_in_file(/vite dev/, "#{@apptmp}/sample_project/Procfile.dev")
assert_match_in_file(/ViteRuby.install_tasks/, "#{@apptmp}/sample_project/Rakefile")
assert_match_in_file(%r{/public/vite\*}, "#{@apptmp}/sample_project/.gitignore")
assert_file_exists("#{@apptmp}/sample_project/app/frontend/entrypoints/application.js")
end
end

describe 'the generator for test component' do
Expand Down
Loading