From 0aa7b73e295e0503e7162497a3526bbe625ecb13 Mon Sep 17 00:00:00 2001 From: Alexander Marks Date: Mon, 15 Jul 2019 12:06:13 -0700 Subject: [PATCH] Fix compatibility with JSC static property optimizations. Fixes https://github.com/Polymer/lit-element/issues/732 --- CHANGELOG.md | 5 +++-- src/lib/updating-element.ts | 21 +++++++++++++++------ src/lit-element.ts | 14 ++++++-------- 3 files changed, 24 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 505a261b..f1abfbca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,11 +10,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/). Unreleased section, uncommenting the header as necessary. --> - +## Unreleased - +### Fixed +* Fixed compatibility with Closure JS Compiler optimizations relating to static properties ([#732](https://github.com/Polymer/lit-element/issues/732)). ## [2.2.0] - 2019-06-11 diff --git a/src/lib/updating-element.ts b/src/lib/updating-element.ts index 67115918..14e0e5f2 100644 --- a/src/lib/updating-element.ts +++ b/src/lib/updating-element.ts @@ -12,6 +12,9 @@ * http://polymer.github.io/PATENTS.txt */ +// So that custom elements work after compilation to es5. +import 'goog:webcomponentsjs.custom_elements.auto_es5_shim'; + /** * When using Closure Compiler, JSCompiler_renameProperty(property, object) is * replaced at compile time by the munged name for object[property]. We cannot @@ -211,10 +214,11 @@ export abstract class UpdatingElement extends HTMLElement { private static _attributeToPropertyMap: AttributeMap; /** - * Marks class as having finished creating properties. + * This property was previously used to record the classes that have already + * been finalized. This property is no longer used, but is still declared here + * for backwards compatability in TypeScript typings. */ - protected static finalized = true; - + protected static finalized : boolean|undefined; /** * Memoized list of all class properties, including any superclass properties. * Created lazily on user subclasses when finalizing the class. @@ -315,8 +319,7 @@ export abstract class UpdatingElement extends HTMLElement { * @nocollapse */ protected static finalize() { - if (this.hasOwnProperty(JSCompiler_renameProperty('finalized', this)) && - this.finalized) { + if (finalized.has(this)) { return; } // finalize any superclasses @@ -324,7 +327,7 @@ export abstract class UpdatingElement extends HTMLElement { if (typeof superCtor.finalize === 'function') { superCtor.finalize(); } - this.finalized = true; + finalized.add(this); this._ensureClassProperties(); // initialize Map populated in observedAttributes this._attributeToPropertyMap = new Map(); @@ -793,3 +796,9 @@ export abstract class UpdatingElement extends HTMLElement { protected firstUpdated(_changedProperties: PropertyValues) { } } + +/** + * Marks class as having finished creating properties. + */ +const finalized = new Set(); +finalized.add(UpdatingElement); diff --git a/src/lit-element.ts b/src/lit-element.ts index f5b9f446..366c6cea 100644 --- a/src/lit-element.ts +++ b/src/lit-element.ts @@ -59,12 +59,6 @@ const flattenStyles = (styles: CSSResultArray): CSSResult[] => styles.flat ? styles.flat(Infinity) : arrayFlat(styles); export class LitElement extends UpdatingElement { - /** - * Ensure this class is marked as `finalized` as an optimization ensuring - * it will not needlessly try to `finalize`. - */ - protected static finalized = true; - /** * Render method used to render the lit-html TemplateResult to the element's * DOM. @@ -85,7 +79,11 @@ export class LitElement extends UpdatingElement { /** @nocollapse */ protected static finalize() { - super.finalize(); + if (this !== LitElement) { + // The Closure JS Compiler does not always preserve the correct "this" + // when calling static super methods, so explicitly bind here. + super.finalize.apply(this); + } // Prepare styling that is stamped at first render time. Styling // is built from user provided `styles` or is inherited from the superclass. this._styles = @@ -180,7 +178,7 @@ export class LitElement extends UpdatingElement { // (3) shadowRoot.adoptedStyleSheets polyfilled: append styles after // rendering if (window.ShadyCSS !== undefined && !window.ShadyCSS.nativeShadow) { - window.ShadyCSS.ScopingShim.prepareAdoptedCssText( + window.ShadyCSS.ScopingShim!.prepareAdoptedCssText( styles.map((s) => s.cssText), this.localName); } else if (supportsAdoptingStyleSheets) { (this.renderRoot as ShadowRoot).adoptedStyleSheets =