Vue In-Browser Compiler Demi

4.71.1 · active · verified Sun Apr 19

vue-inbrowser-compiler-demi is a crucial internal compatibility layer designed to enable vue-inbrowser-compiler to function seamlessly across both Vue 2 and Vue 3 environments. It manages the underlying compiler differences between the two major Vue versions, abstracting them away from the primary compiler package. The current stable version is 4.71.1. As part of the larger vue-styleguidist ecosystem, its release cadence is tied to updates within that project, often seeing patch and minor releases to maintain compatibility with new Vue versions and address bug fixes, such as resolving issues with Vue 3.3.2. Its key differentiator is its role in providing a unified compilation experience for in-browser Vue component evaluation, particularly useful for development tools, playgrounds, or live-editing scenarios where direct browser compilation of Vue Single File Components (SFCs) is required without a build step.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `vue-inbrowser-compiler` to dynamically compile and run a Vue Single File Component (SFC) in the browser, showing how `vue-inbrowser-compiler-demi` facilitates the underlying Vue 2/3 compatibility without direct user intervention.

import { compile, run } from 'vue-inbrowser-compiler';

const componentCode = `
<template>
  <div :style="{ color: dynamicColor }">
    <h1>Hello, {{ name }}!</h1>
    <p>This is a Vue {{ vueVersion }} component compiled in-browser.</p>
    <button @click="changeColor">Change Color</button>
  </div>
</template>

<script>
export default {
  props: { 
    name: { type: String, default: 'Developer' },
    vueVersion: { type: String, default: '?' }
  },
  data() {
    return { dynamicColor: 'blue' };
  },
  methods: {
    changeColor() {
      this.dynamicColor = this.dynamicColor === 'blue' ? 'red' : 'blue';
    }
  }
}
</script>

<style scoped>
h1 { margin-bottom: 5px; }
p { margin-top: 5px; }
</style>
`;

// vue-inbrowser-compiler-demi invisibly handles the Vue 2/3 specific compilation logic here.
// The 'compile' function automatically detects the Vue version based on installed peer dependencies.
const compiledComponent = compile(componentCode, {
  // Optionally pass compiler options, e.g., for Buble (ES2015 transpilation)
  // buble: { target: { ie: 11 } }
});

// Create a mount point in the DOM
const appRoot = document.createElement('div');
appRoot.id = 'app';
document.body.appendChild(appRoot);

// Run the compiled component. For a real app, you'd ensure Vue is globally available.
// Here, we simulate by passing Vue to the context if needed for standalone execution.
// This example assumes Vue is already loaded in the browser context.
run(compiledComponent.code, {
  mountPoint: appRoot,
  scope: { 
    name: 'World',
    vueVersion: '3.x or 2.x (resolved by demi)'
  }
});

console.log('Vue component compiled and mounted in the browser!');

view raw JSON →