Deno provides a comprehensive built-in toolchain, allowing developers to complete formatting, testing, linting, bundling, and other development tasks without installing additional tools. These tools are all integrated into the Deno CLI and are simple and efficient to use.
Built-in Tools Overview
Deno's built-in tools include:
- deno fmt - Code formatting
- deno lint - Code linting
- deno test - Test runner
- deno compile - Compile to executable
- deno bundle - Bundle to single file
- deno doc - Generate documentation
- deno bench - Performance benchmarking
Code Formatting (deno fmt)
Basic Usage
bash# Format all files in current directory deno fmt # Format specific file deno fmt src/main.ts # Check format (don't modify files) deno fmt --check # Use specific configuration deno fmt --options-line-width=100
Configuration Options
bash# Set line width deno fmt --options-line-width=120 # Set indentation deno fmt --options-indent-width=2 # Use single quotes deno fmt --options-single-quote # Use semicolons deno fmt --options-use-tabs
Example
typescript// Before formatting const name="Deno"; function hello(name:string){ console.log(`Hello,${name}!`); } // After formatting const name = "Deno"; function hello(name: string) { console.log(`Hello, ${name}!`); }
Code Linting (deno lint)
Basic Usage
bash# Lint current directory deno lint # Lint specific file deno lint src/main.ts # Auto-fix issues deno lint --fix # Ignore specific rules deno lint --ignore=no-unused-vars
Configuration Rules
bash# Use configuration file deno lint --config=deno.json # Exclude files deno lint --exclude=**/*.test.ts # View all rules deno lint --rules
Common Rules
typescript// Bad example function foo() { var x = 1; // no-var: Use let or const const unused = 2; // no-unused-vars: Unused variable } // Good example function foo() { const x = 1; return x; }
Testing (deno test)
Basic Usage
bash# Run all tests deno test # Run specific test file deno test src/main_test.ts # Watch mode (auto-run on file changes) deno test --watch # Show verbose output deno test --verbose # Run tests in parallel deno test --parallel
Writing Tests
typescript// math_test.ts import { assertEquals, assertThrows } from "https://deno.land/std@0.208.0/testing/asserts.ts"; function add(a: number, b: number): number { return a + b; } function divide(a: number, b: number): number { if (b === 0) { throw new Error("Division by zero"); } return a / b; } Deno.test("add function adds two numbers", () => { assertEquals(add(1, 2), 3); assertEquals(add(-1, 1), 0); }); Deno.test("divide function divides numbers", () => { assertEquals(divide(10, 2), 5); assertEquals(divide(5, 2), 2.5); }); Deno.test("divide throws error on zero", () => { assertThrows( () => divide(10, 0), Error, "Division by zero" ); });
Async Tests
typescriptDeno.test("async function", async () => { const data = await fetchData(); assertEquals(data.length, 10); }); Deno.test("timeout test", async () => { const result = await slowOperation(); assertEquals(result, "done"); }, { timeout: 5000 });
Test Hooks
typescriptDeno.test("with setup and teardown", { sanitizeOps: false, sanitizeResources: false, fn: async () => { // Setup const db = await connectDatabase(); try { // Test const user = await db.createUser({ name: "John" }); assertEquals(user.name, "John"); } finally { // Teardown await db.close(); } } });
Compile to Executable (deno compile)
Basic Usage
bash# Compile to executable deno compile src/main.ts # Specify output filename deno compile --output=myapp src/main.ts # Specify target platform deno compile --target=x86_64-unknown-linux-gnu src/main.ts # Include permissions deno compile --allow-net --allow-read src/main.ts
Example
typescript// cli.ts #!/usr/bin/env -S deno run const name = Deno.args[0] || "World"; console.log(`Hello, ${name}!`);
Compile:
bashdeno compile --output=hello cli.ts ./hello Deno # Output: Hello, Deno!
Bundling (deno bundle)
Basic Usage
bash# Bundle to single file deno bundle src/main.ts bundle.js # Specify output file deno bundle src/main.ts dist/app.bundle.js # Specify source map deno bundle --source-map=inline src/main.ts bundle.js
Example
typescript// main.ts import { greet } from "./greet.ts"; import { farewell } from "./farewell.ts"; console.log(greet("Deno")); console.log(farewell("Deno"));
Bundle:
bashdeno bundle main.ts app.bundle.js
Documentation Generation (deno doc)
Basic Usage
bash# Generate documentation deno doc src/main.ts # Generate HTML documentation deno doc --html src/main.ts # View documentation for specific symbol deno doc src/main.ts MyFunction # Generate JSON documentation deno doc --json src/main.ts
Writing Documentation
typescript/** * Calculate the sum of two numbers * * @param a - First number * @param b - Second number * @returns Sum of the two numbers * * @example * ```ts * add(1, 2); // Returns 3 * ``` */ export function add(a: number, b: number): number { return a + b; } /** * User interface */ export interface User { /** User ID */ id: number; /** Username */ name: string; /** User email */ email: string; }
Performance Benchmarking (deno bench)
Basic Usage
bash# Run benchmarks deno bench # Run specific file deno bench src/bench.ts
Writing Benchmarks
typescriptimport { bench, runBenchmarks } from "https://deno.land/std@0.208.0/testing/bench.ts"; bench("for loop", () => { let sum = 0; for (let i = 0; i < 1000; i++) { sum += i; } }); bench("reduce", () => { const arr = Array.from({ length: 1000 }, (_, i) => i); arr.reduce((sum, num) => sum + num, 0); }); await runBenchmarks();
Configuration File
Use deno.json or deno.jsonc for unified configuration:
json{ "compilerOptions": { "strict": true }, "lint": { "files": { "include": ["src/"], "exclude": ["src/test/"] }, "rules": { "tags": ["recommended"], "exclude": ["no-unused-vars"] } }, "fmt": { "files": { "include": ["src/"], "exclude": ["dist/"] }, "options": { "useTabs": false, "lineWidth": 100, "indentWidth": 2, "singleQuote": true, "proseWrap": "preserve" } }, "tasks": { "dev": "deno run --watch src/main.ts", "test": "deno test --allow-net", "build": "deno compile src/main.ts" } }
Task Running
Deno supports defining and running tasks:
bash# List all tasks deno task # Run specific task deno task dev # Run test task deno task test
Best Practices
- Use configuration files: Configure all tools uniformly in
deno.json - Automate checks: Integrate lint and test in CI/CD
- Maintain consistency: Use fmt to ensure consistent code style
- Write tests: Write tests for critical functionality
- Document code: Use JSDoc comments to generate documentation
- Performance optimization: Use bench to identify performance bottlenecks
Deno's built-in toolchain provides a complete development experience without the need for additional dependencies, greatly simplifying project configuration and maintenance.