Skip to content

Commit 870d5dd

Browse files
adding a little more information to the workspace docs (#1654)
1 parent 560ce74 commit 870d5dd

File tree

1 file changed

+140
-0
lines changed

1 file changed

+140
-0
lines changed

runtime/fundamentals/workspaces.md

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,3 +383,143 @@ root and its members:
383383
| version ||| |
384384
| exports ||| |
385385
| workspace ||| Nested workspaces are not supported. |
386+
387+
## Running Commands Across Workspaces
388+
389+
Deno provides several ways to run commands across all or specific workspace
390+
members:
391+
392+
### Running Tests
393+
394+
To run tests across all workspace members, simply execute `deno test` from the
395+
workspace root:
396+
397+
```sh
398+
deno test
399+
```
400+
401+
This will run tests in all workspace members according to their individual test
402+
configurations.
403+
404+
To run tests for a specific workspace member, you can either:
405+
406+
1. Change to that member's directory and run the test command:
407+
408+
```sh
409+
cd my-directory
410+
deno test
411+
```
412+
413+
2. Or specify the path from the workspace root:
414+
415+
```sh
416+
deno test my-directory/
417+
```
418+
419+
### Formatting and Linting
420+
421+
Similar to testing, formatting and linting commands run across all workspace
422+
members by default:
423+
424+
```sh
425+
deno fmt
426+
deno lint
427+
```
428+
429+
Each workspace member follows its own formatting and linting rules as defined in
430+
its `deno.json` file, with some settings inherited from the root configuration
431+
as shown in the table above.
432+
433+
### Using Workspace Tasks
434+
435+
You can define tasks at both the workspace root and in individual workspace
436+
members:
437+
438+
```json title="deno.json"
439+
{
440+
"workspace": ["./add", "./subtract"],
441+
"tasks": {
442+
"build": "echo 'Building all packages'",
443+
"test:all": "deno test"
444+
}
445+
}
446+
```
447+
448+
```json title="add/deno.json"
449+
{
450+
"name": "@scope/add",
451+
"version": "0.1.0",
452+
"exports": "./mod.ts",
453+
"tasks": {
454+
"build": "echo 'Building add package'",
455+
"test": "deno test"
456+
}
457+
}
458+
```
459+
460+
To run a task defined in a specific package:
461+
462+
```sh
463+
deno task --cwd=add build
464+
```
465+
466+
## Sharing and Managing Dependencies
467+
468+
Workspaces provide powerful ways to share and manage dependencies across
469+
projects:
470+
471+
### Sharing Development Dependencies
472+
473+
Common development dependencies like testing libraries can be defined at the
474+
workspace root:
475+
476+
```json title="deno.json"
477+
{
478+
"workspace": ["./add", "./subtract"],
479+
"imports": {
480+
"@std/testing/": "jsr:@std/testing@^0.218.0/",
481+
"chai": "npm:chai@^4.3.7"
482+
}
483+
}
484+
```
485+
486+
This makes these dependencies available to all workspace members without needing
487+
to redefine them.
488+
489+
### Managing Version Conflicts
490+
491+
When resolving dependencies, workspace members can override dependencies defined
492+
in the root. If both the root and a member specify different versions of the
493+
same dependency, the member's version will be used when resolving within that
494+
member's folder. This allows individual packages to use specific dependency
495+
versions when needed.
496+
497+
However, member-specific dependencies are scoped only to that member's folder.
498+
Outside of member folders, or when working with files at the workspace root
499+
level, the workspace root's import map will be used for resolving dependencies
500+
(including JSR and HTTPS dependencies).
501+
502+
### Interdependent Workspace Members
503+
504+
As shown in the earlier example with the `add` and `subtract` modules, workspace
505+
members can depend on each other. This enables a clean separation of concerns
506+
while maintaining the ability to develop and test interdependent modules
507+
together.
508+
509+
The `subtract` module imports functionality from the `add` module, demonstrating
510+
how workspace members can build upon each other:
511+
512+
```ts title="subtract/mod.ts"
513+
import { add } from "@scope/add";
514+
515+
export function subtract(a: number, b: number): number {
516+
return add(a, b * -1);
517+
}
518+
```
519+
520+
This approach allows you to:
521+
522+
1. Break down complex projects into manageable, single-purpose packages
523+
2. Share code between packages without publishing to a registry
524+
3. Test and develop interdependent modules together
525+
4. Gradually migrate monolithic codebases to modular architecture

0 commit comments

Comments
 (0)