Vue In-Browser Compiler Utilities

4.72.4 · active · verified Sun Apr 19

vue-inbrowser-compiler-utils is a utility library, currently at stable version 4.72.4, designed to facilitate in-browser compilation of Vue components, particularly those incorporating JSX syntax. It functions as a foundational component within the broader vue-styleguidist ecosystem and receives consistent patch updates, typically synchronized with vue-styleguidist releases. This package offers essential functionalities such as parsing Vue Single File Components (SFCs), cleaning source code, and identifying if a given code string represents an SFC. Its primary differentiation lies in enabling live, client-side compilation of Vue components, including support for JSX, which is invaluable for interactive code playgrounds, documentation platforms (like Vue Styleguidist itself), and dynamic rendering environments where server-side compilation is impractical. It also conveniently re-exports select utilities from related packages within the ecosystem.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to parse a Vue SFC string (potentially with JSX) using `parseComponent` from `vue-inbrowser-compiler-utils` and then compile it with `vue-inbrowser-compiler` for in-browser evaluation.

import { parseComponent, evaluate } from 'vue-inbrowser-compiler-utils';
import { compile } from 'vue-inbrowser-compiler'; // vue-inbrowser-compiler is a companion package

// Simulate a Vue component string, potentially with JSX in the render function
const vueComponentSrc = `
<template>
  <div>Hello from {{ name }}!</div>
</template>

<script setup>
import { ref } from 'vue';

const name = ref('Quickstart');

// Example of JSX usage in a render function (requires compilation configuration)
const render = () => (<div class="jsx-example">JSX says Hi!</div>);
</nscript>

<style scoped>
.jsx-example { color: blue; }
</style>
`;

// Step 1: Parse the Vue component using vue-inbrowser-compiler-utils
const parsed = parseComponent(vueComponentSrc);

console.log('Parsed Component Script:', parsed.script);
console.log('Parsed Component Template:', parsed.template);

// Step 2: (Optional, if you want to clean up the source further)
// const cleanedScript = cleanVueSrc(parsed.script);

// Step 3: Compile the parsed component (this step uses vue-inbrowser-compiler)
// In a real browser environment, 'Vue' must be globally available or injected.
const compiledCode = compile(parsed.script, parsed.template, { 
  is         : parsed.scriptLang === 'ts' ? 'ts' : 'js',
  templateLang: parsed.templateLang,
  // Ensure buble is configured for JSX if needed, e.g., via compilerOptions
  compilerOptions: {
    isTS: parsed.scriptLang === 'ts',
    // other options for buble/jsx compilation
  }
});

console.log('Compiled Code:', compiledCode.script);

// Step 4: Evaluate the compiled code in a browser-like environment
// This is typically done in the browser; for Node.js, it simulates.
// In a browser, you would typically mount this result.
try {
  const componentInstance = evaluate(compiledCode.script, compiledCode.template, { 
    // context for the evaluation, e.g., global Vue instance
    Vue: typeof Vue !== 'undefined' ? Vue : {} // Provide Vue if available
  });
  console.log('Component evaluation successful (check browser for full effect).');
  // In a real browser app, you would now mount componentInstance
} catch (e) {
  console.error('Error during evaluation:', e);
}

view raw JSON →