Skip to content

Commit 121e37d

Browse files
committed
Update runtime details for #931
This brings back some details about runtime template compilation that got lost from #813
1 parent f9bd030 commit 121e37d

File tree

1 file changed

+38
-12
lines changed

1 file changed

+38
-12
lines changed

text/0931-template-compiler-api.md

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -118,26 +118,21 @@ class Example extends Component {
118118

119119
## Detailed design
120120

121-
This RFC introduces two new importable APIs:
121+
This RFC introduces a new importable API:
122122

123123
```js
124-
// The ahead-of-time template compiler:
125124
import { template } from "@ember/template-compiler";
126-
127-
// The runtime template compiler:
128-
import { template } from "@ember/template-compiler/runtime";
129125
```
130126

131-
They are intended to be drop-in replacements for each other _except_ for the differences summarized in this table:
127+
The following sections will detail the semantics of this `template()` function. In typical usages, calls to this `template()` will be pre-processed at build time. By default, the template compiler is not included in the build, and if this function is called at runtime without the template compiler, it will throw an runtime error.
132128

133-
| | Template Contents | Scope Param | Syntax Errors | Payload |
134-
| ------------- | ---------------------- | -------------------------------------------- | ------------------------ | ---------------- |
135-
| Ahead-of-Time | Restricted to literals | Restricted to a few explicitly-allowed forms | Stops your build | Smaller & Faster |
136-
| Runtime | Unrestricted | Unrestricted | Can by caught at runtime | Larger & Slower |
129+
However, there are use cases where runtime template compilation is desirable. For that purpose, we further introduce an importable module as an opt-in to include the template compiler:
137130

138-
By putting these two implementations in different importable modules, the problem of "how do you opt-in to including the template compiler in your app?" goes away. If you import it, you will have it, if you don't, you won't.
131+
```js
132+
import "@ember/template-compiler/runtime";
133+
```
139134

140-
The remainder of this design only uses examples with the ahead-of-time template compiler, because everything about the runtime template compiler's API is the same.
135+
When this module is imported into the build, it'll make the template compiler available, which allows the `template()` function to be called at runtime with compatible semantics as the build time pre-processing. Note that this is not an opt-in to disable or otherwise influence build-time compilation, it merely provides the necessary components for the `template()` function to be callable at runtime. See the dedicated section for additional details.
141136

142137
### Scope Access
143138

@@ -289,6 +284,31 @@ When the `component` argument is passed, the return value is that backing class,
289284
> _Aren't route templates "bare templates"? What about them?<br>_
290285
> Yes, this RFC deliberately doesn't say anything about route templates. We expect a future routing RFC to use components to express what today's route templates express. This RFC also doesn't deprecate `precompileTemplate` yet -- although that will clearly be a good goal _after_ a new routing design addresses route templates.
291286
287+
### Runtime Compilation
288+
289+
Ember always had the ability to compile template at runtime. However, because this incur significant costs and most apps do not benefit from it, this feature is disabled by default and requires and explicit opt-in to include the runtime compiler.
290+
291+
Traditionally, this is done with:
292+
293+
```js
294+
// ember-cli-build.js
295+
app.import("node_modules/ember-source/dist/ember-template-compiler.js");
296+
```
297+
298+
The new `"@ember/template-compiler/runtime"` module is interned to serve as a replacement for this, which better aligns with the direction we are headed. For example, this module can be imported on only the routes that needs it, and in conjunction with route-based code splitting that would reduce the performance hit on the initial load.
299+
300+
Note that, even with the template compilation is available at runtime, the result of the compilations may be subtly different – applications may have custom glimmer/handlebars AST plugins in their build, and these plugins will not be available at runtime.
301+
302+
Other than that, the signature and semantics of the `template()` function is intended to be identical between the build time pre-processing and runtime calls, and the build time pre-processing can be thought of as an optimization. In order to guarantee that the build-time optimization can be performed correctly, the next section details some syntactic restrictions. Sticking to the `"@ember/template-compiler"` import and adhering to the permissible subset of syntax enables authoring/emitting isomorphic code that is agnostic to where the compilation actually happens.
303+
304+
That said, as a convenience, the `runtime` module will also re-export the `template()` function:
305+
306+
```js
307+
import { template } from "@ember/template-compiler/runtime";
308+
```
309+
310+
This guarantees that these `template` call will not be touched by any build-time preprocessing, relaxes any static checks for the syntactic restrictions and ensures the runtime compiler is available.
311+
292312
### Syntactic Restrictions
293313

294314
The runtime template compiler has no syntactic restrictions.
@@ -318,6 +338,12 @@ If provided, `params.eval` must be:
318338
- whose body contains exactly one return statment.
319339
- where the return value must be exactly `eval(arguments[0])`.
320340

341+
In summary:
342+
343+
| | Template Contents | Scope Param | Template Syntax Errors | Payload |
344+
| ---------------------- | ------------------------------ | -------------------------------------------- | ------------------------ | ---------------- |
345+
| Build-time compilation | Restricted to a string literal | Restricted to a few explicitly-allowed forms | Stops your build | Smaller & Faster |
346+
| Runtime compilation | Unrestricted | Unrestricted | Can by caught at runtime | Larger & Slower |
321347

322348
### Older things that are intentionally dropped
323349

0 commit comments

Comments
 (0)