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.
pnpm add @infinidream/sharedThe 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.
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 examplesrc/styled-theme.d.ts, containingimport "@infinidream/shared/types/styled-theme";. It typesprops.themein 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/webpout of dependency pre-bundling (optimizeDeps: { exclude: ["@jsquash/webp"] }), as jSquash asks. The library itself needs no Vite settings.
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 WrongInside 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.
Every merge to main that changes the code publishes a new patch version to npm. Nobody bumps a version by hand for that.
- The
publishworkflow runs the checks, takes the highest publishedx.y.*version for thex.yinpackage.json, adds one, publishes it and creates a GitHub release with that tag (v0.1.7). - The version in
package.jsononly carries the major and minor number. For a new minor or major version, change it there, for example to0.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 --latestin the app.