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