Editor’s note: This article was last updated 12 August 2022 to reflect changes to petite-vue
, including bundle size.
According to Evan You, the creator of Vue, petite-vue
is an alternative distribution of Vue inspired by Alpine that is optimized for progressive enhancement. For developers who are familiar with Vue and want to add Vue to a project that renders on the server side, petite-vue
offers the perfect approach.
In this article, we’ll explore what petite-vue
does, how it works, and how it compares to both standard Vue and Alpine. We’ll also cover getting started with petite-vue
and look at a few of its use cases.
petite-vue
petite-vue
compares to Alpinepetite-vue
exclusive features
petite-vue
petite-vue
Progressive enhancement is a methodology that allows a web developer to begin programming with HTML and include other technologies as needed. You can begin building a website statically with just HTML, then add interactivity or client states to pages.
petite-vue
is optimized for small interactions on existing HTML pages that are rendered by a server framework, thereby simplifying progressive enhancement. Now, let’s see how it works.
petite-vue
To understand how petite-vue
works, we have to consider its fundamental features.
With petite-vue
, you don’t have to worry about build tooling. Instead, you can simply include it in a script
tag to get its features in an HTML page:
<script src="https://unpkg.com/petite-vue" defer init></script> <!-- anywhere on the page --> <div v-scope="{ count: 0 }"> {{ count }} <button @click="count++">inc</button> </div>
At the time of writing, the latest versions of Vue and Alpine have respective bundle sizes of 34.2kB and 13.5kB minified and gzipped. petite-vue
, on the other hand, has a bundle size of only 6.9kB and was intended to be lightweight.
A developer who is already familiar with the Vue template syntax will find it easy to transition between Vue and petite-vue
. As a subset of Vue itself, petite-vue
uses most of the same syntax of Vue. For example, petite-vue
uses template interpolation like {{ count }}
and template event listeners like @click
.
Unlike Vue, React, and most other frontend libraries and frameworks, petite-vue
does not use the virtual DOM. Instead, it mutates the DOM in place. Therefore, petite-vue
runs on a webpage and does not need a compiler, decreasing the overall size.
@vue/reactivity
The @vue/reactivity
package is responsible for handling reactivity in both Vue and Alpine; petite-vue
uses this same reactivity technique.
petite-vue
compares to standard Vuepetite-vue
is similar to Vue in many ways. As mentioned, it provides the same template syntax and @vue/reactivity
model provided by standard Vue. However, the most significant difference is that petite-vue
was made for progressive enhancement.
Standard Vue was designed for using a build step to build single-page applications (SPAs) with heavy interactions. Vue uses render functions to replace existing DOM templates, while on the other hand, petite-vue
walks the existing DOM and mutates it in place, so it doesn’t require a build step.
petite-vue
exclusive featurespetite-vue
introduces some features that are not available in standard Vue that aid in optimizing progressive enhancement. Let’s take a look at them!
v-scope
In petite-vue
, the v-scope
is a directive that marks the region of the page controlled by petite-vue
. You can also use the v-scope
directive to pass in states that a particular region of your page will have access to.
v-effect
v-effect
is a directive used to execute inline reactive statements in petite-vue
. In the code snippet below, the count
variable is reactive, so the v-effect
will rerun whenever the count changes, then update the div
with the current value of count
:
<div v-scope="{ count: 0 }"> <div v-effect="$el.textContent = count"></div> <button @click="count++">++</button> </div>
petite-vue
ships with two lifecycle events, @mounted
and @unmounted
, which allow you to listen for when petite-vue
mounts or unmounts on your page.
Now that we’ve seen the new features petite-vue
brings to the table, let’s review its features that already exist in Vue:
{{ }}
: Text bindingsv-bind
and :
: Class and style special handlingv-on
and @
: Event handlingv-model
: Represents all inputs types and non-string :value
bindingsv-if
/ v-else
/ v-else-if
v-for
v-show
v-html
v-pre
v-once
v-cloak
reactive()
nextTick()
Due to its small scope, petite-vue
has dropped some of the features found in standard Vue:
ref()
and computed()
petite-vue
has no virtual DOMMap
, Set
, etc.Transition
, keep-alive
, <teleport>
, and <suspense>
componentsv-for
: Deep destructurev-on="object"
v-is
and <component :is="newComponent">
v-bind:style
auto-prefixingpetite-vue
compares to AlpineThough petite-vue
was inspired by Alpine and addresses similar problems, it differs from Alpine due to its minimalism and compatibility with Vue.
For example, petite-vue
is around half the size of Alpine, and unlike Alpine, it doesn’t ship with a transition system.
Additionally, Alpine and petite-vue
have different designs. Although Alpine resembles Vue’s structure in some ways, petite-vue
is more aligned with standard Vue, minimizing the amount of overhead you’ll have if you want to transition between Vue and petite-vue
.
petite-vue
To get started with petite-vue
, you need to include a script
tag that points to the petite-vue
package. As an example, let’s create a simple voting app powered by petite-vue
.
First, create an index.html
file. In the body of it, add the following code snippet:
<script src="https://unpkg.com/petite-vue" defer init></script> <div v-scope="{ upVotes: 0, downVotes: 0 }"> <p> {{ upVotes }} <button @click="upVotes++">👍</button> </p> <p> {{ downVotes }} <button @click="downVotes++">👎</button> </p> </div>
The defer
attribute on the script
tag causes the script loading petite-vue
to load after the HTML content is parsed by the browser. The init
attribute tells petite-vue
to automatically query and initialize all elements that have v-scope
, and the v-scope
tells petite-vue
what region of the page to handle. We also pass in the upVotes
and downVotes
states to be available to that region.
If you don’t want petite-vue
to automatically initialize all elements that have the v-scope
attribute, you can manually initialize them by changing the script:
<script src="https://unpkg.com/petite-vue"></script> <script> PetiteVue.createApp().mount() </script>
Alternatively, you can use the ES module build of petite-vue
:
<script type="module"> import { createApp } from 'https://unpkg.com/petite-vue?module' createApp().mount() </script>
petite-vue
CDN production URLIn our example, we access petite-vue
using a CDN URL. We are using a shorthand URL https://unpkg.com/petite-vue
, which is fine for prototyping but not great for production. We want to avoid resolving and redirect costs, so we’ll use the full URL while in production.
The global build production URL https://unpkg.com/[email protected]/dist/petite-vue.iife.js
exposes PetiteVue
global and also supports auto init.
The ESM build production URL https://unpkg.com/[email protected]/dist/petite-vue.iife.js
must be used in a <scripttype="module">
block.
petite-vue
We’ve learned a lot about what features petite-vue
has and what it can do. Let’s review what type of situations petite-vue
is best designed for:
petite-vue
is a lighter version of Vue that adds efficient interactions to pages. In this article, we took a first look at petite-vue
by considering its exclusive features, its similarities to standard Vue, and the feature trade-offs made to obtain its light size.
At the time of writing,petite-vue
is still pretty new and includes a disclaimer for any potential bugs. However, petite-vue
is already a functional option with strong potential and usefulness. It is especially helpful for quick prototyping, sprinkling Vue functionality into server-rendered frameworks, and building static pages.
I hope you enjoyed this article! Leave us a comment and let us know if you’re planning to try petite-vue
.
Debugging Vue.js applications can be difficult, especially when there are dozens, if not hundreds of mutations during a user session. If you’re interested in monitoring and tracking Vue mutations for all of your users in production, try LogRocket.
LogRocket is like a DVR for web and mobile apps, recording literally everything that happens in your Vue apps, including network requests, JavaScript errors, performance problems, and much more. Instead of guessing why problems happen, you can aggregate and report on what state your application was in when an issue occurred.
The LogRocket Vuex plugin logs Vuex mutations to the LogRocket console, giving you context around what led to an error and what state the application was in when an issue occurred.
Modernize how you debug your Vue apps — start monitoring for free.
Would you be interested in joining LogRocket's developer community?
Join LogRocket’s Content Advisory Board. You’ll help inform the type of content we create and get access to exclusive meetups, social accreditation, and swag.
Sign up nowOptimize search parameter handling in React and Next.js with nuqs for SEO-friendly, shareable URLs and a better user experience.
Learn how Remix enhances SSR performance, simplifies data fetching, and improves SEO compared to client-heavy React apps.
Explore Fullstory competitors, like LogRocket, to find the best product analytics tool for your digital experience.
Learn how to balance vibrant visuals with accessible, user-centered options like media queries, syntax, and minimized data use.
One Reply to "petite-vue: An Alpine alternative for progressive enhancement"
Im struggling to create components with Petite Vue.
An example would help.
Also v-for and v-if dont work on the same line with PVue