-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed inference when using npm packages
- Loading branch information
1 parent
40b4189
commit e4c86ca
Showing
5 changed files
with
51 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
/node_modules | ||
node_modules/ | ||
/target | ||
.vscode/ | ||
vendor/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,7 @@ | |
* Dependencies are required to be vendored before invocation. | ||
*/ | ||
|
||
import ts, { FunctionDeclaration } from "npm:[email protected]"; | ||
import ts, { FunctionDeclaration, StringLiteralLike } from "npm:[email protected]"; | ||
import { resolve, dirname } from "https://deno.land/[email protected]/path/mod.ts"; | ||
import { existsSync } from "https://deno.land/[email protected]/fs/mod.ts"; | ||
import * as sdk from 'npm:@hasura/[email protected]'; | ||
|
@@ -187,7 +187,7 @@ function pre_vendor(vendorPath: string, filename: string) { | |
// Exampe taken from: | ||
// https://docs.deno.com/runtime/tutorials/subprocess | ||
const deno_exec_path = Deno.execPath(); | ||
const vendor_args = [ "vendor", "--output", vendorPath, "--force", filename ]; | ||
const vendor_args = [ "vendor", "--node-modules-dir", "--output", vendorPath, "--force", filename ]; | ||
|
||
console.error(`Vendoring dependencies: ${[deno_exec_path, ...vendor_args].join(" ")}`); | ||
|
||
|
@@ -370,9 +370,9 @@ export function programInfoException(filename_arg?: string, vendor_arg?: string, | |
} | ||
`); | ||
|
||
const program = ts.createProgram([filename], { | ||
const compilerOptions: ts.CompilerOptions = { | ||
// This should match the version targeted in the deno version that is being used. | ||
target: ts.ScriptTarget.ES2022, | ||
target: ts.ScriptTarget.ES5, | ||
module: ts.ModuleKind.CommonJS, | ||
noImplicitAny: true, | ||
// NOTE: We just declare Deno globally as any in order to allow users to omit it's declaration in their function files | ||
|
@@ -383,7 +383,26 @@ export function programInfoException(filename_arg?: string, vendor_arg?: string, | |
noEmit: true, | ||
baseUrl: '.', | ||
paths: pathsMap | ||
}); | ||
}; | ||
|
||
const host = ts.createCompilerHost(compilerOptions); | ||
host.resolveModuleNameLiterals = (moduleLiterals: StringLiteralLike[], containingFile: string): ts.ResolvedModuleWithFailedLookupLocations[] => { | ||
return moduleLiterals.map(moduleName => { | ||
let moduleNameToResolve = moduleName.text; | ||
// If this looks like a Deno "npm:pkgName[@version][/path]" module import, extract the node module | ||
// name and resolve that instead. So long as we've done a deno vendor with --node-modules-dir | ||
// then we'll have a node_modules directory that the standard TypeScript module resolution | ||
// process can locate the npm package in by its name | ||
const npmDepMatch = /^npm:(?<pkgName>(?:@.+?\/)?[^/\n]+?)(?:@.+)?(?:\/.+)?$/.exec(moduleName.text); | ||
if (npmDepMatch) { | ||
moduleNameToResolve = npmDepMatch.groups?.pkgName!; | ||
} | ||
|
||
return ts.resolveModuleName(moduleNameToResolve, containingFile, compilerOptions, { fileExists: host.fileExists, readFile: host.readFile }); | ||
}) | ||
} | ||
|
||
const program = ts.createProgram([filename], compilerOptions, host); | ||
|
||
Deno.removeSync(deno_d_ts); | ||
|
||
|
@@ -535,4 +554,3 @@ export function programInfoException(filename_arg?: string, vendor_arg?: string, | |
|
||
return result; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters