Welcome to the Cloudflare Workers Monorepo Project Template! This repository provides a structured environment for developing, testing, and deploying Cloudflare Workers using a monorepo setup managed with Yarn workspaces. The project includes shared utilities and a worker template to expedite your serverless application development workflow.
The primary goal of this project is to offer a robust framework for creating and deploying serverless functions using Cloudflare Workers. It aims to provide a seamless development experience with shared resources, TypeScript support, and automated deployment scripts.
The purpose of this repository is to:
- ποΈ Enable fast and efficient development of serverless applications via Cloudflare Workers.
- π Promote code reuse through shared utilities.
- π οΈ Simplify the development and deployment process with comprehensive configurations and tools.
cloudflare-workers-monorepo/
βββ package.json
βββ tsconfig.json
βββ packages
βββ shared
β βββ index.ts
β βββ package.json
β βββ random.ts
βββ worker-template
βββ .dev.vars
βββ package.json
βββ tsconfig.json
βββ wrangler.toml
βββ src
βββ index.ts
package.json
: Root package management with Yarn workspaces. Includes scripts for various tasks.tsconfig.json
: TypeScript configuration for the entire monorepo.packages/shared
: This is an example of shared library packages.packages/worker-template
: This is an example of a worker..dev.vars
: Store local development secrets.src/index.ts
: Example worker function that utilizes shared utilities.package.json
: Worker package management. Set your worker name here.wrangler.toml
: Cloudflare Worker configuration file.
You can import dependencies from local packages using the @packages/<package_name>
alias. For example:
import { generateRandomNumber, generateRandomString } from "@packages/shared";
If you want to create a new package or worker, you can copy the worker-template
folder and adjust the names in both package.json
and wrangler.toml
. This approach avoids the need to run yarn create cloudflare
and set up everything from scratch.
- π§Ά Yarn (v4.3.1)
- π§ Node.js
- βοΈ Cloudflare Account (for deploying workers)
-
Clone this repository:
git clone https://github.com/husniadil/cloudflare-workers-monorepo-project-template cd cloudflare-workers-monorepo-project-template
-
Install dependencies:
yarn
You can alternatively create a new repository by using GitHub's "Use this template" feature, located at the top right corner.
In the root package.json
, several scripts are defined for managing the monorepo and Cloudflare Workers projects:
yarn w <workspace_name> <command> [additional_args...]
: A custom script to run a command in a specific workspace.yarn deploy
: Deploy all workspaces using Wrangler.yarn types
: Generate Cloudflare Workers type definitions for all workspaces.yarn format
: Format the codebase using Biome.
-
Run a command in a specific workspace:
yarn w worker-template dev
-
Deploy all workers:
yarn deploy
-
Generate type definitions for Cloudflare Workers:
yarn types
-
Format the codebase:
yarn format
The wrangler.toml
file is used to configure Cloudflare Workers. Below is a detailed breakdown of its contents:
name
: The name of the worker.compatibility_flags
: Compatibility flags for specific features.node_compat
: Enable Node.js compatibility.
name = "worker-template"
main = "src/index.ts"
compatibility_date = "2024-07-12"
compatibility_flags = ["nodejs_compat"]
vars = { ENVIRONMENT = "dev", KEY = "value" }
kv_namespaces = [
# Run yarn w worker-template wrangler kv:namespace create MY_KV_NAMESPACE, then copy the ID here
{ binding = "MY_KV_NAMESPACE", id = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" },
]
[env.staging]
workers_dev = true
vars = { ENVIRONMENT = "staging", KEY = "value" }
kv_namespaces = [
# Run yarn w worker-template wrangler kv:namespace create MY_KV_NAMESPACE --env staging, then copy the ID here
{ binding = "MY_KV_NAMESPACE", id = "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy" },
]
[env.production]
workers_dev = true
vars = { ENVIRONMENT = "production", KEY = "value" }
# Run yarn w worker-template wrangler kv:namespace create MY_KV_NAMESPACE --env production, then copy the ID here
kv_namespaces = [
{ binding = "MY_KV_NAMESPACE", id = "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz" },
]
To set a secret key for the appropriate environment, run the following commands. These commands will prompt you to input the secret value in the command line.
yarn w worker-template wrangler secret put SECRET_KEY
yarn w worker-template wrangler secret put SECRET_KEY -e staging
yarn w worker-template wrangler secret put SECRET_KEY -e production
For secrets used only in local development (not to be confused with the secrets used in the βdevβ environment), set the secret key as follows. This step is required because it will be used for typing (auto-generated after executing yarn types
). You can put this default value into the packages/<package_name>/.dev.vars
file, in this example packages/worker-template/.dev.vars
. Donβt worry, it will not overwrite the remote secret keys.
SECRET_KEY="value"
To deploy a worker, run the following command:
# dev environment
# https://worker-template.<your-account-name>.workers.dev
yarn w worker-template wrangler deploy
# staging environment
# https://worker-template-staging.<your-account-name>.workers.dev
yarn w worker-template wrangler deploy --env staging
# production environment
# https://worker-template-production.<your-account-name>.workers.dev
yarn w worker-template wrangler deploy --env production
For better development experience, you can install the Biome extension for Visual Studio Code. This extension helps with code formatting and linting directly within your editor.
We welcome contributions! Please follow these steps to contribute:
- Fork the repository.
- Create a new branch (
git checkout -b feature-branch
). - Make your changes.
- Ensure the code passes linting and formatting checks.
- Submit a pull request.
This project is licensed under the MIT License. See the LICENSE file for more details.
Feel free to explore, extend, and customize the configuration to fit your use case. Happy coding!