Skip to content
e-dream-aiPublic

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

@infinidream/shared

Code shared by the infinidream web apps (frontend and studio): API hooks, auth, the socket connection, providers, UI components, styles and translations.

It is published to npm as compiled ES modules with type declarations, for apps built with Vite and React 19.

Install

pnpm add @infinidream/shared

The app must also install the peer dependencies listed in package.json (React, styled-components, React Query, React Router, i18next, react-i18next, react-toastify, Zod, Bugsnag and react-ga4). They are peers so the app and the library share one copy of each.

Use in an app

Import modules by their path, there is no root entry:

import { SharedProviders } from "@infinidream/shared/context/shared-providers";
import { useAuth } from "@infinidream/shared/hooks/useAuth";

Start the library in the app's entry file, before rendering:

import { configureShared } from "@infinidream/shared/config/shared-config";
import ImageEncodeWorker from "@infinidream/shared/utils/image-encode/image-encode.worker?worker";
import { initI18n } from "@infinidream/shared/i18n/init-i18n";
import { startTelemetry } from "@infinidream/shared/telemetry/telemetry";

configureShared({
  apiUrl: import.meta.env.VITE_BACKEND_URL,
  socketUrl: import.meta.env.VITE_SOCKET_URL,
  clientVersion: import.meta.env.VITE_COMMIT_REF,
  environment: "production",
  createImageEncodeWorker: () => new ImageEncodeWorker(),
});
initI18n(appStrings);
const ErrorBoundary = startTelemetry({
  bugsnagApiKey: "...",
  gaMeasurementId: "...",
  gtmId: "...",
});

Then render StyledProvider, ToastRoot and SharedProviders around the app.

createImageEncodeWorker lets the library resize images and convert them to WebP in a worker before uploading. The app creates the worker, here with Vite's ?worker import, so the library never builds a worker URL itself and Vite can pre-bundle it like any other dependency. Without it, images are uploaded as they are.

Two more things the app needs:

  • Theme typing. Add a declaration file to the app's src, for example src/styled-theme.d.ts, containing import "@infinidream/shared/types/styled-theme";. It types props.theme in the app's own styled components.
  • Vite. Set worker: { format: "es" }, because the image encoding worker loads its WebP encoder with a dynamic import. Keep @jsquash/webp out of dependency pre-bundling (optimizeDeps: { exclude: ["@jsquash/webp"] }), as jSquash asks. The library itself needs no Vite settings.

Develop

pnpm install
pnpm test            # vitest
pnpm type-check
pnpm lint
pnpm build           # compiles src to dist
pnpm check:package   # checks the packed package with publint and Are The Types Wrong

Inside src, import from other folders with the @/ alias (@/hooks/useAuth) and from the same folder with ./. The build rewrites both to relative paths ending in .js, so the compiled files also load outside a bundler, for example in an app's tests. ESLint reports a ../ import.

To try a change in an app before releasing it, build the library and point the app at your local copy, for example with pnpm link ../shared from the app, and run pnpm build here after each change.

Release

Every merge to main that changes the code publishes a new patch version to npm. Nobody bumps a version by hand for that.

  • The publish workflow runs the checks, takes the highest published x.y.* version for the x.y in package.json, adds one, publishes it and creates a GitHub release with that tag (v0.1.7).
  • The version in package.json only carries the major and minor number. For a new minor or major version, change it there, for example to 0.2.0; the next merge publishes exactly that, and patches continue from it.
  • Apps pin an exact version. To take a new one, run pnpm up @infinidream/shared --latest in the app.

License

AGPL-3.0

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages