Vue In-Browser Compiler Utilities
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
-
ReferenceError: Vue is not defined
cause The compiled Vue component code is trying to access a global `Vue` object or specific Vue APIs, but they are not available in the current execution context (e.g., in a `script` tag without Vue loaded, or within `evaluate` without `Vue` passed in).fixEnsure Vue is loaded in the global scope (e.g., via a `<script>` tag before your compiled code) or explicitly pass the `Vue` instance into the context object when using `evaluate`. -
TypeError: Cannot read properties of undefined (reading 'script') (or similar for 'template')
cause The `parseComponent` function received an invalid or malformed string that could not be parsed as a Vue Single File Component (SFC), resulting in an incomplete `parsed` object.fixVerify that the input string passed to `parseComponent` is a well-formed Vue SFC, including `<template>` and `<script>` tags, and that there are no syntax errors preventing basic parsing. -
SyntaxError: Unexpected token '<' (or 'JSX element 'div' has no corresponding closing tag')
cause JSX syntax within your component's `<script>` or render function is not being correctly transpiled by `buble` (the underlying JSX compiler used by `vue-inbrowser-compiler`). This often indicates a missing or misconfigured JSX compilation step.fixEnsure your `vue-inbrowser-compiler` configuration correctly enables JSX processing via `buble`. If using a build tool, confirm that `vue-inbrowser-compiler`'s internal JSX handling is active and not being overridden by another preprocessor.
Warnings
- gotcha This package is tightly coupled with `vue-inbrowser-compiler` and the broader `vue-styleguidist` ecosystem. While it provides utilities, its core function is to support the in-browser compilation process, and it's not typically used in isolation for general Vue development.
- gotcha Compatibility with Vue 3.x is maintained, with explicit fixes for `vue@3.3.2` and later versions. However, users employing older Vue 3 releases or specific edge cases in Vue 2 might encounter subtle compatibility issues.
- gotcha JSX compilation is handled via `buble` within the `vue-inbrowser-compiler`. This means it supports a specific flavor of JSX (Buble's JSX transform), which may have differences or limitations compared to Babel's JSX or Vue's official compiler transforms. Advanced JSX features or specific Babel plugins might not be supported out-of-the-box.
- gotcha The `evaluate` function requires a runtime Vue instance in its context to properly render components. In a browser environment, this typically means `Vue` must be globally available. In server-side rendering or isolated environments, `Vue` must be explicitly passed into the context object.
Install
-
npm install vue-inbrowser-compiler-utils -
yarn add vue-inbrowser-compiler-utils -
pnpm add vue-inbrowser-compiler-utils
Imports
- parseComponent
const { parseComponent } = require('vue-inbrowser-compiler-utils');import { parseComponent } from 'vue-inbrowser-compiler-utils'; - cleanVueSrc
import cleanVueSrc from 'vue-inbrowser-compiler-utils/cleanVueSrc';
import { cleanVueSrc } from 'vue-inbrowser-compiler-utils'; - evaluate
import { runCode } from 'vue-inbrowser-compiler-utils';import { evaluate } from 'vue-inbrowser-compiler-utils';
Quickstart
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);
}