Vue In-Browser Compiler

4.72.4 · active · verified Sun Apr 19

The `vue-inbrowser-compiler` package allows for the compilation of Vue Single File Components (SFCs), pseudo-JSX, and plain Vue app definitions directly within the browser environment. It is primarily designed for applications requiring dynamic, user-editable Vue components, such as live code editors, interactive documentation (e.g., Vue Styleguidist), or component playgrounds, eliminating the need for server-side compilation. The current stable version is 4.72.4. Based on its frequent patch and minor updates within the `vue-styleguidist` monorepo, it maintains an active development and release cadence. Its key differentiator is the ability to perform full Vue component compilation dynamically on the client-side, integrating with tools like Buble for JavaScript transpilation, providing a flexible and immediate feedback loop for component development.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to compile a Vue Single File Component (SFC) string dynamically in the browser, inject its styles, and mount it using Vue 3's `createApp`.

import { compile, addScopedStyle } from 'vue-inbrowser-compiler';
import { createApp } from 'vue'; // Assuming Vue 3 for a modern example

const sfcCode = `
<template>
  <div class="hello" :style="{ color: dynamicColor }">
    <h1>Compiled Vue Component</h1>
    <button @click="changeColor">{{ msg }}</button>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        msg: 'Click Me!',
        dynamicColor: '#900'
      }
    },
    methods: {
      changeColor() {
        this.dynamicColor = this.dynamicColor === '#900' ? '#009' : '#900';
      }
    }
  }
</script>

<style scoped>
  .hello {
    text-align: center;
    font-family: sans-serif;
  }
  button {
    padding: 10px 20px;
    border: none;
    border-radius: 5px;
    background-color: #4CAF50;
    color: white;
    cursor: pointer;
    margin-top: 15px;
  }
</style>
`;

function getRunnableComponent(code) {
  const compiled = compile(code, {
    target: { ie: 11 } // Buble configuration for transpilation
  });

  // Add compiled styles to the document with a unique scope
  // The 'scoped' attribute in the SFC is handled by vue-inbrowser-compiler itself.
  // addScopedStyle is for when you manually scope or have global styles to add.
  // For SFCs, compiled.style already contains scoped styles.
  const styleTag = document.createElement('style');
  styleTag.textContent = compiled.style;
  document.head.appendChild(styleTag);

  // Execute the script and return the component definition
  const func = new Function(compiled.script);
  return func();
}

// Mount the compiled component
const appDiv = document.createElement('div');
appDiv.id = 'app';
document.body.appendChild(appDiv);

const componentDefinition = getRunnableComponent(sfcCode);
const app = createApp(componentDefinition);
app.mount('#app');

console.log('Vue component compiled and mounted to #app');

view raw JSON →