Cache tagged template objects in modules

TypeScript 2.6 fixes the tagged string template emit to align better with the ECMAScript spec.As per the ECMAScript spec, every time a template tag is evaluated, the same template strings object (the same TemplateStringsArray) should be passed as the first argument.Before TypeScript 2.6, the generated output was a completely new template object each time.Though the string contents are the same, this emit affects libraries that use the identity of the string for cache invalidation purposes, e.g. lit-html.

Example

  1. export function id(x: TemplateStringsArray) {
  2. return x;
  3. }
  4. export function templateObjectFactory() {
  5. return id`hello world`;
  6. }
  7. let result = templateObjectFactory() === templateObjectFactory(); // true in TS 2.6

Results in the following generated code:

  1. "use strict";
  2. var __makeTemplateObject = (this && this.__makeTemplateObject) || function (cooked, raw) {
  3. if (Object.defineProperty) { Object.defineProperty(cooked, "raw", { value: raw }); } else { cooked.raw = raw; }
  4. return cooked;
  5. };
  6. function id(x) {
  7. return x;
  8. }
  9. var _a;
  10. function templateObjectFactory() {
  11. return id(_a || (_a = __makeTemplateObject(["hello world"], ["hello world"])));
  12. }
  13. var result = templateObjectFactory() === templateObjectFactory();

Note: This change brings a new emit helper, __makeTemplateObject;if you are using —importHelpers with tslib, an updated to version 1.8 or later.