Skip to content

Commit

Permalink
Inject ResizeObserver polyfill where needed (#20754)
Browse files Browse the repository at this point in the history
  • Loading branch information
steverep authored May 24, 2024
1 parent 5d044a0 commit 41e34c0
Show file tree
Hide file tree
Showing 17 changed files with 39 additions and 48 deletions.
21 changes: 20 additions & 1 deletion build-scripts/babel-plugins/custom-polyfill-plugin.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import defineProvider from "@babel/helper-define-polyfill-provider";
import { join } from "node:path";
import paths from "../paths.cjs";

const POLYFILL_DIR = join(paths.polymer_dir, "src/resources/polyfills");

// List of polyfill keys with supported browser targets for the functionality
const PolyfillSupport = {
Expand Down Expand Up @@ -36,14 +40,29 @@ const PolyfillSupport = {
safari: 10.0,
samsung: 5.0,
},
"resize-observer": {
android: 64,
chrome: 64,
edge: 79,
firefox: 69,
ios: 13.4,
opera: 51,
opera_mobile: 47,
safari: 13.1,
samsung: 9.0,
},
};

// Map of global variables and/or instance and static properties to the
// corresponding polyfill key and actual module to import
const polyfillMap = {
global: {
Proxy: { key: "proxy", module: "proxy-polyfill" },
fetch: { key: "fetch", module: "unfetch/polyfill" },
Proxy: { key: "proxy", module: "proxy-polyfill" },
ResizeObserver: {
key: "resize-observer",
module: join(POLYFILL_DIR, "resize-observer.ts"),
},
},
instance: {
attachInternals: {
Expand Down
16 changes: 10 additions & 6 deletions build-scripts/bundle.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -147,19 +147,23 @@ module.exports.babelOptions = ({ latestBuild, isProdBuild, isTestBuild }) => ({
overrides: [
{
// Add plugin to inject various polyfills, excluding the polyfills
// themselves to prevent self- injection.
// themselves to prevent self-injection.
plugins: [
[
path.join(BABEL_PLUGINS, "custom-polyfill-plugin.js"),
{ method: "usage-global" },
],
],
exclude: [
"@webcomponents/scoped-custom-element-registry",
"element-internals-polyfill",
"proxy-polyfill",
"unfetch",
].map((p) => new RegExp(`/node_modules/${p}/`)),
path.join(paths.polymer_dir, "src/resources/polyfills"),
...[
"@lit-labs/virtualizer/polyfills",
"@webcomponents/scoped-custom-element-registry",
"element-internals-polyfill",
"proxy-polyfill",
"unfetch",
].map((p) => new RegExp(`/node_modules/${p}/`)),
],
},
{
// Use unambiguous for dependencies so that require() is correctly injected into CommonJS files
Expand Down
2 changes: 0 additions & 2 deletions src/components/map/ha-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import {
} from "../../common/dom/setup-leaflet-map";
import { computeStateDomain } from "../../common/entity/compute_state_domain";
import { computeStateName } from "../../common/entity/compute_state_name";
import { loadPolyfillIfNeeded } from "../../resources/resize-observer.polyfill";
import { HomeAssistant, ThemeMode } from "../../types";
import { isTouch } from "../../util/is_touch";
import "../ha-icon-button";
Expand Down Expand Up @@ -536,7 +535,6 @@ export class HaMap extends ReactiveElement {

private async _attachObserver(): Promise<void> {
if (!this._resizeObserver) {
await loadPolyfillIfNeeded();
this._resizeObserver = new ResizeObserver(() => {
this.leafletMap?.invalidateSize({ debounceMoveend: true });
});
Expand Down
2 changes: 0 additions & 2 deletions src/components/media-player/ha-media-player-browse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ import {
import { browseLocalMediaPlayer } from "../../data/media_source";
import { isTTSMediaSource } from "../../data/tts";
import { showAlertDialog } from "../../dialogs/generic/show-dialog-box";
import { loadPolyfillIfNeeded } from "../../resources/resize-observer.polyfill";
import { haStyle } from "../../resources/styles";
import type { HomeAssistant } from "../../types";
import {
Expand Down Expand Up @@ -770,7 +769,6 @@ export class HaMediaPlayerBrowse extends LitElement {

private async _attachResizeObserver(): Promise<void> {
if (!this._resizeObserver) {
await loadPolyfillIfNeeded();
this._resizeObserver = new ResizeObserver(
debounce(() => this._measureCard(), 250, false)
);
Expand Down
2 changes: 0 additions & 2 deletions src/panels/lovelace/cards/hui-calendar-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import type {
} from "../../../types";
import "../../calendar/ha-full-calendar";
import { findEntities } from "../common/find-entities";
import { loadPolyfillIfNeeded } from "../../../resources/resize-observer.polyfill";
import "../components/hui-warning";
import type { LovelaceCard, LovelaceCardEditor } from "../types";
import type { CalendarCardConfig } from "./types";
Expand Down Expand Up @@ -210,7 +209,6 @@ export class HuiCalendarCard extends LitElement implements LovelaceCard {

private async _attachObserver(): Promise<void> {
if (!this._resizeObserver) {
await loadPolyfillIfNeeded();
this._resizeObserver = new ResizeObserver(
debounce(() => this._measureCard(), 250, false)
);
Expand Down
2 changes: 0 additions & 2 deletions src/panels/lovelace/cards/hui-media-control-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ import {
import type { HomeAssistant } from "../../../types";
import { findEntities } from "../common/find-entities";
import { hasConfigOrEntityChanged } from "../common/has-changed";
import { loadPolyfillIfNeeded } from "../../../resources/resize-observer.polyfill";
import "../components/hui-marquee";
import { createEntityNotFoundWarning } from "../components/hui-warning";
import type { LovelaceCard, LovelaceCardEditor } from "../types";
Expand Down Expand Up @@ -482,7 +481,6 @@ export class HuiMediaControlCard extends LitElement implements LovelaceCard {

private async _attachObserver(): Promise<void> {
if (!this._resizeObserver) {
await loadPolyfillIfNeeded();
this._resizeObserver = new ResizeObserver(
debounce(() => this._measureCard(), 250, false)
);
Expand Down
2 changes: 0 additions & 2 deletions src/panels/lovelace/cards/hui-weather-forecast-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import {
weatherAttrIcons,
weatherSVGStyles,
} from "../../../data/weather";
import { loadPolyfillIfNeeded } from "../../../resources/resize-observer.polyfill";
import type { HomeAssistant } from "../../../types";
import { actionHandler } from "../common/directives/action-handler-directive";
import { findEntities } from "../common/find-entities";
Expand Down Expand Up @@ -419,7 +418,6 @@ class HuiWeatherForecastCard extends LitElement implements LovelaceCard {

private async _attachObserver(): Promise<void> {
if (!this._resizeObserver) {
await loadPolyfillIfNeeded();
this._resizeObserver = new ResizeObserver(
debounce(() => this._measureCard(), 250, false)
);
Expand Down
2 changes: 0 additions & 2 deletions src/panels/lovelace/components/hui-energy-period-selector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ import "../../../components/ha-icon-button-next";
import "../../../components/ha-icon-button-prev";
import { EnergyData, getEnergyDataCollection } from "../../../data/energy";
import { SubscribeMixin } from "../../../mixins/subscribe-mixin";
import { loadPolyfillIfNeeded } from "../../../resources/resize-observer.polyfill";
import { HomeAssistant } from "../../../types";

@customElement("hui-energy-period-selector")
Expand Down Expand Up @@ -89,7 +88,6 @@ export class HuiEnergyPeriodSelector extends SubscribeMixin(LitElement) {

private async _attachObserver(): Promise<void> {
if (!this._resizeObserver) {
await loadPolyfillIfNeeded();
this._resizeObserver = new ResizeObserver(
debounce(() => this._measure(), 250, false)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import "../../../components/ha-slider";
import "../../../components/ha-textfield";
import { isUnavailableState } from "../../../data/entity";
import { setValue } from "../../../data/input_text";
import { loadPolyfillIfNeeded } from "../../../resources/resize-observer.polyfill";
import { HomeAssistant } from "../../../types";
import { hasConfigOrEntityChanged } from "../common/has-changed";
import "../components/hui-generic-entity-row";
Expand Down Expand Up @@ -161,7 +160,6 @@ class HuiInputNumberEntityRow extends LitElement implements LovelaceRow {

private async _attachObserver(): Promise<void> {
if (!this._resizeObserver) {
await loadPolyfillIfNeeded();
this._resizeObserver = new ResizeObserver(
debounce(() => this._measureCard(), 250, false)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import {
MediaPlayerEntityFeature,
computeMediaDescription,
} from "../../../data/media-player";
import { loadPolyfillIfNeeded } from "../../../resources/resize-observer.polyfill";
import type { HomeAssistant } from "../../../types";
import { hasConfigOrEntityChanged } from "../common/has-changed";
import "../components/hui-generic-entity-row";
Expand Down Expand Up @@ -291,7 +290,6 @@ class HuiMediaPlayerEntityRow extends LitElement implements LovelaceRow {

private async _attachObserver(): Promise<void> {
if (!this._resizeObserver) {
await loadPolyfillIfNeeded();
this._resizeObserver = new ResizeObserver(
debounce(() => this._measureCard(), 250, false)
);
Expand Down
2 changes: 0 additions & 2 deletions src/panels/lovelace/entity-rows/hui-number-entity-row.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import "../../../components/ha-slider";
import "../../../components/ha-textfield";
import { UNAVAILABLE } from "../../../data/entity";
import { setValue } from "../../../data/input_text";
import { loadPolyfillIfNeeded } from "../../../resources/resize-observer.polyfill";
import { HomeAssistant } from "../../../types";
import { hasConfigOrEntityChanged } from "../common/has-changed";
import "../components/hui-generic-entity-row";
Expand Down Expand Up @@ -167,7 +166,6 @@ class HuiNumberEntityRow extends LitElement implements LovelaceRow {

private async _attachObserver(): Promise<void> {
if (!this._resizeObserver) {
await loadPolyfillIfNeeded();
this._resizeObserver = new ResizeObserver(
debounce(() => this._measureCard(), 250, false)
);
Expand Down
4 changes: 0 additions & 4 deletions src/resources/compatibility.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
// Caution before editing - For latest builds, this module is replaced with emptiness and thus not imported (see build-scripts/bundle.js)
import "lit/polyfill-support";

import ResizeObserver from "@lit-labs/virtualizer/polyfills/resize-observer-polyfill/ResizeObserver";

window.ResizeObserver = ResizeObserver;

// Source: https://github.com/jserz/js_piece/blob/master/DOM/ParentNode/append()/append().md
(function (arr) {
arr.forEach((item) => {
Expand Down
9 changes: 9 additions & 0 deletions src/resources/polyfills/resize-observer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
if (typeof window.ResizeObserver !== "function") {
window.ResizeObserver = (
await import(
"@lit-labs/virtualizer/polyfills/resize-observer-polyfill/ResizeObserver"
)
).default;
}

export {};
12 changes: 0 additions & 12 deletions src/resources/resize-observer.polyfill.ts

This file was deleted.

3 changes: 0 additions & 3 deletions src/resources/virtualizer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import { loadPolyfillIfNeeded } from "./resize-observer.polyfill";

export const loadVirtualizer = async () => {
await loadPolyfillIfNeeded();
await import("@lit-labs/virtualizer");
};
2 changes: 0 additions & 2 deletions src/state-summary/state-card-input_number.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import "../components/ha-slider";
import "../components/ha-textfield";
import { isUnavailableState } from "../data/entity";
import { setValue } from "../data/input_text";
import { loadPolyfillIfNeeded } from "../resources/resize-observer.polyfill";
import { HomeAssistant } from "../types";

@customElement("state-card-input_number")
Expand Down Expand Up @@ -132,7 +131,6 @@ class StateCardInputNumber extends LitElement {

private async _attachObserver(): Promise<void> {
if (!this._resizeObserver) {
await loadPolyfillIfNeeded();
this._resizeObserver = new ResizeObserver(
debounce(() => this._measureCard(), 250, false)
);
Expand Down
2 changes: 0 additions & 2 deletions src/state-summary/state-card-number.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import "../components/ha-slider";
import "../components/ha-textfield";
import { HomeAssistant } from "../types";
import { haStyle } from "../resources/styles";
import { loadPolyfillIfNeeded } from "../resources/resize-observer.polyfill";
import { isUnavailableState } from "../data/entity";
import { debounce } from "../common/util/debounce";

Expand Down Expand Up @@ -109,7 +108,6 @@ class StateCardNumber extends LitElement {

private async _attachObserver(): Promise<void> {
if (!this._resizeObserver) {
await loadPolyfillIfNeeded();
this._resizeObserver = new ResizeObserver(
debounce(() => this._measureCard(), 250, false)
);
Expand Down

0 comments on commit 41e34c0

Please sign in to comment.