# frozen_string_literal: true # Internal: Adds all assets generated by Vite to the static_files list, so # that they are copied over to the built site. class Jekyll::Vite::Generator < Jekyll::Generator safe true priority :highest class ViteAssetFile < Jekyll::StaticFile # Override: Copy to the configured public_output_dir def cleaned_relative_path super.sub( ViteRuby.config.build_output_dir.relative_path_from(@site.source).to_s, ViteRuby.config.public_output_dir, ) end end # Internal: Set the mode based on which command was run. # Builds assets with Vite only if `jekyll build` was run. def generate(site) serving = site.config['serving'] ENV['JEKYLL_ENV'] ||= serving ? 'development' : 'production' generate_vite_build(site) unless serving end # Internal: Build all assets with Vite and add them to the site's static files. def generate_vite_build(site) ViteRuby.commands.build_from_task assets_dir = ViteRuby.config.build_output_dir.relative_path_from(site.source) files = Dir.chdir(ViteRuby.config.build_output_dir.to_s) { Dir.glob('**/*').select { |f| File.file?(f) } } site.static_files.concat(files.map { |file| ViteAssetFile.new(site, site.source, assets_dir, file) }) end end