Class: Geoblacklight::Assets::ViteGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Defined in:
lib/generators/geoblacklight/assets/vite_generator.rb

Instance Method Summary collapse

Instance Method Details

#add_javascriptObject

Replace the default generated Vite entrypoint with our own



76
77
78
79
# File 'lib/generators/geoblacklight/assets/vite_generator.rb', line 76

def add_javascript
  remove_file "app/javascript/entrypoints/application.js"
  copy_file "assets/application.js", "app/javascript/entrypoints/application.js"
end

#add_stylesheetsObject

Add our own stylesheets that reference the versions from npm



69
70
71
72
73
# File 'lib/generators/geoblacklight/assets/vite_generator.rb', line 69

def add_stylesheets
  copy_file "assets/_customizations.scss", "app/javascript/stylesheets/_customizations.scss"
  copy_file "assets/geoblacklight.scss", "app/javascript/stylesheets/geoblacklight.scss"
  copy_file "assets/application.scss", "app/javascript/entrypoints/application.scss"
end

#copy_config_vite_jsonObject

Copy Vite config files



35
36
37
38
# File 'lib/generators/geoblacklight/assets/vite_generator.rb', line 35

def copy_config_vite_json
  copy_file "vite.json", "config/vite.json"
  copy_file "vite.config.ts", "vite.config.ts"
end

#geoblacklight_base_layoutObject

Add our version of the Blacklight base layout with Vite helper tags



30
31
32
# File 'lib/generators/geoblacklight/assets/vite_generator.rb', line 30

def geoblacklight_base_layout
  copy_file "base.html.erb", "app/views/layouts/blacklight/base.html.erb"
end

#install_dev_dependenciesObject

The vite_rails gem doesn’t currently install the vite-plugin-rails node package, so we need to do that manually.



56
57
58
# File 'lib/generators/geoblacklight/assets/vite_generator.rb', line 56

def install_dev_dependencies
  run "yarn add --dev vite-plugin-rails"
end

#install_vite_railsObject

Install Vite



24
25
26
27
# File 'lib/generators/geoblacklight/assets/vite_generator.rb', line 24

def install_vite_rails
  gem "vite_rails", "~> 3.0"
  run "bundle install"
end

#pin_vite_plugin_rubyObject

The vite-plugin-ruby package has a breaking change in 5.1.2, so we need to resolve to a specific version to avoid issues. Remove after github.com/ElMassimo/vite_ruby/issues/586 is fixed.



48
49
50
51
52
# File 'lib/generators/geoblacklight/assets/vite_generator.rb', line 48

def pin_vite_plugin_ruby
  inject_into_file "package.json", before: "  \"dependencies\": {" do
    "  \"resolutions\": { \"vite-plugin-ruby\": \"5.1.1\" },\n"
  end
end

#setup_npm_scriptsObject

Remove generated npm scripts from rollup and replace with our own. Adds a shortcut so that ‘yarn build’ runs our vite pipeline No easy way to do this with yarn, so we use ‘npm pkg`…



63
64
65
66
# File 'lib/generators/geoblacklight/assets/vite_generator.rb', line 63

def setup_npm_scripts
  run "npm pkg delete scripts"
  run "npm pkg set scripts.build=\"vite build\""
end

#setup_viteObject

Run the vite install generator (create binstubs, etc.)



41
42
43
# File 'lib/generators/geoblacklight/assets/vite_generator.rb', line 41

def setup_vite
  run "bundle exec vite install"
end