From c039845c16255cfe1b167c2eaa76d9c4efb042df Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Fri, 26 Mar 2021 20:23:43 +1000 Subject: [PATCH] use singleton + free functions for i18n in ts This allows for tree shaking, and reduces the congrats page from 150k with the old enum solution to about 80k. --- ts/congrats/CongratsPage.svelte | 21 +++++----- ts/congrats/index.ts | 4 +- ts/congrats/lib.ts | 12 +++--- ts/graphs/AddedGraph.svelte | 14 +++---- ts/graphs/ButtonsGraph.svelte | 13 +++---- ts/graphs/CalendarGraph.svelte | 9 ++--- ts/graphs/CardCounts.svelte | 9 ++--- ts/graphs/EaseGraph.svelte | 13 +++---- ts/graphs/FutureDue.svelte | 17 ++++---- ts/graphs/GraphRangeRadios.svelte | 11 +++--- ts/graphs/GraphsPage.svelte | 7 ++-- ts/graphs/HistogramGraph.svelte | 6 +-- ts/graphs/HourGraph.svelte | 13 +++---- ts/graphs/IntervalsGraph.svelte | 17 ++++---- ts/graphs/NoDataOverlay.svelte | 5 +-- ts/graphs/RangeBox.svelte | 13 +++---- ts/graphs/ReviewsGraph.svelte | 20 +++++----- ts/graphs/TableData.svelte | 4 +- ts/graphs/TodayStats.svelte | 4 +- ts/graphs/added.ts | 20 +++++----- ts/graphs/buttons.ts | 16 ++++---- ts/graphs/calendar.ts | 6 +-- ts/graphs/card-counts.ts | 27 +++++++------ ts/graphs/ease.ts | 8 ++-- ts/graphs/future-due.ts | 22 +++++------ ts/graphs/hours.ts | 7 ++-- ts/graphs/index.ts | 3 +- ts/graphs/intervals.ts | 19 +++++---- ts/graphs/reviews.ts | 60 +++++++++++++---------------- ts/graphs/today.ts | 19 ++++----- ts/lib/BUILD.bazel | 4 +- ts/lib/genfluent.py | 14 +++---- ts/lib/{i18n.ts => i18n_helpers.ts} | 16 ++++---- ts/lib/time.ts | 45 +++++++++++----------- 34 files changed, 231 insertions(+), 267 deletions(-) rename ts/lib/{i18n.ts => i18n_helpers.ts} (86%) diff --git a/ts/congrats/CongratsPage.svelte b/ts/congrats/CongratsPage.svelte index 908339fef..5f3a2f842 100644 --- a/ts/congrats/CongratsPage.svelte +++ b/ts/congrats/CongratsPage.svelte @@ -1,23 +1,22 @@ diff --git a/ts/congrats/index.ts b/ts/congrats/index.ts index e56123e56..d134011a3 100644 --- a/ts/congrats/index.ts +++ b/ts/congrats/index.ts @@ -9,10 +9,10 @@ import CongratsPage from "./CongratsPage.svelte"; export async function congrats(target: HTMLDivElement): Promise { checkNightMode(); - const i18n = await setupI18n(); + await setupI18n(); const info = await getCongratsInfo(); new CongratsPage({ target, - props: { info, i18n }, + props: { info }, }); } diff --git a/ts/congrats/lib.ts b/ts/congrats/lib.ts index a50c74665..d587a7645 100644 --- a/ts/congrats/lib.ts +++ b/ts/congrats/lib.ts @@ -4,7 +4,8 @@ import pb from "anki/backend_proto"; import { postRequest } from "anki/postrequest"; import { naturalUnit, unitAmount, unitName } from "anki/time"; -import type { I18n } from "anki/i18n"; + +import * as tr from "anki/i18n"; export async function getCongratsInfo(): Promise { return pb.BackendProto.CongratsInfoOut.decode( @@ -12,10 +13,7 @@ export async function getCongratsInfo(): Promise 86_400) { @@ -25,11 +23,11 @@ export function buildNextLearnMsg( const unit = naturalUnit(secsUntil); const amount = Math.round(unitAmount(unit, secsUntil)); const unitStr = unitName(unit); - const nextLearnDue = i18n.schedulingNextLearnDue({ + const nextLearnDue = tr.schedulingNextLearnDue({ amount, unit: unitStr, }); - const remaining = i18n.schedulingLearnRemaining({ + const remaining = tr.schedulingLearnRemaining({ remaining: info.learnRemaining, }); return `${nextLearnDue} ${remaining}`; diff --git a/ts/graphs/AddedGraph.svelte b/ts/graphs/AddedGraph.svelte index 3d7e9208a..3c82b8ee7 100644 --- a/ts/graphs/AddedGraph.svelte +++ b/ts/graphs/AddedGraph.svelte @@ -1,5 +1,4 @@ - + - + - + diff --git a/ts/graphs/ButtonsGraph.svelte b/ts/graphs/ButtonsGraph.svelte index d6c6147d5..ea2a61b49 100644 --- a/ts/graphs/ButtonsGraph.svelte +++ b/ts/graphs/ButtonsGraph.svelte @@ -1,6 +1,5 @@ - + - + diff --git a/ts/graphs/CalendarGraph.svelte b/ts/graphs/CalendarGraph.svelte index 3865745ed..3d352020f 100644 --- a/ts/graphs/CalendarGraph.svelte +++ b/ts/graphs/CalendarGraph.svelte @@ -1,6 +1,6 @@ @@ -88,6 +87,6 @@ - + diff --git a/ts/graphs/CardCounts.svelte b/ts/graphs/CardCounts.svelte index 4e4f8100e..06aaf4feb 100644 --- a/ts/graphs/CardCounts.svelte +++ b/ts/graphs/CardCounts.svelte @@ -1,7 +1,6 @@