Skip to content

create deno workspace #4

@hemedani

Description

@hemedani

To set up a Deno workspace for your project, you'll want to structure it in a way that allows modularity, ease of development, and scalability. Here's a step-by-step guide to creating a workspace in Deno:

1. Workspace Overview

In Deno, a workspace setup helps you manage multiple projects (or packages/modules) under a single repository. This is ideal for separating concerns such as backend, frontend, shared libraries, and utilities while keeping everything unified.

A basic structure might look like this:

my-deno-workspace/
├── backend/
│   ├── main.ts
│   ├── deps.ts
│   └── tests/
├── frontend/
│   ├── components/
│   ├── routes/
│   └── main.ts
├── shared/
│   ├── utils.ts
│   └── types.ts
├── .github/
│   └── workflows/
├── README.md
├── deno.json
└── deno.lock

2. Setting Up the Workspace

2.1. Root Directory

Create a root directory for your workspace, which will contain the backend, frontend, and shared code. This directory will also include the configuration files (deno.json, deno.lock).

2.2. Create a deno.json File

In the root of your workspace, create a deno.json file to manage dependencies, import maps, and compiler options for the entire project. This file allows you to configure how Deno resolves imports across the workspace.

{
  "importMap": "./import_map.json",
  "tasks": {
    "start:backend": "deno run --allow-net --allow-read backend/main.ts",
    "start:frontend": "deno run --allow-net --allow-read frontend/main.ts",
    "test": "deno test --coverage"
  },
  "compilerOptions": {
    "lib": ["dom", "deno.ns"],
    "strict": true
  }
}

2.3. Create an import_map.json

To avoid relative paths for shared code and dependencies, create an import_map.json file to centralize import paths.

{
  "imports": {
    "http/": "https://deno.land/std@0.205.0/http/",
    "shared/": "./shared/"
  }
}

2.4. Backend Directory

In the backend/ directory:

  • main.ts: This file will be the entry point for the backend service. It should start the server and handle routing for APIs.
  • deps.ts: This file manages the backend dependencies (e.g., third-party modules).

Example main.ts for a simple server:

import { serve } from "http/server.ts";

serve(() => new Response("Hello from the backend!"), { port: 8000 });

2.5. Frontend Directory

In the frontend/ directory:

  • main.ts: Entry point for the Deno Fresh-based frontend.
  • components/ and routes/ folders for Fresh components and routes.

Example main.ts:

import { start } from "$fresh/server.ts";
import manifest from "./fresh.gen.ts";

await start(manifest);

2.6. Shared Directory

The shared/ folder is where you place reusable modules, such as utility functions, types, or constants that both the backend and frontend can use.

  • utils.ts: For shared utility functions.
  • types.ts: For TypeScript types or interfaces shared across the workspace.

2.7. GitHub Actions for CI/CD

Set up GitHub Actions for CI/CD in .github/workflows/. You might want to automate tasks such as running tests, building the project, and deploying.

Example CI workflow (ci.yml):

name: CI

on:
  push:
    branches:
      - main
  pull_request:

jobs:
  test:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2
    - name: Setup Deno
      uses: denoland/setup-deno@v1
      with:
        deno-version: v1.x
    - run: deno task test

2.8. README.md

Create a README.md that documents the structure of your workspace, how to set up the environment, and how to run both the backend and frontend.

3. Running and Testing the Workspace

With the workspace set up, you can start different services via the deno.json task system:

  • Start the backend: deno task start:backend
  • Start the frontend: deno task start:frontend
  • Run tests: deno task test

4. Benefits of Deno Workspace

  • Modularity: Each part (backend, frontend, shared) is cleanly separated but can easily share code.
  • Centralized Configurations: Manage dependencies, imports, and tasks from a single source using deno.json.
  • Reusability: Shared code between frontend and backend, reducing code duplication.

This setup allows for flexibility and organization, making it easier to manage and contribute to your open-source project.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

Status

Ready

Relationships

None yet

Development

No branches or pull requests

Issue actions