MoonBit Navigator
VS Code extension that enables "Go to Definition" (F12) for MoonBit imports in JavaScript/TypeScript files.
What is this for?
This extension is designed to work with @mizchi/vite-plugin-moonbit, a Vite plugin that enables seamless integration between MoonBit and JavaScript/TypeScript projects.
When using vite-plugin-moonbit, you can import MoonBit modules using the mbt: prefix:
import { greet } from "mbt:myproject/lib";
However, VS Code cannot resolve these virtual imports by default, breaking "Go to Definition" functionality. MoonBit Navigator solves this by mapping mbt: imports back to the actual .mbt source files.
Features
- Go to Definition: Navigate from
mbt:project/package imports directly to MoonBit source files
- Symbol Navigation: Jump to specific
pub fn, pub struct, pub enum, and pub type definitions
- Multi-file Support: Works across multiple
.mbt files in a package
- Automatic Discovery: Automatically discovers
moon.mod.json files in your workspace
- Cache Invalidation: Watches for changes to
moon.mod.json files and updates automatically
Usage
- Install the extension
- Open a workspace containing MoonBit projects (with
moon.mod.json files)
- Open a JavaScript/TypeScript file with
mbt: imports
- Use F12 (Go to Definition) or Ctrl+Click on an import path
Supported Import Patterns
// Named imports - F12 on symbol jumps to its definition
import { greet, Point } from "mbt:myproject/lib";
// Namespace imports - F12 jumps to the first .mbt file
import * as lib from "mbt:myproject/lib";
// Dynamic imports
const pkg = await import("mbt:myproject/lib");
Requirements
- VS Code 1.85.0 or higher
- MoonBit project with
moon.mod.json file
Configuration
| Setting |
Default |
Description |
mbtNavigator.excludedDirectories |
["node_modules", "_build", "target"] |
Directories to exclude from search |
mbtNavigator.debugLogging |
false |
Enable debug logging in the output channel |
Commands
| Command |
Description |
MoonBit Navigator: Show Logs |
Show the extension output channel |
MoonBit Navigator: Refresh Cache |
Manually refresh the moon.mod.json cache |
Path Resolution
The extension supports two methods for resolving mbt: import paths:
1. moon.mod.json (Default)
Automatically discovers moon.mod.json files and uses the name field to match projects.
2. jsconfig.json / tsconfig.json paths
For projects using custom path mappings (e.g., with @aspect-build/aspect or @aspect-build/aspect), you can configure paths in your jsconfig.json or tsconfig.json:
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"mbt:myproject/*": ["resources/moon/*"]
}
}
}
This is useful when:
- Your MoonBit source is in a subdirectory (e.g.,
resources/moon/)
- You're using a bundler like Vite with
@mizchi/vite-plugin-moonbit
- The
moon.mod.json name doesn't match your import pattern
How It Works
- Import Detection: Parses
mbt:{projectName}/{packageName} pattern from import statements
- Project Resolution: First tries
moon.mod.json, then falls back to jsconfig/tsconfig paths
- Package Mapping: Locates the package directory based on the resolved path
- Symbol Search: Scans
.mbt files for public symbol definitions matching the imported name
Supported MoonBit Declarations
The extension recognizes the following public declarations:
pub fn function_name() -> Type { ... }
pub struct StructName { ... }
pub enum EnumName { ... }
pub type TypeAlias = Type
// Visibility modifiers are also supported
pub(all) fn public_all() -> Unit { ... }
pub(readonly) struct ReadOnly { ... }
pub(open) enum OpenEnum { ... }
[!NOTE]
Currently, symbol detection uses regex-based parsing. This covers most common cases but may not handle complex or multi-line declarations. MoonBit LSP integration is planned for future versions to provide more accurate symbol resolution.
Development
# Install dependencies
npm install
# Compile
npm run compile
# Watch mode
npm run watch
# Run tests
npm run test
# Lint
npm run lint
# Package extension
npm run package
License
MIT