hemedani/fresh-barg

implement deno 2 workspace config

Open

#1 opened on Oct 28, 2024

 (0 comments) (0 reactions) (3 assignees)TypeScript (2 forks)auto 404
Workspaceenhancementgood first issue

Repository metrics

Stars
 (0 stars)
PR merge metrics
 (PR metrics pending)

Description

Here is the chatgpt help for implementing:

Implementing a workspace monorepo in Deno 2 can be streamlined with the deno.json configuration, which now supports monorepos out of the box through workspaces. Here’s how you can set it up effectively.

Steps to Implement a Monorepo in Deno 2

  1. Set Up the Project Structure
    First, define your workspace structure. A common setup might include several packages (e.g., backend and frontend), along with shared utilities or libraries:

    my-deno-workspace/
    ├── deno.json
    ├── packages/
    │   ├── backend/
    │   │   └── main.ts
    │   └── frontend/
    │       └── main.ts
    └── libs/
        └── utils/
            └── helper.ts
    
  2. Configure the Workspace in deno.json
    In your root deno.json, configure your workspace by specifying the directories that make up your packages or libraries. This allows Deno to recognize the entire project as a workspace and link these dependencies automatically.

    {
      "tasks": {
        "start-backend": "deno run --allow-net packages/backend/main.ts",
        "start-frontend": "deno run --allow-net packages/frontend/main.ts"
      },
      "importMap": "./import_map.json",
      "fmt": {
        "files": {
          "include": ["packages", "libs"]
        }
      },
      "lint": {
        "files": {
          "include": ["packages", "libs"]
        }
      },
      "compilerOptions": {
        "checkJs": true
      },
      "workspace": [
        "packages/*",
        "libs/*"
      ]
    }
    
    • Tasks: Define specific tasks for each package (e.g., start-backend and start-frontend). This way, you can easily start each service from the root.
    • Formatter and Linter: Specify the folders to include in linting and formatting.
    • Compiler Options: You may adjust compiler settings as needed.
    • Workspace: Point to each directory in the workspace (e.g., packages/* and libs/*).
  3. Set Up the Import Map
    Using an import map makes it easier to manage dependencies across packages in your workspace. Create an import_map.json in the root directory to centralize module imports:

    {
      "imports": {
        "utils/": "./libs/utils/"
      }
    }
    

    In each module, you can import shared utilities as defined in the import map:

    import { helperFunction } from "utils/helper.ts";
    
  4. Run the Workspace Tasks
    You can run specific tasks from the monorepo root using deno task:

    deno task start-backend
    deno task start-frontend
    
  5. Dependency Management
    If you’re using third-party modules, it’s often helpful to centralize them in the import map or ensure each package has its own deno.json configuration if they have different dependency needs. Just ensure they’re compatible with your workspace settings.

  6. Testing the Monorepo
    With Deno, you can run tests across your workspace by defining a test task in the root deno.json:

    "tasks": {
      "test": "deno test --allow-net"
    }
    

    Then, add test files to each package or library as needed, and run all tests from the root:

    deno task test
    

Example: Importing and Using a Shared Library

To use a utility function from a shared library in the backend or frontend package, you might do something like:

// packages/backend/main.ts
import { helperFunction } from "utils/helper.ts";

helperFunction();

With this setup, Deno will resolve helperFunction from libs/utils/helper.ts based on your import map. This structure keeps the workspace DRY and maintainable.

Benefits of This Setup

  • Centralized Configuration: Manage tasks, dependencies, and configuration from a single location.
  • Code Reusability: Easily share utilities and components across packages.
  • Scalability: Adding more packages is straightforward with minimal configuration tweaks.
  • Efficient Development: Run and test specific packages without affecting others.

Contributor guide