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



83
84
85
86
# File 'lib/generators/geoblacklight/assets/vite_generator.rb', line 83

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



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

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



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

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



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

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.



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

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

Symlink geoblacklight’s frontend assets in CI



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

def link_geoblacklight_package
  if options[:test]
    run "yarn link @geoblacklight/frontend"
  end
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.



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

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`…



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

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.)



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

def setup_vite
  run "bundle exec vite install"
end