### Run Local Development Server Source: https://github.com/ancez/static-site-builder/blob/main/README.md This command starts a local development server for your static site. The server automatically rebuilds the site and refreshes the browser when file changes are detected. It requires `bundle install` to be run first to install project dependencies. The server typically runs on port 3000, but this can be customized. ```bash cd my-site bundle install # Start development server (auto-rebuilds on file changes) rake dev:server ``` -------------------------------- ### Build Static Site Locally Source: https://github.com/ancez/static-site-builder/blob/main/guides/deployment-other.md Installs Ruby and Node.js dependencies, then builds all site assets locally. Ensure you have Ruby and Node.js installed. ```bash bundle install npm install bundle exec rake build:all ``` -------------------------------- ### Install ESBuild using npm Source: https://github.com/ancez/static-site-builder/blob/main/guides/setup-esbuild.md Installs ESBuild as a development dependency in your project using npm. This is the first step to enable JavaScript bundling and minification. ```bash npm install --save-dev esbuild ``` -------------------------------- ### Install Vercel CLI (Bash) Source: https://github.com/ancez/static-site-builder/blob/main/guides/deployment-vercel.md Installs the Vercel command-line interface globally using npm. This is an optional step if you prefer using the Vercel dashboard for deployment. ```bash npm i -g vercel ``` -------------------------------- ### Create a New Static Site Project Source: https://github.com/ancez/static-site-builder/blob/main/README.md This command initiates the creation of a new static site project using the static-site-builder CLI. It sets up the basic project structure and configuration files necessary for a new website. This is the starting point for any new project with this builder. ```bash static-site-builder new my-site ``` -------------------------------- ### Install Tailwind CSS (Bash) Source: https://github.com/ancez/static-site-builder/blob/main/guides/setup-tailwind.md Installs Tailwind CSS and its peer dependencies as development dependencies using npm. It then initializes Tailwind CSS by generating `tailwind.config.js` and `postcss.config.js`. ```bash npm install -D tailwindcss npx tailwindcss init ``` -------------------------------- ### ERB Partial Rendering Example Source: https://github.com/ancez/static-site-builder/blob/main/README.md Demonstrates how to render a partial view within an ERB template, including passing local variables. This follows Rails-like conventions for partials. ```erb <%= render partial: 'shared/header', locals: { title: 'Home' } %> ``` -------------------------------- ### Alternative Development Server with Listen Gem Source: https://context7.com/ancez/static-site-builder/llms.txt Provides an alternative development server implementation using the 'Listen' gem for efficient file watching. It starts both an HTTP server (WEBrick) and a WebSocket server for live reloading, offering a robust development environment. The server can be gracefully shut down. ```ruby require 'static_site_builder/dev_server' # Create and start development server dev_server = StaticSiteBuilder::DevServer.new( root: Dir.pwd, port: 3000, # HTTP server port ws_port: 3001 # WebSocket server port ) dev_server.start # Output: # Building site... # Compiling page: index.html... # ā Created index.html # # Starting development server... # HTTP server: http://localhost:3000 # WebSocket server: ws://localhost:3001 # Watching for changes... (Ctrl+C to stop) # DevServer features: # - Uses Listen gem for efficient file watching # - Watches app/ and config/ directories # - Rebuilds on .erb, .rb, .js file changes # - Starts WebSocket server for live reload # - Starts WEBrick HTTP server for serving dist/ # - Graceful shutdown on SIGINT/SIGTERM dev_server.stop # Cleanup when done ``` -------------------------------- ### Generate New Site with CLI Source: https://context7.com/ancez/static-site-builder/llms.txt Provides a command-line interface for generating new static site projects. Users can install the gem and use the 'static-site-builder new' command or run the generator script directly from the source repository. ```bash # Install the gem gem install static-site-builder # Generate a new site static-site-builder new my-site # Or use from source git clone https://github.com/Ancez/static-site-builder cd static-site-builder bundle install ruby bin/generate my-site # Generated site includes: # - ERB templates with ActionView support # - Live reload development server # - Automatic sitemap generation # - Helper auto-loading # - Asset management ``` -------------------------------- ### Deploy Project to Vercel (Bash) Source: https://github.com/ancez/static-site-builder/blob/main/guides/deployment-vercel.md Commands to build the project locally and then deploy it to Vercel's production environment. Ensure all dependencies are installed before running the build command. ```bash # Build locally first bundle install && npm install && bundle exec rake build:all # Deploy vercel --prod ``` -------------------------------- ### Install Static Site Builder Gem Source: https://github.com/ancez/static-site-builder/blob/main/README.md Installs the static-site-builder gem using RubyGems. This is the primary method for users who want to use the builder as a dependency. ```bash gem install static-site-builder # or git clone https://github.com/Ancez/static-site-builder cd static-site-builder bundle install ``` -------------------------------- ### Create New Static Site Project Source: https://github.com/ancez/static-site-builder/blob/main/README.md Creates a new static site project. This command can be run after installing the gem or by using the builder directly from its repository. ```bash gem install static-site-builder static-site-builder new my-site ``` ```bash git clone https://github.com/Ancez/static-site-builder cd static-site-builder bundle install ruby bin/generate my-site ``` -------------------------------- ### ERB Layout Template Example Source: https://github.com/ancez/static-site-builder/blob/main/README.md Defines a basic HTML layout template using ERB. This includes structure for head, body, stylesheets, and yielding page content. It also demonstrates how to handle JavaScript sections. ```erb
This is your generated static site.
<%= render partial: 'shared/header' %> # Looks in app/views/shared/_header.html.erb <%= render 'shared/footer' %> # Shorthand syntax # app/views/layouts/application.html.erb <%= display_meta_tags %> <%= yield %> <% if content_for?(:javascript) %> <%= yield(:javascript) %> <% end %> # Page templates can inject scripts into layout <% content_for(:javascript) do %> <% end %> # Builder automatically: # - Resolves partials relative to template directory # - Includes helpers from app/helpers/ # - Renders layout with yield for page content # - Adds template path annotations in development ``` -------------------------------- ### Generate New Static Site Project with Ruby Source: https://context7.com/ancez/static-site-builder/llms.txt Initializes a new static site project using the StaticSiteBuilder::Generator class. This creates a predefined project structure including Gemfile, Rakefile, configuration, and application directories. It requires the static_site_builder gem. ```ruby require "static_site_builder" # Generate a new site generator = StaticSiteBuilder::Generator.new("my-site") generator.generate # This creates the following structure: # my-site/ # āāā Gemfile # Dependencies (rake, actionview, webrick, sitemap_generator) # āāā Rakefile # Build tasks (build:all, build:html, build:css, dev:server) # āāā config/ # ā āāā sitemap.rb # Sitemap configuration # āāā app/ # ā āāā views/ # ā ā āāā layouts/application.html.erb # ā ā āāā index.html.erb # ā āāā helpers/ # Auto-loaded helper modules # ā āāā javascript/application.js # ā āāā assets/stylesheets/application.css # āāā lib/ # ā āāā site_builder.rb # Self-contained builder and WebSocket server # āāā public/ # Static files copied to dist/ # After generation: puts "\nNext steps:" puts " cd my-site" puts " bundle install" puts " bundle exec rake dev:server # Start development server" puts " bundle exec rake build:all # Build for deployment" ``` -------------------------------- ### Run Project Tests Source: https://github.com/ancez/static-site-builder/blob/main/README.md This command executes all tests within the static-site-builder project. It uses RSpec for unit, integration, and end-to-end testing. Running tests is crucial for ensuring the stability and correctness of the builder, especially when making changes or contributions. ```bash bundle exec rspec ``` -------------------------------- ### Helper Integration in Static Site Builder Source: https://context7.com/ancez/static-site-builder/llms.txt Automatically loads helper files (ending in _helper.rb) and includes them in the ActionView context. Module names are derived from file paths using Rails conventions. Helpers can be directly used in templates for dynamic content rendering. ```ruby # Builder automatically loads all *_helper.rb files and includes them # The module name is derived from the file name (Rails convention) # application_helper.rb -> ApplicationHelper # blog/post_helper.rb -> Blog::PostHelper # Use helpers directly in templatesPublished on <%= format_date(Date.today) %>
<%= nav_link "Home", "/" %> # Builder implementation: # 1. Scans app/helpers/ for *_helper.rb files # 2. Loads each file using load() for hot reloading # 3. Derives module name from file path # 4. Includes module in ActionView context ``` -------------------------------- ### Configure Sitemap Host in Ruby Source: https://github.com/ancez/static-site-builder/blob/main/README.md This code snippet shows how to set the default host for sitemap generation using the `SitemapGenerator` gem. This configuration is essential for correctly generating absolute URLs in your sitemap. It requires the `sitemap_generator` gem to be included in your project's `Gemfile`. ```ruby SitemapGenerator::Sitemap.default_host = 'https://yourdomain.com' ``` -------------------------------- ### Sitemap Generation Configuration Source: https://context7.com/ancez/static-site-builder/llms.txt Configuration for the `sitemap_generator` gem to automatically create a sitemap.xml file. It sets the default host, sitemap path, and public output directory. The code dynamically discovers ERB templates in `app/views/`, converts their paths to URLs, and adds them to the sitemap with metadata. ```ruby require 'sitemap_generator' require 'pathname' # Configure sitemap generator SitemapGenerator::Sitemap.default_host = 'https://example.com' SitemapGenerator::Sitemap.sitemaps_path = 'sitemaps' SitemapGenerator::Sitemap.public_path = 'dist' # Generate sitemap from all page templates # Automatically discovers pages in app/views/ (excluding layouts and partials) SitemapGenerator::Sitemap.create do views_dir = Pathname.new('app/views') if views_dir.exist? Dir.glob(views_dir.join('**', '*.html.erb')).each do |erb_file| relative_path = Pathname.new(erb_file).relative_path_from(views_dir) file_name = Pathname.new(erb_file).basename.to_s # Skip partials (start with _) and layouts if !file_name.start_with?('_') && !relative_path.to_s.start_with?('layouts/') page_name = relative_path.to_s.gsub(/\.html\.erb$/, '') # Convert to URL path # index -> / # blog/index -> /blog/ # about -> /about path = if page_name == 'index' '/' elsif page_name.end_with?('/index') "/#{page_name[0..-7]}/" else "/#{page_name}" end add path, lastmod: File.mtime(erb_file), changefreq: 'weekly', priority: 0.5 end end end end ``` -------------------------------- ### WebSocket Server for Live Reload Source: https://context7.com/ancez/static-site-builder/llms.txt Implements a WebSocket server responsible for broadcasting live reload events to connected clients. It operates by polling a specified file for changes and sending a 'reload' message when the file's modification time is updated. This enables real-time updates in the browser during development. ```ruby require 'static_site_builder/websocket_server' # Create WebSocket server for live reload ws_server = StaticSiteBuilder::WebSocketServer.new( port: 3001, reload_file: Pathname.new('.reload') ) ws_server.start # Starts in background threads: # - Accept thread: handles incoming WebSocket connections # - Watch thread: polls .reload file for changes (every 0.3s) # When builder writes to .reload file: File.write('.reload', Time.now.to_f.to_s) # WebSocket server: # 1. Detects .reload file mtime change # 2. Broadcasts "reload" message to all connected clients ``` -------------------------------- ### Cloudflare Pages Build and Deploy Commands Source: https://github.com/ancez/static-site-builder/blob/main/guides/deployment-cloudflare.md These commands define how your static site is built and deployed on Cloudflare Pages. The build command cleans and rebuilds the site into the 'dist/' directory, and the deploy command uploads these assets. ```bash bundle exec rake build:all ``` ```bash npx wrangler deploy --assets=./dist ``` ```bash npx wrangler versions upload --assets=./dist ``` -------------------------------- ### Include Bundled JavaScript in ERB Templates Source: https://github.com/ancez/static-site-builder/blob/main/guides/setup-esbuild.md Demonstrates how to include the bundled JavaScript file in your ERB templates using the `content_for` helper. This ensures the minified and bundled script is correctly referenced in the final HTML output. ```erb <% content_for :javascript do %> <% end %> ``` -------------------------------- ### Rake Build Tasks for Static Site Generation Source: https://context7.com/ancez/static-site-builder/llms.txt A set of Rake tasks to manage the static site build process. It includes tasks for cleaning the distribution directory, building assets (JavaScript, CSS), compiling HTML pages, and generating a sitemap. The 'build:all' task orchestrates the entire build process. ```ruby namespace :build do desc "Build everything (assets + HTML + CSS + sitemap)" task :all do Rake::Task['build:clean'].invoke Rake::Task['build:assets'].invoke Rake::Task['build:html'].invoke Rake::Task['build:css'].invoke Rake::Task['build:sitemap'].invoke puts "\nā Build complete!" end desc "Build JavaScript assets" task :assets do # If package.json with scripts.build exists: runs npm run build # Otherwise: copies app/javascript/ to dist/assets/javascripts/ end desc "Compile all pages to static HTML" task :html => [:assets] do builder = SiteBuilder::Builder.new(root: Dir.pwd) builder.build end desc "Build CSS (runs after HTML so dist directory exists)" task :css do # If package.json with scripts.build:css exists: runs npm run build:css # If tailwind.config.js exists: runs tailwindcss CLI # Otherwise: copies app/assets/stylesheets/ to dist/assets/stylesheets/ end desc "Clean dist directory" task :clean do dist_dir = Pathname.new(Dir.pwd).join('dist') FileUtils.rm_rf(dist_dir) if dist_dir.exist? end desc "Generate sitemap from actual pages" task :sitemap do require './config/sitemap' end end ``` -------------------------------- ### Compile ERB Templates to Static HTML with Ruby Source: https://context7.com/ancez/static-site-builder/llms.txt Compiles ERB templates into static HTML files, including asset management and optional template file name annotations. The builder outputs the compiled site to a 'dist/' directory. It requires the static_site_builder gem and can be configured with options like 'annotate_template_file_names'. Environment variables like 'PRODUCTION' can control build behavior. ```ruby require "static_site_builder" # Configure the builder builder = StaticSiteBuilder::Builder.new( root: Dir.pwd, annotate_template_file_names: true # Adds HTML comments showing template sources ) # Build the site (outputs to dist/) builder.build # Output: # Building static site... # Copying assets... # Compiling page: index.html... # ā Created index.html # Copying static files from public/... # # ā Build complete! Output in /path/to/dist # Annotations are auto-enabled in development (LIVE_RELOAD=true) # Production builds clean dist directory first (PRODUCTION=true) ENV["PRODUCTION"] = "true" builder.build ``` -------------------------------- ### Build CSS with Tailwind (Bash) Source: https://github.com/ancez/static-site-builder/blob/main/guides/setup-tailwind.md Compiles your Tailwind CSS for production using the `--minify` flag for a smaller file size. It takes an input CSS file and outputs a minified CSS file to the specified distribution directory. ```bash tailwindcss -i ./app/assets/stylesheets/application.css -o ./dist/assets/stylesheets/application.css --minify ``` -------------------------------- ### Enable Template Path Annotations in Ruby Source: https://context7.com/ancez/static-site-builder/llms.txt This snippet demonstrates how to enable template path annotations within a Ruby application using the StaticSiteBuilder::Builder. Annotations are useful for debugging template rendering in development environments and are automatically removed in production builds. This can be enabled manually or automatically when LIVE_RELOAD is set. ```ruby builder = StaticSiteBuilder::Builder.new( root: Dir.pwd, annotate_template_file_names: true ) ENV["LIVE_RELOAD"] = "true" ``` -------------------------------- ### Client-side JavaScript for Live Reloading Source: https://context7.com/ancez/static-site-builder/llms.txt This JavaScript snippet is injected by the builder during development to establish a WebSocket connection. When a 'reload' message is received from the server, it triggers a page reload in the browser. It includes logic to automatically reconnect on connection close. ```javascript (function() { function connect() { var ws = new WebSocket('ws://localhost:3001'); ws.onmessage = function(e) { if (e.data === 'reload') window.location.reload(); }; ws.onclose = function() { setTimeout(connect, 500); }; ws.onerror = function() {}; } connect(); })(); ``` -------------------------------- ### Define Rake Task for JavaScript Build Source: https://github.com/ancez/static-site-builder/blob/main/guides/setup-esbuild.md Adds a Rake task named `build_js` to your `Rakefile`. This task executes the ESBuild configuration script using Node.js, automating the JavaScript build process. ```ruby task :build_js do sh "node esbuild.config.js" end ``` -------------------------------- ### Meta Tags Integration with meta-tags gem Source: https://context7.com/ancez/static-site-builder/llms.txt Integrates the 'meta-tags' gem to manage SEO-friendly meta tags. This functionality is available even without a full Rails application, using an ActionController stub. Meta tags are set in ERB templates and rendered in the layout. ```erb <%# The Builder includes meta-tags gem with ActionController stub # This allows using meta-tags in standalone ActionView (no Rails needed) %> <%# app/views/index.html.erb %> <% set_meta_tags title: 'My Site - Home', description: 'A great static site', keywords: 'ruby, static site', og: { title: 'My Site', type: 'website', url: 'https://example.com', image: 'https://example.com/image.jpg' }, twitter: { card: 'summary_large_image', site: '@mysite' } %> <%# Layout renders meta tags %> <%= display_meta_tags %> <%# Outputs: