Shopify Vite Plugin でテーマ拡張機能の開発体験を改善する

Improve your theme extension development experience with the Shopify Vite Plugin

小笠原 京平

This article explains how to improve your theme extension development experience by implementing the Barrel-provided Shopify Vite Plugin.

Target audience

People who have experience developing theme extensions

Introduction

Shopify theme extensions (Theme app extensions) are a feature for adding functionality to the storefront when developing Shopify apps.

You can develop them using Liquid, JavaScript, CSS, and more, but compared with regular app development, the local development experience is not very good.

So this time, we will introduce the Shopify Vite Plugin to improve the development experience.

What is the Shopify Vite Plugin?

It is a Vite plugin provided by Barrel. It was created to improve the development experience by incorporating Vite into Shopify theme development, but theme extensions can also benefit from it.

Its main feature is that scripts built with Vite can be easily used from Liquid. It can also support hot reloading. (Hot reloading scripts is difficult if you simply incorporate Vite.)

A collection of sample projects for Shopify theme extensions is also available.

From here, we will introduce the setup based on the sample project provided by the project.

Installing the Shopify Vite Plugin

Environment

  • node: v18.12.0
  • npm: v8.19.2 (pnpm or yarn is also fine)

We will proceed on the assumption that you already have a theme extension directory. If you do not have one, create it by referring to the Shopify documentation.

Because we will install packages into a plain theme extension directory (example: extensions/theme-extension), first run npm init.

Next, install the required packages.

npm i -D vite vite vite-plugin-page-reload vite-plugin-shopify

Configuring vite.config.js

Create a vite.config.js file in your theme extension directory.

import { defineConfig } from 'vite'
import pageReload from 'vite-plugin-page-reload'
import shopify from 'vite-plugin-shopify'

export default defineConfig({
  plugins: [
    shopify(),
    pageReload('/tmp/extension.update', {
      delay: 1800,
    }),
  ],
})

.shopifyignore settings

Create a .shopifyignore file in your theme extension directory.

When deploying a theme extension (shopify app deploy), an error will occur if there are directories or files unrelated to the theme extension, so specify the following directories and files to ignore.

assets/*.json
frontend/
node_modules/
package.json
vite.config.js

Try it out

Creating an entry point

  1. Create a frontend/entrypoints directory in your theme extension directory
  2. Create an entry.js file inside the above directory
  3. Write the code to display Hello World in the entry.js file
function listener() {
  const helloWorld = document.createElement('p')
  helloWorld.textContent = 'Hello World'
  document.body.insertBefore(helloWorld, document.body.firstChild)
}

const options = { passive: true, once: true }
document.addEventListener('DOMContentLoaded', listener, options)

call entry point

Add the following to the top of blocks/star_rating.liquid, which already exists in the plain theme extension directory.

{% liquid
  render 'vite-tag' with 'entry.js'
-%}

Preview the theme extension

Run vite in your theme extension directory, then preview the theme extension as usual.

After confirming that Hello World is displayed at the top of the page, let's change the code in entry.js. It is OK if you can confirm that the changes are reflected by hot reloading!

Summary

We introduced how to install the Shopify Vite Plugin with the minimum necessary steps. What did you think?

As the official sample project appears to use Preact, I think it will be very useful when developing medium- to large-scale theme extensions.

We would like to actively adopt it in our apps as well.

👆️目次はShopifyアプリ「RuffRuff 目次作成」を利用

👆️Table of contents by the Shopify app RuffRuff Table of Contents

Back to blog
  • Shopifyで使える決済方法一覧|手数料・導入サービス・選び方

    [Complete Guide] A Roundup of Payment Methods A...

    Tsunライターチーム

    This article gives a detailed explanation of the many payment methods available on Shopify and their respective payment processing fees and transaction fees. We also introduce handy Shopify apps that...

    [Complete Guide] A Roundup of Payment Methods A...

    Tsunライターチーム

    This article gives a detailed explanation of the many payment methods available on Shopify and their respective payment processing fees and transaction fees. We also introduce handy Shopify apps that...

  • GMOイプシロンの手数料・決済方法・入金サイクル|Shopifyの条件も解説

    GMO Epsilon Fees, Payment Methods, and Payout C...

    Tsun ライターチーム

    GMO Epsilon payment methods and fees, organized by contract type. Covers Shopify-specific terms, payouts, authorization and capture deadlines, refunds, and cancellation. Also introduces ways to use apps to control which...

    GMO Epsilon Fees, Payment Methods, and Payout C...

    Tsun ライターチーム

    GMO Epsilon payment methods and fees, organized by contract type. Covers Shopify-specific terms, payouts, authorization and capture deadlines, refunds, and cancellation. Also introduces ways to use apps to control which...

  • Shopify向けSBペイメントサービス|対応決済・手数料・導入時の注意点

    SB Payment Service for Shopify: Supported Payme...

    Tsun ライターチーム

    This article organizes the payment methods and pricing available with SBPS for Shopify. Compare cost estimates that include fixed costs, authentication fees, and Shopify fees, along with capture deadlines and...

    SB Payment Service for Shopify: Supported Payme...

    Tsun ライターチーム

    This article organizes the payment methods and pricing available with SBPS for Shopify. Compare cost estimates that include fixed costs, authentication fees, and Shopify fees, along with capture deadlines and...

  • KOMOJUとShopifyの連携|決済方法・手数料・オーソリ期限

    Integrating KOMOJU with Shopify: Payment Method...

    Tsunライターチーム

    This guide lists the KOMOJU payment methods available on Shopify and their fees. It covers rates for physical goods and digital content, additional Shopify fees, setup steps, authorization periods, payouts,...

    Integrating KOMOJU with Shopify: Payment Method...

    Tsunライターチーム

    This guide lists the KOMOJU payment methods available on Shopify and their fees. It covers rates for physical goods and digital content, additional Shopify fees, setup steps, authorization periods, payouts,...

  • Shopifyのセキュリティ対策|決済・アプリ要件・バグバウンティ

    Shopify Security: Payments, App Requirements, a...

    Tsun ライターチーム

    For businesses considering Shopify, this article explains security safeguards for communications, customer information, payments, and apps, along with third-party assessments. Review the scope of protection, compensation requirements, operational preparations, and...

    Shopify Security: Payments, App Requirements, a...

    Tsun ライターチーム

    For businesses considering Shopify, this article explains security safeguards for communications, customer information, payments, and apps, along with third-party assessments. Review the scope of protection, compensation requirements, operational preparations, and...

  • Shopifyサーバーの費用・稼働率・障害時の対処法

    Shopify server costs, uptime, and how to respon...

    Tsun ライターチーム

    Shopify web hosting is included in the plan fee. This article organizes costs and the scope of management, the difference between official uptime figures and contractual guarantees, the checks to...

    Shopify server costs, uptime, and how to respon...

    Tsun ライターチーム

    Shopify web hosting is included in the plan fee. This article organizes costs and the scope of management, the difference between official uptime figures and contractual guarantees, the checks to...

1 of 6
  • AI仮想顧客の改善ツール「SimGym」を使ったら売上はどうなった?実行から3か月後までの記録

    What Happened to Sales After Using SimGym, an A...

    テキサス・アユミ

    A record of trying SimGym on the miso specialty store ドットミソ, from checking the proposals through carrying out changes to the look-back three months later. It covers how the order...

    What Happened to Sales After Using SimGym, an A...

    テキサス・アユミ

    A record of trying SimGym on the miso specialty store ドットミソ, from checking the proposals through carrying out changes to the look-back three months later. It covers how the order...

  • 【ストアインタビュー後編】やりながら育てるShopify。「野菜をMOTTO」の試行錯誤:モンマルシェ株式会社 河野雄士さん

    [Store Interview, Part 2] Building and Growing ...

    テキサス・アユミ

    "Even when e-commerce is the main battlefield, e-commerce isn't the goal." An in-depth look at our interview with Yuji Kono of Mon Marché, whose brand "Yasai wo MOTTO" grew its...

    [Store Interview, Part 2] Building and Growing ...

    テキサス・アユミ

    "Even when e-commerce is the main battlefield, e-commerce isn't the goal." An in-depth look at our interview with Yuji Kono of Mon Marché, whose brand "Yasai wo MOTTO" grew its...

  • 【ストアインタビュー前編】 ECが主戦場でも、ECが目的ではない。「野菜をMOTTO」が貫くEC戦略:モンマルシェ株式会社 河野雄士さん

    [Store Interview, Part 1] Even When E-Commerce ...

    テキサス・アユミ

    "Even when e-commerce is the main battlefield, e-commerce isn't the goal." Part 1 of our interview with Yuji Kono of Mon Marché, whose brand "Yasai wo MOTTO" grew its e-commerce...

    [Store Interview, Part 1] Even When E-Commerce ...

    テキサス・アユミ

    "Even when e-commerce is the main battlefield, e-commerce isn't the goal." Part 1 of our interview with Yuji Kono of Mon Marché, whose brand "Yasai wo MOTTO" grew its e-commerce...

1 of 3
  • Our Editorial Process

    For readers new to e-commerce and Shopify, we write our articles with care to explain difficult commerce and marketing terms, along with how everything works from building a store through to selling, as “gently,” “clearly,” and “accurately” as we can.

    Learn more 
  • About the information on this site

    The information published here is based on Tsun Inc.’s own experience and research. While we make every effort to publish information that is as “current” and “accurate” as possible, we cannot fully guarantee its contents. Tsun Inc. accepts no liability whatsoever for any damages arising from this site. If you find an error in the information published here, we would be grateful if you would contact Tsun Inc.