|
1 | 1 | import type { Callback } from "./types.ts";
|
2 | 2 |
|
3 |
| -import type { Denops } from "@denops/std"; |
4 |
| -import * as op from "@denops/std/option"; |
5 |
| - |
| 3 | +import { |
| 4 | + type ImportMap, |
| 5 | + ImportMapImporter, |
| 6 | + loadImportMap, |
| 7 | +} from "@lambdalisue/import-map-importer"; |
6 | 8 | import { is } from "@core/unknownutil/is";
|
7 | 9 | import { assertEquals } from "@std/assert/equals";
|
| 10 | +import { toFileUrl } from "@std/path/to-file-url"; |
| 11 | +import { fromFileUrl } from "@std/path/from-file-url"; |
| 12 | +import { join } from "@std/path/join"; |
| 13 | +import { dirname } from "@std/path/dirname"; |
| 14 | + |
| 15 | +import type { Denops } from "@denops/std"; |
| 16 | +import * as op from "@denops/std/option"; |
8 | 17 |
|
9 | 18 | export async function convertKeywordPattern(
|
10 | 19 | denops: Denops,
|
@@ -134,6 +143,51 @@ export async function callCallback(
|
134 | 143 | }
|
135 | 144 | }
|
136 | 145 |
|
| 146 | +export async function tryLoadImportMap( |
| 147 | + script: string, |
| 148 | +): Promise<ImportMap | undefined> { |
| 149 | + if (script.startsWith("http://") || script.startsWith("https://")) { |
| 150 | + // We cannot load import maps for remote scripts |
| 151 | + return undefined; |
| 152 | + } |
| 153 | + const PATTERNS = [ |
| 154 | + "deno.json", |
| 155 | + "deno.jsonc", |
| 156 | + "import_map.json", |
| 157 | + "import_map.jsonc", |
| 158 | + ]; |
| 159 | + // Convert file URL to path for file operations |
| 160 | + const scriptPath = script.startsWith("file://") |
| 161 | + ? fromFileUrl(new URL(script)) |
| 162 | + : script; |
| 163 | + const parentDir = dirname(scriptPath); |
| 164 | + for (const pattern of PATTERNS) { |
| 165 | + const importMapPath = join(parentDir, pattern); |
| 166 | + try { |
| 167 | + return await loadImportMap(importMapPath); |
| 168 | + } catch (err: unknown) { |
| 169 | + if (err instanceof Deno.errors.NotFound) { |
| 170 | + // Ignore NotFound errors and try the next pattern |
| 171 | + continue; |
| 172 | + } |
| 173 | + throw err; // Rethrow other errors |
| 174 | + } |
| 175 | + } |
| 176 | + return undefined; |
| 177 | +} |
| 178 | + |
| 179 | +export async function importPlugin(path: string): Promise<unknown> { |
| 180 | + const suffix = performance.now(); |
| 181 | + const url = toFileUrl(path).href; |
| 182 | + const importMap = await tryLoadImportMap(path); |
| 183 | + if (importMap) { |
| 184 | + const importer = new ImportMapImporter(importMap); |
| 185 | + return await importer.import(`${url}#${suffix}`); |
| 186 | + } else { |
| 187 | + return await import(`${url}#${suffix}`); |
| 188 | + } |
| 189 | +} |
| 190 | + |
137 | 191 | Deno.test("vimoption2ts", () => {
|
138 | 192 | assertEquals(vimoption2ts("@,48-57,_,\\"), "a-zA-Z0-9_\\\\");
|
139 | 193 | assertEquals(vimoption2ts("@,-,48-57,_"), "a-zA-Z0-9_-");
|
|
0 commit comments