Skip to content
This repository was archived by the owner on Sep 4, 2020. It is now read-only.

Commit 192a5af

Browse files
committed
support import maps
Signed-off-by: 迷渡 <justjavac@gmail.com>
1 parent 868e518 commit 192a5af

File tree

4 files changed

+54
-9
lines changed

4 files changed

+54
-9
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
},
2121
"dependencies": {
2222
"crypto": "^1.0.1",
23+
"import-maps": "^0.2.1",
2324
"merge-deep": "^3.0.2",
2425
"mock-require": "^3.0.3"
2526
},

src/index.ts

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,28 @@
11
// modified from https://github.com/Microsoft/typescript-tslint-plugin
2+
import path from 'path'
23
import merge from "merge-deep";
34
import mockRequire from "mock-require";
45
import ts_module, { ResolvedModuleFull, CompilerOptions } from "typescript/lib/tsserverlibrary";
6+
import { parseFromString, resolve, ImportMaps } from 'import-maps'
57

68
import { Logger } from "./logger";
79
import {
810
getGlobalDtsPath,
911
getLocalDtsPath,
1012
getDtsPathForVscode,
13+
normalizeFilepath,
14+
pathExistsSync,
1115
} from "./utils";
1216

1317
import { universalModuleResolver } from "./module_resolver/universal_module_resolver";
18+
import { readFileSync } from 'fs';
19+
import { URL } from 'url';
1420

1521
let logger: Logger;
1622

1723
type DenoPluginConfig = {
1824
enable: boolean;
19-
import_map?: string;
25+
importmap?: string;
2026
dtsPath?: string;
2127
};
2228

@@ -79,6 +85,13 @@ module.exports = function init(
7985
// TypeScript plugins have a `cwd` of `/`, which causes issues with import resolution.
8086
process.chdir(projectDirectory);
8187

88+
let parsedImportMap: ImportMaps | null = null;
89+
90+
if (config.importmap != null) {
91+
logger.info('use import maps: ' + config.importmap);
92+
parsedImportMap = parseImportMapFromFile(projectDirectory, config.importmap);
93+
}
94+
8295
const resolveTypeReferenceDirectives =
8396
tsLsHost.resolveTypeReferenceDirectives;
8497

@@ -89,8 +102,6 @@ module.exports = function init(
89102
redirectedReference: ts_module.ResolvedProjectReference | undefined,
90103
options: ts_module.CompilerOptions,
91104
): (ts_module.ResolvedTypeReferenceDirective | undefined)[] => {
92-
logger.info(`typeDirectiveNames: ${typeDirectiveNames}`);
93-
logger.info(`containingFile: ${containingFile}`);
94105
const ret = resolveTypeReferenceDirectives.call(
95106
tsLsHost,
96107
typeDirectiveNames,
@@ -99,8 +110,6 @@ module.exports = function init(
99110
options,
100111
);
101112

102-
logger.info(JSON.stringify(ret, null, " "));
103-
104113
return ret;
105114
};
106115
}
@@ -116,7 +125,17 @@ module.exports = function init(
116125
const resolvedModules: (ResolvedModuleFull | undefined)[] = [];
117126

118127
// try resolve typeReferenceDirectives
119-
for (const moduleName of moduleNames) {
128+
for (let moduleName of moduleNames) {
129+
if (parsedImportMap !== null) {
130+
try{
131+
const moduleUrl = resolve(moduleName, parsedImportMap, new URL(projectDirectory, 'file:///'))
132+
moduleName = moduleUrl.protocol === 'file:' ? moduleUrl.pathname : moduleUrl.href;
133+
} catch (e){
134+
resolvedModules.push(undefined);
135+
continue;
136+
}
137+
}
138+
120139
const resolvedModule = universalModuleResolver.resolve(
121140
moduleName,
122141
containingFile,
@@ -186,8 +205,6 @@ module.exports = function init(
186205
info.languageServiceHost,
187206
);
188207

189-
logger.info(`getScriptFileNames:${JSON.stringify(scriptFileNames)}`);
190-
191208
const denoDtsPath = getDtsPathForVscode(info) ||
192209
getGlobalDtsPath() ||
193210
getLocalDtsPath(info.languageServiceHost);
@@ -294,3 +311,25 @@ module.exports = function init(
294311
},
295312
};
296313
};
314+
315+
function parseImportMapFromFile(cwd: string, file: string): ImportMaps | null {
316+
if (!path.isAbsolute(file)) {
317+
file = path.resolve(cwd, file)
318+
}
319+
320+
const fullFilePath = normalizeFilepath(file)
321+
322+
if (!pathExistsSync(fullFilePath)) {
323+
return null;
324+
}
325+
326+
const content = readFileSync(fullFilePath, {
327+
encoding: "utf8",
328+
});
329+
330+
try {
331+
return parseFromString(content, `file://${cwd}/`);
332+
} catch {
333+
return null;
334+
}
335+
}

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"outDir": "out",
77
"noImplicitAny": true,
88
"noUnusedParameters": true,
9-
"noUnusedLocals": true,
9+
// "noUnusedLocals": true,
1010
"esModuleInterop": true,
1111
"allowSyntheticDefaultImports": true,
1212
"lib": ["es6"],

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ get-caller-file@^1.0.2:
5757
resolved "https://registry.npm.taobao.org/get-caller-file/download/get-caller-file-1.0.3.tgz#f978fa4c90d1dfe7ff2d6beda2a515e713bdcf4a"
5858
integrity sha1-+Xj6TJDR3+f/LWvtoqUV5xO9z0o=
5959

60+
import-maps@^0.2.1:
61+
version "0.2.1"
62+
resolved "https://registry.npm.taobao.org/import-maps/download/import-maps-0.2.1.tgz?cache=0&sync_timestamp=1588752776277&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fimport-maps%2Fdownload%2Fimport-maps-0.2.1.tgz#ca641bf85c21077a186df1461008b8dc8867fedb"
63+
integrity sha1-ymQb+FwhB3oYbfFGEAi43Ihn/ts=
64+
6065
is-buffer@^1.0.2, is-buffer@^1.1.5:
6166
version "1.1.6"
6267
resolved "https://registry.npm.taobao.org/is-buffer/download/is-buffer-1.1.6.tgz?cache=0&sync_timestamp=1588707106955&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fis-buffer%2Fdownload%2Fis-buffer-1.1.6.tgz#efaa2ea9daa0d7ab2ea13a97b2b8ad51fefbe8be"

0 commit comments

Comments
 (0)