Registry / web-framework / vue-inbrowser-compiler-sucrase

vue-inbrowser-compiler-sucrase

JSON →
library4.72.4jsnpmunverified

This package provides the capability to compile Vue components, including Single File Components (SFCs), pseudo-JSX, and standard template strings, directly within the browser environment. It is built on Sucrase for fast TypeScript and modern JavaScript transformations. Currently at version 4.72.4, it is actively maintained as a core part of the `vue-styleguidist` project, implying a consistent release cadence aligned with its parent project's needs. Its key differentiator is enabling dynamic, in-browser compilation of Vue code, which is essential for live code playgrounds, documentation generators, and interactive component editors. It abstracts away the complexities of browser-based parsing and transformation of Vue syntax, allowing developers to render components from string inputs without server-side compilation or heavy build tools.

npm install vue-inbrowser-compiler-sucrase
INSTALL
IMPORT
SIG · VUE-INBROWSER-COMP
V
vue-inbrowser-compiler-sucrase
web-frameworkjavascriptv4.72.4
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

compile
import { compile } from 'vue-inbrowser-compiler-sucrase'
const compile = require('vue-inbrowser-compiler-sucrase').compile
The primary function for compiling Vue component strings. Best used with ESM imports in modern browser environments.
isCodeVueSfc
import { isCodeVueSfc } from 'vue-inbrowser-compiler-sucrase'
const { isCodeVueSfc } = require('vue-inbrowser-compiler-sucrase')
Utility to detect if a given code string is a Vue Single File Component. Follows standard named import patterns.
adaptCreateElement
import { adaptCreateElement } from 'vue-inbrowser-compiler-sucrase'
const { adaptCreateElement } = require('vue-inbrowser-compiler-sucrase')
Required when configuring the compiler to output JSX that uses a custom pragma and needs Vue's `h` function. Ensure this is passed to the compiled function.

Demonstrates how to compile various string formats (Vue SFC, pseudo-JSX, standard template) into runnable Vue component options objects using `compile` for in-browser dynamic component creation.

import { compile } from 'vue-inbrowser-compiler-sucrase'; /** * Dynamically compiles a Vue component string into a Vue component options object. * This function uses `new Function()` for execution, making it suitable for browser environments. * Note: For JSX input that explicitly needs `h`, you would also import `adaptCreateElement` * and pass `{ jsx: '__pragma__(h)' }` to `compile`, then invoke the resulting function * with `func(adaptCreateElement)`. */ function getVueComponentOptions(codeString) { try { const compiledOutput = compile(codeString, {}); // The compiled script returns a Vue component options object or an app instance const componentFactory = new Function(compiledOutput.script); return componentFactory(); } catch (e) { console.error('Error compiling code:', e); return null; } } // 1. Example: Compile a basic Vue template string with data and methods. const basicTemplateCode = ` <template> <div> <h1>Hello from Compiler!</h1> <button @click="count++">Clicked {{ count }} times</button> </div> </template> <script> export default { data() { return { count: 0 }; }, methods: { // Example method, though direct template expressions are often sufficient } } </script> <style> div { border: 1px solid #ccc; padding: 10px; } h1 { color: #007bff; } button { padding: 8px 15px; background-color: #007bff; color: white; border: none; border-radius: 4px; cursor: pointer; } button:hover { background-color: #0056b3; } </style> `; const BasicComponentOptions = getVueComponentOptions(basicTemplateCode); console.log('Successfully compiled basic template:', BasicComponentOptions); // 2. Example: Compile a simple pseudo-JSX string (which the compiler transforms into a Vue template). const pseudoJsxCode = ` let greeting = 'Browser'; <div> <p>Hello, {{ greeting }}!</p> <input type="text" v-model="greeting" placeholder="Enter name" /> </div> `; const PseudoJsxComponentOptions = getVueComponentOptions(pseudoJsxCode); console.log('Successfully compiled pseudo-JSX:', PseudoJsxComponentOptions); // In a real application, you would then mount these options using Vue.createApp (Vue 3) or new Vue() (Vue 2). // For instance: // import { createApp } from 'vue'; // if (BasicComponentOptions) { // createApp(BasicComponentOptions).mount('#my-app-element'); // }
Debug
Known issues
gotchaThe package `vue-inbrowser-compiler-sucrase` utilizes Sucrase for faster compilation. However, the `compile` function's second argument is often typed as `BubleConfig`, a remnant from previous implementations or a compatibility layer. Developers should be aware that the actual compilation engine is Sucrase, which has specific transform limitations compared to full Babel or TypeScript compilers.
fix
Refer to Sucrase's documentation for supported syntax and transformations. While `BubleConfig` is accepted, not all historical Buble features may be fully supported or behave identically.
affects: >=4.0.0
gotchaThis package is designed for in-browser compilation and should not be used for server-side compilation with Node.js. Its dependencies and runtime expectations are optimized for browser environments.
fix
For server-side Vue compilation, consider `@vue/compiler-sfc` or other build-time tools like Vite or Webpack with Vue loaders.
affects: *
gotchaWhen compiling JSX inputs that require an `h` function (createElement), you must explicitly configure the `jsx` option in the `compile` function's configuration and provide the `adaptCreateElement` function.
fix
Pass `{ jsx: '__pragma__(h)' }` as the second argument to `compile`, and ensure the resulting function is invoked with `adaptCreateElement`: `const func = new Function('__pragma__', compiledCode.script); return func(adaptCreateElement);`
affects: >=4.0.0
gotchaCompatibility with Vue versions is governed by the peer dependency `vue: '>=2'`. Ensure your project's Vue version meets this requirement to avoid runtime issues with compiled components.
fix
Verify your project's `vue` version (e.g., in `package.json`) and upgrade if necessary to satisfy the `'>=2'` peer dependency.
affects: >=4.0.0
Errors
Common errors & fixes
ReferenceError: h is not defined
Attempting to compile or run JSX output without providing the Vue `createElement` (or `h`) function, or incorrect JSX pragma configuration.
fix
If compiling JSX input, ensure `compile` is called with `{ jsx: '__pragma__(h)' }` and the `new Function()`'s result is invoked with `adaptCreateElement` as `func(adaptCreateElement)`.
SyntaxError: Cannot use import statement outside a module
The compiled script (`compiledCode.script`) might contain ES module syntax (`import`/`export`) which `new Function()` cannot directly execute in a non-module browser context, or bundler issues.
fix
Ensure the compilation configuration (if available) targets an appropriate ES version for `new Function()`. If using a bundler, ensure it correctly handles the compiled output. Consider using a `script type="module"` if executing the output directly in a browser HTML file.
TypeError: Cannot read properties of undefined (reading 'template')
The input code string did not result in a valid Vue component options object, or `new Function()` failed to return a value.
fix
Verify that your input string is a well-formed Vue component (SFC, pseudo-JSX resulting in template+data, or `new Vue({...})` structure). Check console for other compilation errors before this `TypeError`.
Vue template compiler cannot compile template
The template section within your input string contains invalid or malformed Vue template syntax that the compiler cannot parse.
fix
Carefully review the `<template>` content or the template portion of your pseudo-JSX/Vue app code for typos, unclosed tags, incorrect directives, or other syntax errors.
Upgrade
Version history
4.72.4latest on npm
Audit
Dependencies
vuerequiredRuntime dependency for the compiled Vue components to function.
vue-inbrowser-compiler-utilsrequiredProvides utility functions essential for the compiler's operations.
Agent activity
7 hits · last 30 days
node
6
OpenAI (training)
1
Resources