Precompiling Templates
Owl is designed to be used by the Odoo javascript framework. Since Odoo handles its assets in its own non standard way, it was decided/assumed that Owl would compile templates at runtime.
However, in some cases, it is not optimal, or even worse, not possible to do that.
For example, browser extensions do not allow javascript code to create a new
function (using the new Function(...) syntax).
Therefore, in these cases, it is required to compile templates ahead of time. It is possible to do that in Owl, but the tooling is still rough. For now, the process is the following:
- write your templates in xml files (with a
t-namedirective to declare the name of the template) - Compile them in a
templates.jsfile - get the
owl.iife.runtime.jsfile (which is a owl build without the compiler) - bundle
owl.iife.runtime.jsandtemplate.jswith your assets (owl needs to be positioned before the templates)
Here is a more detailed explanation on how to compile xml files into a js file:
- clone the owl repository locally
npm installto install all the required toolingnpm run build:runtimeto build theowl.iife.runtime.jsfilenpm run build:compilerto build the template compilernpm run compile_templates -- path/to/your/templateswill scan your target folder, find all xml files, get all templates, compile them, and generate atemplates.jsfile.