mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-15 13:26:34 +00:00
Inject ResizeObserver polyfill where needed (#20754)
This commit is contained in:
parent
5d044a06eb
commit
41e34c0d61
@ -1,4 +1,8 @@
|
|||||||
import defineProvider from "@babel/helper-define-polyfill-provider";
|
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
|
// List of polyfill keys with supported browser targets for the functionality
|
||||||
const PolyfillSupport = {
|
const PolyfillSupport = {
|
||||||
@ -36,14 +40,29 @@ const PolyfillSupport = {
|
|||||||
safari: 10.0,
|
safari: 10.0,
|
||||||
samsung: 5.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
|
// Map of global variables and/or instance and static properties to the
|
||||||
// corresponding polyfill key and actual module to import
|
// corresponding polyfill key and actual module to import
|
||||||
const polyfillMap = {
|
const polyfillMap = {
|
||||||
global: {
|
global: {
|
||||||
Proxy: { key: "proxy", module: "proxy-polyfill" },
|
|
||||||
fetch: { key: "fetch", module: "unfetch/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: {
|
instance: {
|
||||||
attachInternals: {
|
attachInternals: {
|
||||||
|
@ -147,7 +147,7 @@ module.exports.babelOptions = ({ latestBuild, isProdBuild, isTestBuild }) => ({
|
|||||||
overrides: [
|
overrides: [
|
||||||
{
|
{
|
||||||
// Add plugin to inject various polyfills, excluding the polyfills
|
// Add plugin to inject various polyfills, excluding the polyfills
|
||||||
// themselves to prevent self- injection.
|
// themselves to prevent self-injection.
|
||||||
plugins: [
|
plugins: [
|
||||||
[
|
[
|
||||||
path.join(BABEL_PLUGINS, "custom-polyfill-plugin.js"),
|
path.join(BABEL_PLUGINS, "custom-polyfill-plugin.js"),
|
||||||
@ -155,11 +155,15 @@ module.exports.babelOptions = ({ latestBuild, isProdBuild, isTestBuild }) => ({
|
|||||||
],
|
],
|
||||||
],
|
],
|
||||||
exclude: [
|
exclude: [
|
||||||
|
path.join(paths.polymer_dir, "src/resources/polyfills"),
|
||||||
|
...[
|
||||||
|
"@lit-labs/virtualizer/polyfills",
|
||||||
"@webcomponents/scoped-custom-element-registry",
|
"@webcomponents/scoped-custom-element-registry",
|
||||||
"element-internals-polyfill",
|
"element-internals-polyfill",
|
||||||
"proxy-polyfill",
|
"proxy-polyfill",
|
||||||
"unfetch",
|
"unfetch",
|
||||||
].map((p) => new RegExp(`/node_modules/${p}/`)),
|
].map((p) => new RegExp(`/node_modules/${p}/`)),
|
||||||
|
],
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
// Use unambiguous for dependencies so that require() is correctly injected into CommonJS files
|
// Use unambiguous for dependencies so that require() is correctly injected into CommonJS files
|
||||||
|
@ -22,7 +22,6 @@ import {
|
|||||||
} from "../../common/dom/setup-leaflet-map";
|
} from "../../common/dom/setup-leaflet-map";
|
||||||
import { computeStateDomain } from "../../common/entity/compute_state_domain";
|
import { computeStateDomain } from "../../common/entity/compute_state_domain";
|
||||||
import { computeStateName } from "../../common/entity/compute_state_name";
|
import { computeStateName } from "../../common/entity/compute_state_name";
|
||||||
import { loadPolyfillIfNeeded } from "../../resources/resize-observer.polyfill";
|
|
||||||
import { HomeAssistant, ThemeMode } from "../../types";
|
import { HomeAssistant, ThemeMode } from "../../types";
|
||||||
import { isTouch } from "../../util/is_touch";
|
import { isTouch } from "../../util/is_touch";
|
||||||
import "../ha-icon-button";
|
import "../ha-icon-button";
|
||||||
@ -536,7 +535,6 @@ export class HaMap extends ReactiveElement {
|
|||||||
|
|
||||||
private async _attachObserver(): Promise<void> {
|
private async _attachObserver(): Promise<void> {
|
||||||
if (!this._resizeObserver) {
|
if (!this._resizeObserver) {
|
||||||
await loadPolyfillIfNeeded();
|
|
||||||
this._resizeObserver = new ResizeObserver(() => {
|
this._resizeObserver = new ResizeObserver(() => {
|
||||||
this.leafletMap?.invalidateSize({ debounceMoveend: true });
|
this.leafletMap?.invalidateSize({ debounceMoveend: true });
|
||||||
});
|
});
|
||||||
|
@ -39,7 +39,6 @@ import {
|
|||||||
import { browseLocalMediaPlayer } from "../../data/media_source";
|
import { browseLocalMediaPlayer } from "../../data/media_source";
|
||||||
import { isTTSMediaSource } from "../../data/tts";
|
import { isTTSMediaSource } from "../../data/tts";
|
||||||
import { showAlertDialog } from "../../dialogs/generic/show-dialog-box";
|
import { showAlertDialog } from "../../dialogs/generic/show-dialog-box";
|
||||||
import { loadPolyfillIfNeeded } from "../../resources/resize-observer.polyfill";
|
|
||||||
import { haStyle } from "../../resources/styles";
|
import { haStyle } from "../../resources/styles";
|
||||||
import type { HomeAssistant } from "../../types";
|
import type { HomeAssistant } from "../../types";
|
||||||
import {
|
import {
|
||||||
@ -770,7 +769,6 @@ export class HaMediaPlayerBrowse extends LitElement {
|
|||||||
|
|
||||||
private async _attachResizeObserver(): Promise<void> {
|
private async _attachResizeObserver(): Promise<void> {
|
||||||
if (!this._resizeObserver) {
|
if (!this._resizeObserver) {
|
||||||
await loadPolyfillIfNeeded();
|
|
||||||
this._resizeObserver = new ResizeObserver(
|
this._resizeObserver = new ResizeObserver(
|
||||||
debounce(() => this._measureCard(), 250, false)
|
debounce(() => this._measureCard(), 250, false)
|
||||||
);
|
);
|
||||||
|
@ -25,7 +25,6 @@ import type {
|
|||||||
} from "../../../types";
|
} from "../../../types";
|
||||||
import "../../calendar/ha-full-calendar";
|
import "../../calendar/ha-full-calendar";
|
||||||
import { findEntities } from "../common/find-entities";
|
import { findEntities } from "../common/find-entities";
|
||||||
import { loadPolyfillIfNeeded } from "../../../resources/resize-observer.polyfill";
|
|
||||||
import "../components/hui-warning";
|
import "../components/hui-warning";
|
||||||
import type { LovelaceCard, LovelaceCardEditor } from "../types";
|
import type { LovelaceCard, LovelaceCardEditor } from "../types";
|
||||||
import type { CalendarCardConfig } from "./types";
|
import type { CalendarCardConfig } from "./types";
|
||||||
@ -210,7 +209,6 @@ export class HuiCalendarCard extends LitElement implements LovelaceCard {
|
|||||||
|
|
||||||
private async _attachObserver(): Promise<void> {
|
private async _attachObserver(): Promise<void> {
|
||||||
if (!this._resizeObserver) {
|
if (!this._resizeObserver) {
|
||||||
await loadPolyfillIfNeeded();
|
|
||||||
this._resizeObserver = new ResizeObserver(
|
this._resizeObserver = new ResizeObserver(
|
||||||
debounce(() => this._measureCard(), 250, false)
|
debounce(() => this._measureCard(), 250, false)
|
||||||
);
|
);
|
||||||
|
@ -38,7 +38,6 @@ import {
|
|||||||
import type { HomeAssistant } from "../../../types";
|
import type { HomeAssistant } from "../../../types";
|
||||||
import { findEntities } from "../common/find-entities";
|
import { findEntities } from "../common/find-entities";
|
||||||
import { hasConfigOrEntityChanged } from "../common/has-changed";
|
import { hasConfigOrEntityChanged } from "../common/has-changed";
|
||||||
import { loadPolyfillIfNeeded } from "../../../resources/resize-observer.polyfill";
|
|
||||||
import "../components/hui-marquee";
|
import "../components/hui-marquee";
|
||||||
import { createEntityNotFoundWarning } from "../components/hui-warning";
|
import { createEntityNotFoundWarning } from "../components/hui-warning";
|
||||||
import type { LovelaceCard, LovelaceCardEditor } from "../types";
|
import type { LovelaceCard, LovelaceCardEditor } from "../types";
|
||||||
@ -482,7 +481,6 @@ export class HuiMediaControlCard extends LitElement implements LovelaceCard {
|
|||||||
|
|
||||||
private async _attachObserver(): Promise<void> {
|
private async _attachObserver(): Promise<void> {
|
||||||
if (!this._resizeObserver) {
|
if (!this._resizeObserver) {
|
||||||
await loadPolyfillIfNeeded();
|
|
||||||
this._resizeObserver = new ResizeObserver(
|
this._resizeObserver = new ResizeObserver(
|
||||||
debounce(() => this._measureCard(), 250, false)
|
debounce(() => this._measureCard(), 250, false)
|
||||||
);
|
);
|
||||||
|
@ -31,7 +31,6 @@ import {
|
|||||||
weatherAttrIcons,
|
weatherAttrIcons,
|
||||||
weatherSVGStyles,
|
weatherSVGStyles,
|
||||||
} from "../../../data/weather";
|
} from "../../../data/weather";
|
||||||
import { loadPolyfillIfNeeded } from "../../../resources/resize-observer.polyfill";
|
|
||||||
import type { HomeAssistant } from "../../../types";
|
import type { HomeAssistant } from "../../../types";
|
||||||
import { actionHandler } from "../common/directives/action-handler-directive";
|
import { actionHandler } from "../common/directives/action-handler-directive";
|
||||||
import { findEntities } from "../common/find-entities";
|
import { findEntities } from "../common/find-entities";
|
||||||
@ -419,7 +418,6 @@ class HuiWeatherForecastCard extends LitElement implements LovelaceCard {
|
|||||||
|
|
||||||
private async _attachObserver(): Promise<void> {
|
private async _attachObserver(): Promise<void> {
|
||||||
if (!this._resizeObserver) {
|
if (!this._resizeObserver) {
|
||||||
await loadPolyfillIfNeeded();
|
|
||||||
this._resizeObserver = new ResizeObserver(
|
this._resizeObserver = new ResizeObserver(
|
||||||
debounce(() => this._measureCard(), 250, false)
|
debounce(() => this._measureCard(), 250, false)
|
||||||
);
|
);
|
||||||
|
@ -54,7 +54,6 @@ import "../../../components/ha-icon-button-next";
|
|||||||
import "../../../components/ha-icon-button-prev";
|
import "../../../components/ha-icon-button-prev";
|
||||||
import { EnergyData, getEnergyDataCollection } from "../../../data/energy";
|
import { EnergyData, getEnergyDataCollection } from "../../../data/energy";
|
||||||
import { SubscribeMixin } from "../../../mixins/subscribe-mixin";
|
import { SubscribeMixin } from "../../../mixins/subscribe-mixin";
|
||||||
import { loadPolyfillIfNeeded } from "../../../resources/resize-observer.polyfill";
|
|
||||||
import { HomeAssistant } from "../../../types";
|
import { HomeAssistant } from "../../../types";
|
||||||
|
|
||||||
@customElement("hui-energy-period-selector")
|
@customElement("hui-energy-period-selector")
|
||||||
@ -89,7 +88,6 @@ export class HuiEnergyPeriodSelector extends SubscribeMixin(LitElement) {
|
|||||||
|
|
||||||
private async _attachObserver(): Promise<void> {
|
private async _attachObserver(): Promise<void> {
|
||||||
if (!this._resizeObserver) {
|
if (!this._resizeObserver) {
|
||||||
await loadPolyfillIfNeeded();
|
|
||||||
this._resizeObserver = new ResizeObserver(
|
this._resizeObserver = new ResizeObserver(
|
||||||
debounce(() => this._measure(), 250, false)
|
debounce(() => this._measure(), 250, false)
|
||||||
);
|
);
|
||||||
|
@ -12,7 +12,6 @@ import "../../../components/ha-slider";
|
|||||||
import "../../../components/ha-textfield";
|
import "../../../components/ha-textfield";
|
||||||
import { isUnavailableState } from "../../../data/entity";
|
import { isUnavailableState } from "../../../data/entity";
|
||||||
import { setValue } from "../../../data/input_text";
|
import { setValue } from "../../../data/input_text";
|
||||||
import { loadPolyfillIfNeeded } from "../../../resources/resize-observer.polyfill";
|
|
||||||
import { HomeAssistant } from "../../../types";
|
import { HomeAssistant } from "../../../types";
|
||||||
import { hasConfigOrEntityChanged } from "../common/has-changed";
|
import { hasConfigOrEntityChanged } from "../common/has-changed";
|
||||||
import "../components/hui-generic-entity-row";
|
import "../components/hui-generic-entity-row";
|
||||||
@ -161,7 +160,6 @@ class HuiInputNumberEntityRow extends LitElement implements LovelaceRow {
|
|||||||
|
|
||||||
private async _attachObserver(): Promise<void> {
|
private async _attachObserver(): Promise<void> {
|
||||||
if (!this._resizeObserver) {
|
if (!this._resizeObserver) {
|
||||||
await loadPolyfillIfNeeded();
|
|
||||||
this._resizeObserver = new ResizeObserver(
|
this._resizeObserver = new ResizeObserver(
|
||||||
debounce(() => this._measureCard(), 250, false)
|
debounce(() => this._measureCard(), 250, false)
|
||||||
);
|
);
|
||||||
|
@ -33,7 +33,6 @@ import {
|
|||||||
MediaPlayerEntityFeature,
|
MediaPlayerEntityFeature,
|
||||||
computeMediaDescription,
|
computeMediaDescription,
|
||||||
} from "../../../data/media-player";
|
} from "../../../data/media-player";
|
||||||
import { loadPolyfillIfNeeded } from "../../../resources/resize-observer.polyfill";
|
|
||||||
import type { HomeAssistant } from "../../../types";
|
import type { HomeAssistant } from "../../../types";
|
||||||
import { hasConfigOrEntityChanged } from "../common/has-changed";
|
import { hasConfigOrEntityChanged } from "../common/has-changed";
|
||||||
import "../components/hui-generic-entity-row";
|
import "../components/hui-generic-entity-row";
|
||||||
@ -291,7 +290,6 @@ class HuiMediaPlayerEntityRow extends LitElement implements LovelaceRow {
|
|||||||
|
|
||||||
private async _attachObserver(): Promise<void> {
|
private async _attachObserver(): Promise<void> {
|
||||||
if (!this._resizeObserver) {
|
if (!this._resizeObserver) {
|
||||||
await loadPolyfillIfNeeded();
|
|
||||||
this._resizeObserver = new ResizeObserver(
|
this._resizeObserver = new ResizeObserver(
|
||||||
debounce(() => this._measureCard(), 250, false)
|
debounce(() => this._measureCard(), 250, false)
|
||||||
);
|
);
|
||||||
|
@ -12,7 +12,6 @@ import "../../../components/ha-slider";
|
|||||||
import "../../../components/ha-textfield";
|
import "../../../components/ha-textfield";
|
||||||
import { UNAVAILABLE } from "../../../data/entity";
|
import { UNAVAILABLE } from "../../../data/entity";
|
||||||
import { setValue } from "../../../data/input_text";
|
import { setValue } from "../../../data/input_text";
|
||||||
import { loadPolyfillIfNeeded } from "../../../resources/resize-observer.polyfill";
|
|
||||||
import { HomeAssistant } from "../../../types";
|
import { HomeAssistant } from "../../../types";
|
||||||
import { hasConfigOrEntityChanged } from "../common/has-changed";
|
import { hasConfigOrEntityChanged } from "../common/has-changed";
|
||||||
import "../components/hui-generic-entity-row";
|
import "../components/hui-generic-entity-row";
|
||||||
@ -167,7 +166,6 @@ class HuiNumberEntityRow extends LitElement implements LovelaceRow {
|
|||||||
|
|
||||||
private async _attachObserver(): Promise<void> {
|
private async _attachObserver(): Promise<void> {
|
||||||
if (!this._resizeObserver) {
|
if (!this._resizeObserver) {
|
||||||
await loadPolyfillIfNeeded();
|
|
||||||
this._resizeObserver = new ResizeObserver(
|
this._resizeObserver = new ResizeObserver(
|
||||||
debounce(() => this._measureCard(), 250, false)
|
debounce(() => this._measureCard(), 250, false)
|
||||||
);
|
);
|
||||||
|
@ -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)
|
// 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 "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
|
// Source: https://github.com/jserz/js_piece/blob/master/DOM/ParentNode/append()/append().md
|
||||||
(function (arr) {
|
(function (arr) {
|
||||||
arr.forEach((item) => {
|
arr.forEach((item) => {
|
||||||
|
9
src/resources/polyfills/resize-observer.ts
Normal file
9
src/resources/polyfills/resize-observer.ts
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
if (typeof window.ResizeObserver !== "function") {
|
||||||
|
window.ResizeObserver = (
|
||||||
|
await import(
|
||||||
|
"@lit-labs/virtualizer/polyfills/resize-observer-polyfill/ResizeObserver"
|
||||||
|
)
|
||||||
|
).default;
|
||||||
|
}
|
||||||
|
|
||||||
|
export {};
|
@ -1,12 +0,0 @@
|
|||||||
export const loadPolyfillIfNeeded = async () => {
|
|
||||||
try {
|
|
||||||
// eslint-disable-next-line no-new
|
|
||||||
new ResizeObserver(() => {});
|
|
||||||
} catch (e) {
|
|
||||||
window.ResizeObserver = (
|
|
||||||
await import(
|
|
||||||
"@lit-labs/virtualizer/polyfills/resize-observer-polyfill/ResizeObserver"
|
|
||||||
)
|
|
||||||
).default;
|
|
||||||
}
|
|
||||||
};
|
|
@ -1,6 +1,3 @@
|
|||||||
import { loadPolyfillIfNeeded } from "./resize-observer.polyfill";
|
|
||||||
|
|
||||||
export const loadVirtualizer = async () => {
|
export const loadVirtualizer = async () => {
|
||||||
await loadPolyfillIfNeeded();
|
|
||||||
await import("@lit-labs/virtualizer");
|
await import("@lit-labs/virtualizer");
|
||||||
};
|
};
|
||||||
|
@ -7,7 +7,6 @@ import "../components/ha-slider";
|
|||||||
import "../components/ha-textfield";
|
import "../components/ha-textfield";
|
||||||
import { isUnavailableState } from "../data/entity";
|
import { isUnavailableState } from "../data/entity";
|
||||||
import { setValue } from "../data/input_text";
|
import { setValue } from "../data/input_text";
|
||||||
import { loadPolyfillIfNeeded } from "../resources/resize-observer.polyfill";
|
|
||||||
import { HomeAssistant } from "../types";
|
import { HomeAssistant } from "../types";
|
||||||
|
|
||||||
@customElement("state-card-input_number")
|
@customElement("state-card-input_number")
|
||||||
@ -132,7 +131,6 @@ class StateCardInputNumber extends LitElement {
|
|||||||
|
|
||||||
private async _attachObserver(): Promise<void> {
|
private async _attachObserver(): Promise<void> {
|
||||||
if (!this._resizeObserver) {
|
if (!this._resizeObserver) {
|
||||||
await loadPolyfillIfNeeded();
|
|
||||||
this._resizeObserver = new ResizeObserver(
|
this._resizeObserver = new ResizeObserver(
|
||||||
debounce(() => this._measureCard(), 250, false)
|
debounce(() => this._measureCard(), 250, false)
|
||||||
);
|
);
|
||||||
|
@ -6,7 +6,6 @@ import "../components/ha-slider";
|
|||||||
import "../components/ha-textfield";
|
import "../components/ha-textfield";
|
||||||
import { HomeAssistant } from "../types";
|
import { HomeAssistant } from "../types";
|
||||||
import { haStyle } from "../resources/styles";
|
import { haStyle } from "../resources/styles";
|
||||||
import { loadPolyfillIfNeeded } from "../resources/resize-observer.polyfill";
|
|
||||||
import { isUnavailableState } from "../data/entity";
|
import { isUnavailableState } from "../data/entity";
|
||||||
import { debounce } from "../common/util/debounce";
|
import { debounce } from "../common/util/debounce";
|
||||||
|
|
||||||
@ -109,7 +108,6 @@ class StateCardNumber extends LitElement {
|
|||||||
|
|
||||||
private async _attachObserver(): Promise<void> {
|
private async _attachObserver(): Promise<void> {
|
||||||
if (!this._resizeObserver) {
|
if (!this._resizeObserver) {
|
||||||
await loadPolyfillIfNeeded();
|
|
||||||
this._resizeObserver = new ResizeObserver(
|
this._resizeObserver = new ResizeObserver(
|
||||||
debounce(() => this._measureCard(), 250, false)
|
debounce(() => this._measureCard(), 250, false)
|
||||||
);
|
);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user