|
| 1 | +# Managing Dependencies with Import Maps |
| 2 | + |
| 3 | +In the previous examples, we used direct URL imports like |
| 4 | +`jsr:@denops/std@^8.0.0`. While this works, the recommended approach for Denops |
| 5 | +plugins (v8.0.0+) is to use import maps with `deno.jsonc` for cleaner and more |
| 6 | +maintainable dependency management. |
| 7 | + |
| 8 | +## Why Use Import Maps? |
| 9 | + |
| 10 | +The main reason to use import maps is to avoid conflicts between multiple Denops plugins. Each Denops plugin must have a unique directory name under `denops/`, but root-level configuration files could potentially conflict: |
| 11 | + |
| 12 | +``` |
| 13 | +# Multiple plugins installed: |
| 14 | +~/.vim/pack/plugins/start/plugin-a/ |
| 15 | +├── deno.jsonc # Could conflict |
| 16 | +└── denops/plugin-a/ # Always unique |
| 17 | +
|
| 18 | +~/.vim/pack/plugins/start/plugin-b/ |
| 19 | +├── deno.jsonc # Could conflict |
| 20 | +└── denops/plugin-b/ # Always unique |
| 21 | +``` |
| 22 | + |
| 23 | +Some plugin managers have a "merge" feature that combines plugin directories, but even without merging, placing configuration files in plugin-specific directories (`denops/plugin-name/`) ensures no conflicts can occur regardless of how plugins are installed or managed. |
| 24 | + |
| 25 | +## Setting Up Your Plugin Structure |
| 26 | + |
| 27 | +Update your `denops-helloworld` structure to include configuration files: |
| 28 | + |
| 29 | +``` |
| 30 | +denops-helloworld/ |
| 31 | +├── deno.jsonc # Development configuration |
| 32 | +├── denops/ |
| 33 | +│ └── denops-helloworld/ |
| 34 | +│ ├── deno.jsonc # Runtime dependencies |
| 35 | +│ └── main.ts |
| 36 | +└── plugin/ |
| 37 | + └── denops-helloworld.vim |
| 38 | +``` |
| 39 | + |
| 40 | +### Root deno.jsonc (Development) |
| 41 | + |
| 42 | +Create a `deno.jsonc` in your repository root for workspace configuration: |
| 43 | + |
| 44 | +```json |
| 45 | +{ |
| 46 | + "workspace": [ |
| 47 | + "./denops/denops-helloworld" |
| 48 | + ] |
| 49 | +} |
| 50 | +``` |
| 51 | + |
| 52 | +This enables Deno commands like `deno fmt`, `deno lint`, and `deno test` to work from |
| 53 | +your project root and discover your plugin's configuration. |
| 54 | + |
| 55 | +### Plugin deno.jsonc (Runtime) |
| 56 | + |
| 57 | +Create `denops/denops-helloworld/deno.jsonc` for runtime dependencies: |
| 58 | + |
| 59 | +```json |
| 60 | +{ |
| 61 | + "imports": { |
| 62 | + "@denops/std": "jsr:@denops/std@^8.0.0", |
| 63 | + "@core/unknownutil": "jsr:@core/unknownutil@^4.3.0" |
| 64 | + } |
| 65 | +} |
| 66 | +``` |
| 67 | + |
| 68 | +## Updating Your Code |
| 69 | + |
| 70 | +With import maps configured, update your imports from: |
| 71 | + |
| 72 | +```typescript |
| 73 | +import type { Entrypoint } from "jsr:@denops/std@^8.0.0"; |
| 74 | +import { assert, is } from "jsr:@core/unknownutil@^4.3.0"; |
| 75 | +``` |
| 76 | + |
| 77 | +To cleaner versions: |
| 78 | + |
| 79 | +```typescript |
| 80 | +import type { Entrypoint } from "@denops/std"; |
| 81 | +import { assert, is } from "@core/unknownutil"; |
| 82 | +``` |
| 83 | + |
| 84 | +## Alternative: import_map.json |
| 85 | + |
| 86 | +Denops also supports `import_map.json(c)` files, but they require more verbose |
| 87 | +configuration due to the [Import Maps Standard](https://github.com/WICG/import-maps): |
| 88 | + |
| 89 | +```json |
| 90 | +// denops/denops-helloworld/import_map.json |
| 91 | +{ |
| 92 | + "imports": { |
| 93 | + "@denops/std": "jsr:@denops/std@^8.0.0", |
| 94 | + "@denops/std/": "jsr:/@denops/std@^8.0.0/" // Required for submodules |
| 95 | + } |
| 96 | +} |
| 97 | +``` |
| 98 | + |
| 99 | +We recommend using `deno.jsonc` as it's less verbose and integrates better with |
| 100 | +Deno tooling. For more details about the differences, see the [Deno documentation](https://docs.deno.com/runtime/fundamentals/modules/#differentiating-between-imports-or-importmap-in-deno.json-and---import-map-option). |
| 101 | + |
| 102 | +> [!IMPORTANT] |
| 103 | +> |
| 104 | +> Import map features require Denops v8.0.0 or later. For older versions, |
| 105 | +> continue using direct URL imports. |
| 106 | +
|
| 107 | +## Benefits |
| 108 | + |
| 109 | +1. **Cleaner imports**: No more long URLs in your code |
| 110 | +2. **Version management**: Update dependencies in one place |
| 111 | +3. **Better IDE support**: Auto-completion and type checking work seamlessly |
| 112 | +4. **No conflicts**: Each plugin manages its own dependencies |
| 113 | +5. **Development tools**: Format and lint your code from the project root |
0 commit comments