Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions build-scripts/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ module.exports.babelOptions = ({ latestBuild }) => ({
!latestBuild && [
require("@babel/preset-env").default,
{
modules: false,
useBuiltIns: "entry",
corejs: "3.6",
},
Expand All @@ -71,7 +70,6 @@ module.exports.babelOptions = ({ latestBuild }) => ({
// Only support the syntax, Webpack will handle it.
"@babel/plugin-syntax-import-meta",
"@babel/plugin-syntax-dynamic-import",
"@babel/plugin-syntax-top-level-await",
"@babel/plugin-proposal-optional-chaining",
"@babel/plugin-proposal-nullish-coalescing-operator",
[
Expand Down
12 changes: 9 additions & 3 deletions build-scripts/webpack.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,6 @@ const createWebpackConfig = ({
}),
],
},
experiments: {
topLevelAwait: true,
},
plugins: [
new ManifestPlugin({
// Only include the JS of entrypoints
Expand Down Expand Up @@ -98,6 +95,15 @@ const createWebpackConfig = ({
new RegExp(bundle.emptyPackages({ latestBuild }).join("|")),
path.resolve(paths.polymer_dir, "src/util/empty.js")
),
// We need to change the import of the polyfill for EventTarget, so we replace the polyfill file with our customized one
new webpack.NormalModuleReplacementPlugin(
new RegExp(
require.resolve(
"lit-virtualizer/lib/uni-virtualizer/lib/polyfillLoaders/EventTarget.js"
)
),
path.resolve(paths.polymer_dir, "src/resources/EventTarget-ponyfill.js")
),
],
resolve: {
extensions: [".ts", ".js", ".json"],
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@
"@babel/plugin-proposal-optional-chaining": "^7.11.0",
"@babel/plugin-syntax-dynamic-import": "^7.8.3",
"@babel/plugin-syntax-import-meta": "^7.10.4",
"@babel/plugin-syntax-top-level-await": "^7.10.4",
"@babel/preset-env": "^7.11.5",
"@babel/preset-typescript": "^7.10.4",
"@rollup/plugin-commonjs": "^11.1.0",
Expand Down
17 changes: 12 additions & 5 deletions src/common/translations/localize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@ export interface FormatsType {
time: FormatType;
}

if (shouldPolyfill()) {
await import("@formatjs/intl-pluralrules/polyfill-locales");
}
let polyfillLoaded = !shouldPolyfill();
const polyfillProm = polyfillLoaded
? import("@formatjs/intl-pluralrules/polyfill-locales").then(() => {
polyfillLoaded = true;
})
: undefined;

/**
* Adapted from Polymer app-localize-behavior.
Expand All @@ -38,12 +41,16 @@ if (shouldPolyfill()) {
* }
*/

export const computeLocalize = (
export const computeLocalize = async (
cache: any,
language: string,
resources: Resources,
formats?: FormatsType
): LocalizeFunc => {
): Promise<LocalizeFunc> => {
if (!polyfillLoaded) {
await polyfillProm;
}

// Everytime any of the parameters change, invalidate the strings cache.
cache._localizationCache = {};

Expand Down
4 changes: 2 additions & 2 deletions src/fake_data/provide_hass.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export const provideHass = (

function updateTranslations(fragment: null | string, language?: string) {
const lang = language || getLocalLanguage();
getTranslation(fragment, lang).then((translation) => {
getTranslation(fragment, lang).then(async (translation) => {
const resources = {
[lang]: {
...(hass().resources && hass().resources[lang]),
Expand All @@ -64,7 +64,7 @@ export const provideHass = (
};
hass().updateHass({
resources,
localize: computeLocalize(elements[0], lang, resources),
localize: await computeLocalize(elements[0], lang, resources),
});
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/managers/notification-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class NotificationManager extends LitElement {

@internalProperty() private _noCancelOnOutsideClick = false;

@query("ha-toast", true) private _toast!: HaToast;
@query("ha-toast") private _toast!: HaToast;

public async showDialog({
message,
Expand Down
6 changes: 4 additions & 2 deletions src/mixins/lit-localize-lite-mixin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,13 @@ export const litLocalizeLiteMixin = <T extends Constructor<LitElement>>(
(changedProperties.has("language") ||
changedProperties.has("resources"))
) {
this.localize = computeLocalize(
computeLocalize(
this.constructor.prototype,
this.language,
this.resources
);
).then((localize) => {
this.localize = localize;
});
}
}

Expand Down
14 changes: 14 additions & 0 deletions src/resources/EventTarget-ponyfill.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
let ET;
export default async function EventTarget() {
return ET || init();
}
async function init() {
ET = window.EventTarget;
try {
// eslint-disable-next-line no-new
new ET();
} catch (_a) {
ET = (await import("event-target-shim")).default.EventTarget;
}
return ET;
}
8 changes: 4 additions & 4 deletions src/state/translations-mixin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ export default <T extends Constructor<HassBaseEl>>(superClass: T) =>
return this.hass!.localize;
}

this._updateResources(language, resources);
await this._updateResources(language, resources);
return this.hass!.localize;
}

Expand Down Expand Up @@ -187,7 +187,7 @@ export default <T extends Constructor<HassBaseEl>>(superClass: T) =>
return this.hass!.localize;
}

this._updateResources(language, resources);
await this._updateResources(language, resources);
return this.hass!.localize;
}

Expand Down Expand Up @@ -216,7 +216,7 @@ export default <T extends Constructor<HassBaseEl>>(superClass: T) =>
}
}

private _updateResources(language: string, data: any) {
private async _updateResources(language: string, data: any) {
// Update the language in hass, and update the resources with the newly
// loaded resources. This merges the new data on top of the old data for
// this language, so that the full translation set can be loaded across
Expand All @@ -229,7 +229,7 @@ export default <T extends Constructor<HassBaseEl>>(superClass: T) =>
};
const changes: Partial<HomeAssistant> = { resources };
if (this.hass && language === this.hass.language) {
changes.localize = computeLocalize(this, language, resources);
changes.localize = await computeLocalize(this, language, resources);
}
this._updateHass(changes);
}
Expand Down