Volt Shopify Vite Plugin

website·Indexed 29 days ago

https://shopify-vite.netlify.app/

A Vite plugin designed to integrate Vite into Shopify development, providing fast Hot Module Replacement (HMR) and a streamlined developer experience for Shopify themes.

Tokens
2.4K
Snippets
17
Records
23
Agent score
75%

What's inside Volt

  1. Preload stylesheets in Shopify themes

    You can enable the preload parameter of the stylesheet_tag filter by passing preload_stylesheet: true to the vite-tag snippet. This should be used sparingly for render-blocking stylesheets.
    {% render 'vite-tag', entry: 'theme.scss', preload_stylesheet: true %}
  2. Manually install and setup Shopify Vite Plugin

    To manually integrate the Shopify Vite Plugin, ensure Node.js (16+) is installed. Initialize a project, install the dependencies, and configure the package.json. Note that the plugin is ESM-only; ensure your package.json contains "type": "module" or use .mjs/.mts extensions for config files.
    # Initialize project
    npm init -y
    
    # Install Vite and the Shopify plugin
    npm i -D vite vite-plugin-shopify

    package.json scripts

    { "type": "module", "scripts": { "dev": "vite dev", "build": "vite build" } }

  3. Clean up the assets folder while preserving third-party app files

    By default, Vite empties the outDir on build. To remove only the files generated by Vite and preserve files added by third-party apps in the assets/ folder, disable build.emptyOutDir and use the @driver-digital/vite-plugin-shopify-clean plugin.
    $ npm i -D @driver-digital/vite-plugin-shopify-clean
    import shopify from 'vite-plugin-shopify'
    import shopifyClean from '@driver-digital/vite-plugin-shopify-clean'
    
    export default {
      build: {
        emptyOutDir: false
      },
      plugins: [
        shopifyClean(),
        shopify()
      ]
    }
  4. Load Vite assets in Shopify themes using vite-tag

    The plugin provides a vite-tag snippet to load assets. Add this to the <head> of your theme's layout. In development, it enables Hot Module Replacement (HMR); in production, it serves hashed assets from the Shopify CDN. Use the named entry: parameter for compatibility with Shopify's strict Liquid parser.
    {% liquid
      # Recommended: New syntax for strict Liquid parser
      render 'vite-tag', entry: 'theme.scss'
      render 'vite-tag', entry: 'theme.ts'
    %}
    
    {# Legacy syntax (backward compatibility) #}
    {% render 'vite-tag' with 'theme.scss' %}
  5. Launch Shopify and Vite servers in parallel

    To run both the Shopify theme dev server and the Vite dev server simultaneously, use a tool like npm-run-all or concurrently within your package.json scripts.
    {
      "scripts": {
        "dev": "run-p -sr \"shopify:dev -- {@}\" \"vite:dev\" --",
        "shopify:dev": "shopify theme dev",
        "vite:dev": "vite",
        "shopify:push": "shopify theme push",
        "build": "vite build"
      },
      "devDependencies": {
        "npm-run-all": "^4.1.5",
        "vite": "^4.2.1",
        "vite-plugin-shopify": "^2.0.2"
      }
    }

    // Execution command: // $ npm run dev -- --store johns-apparel --live-reload full-page

  6. Quickly start Shopify Vite using the preset

    The fastest way to set up Shopify Vite is to use the official preset in a fresh Shopify theme. This automatically configures Vite and Tailwind CSS. You can use flags to skip specific installations.
    # Apply the preset
    npx @preset/cli apply barrel/shopify-vite --path preset
    
    # Optional flags:
    # --no-base (Skip Vite installation)
    # --no-tailwindcss (Skip Tailwind CSS installation)
  7. Integrate React with Shopify Vite

    To use React, install the @vitejs/plugin-react plugin and add it to your Vite configuration. Ensure JSX files use .jsx or .tsx extensions. The vite-tag snippet automatically includes the React refresh script during development.
    npm i -D @vitejs/plugin-react
    import { defineConfig } from 'vite'
    import shopify from 'vite-plugin-shopify'
    import react from '@vitejs/plugin-react'
    
    export default defineConfig({
      plugins: [
        shopify(),
        react()
      ]
    })
  8. Use ngrok as a tunnel workaround for Cloudflare errors

    If you experience Cloudflare tunnel errors, you can use ngrok to expose your local server. Start ngrok (e.g., ngrok http 5173), copy the provided ngrok-free.app URL, and pass it to the tunnel option in the shopify plugin configuration.
    import shopify from 'vite-plugin-shopify'
    
    export default {
      plugins: [
        shopify({
          tunnel: 'https://123abc.ngrok-free.app'
        })
      ]
    }
  9. Configure directory paths for Volt Shopify plugin

    Customize the directory structure for your Shopify theme development using the following options:

    • themeRoot (string, default: ./): The root path to your Shopify theme directory.
    • sourceCodeDir (string, default: frontend): The directory containing your front-end source code.
    • entrypointsDir (string, default: frontend/entrypoints): The directory containing your front-end entry points.
  10. Resolve CORS issues when fetching assets from Vite dev server

    If you encounter CORS errors while fetching assets from the Vite dev server, you can allow specific origins using the server.cors.origin option in vite.config.js. This allows you to specify a list of allowed domains or regex patterns.
    import shopify from 'vite-plugin-shopify'
    
    export default {
      plugins: [
        shopify()
      ],
      server: {
        cors: {
          origin: [
            /^https?:\/\/(?:(?:[^:]+\.)?localhost|127\.0\.0\.1|\[::1\])(?::\d+)?$/,
            'https://johns-apparel.myshopify.com',
            'https://johnsapparel.com'
          ]
        }
      }
    }
  11. Fix Chrome Local Network Access CORS errors in Theme Editor

    Chrome security updates may block public websites from requesting assets from private local servers (localhost). To fix this in the Shopify Theme Editor, enable the tunnel feature in the vite-plugin-shopify configuration to create a public HTTPS connection.

    Starting with v4.1.0, the plugin automatically sets server.cors defaults and configures server.allowedHosts when tunnel: true is enabled.

    export default defineConfig({
      plugins: [
        shopify({ tunnel: true }),
        tailwindcss()
      ],
      server: {
        allowedHosts: ['.trycloudflare.com'],
        cors: {
          origin: [
            /^https?:\/\/(?:(?:[^:]+\.)?localhost|127\.0\.0\.1|\[::1\])(?::\d+)?$/,
            /\.myshopify\.com$/
          ]
        }
      }
    })
    
  12. Manage asset entry points and loading in Volt

    Control how assets are bundled and loaded into the Shopify theme:

    • additionalEntrypoints (string[], default: []): An array of file paths or glob patterns for additional files to be used as entry points.
    • snippetFile (string, default: vite-tag.liquid): The name of the Liquid snippet file used to load assets.
    • versionNumbers (boolean, default: false): When enabled, appends version numbers to production-ready asset URLs within the snippetFile.