Extract minimal, dependency-aware code context from an entry file for use in AI prompts.
code-slicer is a CLI that traverses local imports from an entry file and
outputs relevant code in traversal order. It is designed for AI workflows where
precise context matters more than full repository dumps.
npm install --save-dev code-slicercode-slicer <entry-file>code-slicer <entry-file> --format <plain|markdown|html|xml>code-slicer src/bin.ts- Starts from a single entry file
- Follows local imports and dependencies
- Supports JavaScript, TypeScript, JSX, TSX, React, and Vue
- Outputs files in traversal order
- Includes full source code for each file
- Excludes external packages and unresolved modules
Supported formats:
plain(default)markdownhtmlxml
code-slicer src/bin.tsrelative/path/to/file.ts
<source code>
relative/path/to/another-file.ts
<source code>
code-slicer src/bin.ts --format markdown### relative/path/to/file.ts
```
<source code>
```
### relative/path/to/another-file.ts
```
<source code>
```
code-slicer src/bin.ts --format html<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>code-slicer output</title>
</head>
<body>
<main class="code-slicer-output">
<section class="code-slicer-file">
<h3>relative/path/to/file.ts</h3>
<pre><code><source code></code></pre>
</section>
</main>
</body>
</html>
code-slicer src/bin.ts --format xml<?xml version="1.0" encoding="UTF-8"?>
<files>
<file path="relative/path/to/file.ts">
<source><source code></source>
</file>
</files>
- Only local file dependencies are included
node_modulesis ignored- No bundling or code transformation
- No runtime evaluation
- Output is deterministic and file-based
- Node.js >= 22
- Detect file type (JavaScript, TypeScript, Vue)
- Parse source
- TypeScript AST for JS/TS/JSX/TSX
- Vue SFC parser for
.vuefiles
- Extract imports (
import,export ... from,import()) - Resolve modules using TypeScript resolution with fallback logic
- Recursively traverse dependencies
- Track visited files to avoid cycles
- Skip external and unresolved modules
- AI prompt generation with relevant code context
- Dependency inspection
- Minimal reproduction cases
- Code review context extraction
See CONTRIBUTING.md for process, standards, and testing
expectations.
MIT