A Java port of Vue 3's template compiler (@vue/compiler-dom, from vuejs/core v3.5.43). It produces exactly what Vue's own production browser build produces in function mode, byte for byte, with no dependencies beyond the JDK.
String code = vue3.compiler.VueCompiler.compile("<div :class=\"c\">{{ msg }}</div>");code is the body of a function that takes the global Vue and returns a render function:
const _Vue = Vue
const { ... } = _Vue
const _hoisted_1 = ...
return function render(_ctx, _cache) {
with (_ctx) {
...
}
}A bundler wraps it like this:
render: Object.assign((function() { <code> })(), {_rc: true})The _rc flag is what Vue's own runtime compiler sets on what it compiles. It gives the render function the proxy that with (_ctx) needs, which handles globals and Symbol.unscopables. Without the flag, Vue warns on every render.
The output is for the Vue runtime of the same version (VueCompiler.UPSTREAM), because it calls that version's helpers and encodes its patch flags.
Needs Java 21+ and ant.
ant dist # dist/vue3-template-compiler.jar
ant test # golden tests
test/templates holds 924 templates:
- every template in web-ui
- every template string in Vue's own
compiler-coreandcompiler-domtest suites - a set written for this port (
extra/) covering entities, whitespace, Unicode, inline styles, event modifiers,v-model,v-for, slots, built-in components and static caching
test/golden holds what Vue's compiler produced for each one, either the code or the error it reported. GoldenTest requires the port to produce exactly the same.
The golden files are generated by running Vue's published compiler-dom.global.prod.js (in test/reference) in headless Chromium. It has to be a real browser because the browser build decodes HTML entities through the DOM.
java tools/ExtractTemplates.java webui ../web-ui/src
java tools/ExtractTemplates.java vue path/to/vuejs/core
java tools/GenerateGolden.java [chromium binary]
It ports compiler-core, compiler-dom and the parts of @vue/shared the compiler uses, as Vue builds them for the production browser bundle:
- Function mode only. There is no
prefixIdentifiers, module mode, SSR or source maps, so no Babel or other JavaScript parser is needed. Template expressions are passed through as written, and identifiers resolve throughwith (_ctx)at runtime. - Flags.
Flags.javafixes__BROWSER__ = true,__DEV__ = falseand__COMPAT__ = true. Each upstream condition is kept as written against these flags, so later upstream diffs are easy to port. Compat features stay off without acompatConfig, which this doesn't support. Only the two__COMPAT__branches that don't consultcompatConfighave an effect. - Errors. The production browser build marks
emitError(,context.onError(andcreateCompilerError(as pure. The minifier then drops most of the parser's error reports, so the browser build compiles many malformed templates rather than rejecting them. The port reports exactly the errors that survive in Vue's bundle, with Vue's numeric codes (CompilerError.code), and carries on past the rest just as Vue does. - Entities. The browser build decodes entities by setting
innerHTML(or an attribute) and reading the text back.HtmlEntitiesreproduces that from the HTML spec and the WHATWG entity table: legacy references without semicolons, the attribute rule, numeric references with the C1 remapping, newline normalisation, and markup inside the decoded text (<textarea>content) contributing no text. - JavaScript semantics where they differ from Java's. These are in
Js.java: whattrim()and\scount as whitespace,JSON.stringifyescaping,String.prototype.slicewith negative indices, JavaScript object key order for parsed inline styles, and identifier character classes matched by UTF-16 unit.
- Port the changes with
git diff v3.5.43..vX.Y.Z -- packages/compiler-core/src packages/compiler-dom/src packages/shared/src. The Java files are named after, and laid out like, the TypeScript ones. - Check Vue's
rollup.config.jsfor changes to the production replacements, which decide which errors the browser build reports. - Replace
test/reference/compiler-dom.global.prod.jswith the new release's, regenerate the golden files, and runant test. - Update
VueCompiler.UPSTREAM. The runtime must be upgraded to match.
MIT, as Vue is. The tokenizer is adapted, as Vue's is, from htmlparser2 (MIT, Copyright 2010, 2011, Chris Winberry).