Skip to content
This repository has been archived by the owner on May 27, 2020. It is now read-only.

Commit

Permalink
feat: support external type definitions with X-TypeScript-Types hea…
Browse files Browse the repository at this point in the history
…ders. close #35
  • Loading branch information
axetroy committed Feb 13, 2020
1 parent d9237c9 commit 98253dd
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 30 deletions.
25 changes: 14 additions & 11 deletions server/src/deno.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,11 @@ type Deps = {
filepath: string;
};

type DenoModuleHeaders = {
interface IDenoModuleHeaders {
mime_type: string;
redirect_to: string;
};
redirect_to?: string;
x_typescript_types?: string;
}

class Deno {
public version!: Version | void;
Expand Down Expand Up @@ -243,14 +244,16 @@ class Deno {
if (!ts.sys.fileExists(moduleName)) {
const headersPath = `${moduleName}.headers.json`;
if (ts.sys.fileExists(headersPath)) {
interface IDenoModuleHeaders {
mime_type: string;
redirect_to: string;
}
const headers: IDenoModuleHeaders = JSON.parse(
await fs.readFile(headersPath, { encoding: "utf8" })
);
if (headers.redirect_to !== raw) {
let headers: IDenoModuleHeaders = {
mime_type: "application/typescript"
};
try {
headers = JSON.parse(
await fs.readFile(headersPath, { encoding: "utf8" })
);
} catch {}

if (headers.redirect_to && headers.redirect_to !== raw) {
moduleName = (
await this.resolveModule(
importMaps,
Expand Down
57 changes: 38 additions & 19 deletions typescript-deno-plugin/src/module_resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ type ImportMaps = {

type DenoModuleHeaders = {
mime_type: string;
redirect_to: string;
redirect_to?: string;
x_typescript_types?: string;
};

// the resolver defined how to resolve Deno module
Expand All @@ -28,10 +29,10 @@ export class ModuleResolver {
private readonly file: string,
private readonly logger: Logger,
private readonly workspaceDir: string,
importMapsFile?: string
private importMapsFile?: string
) {
if (importMapsFile) {
this.importMaps = this.resolveImportMaps(importMapsFile);
if (this.importMapsFile) {
this.importMaps = this.resolveImportMaps(this.importMapsFile);
}
}
// resolve modules
Expand Down Expand Up @@ -116,26 +117,44 @@ export class ModuleResolver {
return moduleName;
}
private convertRemoteToLocalCache(moduleName: string): string {
if (!/^https?:\/\//.test(moduleName)) {
return moduleName;
let filepath: string = moduleName;

if (/^https?:\/\//.test(moduleName)) {
// "https://deno.land/x/std/log/mod" to "$DENO_DIR/deps/https/deno.land/x/std/log/mod" (no ".ts" because stripped)
filepath = path.resolve(Deno.DENO_DEPS, moduleName.replace("://", "/"));
}

// "https://deno.land/x/std/log/mod" to "$DENO_DIR/deps/https/deno.land/x/std/log/mod" (no ".ts" because stripped)
let filepath = path.resolve(Deno.DENO_DEPS, moduleName.replace("://", "/"));
const headersPath = `${filepath}.headers.json`;

// if header.json file exist
if (pathExistsSync(headersPath)) {
let headers: DenoModuleHeaders = { mime_type: "application/typescript" };

if (!pathExistsSync(filepath)) {
const headersPath = `${filepath}.headers.json`;
if (pathExistsSync(headersPath)) {
const headers: DenoModuleHeaders = JSON.parse(
try {
headers = JSON.parse(
fs.readFileSync(headersPath, { encoding: "utf-8" })
);
if (moduleName !== headers.redirect_to) {
const redirectFilepath = this.convertRemoteToLocalCache(
headers.redirect_to
);
this.logger.info(`redirect "${filepath}" to "${redirectFilepath}".`);
filepath = redirectFilepath;
}
} catch {}

// If the declaration file exists, then load the declaration file
if (headers.x_typescript_types) {
let [moduleName] = this.resolveModuleNames([
headers.x_typescript_types
]);

this.logger.info(
`redirect '${filepath}' to declaration file "${moduleName.filepath}"`
);

filepath = moduleName.filepath;
}
// If a redirect exists, redirect to a new file
else if (headers.redirect_to && moduleName !== headers.redirect_to) {
const redirectFilepath = this.convertRemoteToLocalCache(
headers.redirect_to
);
this.logger.info(`redirect "${filepath}" to "${redirectFilepath}".`);
filepath = redirectFilepath;
}
}

Expand Down

0 comments on commit 98253dd

Please sign in to comment.