Skip to content

About

A Java port of the vue3 template compiler

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

4 Commits

Folders and files

Repository files navigation

vue3-template-compiler-java

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.

Building and testing

Needs Java 21+ and ant.

ant dist   # dist/vue3-template-compiler.jar
ant test   # golden tests

How it is tested

test/templates holds 924 templates:

  • every template in web-ui
  • every template string in Vue's own compiler-core and compiler-dom test 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]

What it is a port of

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 through with (_ctx) at runtime.
  • Flags. Flags.java fixes __BROWSER__ = true, __DEV__ = false and __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 a compatConfig, which this doesn't support. Only the two __COMPAT__ branches that don't consult compatConfig have an effect.
  • Errors. The production browser build marks emitError(, context.onError( and createCompilerError( 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. HtmlEntities reproduces 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: what trim() and \s count as whitespace, JSON.stringify escaping, String.prototype.slice with negative indices, JavaScript object key order for parsed inline styles, and identifier character classes matched by UTF-16 unit.

Upgrading to a new Vue release

  1. 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.
  2. Check Vue's rollup.config.js for changes to the production replacements, which decide which errors the browser build reports.
  3. Replace test/reference/compiler-dom.global.prod.js with the new release's, regenerate the golden files, and run ant test.
  4. Update VueCompiler.UPSTREAM. The runtime must be upgraded to match.

License

MIT, as Vue is. The tokenizer is adapted, as Vue's is, from htmlparser2 (MIT, Copyright 2010, 2011, Chris Winberry).

About

A Java port of the vue3 template compiler

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages