npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

vite-plugin-shopify-assets

v0.1.9

Published

Vite plugin to handle static assets in Vite Shopify themes

Downloads

378

Readme

Vite Plugin Shopify Assets

Created to be used alongside vite-plugin-shopify, for those who need to watch static assets, keeping the Shopify theme assets folder always in sync.

This plugin is not yet stable, so use at your own risk.

Please give your feedback, and send me any questions. A better documentation is in progress.

Installation

npm i -D vite-plugin-shopify-assets # npm
yarn add -D vite-plugin-shopify-assets # yarn

Basic Usage

Assumptions for the basic usage example:

  • Your Shopify theme folders are located in the root.
  • Your Vite publicDir config option is not set (will default to 'public').
  • You have a public/ folder, which holds all your static files, located in the root.
  • You have not added custom configuration to vite-plugin-shopify.
  • You have not added custom configuration to vite-plugin-shopify-assets.

In your vite.config.js file, you need to import the plugin:

// vite.config.js
import shopify from 'vite-plugin-shopify';
import shopifyAssets from 'vite-plugin-shopify-assets';

export default defineConfig({
  plugins: [shopify(), shopifyAssets()],
});

Based on the folder structure below...

my-shopify-project/
  ├── public/
  │   ├── static-script.js
  │   └── static-style.css
  ├── frontend/
  │   └── entrypoints/
  │       ├── main.ts
  │       └── style.css
  ├── assets/
  ├── config/
  ├── layout/
  ├── locales/
  ├── sections/
  ├── snippets/
  └── templates/

... Your assets folder when running vite build will look like this:

assets/
├── .vite/manifest.json
├── main-[HASH].js
├── style-[HASH].css
├── static-script.js
└── static-style.css

It works on dev, watch and build.

{
  "name": "my-shopify-project",
  "scripts": {
    "dev": "vite",
    "watch": "vite build --watch",
    "build": "vite build"
  }
}
npm run dev
# yarn watch
# pnpm build

Advanced usage

In your vite.config.js file, you can add custom configuration options:

// vite.config.js
import shopify from 'vite-plugin-shopify';
import shopifyAssets from 'vite-plugin-shopify-assets';

export default defineConfig({
  plugins: [
    // Barrel's vite-plugin-shopify
    shopify({
      themeRoot: 'theme',
      sourceCodeDir: 'frontend',
    }),

    // This plugin
    shopifyAssets({
      themeRoot: 'theme',
      publicDir: 'frontend/assets',
      targets: [
        // when targets are passed as strings, all target options use the default
        // Note: target sources are relative to publicDir
        'fonts/*.{woff,woff2,ttf,otf,svg}',
        'images/*.{jpg,jpeg,gif,png,webp,svg}',

        // when targets are passed as objects, you can specify options
        {
          // glob pattern for source assets
          src: '**/*.{js,liquid,text,md,json,css}',

          // glob patterns to ignore
          ignore: ['other-ignored/**/*', 'icons/**/*', 'images/**/*'],
        },

        {
          // when a non-static asset (eg: used in js) also needs to be copied as a static asset,
          // you can tell the plugin to find it elsewhere. Note: relative to publicDir
          src: '../icons/icon-*.svg',

          // the default destination is {themeRoot}/assets, but you can specify
          // a different source (relative to themeRoot)
          dest: 'snippets',

          // rename function, useful for making liquid snippets out of svg files for example
          rename: (file, ext, src) => `${file}.liquid`,

          // cleanMatch - USE WITH CAUTION:
          // glob pattern, relative to the dest folder, of files that should be cleaned/deleted
          // useful when the dest folder is not the default `{themeRoot}/assets` to avoid unused asset files
          // being shipped with the theme
          //
          // Only use this when you are certain that ALL files matching the pattern
          // have their source elsewhere and thus can be safely deleted
          cleanMatch: 'icon-*.liquid',
        },
      ],
    }),
  ],
});

The above configuration assumes the below folder structure:

my-shopify-project/
  ├── frontend/
  │   ├── assets/
  │   │   ├── fonts/
  │   │   │   └── static-font.woff
  │   │   ├── images/
  │   │   │   └── static-image.png
  │   │   ├── other/
  │   │   │   └── static-script-1.js
  │   │   └── static-script-2.js
  │   └── icons/
  │       └── icon-arrow.svg
  └── theme/
      ├── assets/
      ├── config/
      ├── layout/
      ├── locales/
      ├── sections/
      ├── snippets/
      └── templates/

Acknowledgements