Writing Packages
Introduction
Freestyle enables a new kind of opensource package: Full Stack Feature Packages. Full Stack Feature Packages come with a whole feature: frontend, backend, and cloudstate, all in one package. When a user installs them, the backend can be deployed in their backend, and the components can be used in their frontend. This removes the need for third-party services, allows for better customization, and empowers opensource developers to share features as packages.
Writing a Package
Prerequisites
Setting up a new package
-
Create a new package
-
Install Freestyle:
Writing the Package
To show you how to write a package, we will write a simple counter package. The package will have a counter that can be incremented and decremented.
-
Create a new file
src/index.ts
: -
Create the frontend in
src/Counter.tsx
: -
Configure
package.json
:Fullstack Feature Packages can support multiple frontend frameworks from the same package, we recommend you configure
exports
in yourpackage.json
.This pattern allows your single package to support multiple frontend frameworks and to work with ones you haven’t built yet.
-
Publish your package:
Advantages
-
Extensibility: Users can extend your package by adding new features or modifying existing ones.
For example, a user could add a
reset
function to theSimpleCounter
class: -
Composability: Database packages for other databases never took off, because they were too hard to make work with your other code. Your packages automatically fit onto the user’s backend, frontend, and other packages.
-
Integration: Many existing opensource providers provide packages that need to be hosted separately from users’ code. Your packages can be installed and run in the user’s codebase.