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_frontendObject

Pick a version of the frontend asset package and install it.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/generators/geoblacklight/assets/vite_generator.rb', line 46

def add_frontend
  # If a branch was specified (e.g. you are running a template.rb build
  # against a test branch), use the latest version available on npm
  if ENV["BRANCH"]
    run "yarn add @geoblacklight/frontend@latest"

  # Otherwise, pick the version from npm that matches our Geoblacklight
  # gem version
  else
    run "yarn add @geoblacklight/frontend@#{Geoblacklight::VERSION}"
  end

  # If in local development or CI, also create a link. This will make it so
  # changes made in the outer directory are picked up automatically.
  # `yarn link` has to have already been run in the outer directory first.
  run "yarn link @geoblacklight/frontend" if options[:test]
end

#add_javascriptObject

Replace the default generated Vite entrypoint with our own



86
87
88
89
# File 'lib/generators/geoblacklight/assets/vite_generator.rb', line 86

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



79
80
81
82
83
# File 'lib/generators/geoblacklight/assets/vite_generator.rb', line 79

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.



66
67
68
# File 'lib/generators/geoblacklight/assets/vite_generator.rb', line 66

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

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



73
74
75
76
# File 'lib/generators/geoblacklight/assets/vite_generator.rb', line 73

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