From 383a692042ef50be32518cff94dac9172bfb4829 Mon Sep 17 00:00:00 2001 From: Kristóf Marussy Date: Thu, 27 Jan 2022 03:14:27 +0100 Subject: chore: Annotate shared packages for purity MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Enabled better tree shaking and smaller bundle sizes by excluding mobx-state-tree and zod dependencies whenever possible. Signed-off-by: Kristóf Marussy --- packages/service-shared/esbuild.config.js | 2 + packages/service-shared/src/schemas.ts | 18 +++++---- packages/shared/esbuild.config.js | 2 + packages/shared/src/schemas.ts | 58 ++++++++++++--------------- packages/shared/src/stores/GlobalSettings.ts | 10 +++-- packages/shared/src/stores/Profile.ts | 9 +++-- packages/shared/src/stores/ProfileSettings.ts | 7 ++-- packages/shared/src/stores/Service.ts | 29 +++++++------- packages/shared/src/stores/ServiceSettings.ts | 13 +++--- packages/shared/src/stores/SharedStore.ts | 19 ++++----- 10 files changed, 88 insertions(+), 79 deletions(-) diff --git a/packages/service-shared/esbuild.config.js b/packages/service-shared/esbuild.config.js index 62e3d2a..6f639e3 100644 --- a/packages/service-shared/esbuild.config.js +++ b/packages/service-shared/esbuild.config.js @@ -10,6 +10,8 @@ export default getEsbuildConfig({ // The package that includes this one will have a header comment, // no need to have an additional one here. banner: {}, + // Keep @__PURE__ annotations intact. + minify: false, platform: 'node', target: [chrome, node], external: ['zod'], diff --git a/packages/service-shared/src/schemas.ts b/packages/service-shared/src/schemas.ts index 0b31eb7..bb1926f 100644 --- a/packages/service-shared/src/schemas.ts +++ b/packages/service-shared/src/schemas.ts @@ -20,10 +20,11 @@ import { z } from 'zod'; -export const UnreadCount = z.object({ - direct: z.number().nonnegative().optional(), - indirect: z.number().nonnegative().optional(), -}); +export const UnreadCount = /* @__PURE__ */ (() => + z.object({ + direct: z.number().nonnegative().optional(), + indirect: z.number().nonnegative().optional(), + }))(); /* eslint-disable-next-line @typescript-eslint/no-redeclare -- @@ -31,10 +32,11 @@ export const UnreadCount = z.object({ */ export type UnreadCount = z.infer; -export const WebSource = z.object({ - code: z.string(), - url: z.string().nonempty(), -}); +export const WebSource = /* @__PURE__ */ (() => + z.object({ + code: z.string(), + url: z.string().nonempty(), + }))(); /* eslint-disable-next-line @typescript-eslint/no-redeclare -- diff --git a/packages/shared/esbuild.config.js b/packages/shared/esbuild.config.js index 7a79ce6..e8dcc2f 100644 --- a/packages/shared/esbuild.config.js +++ b/packages/shared/esbuild.config.js @@ -10,6 +10,8 @@ export default getEsbuildConfig({ // The package that includes this one will have a header comment, // no need to have an additional one here. banner: {}, + // Keep @__PURE__ annotations intact. + minify: false, platform: 'node', target: [chrome, node], external: ['mobx', 'mobx-state-tree', 'zod'], diff --git a/packages/shared/src/schemas.ts b/packages/shared/src/schemas.ts index edf3741..1e734b2 100644 --- a/packages/shared/src/schemas.ts +++ b/packages/shared/src/schemas.ts @@ -20,17 +20,13 @@ import { z } from 'zod'; -const SetSelectedServiceId = z.object({ - action: z.literal('set-selected-service-id'), - serviceId: z.string(), -}); - -export const BrowserViewBounds = z.object({ - x: z.number().int().nonnegative(), - y: z.number().int().nonnegative(), - width: z.number().int().nonnegative(), - height: z.number().int().nonnegative(), -}); +export const BrowserViewBounds = /* @__PURE__ */ (() => + z.object({ + x: z.number().int().nonnegative(), + y: z.number().int().nonnegative(), + width: z.number().int().nonnegative(), + height: z.number().int().nonnegative(), + }))(); /* eslint-disable-next-line @typescript-eslint/no-redeclare -- @@ -38,12 +34,7 @@ export const BrowserViewBounds = z.object({ */ export type BrowserViewBounds = z.infer; -const SetBrowserViewBoundsAction = z.object({ - action: z.literal('set-browser-view-bounds'), - browserViewBounds: BrowserViewBounds, -}); - -export const ThemeSource = z.enum(['system', 'light', 'dark']); +export const ThemeSource = /* @__PURE__ */ z.enum(['system', 'light', 'dark']); /* eslint-disable-next-line @typescript-eslint/no-redeclare -- @@ -51,21 +42,24 @@ export const ThemeSource = z.enum(['system', 'light', 'dark']); */ export type ThemeSource = z.infer; -const SetThemeSourceAction = z.object({ - action: z.literal('set-theme-source'), - themeSource: ThemeSource, -}); - -const ReloadAllServicesAction = z.object({ - action: z.literal('reload-all-services'), -}); - -export const Action = z.union([ - SetSelectedServiceId, - SetBrowserViewBoundsAction, - SetThemeSourceAction, - ReloadAllServicesAction, -]); +export const Action = /* @__PURE__ */ (() => + z.union([ + z.object({ + action: z.literal('set-selected-service-id'), + serviceId: z.string(), + }), + z.object({ + action: z.literal('set-browser-view-bounds'), + browserViewBounds: BrowserViewBounds, + }), + z.object({ + action: z.literal('set-theme-source'), + themeSource: ThemeSource, + }), + z.object({ + action: z.literal('reload-all-services'), + }), + ]))(); /* eslint-disable-next-line @typescript-eslint/no-redeclare -- diff --git a/packages/shared/src/stores/GlobalSettings.ts b/packages/shared/src/stores/GlobalSettings.ts index 3a813b8..1c6d7f3 100644 --- a/packages/shared/src/stores/GlobalSettings.ts +++ b/packages/shared/src/stores/GlobalSettings.ts @@ -22,9 +22,13 @@ import { Instance, types, SnapshotIn, SnapshotOut } from 'mobx-state-tree'; import { ThemeSource } from '../schemas'; -const GlobalSettings = types.model('GlobalSettings', { - themeSource: types.optional(types.enumeration(ThemeSource.options), 'system'), -}); +const GlobalSettings = /* @__PURE__ */ (() => + types.model('GlobalSettings', { + themeSource: types.optional( + types.enumeration(ThemeSource.options), + 'system', + ), + }))(); /* eslint-disable-next-line @typescript-eslint/no-redeclare -- diff --git a/packages/shared/src/stores/Profile.ts b/packages/shared/src/stores/Profile.ts index 256c33e..49c5195 100644 --- a/packages/shared/src/stores/Profile.ts +++ b/packages/shared/src/stores/Profile.ts @@ -22,10 +22,11 @@ import { Instance, types } from 'mobx-state-tree'; import ProfileSettings from './ProfileSettings'; -const Profile = types.model('Profile', { - id: types.identifier, - settings: ProfileSettings, -}); +const Profile = /* @__PURE__ */ (() => + types.model('Profile', { + id: types.identifier, + settings: ProfileSettings, + }))(); /* eslint-disable-next-line @typescript-eslint/no-redeclare -- diff --git a/packages/shared/src/stores/ProfileSettings.ts b/packages/shared/src/stores/ProfileSettings.ts index 9f2b27c..2ce5d73 100644 --- a/packages/shared/src/stores/ProfileSettings.ts +++ b/packages/shared/src/stores/ProfileSettings.ts @@ -20,9 +20,10 @@ import { Instance, types, SnapshotIn, SnapshotOut } from 'mobx-state-tree'; -const ProfileSettings = types.model('ProfileSettings', { - name: types.string, -}); +const ProfileSettings = /* @__PURE__ */ (() => + types.model('ProfileSettings', { + name: types.string, + }))(); /* eslint-disable-next-line @typescript-eslint/no-redeclare -- diff --git a/packages/shared/src/stores/Service.ts b/packages/shared/src/stores/Service.ts index 4a7334d..a4e3c92 100644 --- a/packages/shared/src/stores/Service.ts +++ b/packages/shared/src/stores/Service.ts @@ -22,20 +22,21 @@ import { Instance, types } from 'mobx-state-tree'; import ServiceSettings from './ServiceSettings'; -const Service = types.model('Service', { - id: types.identifier, - settings: ServiceSettings, - currentUrl: types.maybe(types.string), - canGoBack: false, - canGoForward: false, - title: types.maybe(types.string), - state: types.optional( - types.enumeration('ServiceState', ['loading', 'loaded', 'crashed']), - 'loading', - ), - directMessageCount: 0, - indirectMessageCount: 0, -}); +const Service = /* @__PURE__ */ (() => + types.model('Service', { + id: types.identifier, + settings: ServiceSettings, + currentUrl: types.maybe(types.string), + canGoBack: false, + canGoForward: false, + title: types.maybe(types.string), + state: types.optional( + types.enumeration('ServiceState', ['loading', 'loaded', 'crashed']), + 'loading', + ), + directMessageCount: 0, + indirectMessageCount: 0, + }))(); /* eslint-disable-next-line @typescript-eslint/no-redeclare -- diff --git a/packages/shared/src/stores/ServiceSettings.ts b/packages/shared/src/stores/ServiceSettings.ts index 6ba1dfa..a5811f5 100644 --- a/packages/shared/src/stores/ServiceSettings.ts +++ b/packages/shared/src/stores/ServiceSettings.ts @@ -22,12 +22,13 @@ import { Instance, types, SnapshotIn, SnapshotOut } from 'mobx-state-tree'; import Profile from './Profile'; -const ServiceSettings = types.model('ServiceSettings', { - name: types.string, - profile: types.reference(Profile), - // TODO: Remove this once recipes are added. - url: types.string, -}); +const ServiceSettings = /* @__PURE__ */ (() => + types.model('ServiceSettings', { + name: types.string, + profile: types.reference(Profile), + // TODO: Remove this once recipes are added. + url: types.string, + }))(); /* eslint-disable-next-line @typescript-eslint/no-redeclare -- diff --git a/packages/shared/src/stores/SharedStore.ts b/packages/shared/src/stores/SharedStore.ts index 0cac3a5..4e064c6 100644 --- a/packages/shared/src/stores/SharedStore.ts +++ b/packages/shared/src/stores/SharedStore.ts @@ -30,15 +30,16 @@ import GlobalSettings from './GlobalSettings'; import Profile from './Profile'; import Service from './Service'; -const SharedStore = types.model('SharedStore', { - settings: types.optional(GlobalSettings, {}), - profilesById: types.map(Profile), - profiles: types.array(types.reference(Profile)), - servicesById: types.map(Service), - services: types.array(types.reference(Service)), - selectedService: types.safeReference(Service), - shouldUseDarkColors: false, -}); +const SharedStore = /* @__PURE__ */ (() => + types.model('SharedStore', { + settings: types.optional(GlobalSettings, {}), + profilesById: types.map(Profile), + profiles: types.array(types.reference(Profile)), + servicesById: types.map(Service), + services: types.array(types.reference(Service)), + selectedService: types.safeReference(Service), + shouldUseDarkColors: false, + }))(); /* eslint-disable-next-line @typescript-eslint/no-redeclare -- -- cgit v1.2.3-54-g00ecf