code-slicer
v0.5.0
Published
Extract minimal, dependency-aware code context from an entry file for use in AI prompts.
Downloads
1,266
Maintainers
Readme
code-slicer
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.
Quickstart
Installation
npm install --save-dev code-slicerUsage
code-slicer <entry-file>code-slicer <entry-file> --format <plain|markdown|html|xml>Example
code-slicer src/bin.tsWhat it does
- 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
Output formats
Supported formats:
plain(default)markdownhtmlxml
Plain (default)
code-slicer src/bin.tsrelative/path/to/file.ts
<source code>
relative/path/to/another-file.ts
<source code>Markdown
code-slicer src/bin.ts --format markdown### relative/path/to/file.ts
```
<source code>
```
### relative/path/to/another-file.ts
```
<source code>
```HTML
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>XML
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>Constraints
- Only local file dependencies are included
node_modulesis ignored- No bundling or code transformation
- No runtime evaluation
- Output is deterministic and file-based
Requirements
- Node.js >= 22
How it works
- 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
Use cases
- AI prompt generation with relevant code context
- Dependency inspection
- Minimal reproduction cases
- Code review context extraction
Contributing
See CONTRIBUTING.md for process, standards, and testing
expectations.
License
MIT
