vbuild - Vue Single File Component Parser
vbuild is a Python library (v0.8.2) designed to extract HTML, JavaScript, and CSS from Vue.js `.vue` single-file components. Its primary advantage is operating without a Node.js environment, making it suitable for Python-only backends. It supports minification and targets ES2015-compliant JavaScript. The project is currently in active maintenance with infrequent updates.
Common errors
-
ModuleNotFoundError: No module named 'vbuild.vbuild'
cause The VBuild class is located in a nested submodule. Users often try to import directly from the top-level package.fixUse `from vbuild.vbuild import VBuild` instead of `from vbuild import VBuild`. -
AttributeError: 'VBuild' object has no attribute 'compiled_js'
cause Users often mistakenly expect `vbuild` to compile or transpile the JavaScript in a Vue SFC. However, `vbuild` only extracts the raw script content.fixAccess the raw JavaScript content via `vbuild.script`. If further compilation or transpilation is needed, use external tools like Babel, TypeScript, or `esbuild` on the extracted script content. -
IndexError: list index out of range (or similar parsing errors during initialization)
cause This often occurs when the regex-based parser encounters complex, deeply nested, or slightly malformed Vue SFC content that it cannot correctly deconstruct.fixSimplify the structure of your Vue SFC. Ensure all tags are correctly closed and nested. For highly complex or dynamic SFCs, consider a more robust parser like `vuejs-sfc-parser`.
Warnings
- gotcha vbuild relies on a regex-based parser, which is inherently less robust than a full Abstract Syntax Tree (AST) parser. It may struggle with complex, non-standard, or slightly malformed Vue SFC syntax, leading to incomplete extractions or parsing errors that a dedicated VueJS parser would handle.
- gotcha vbuild only extracts the raw HTML, JavaScript, and CSS content from a Vue SFC; it does not perform any JavaScript transpilation (e.g., converting ESNext to ES5) or Vue-specific compilation beyond basic extraction. The `script` content provided by `vbuild.script` is raw and may require further processing by external tools if it contains modern JavaScript features or complex Vue logic.
Install
-
pip install vbuild
Imports
- VBuild
from vbuild import VBuild
from vbuild.vbuild import VBuild
Quickstart
from vbuild.vbuild import VBuild
vue_sfc_content = """
<template>
<div id="app">
<h1>Hello {{ name }}</h1>
<p>Message: {{ msg }}</p>
</div>
</template>
<script>
export default {
data() {
return {
name: 'Registry',
msg: 'This is a vbuild example'
}
}
}
</script>
<style scoped>
#app {
font-family: Arial, sans-serif;
color: #333;
}
h1 {
color: #007bff;
}
</style>
"""
vb = VBuild(vue_sfc_content)
print("--- HTML ---")
print(vb.html)
print("\n--- Script ---")
print(vb.script)
print("\n--- Style ---")
print(vb.style)
# Example with minification
vb_minified = VBuild(vue_sfc_content, minify=True, minify_js=True)
print("\n--- Minified Script (first 100 chars) ---")
print(vb_minified.script[:100])