Skip to content

Commit

Permalink
Fixes #313 by correctly using projects moduleResolution strategy if c…
Browse files Browse the repository at this point in the history
…onfigured (#333)

* Fixes #313 by correctly using projects moduleResolution strategy if provided

* Simplifies conditional for detecting module specifiers for use in getModeForUsageLocation
  • Loading branch information
benjamind authored Dec 8, 2023
1 parent a8278e2 commit 0b8c8ba
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 897 deletions.
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@
"lint-staged": "^10.2.10",
"nodemon": "^2.0.4",
"prettier": "^2.4.1",
"typescript": "~4.4.4",
"typescript": "~4.8.4",
"wireit": "^0.9.5"
},
"husky": {
Expand Down
4 changes: 2 additions & 2 deletions packages/lit-analyzer/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,15 @@ function emitDirectModuleImportWithName(moduleSpecifier: string, node: Node, con
result = (context.program as any)["getResolvedModuleWithFailedLookupLocationsFromCache"](moduleSpecifier, fromSourceFile.fileName);
} else {
const cache = (context.program as MaybeModernProgram).getModuleResolutionCache?.();
let mode: tsModule.ModuleKind.CommonJS | tsModule.ModuleKind.ESNext | undefined = undefined;
if (context.ts.isImportDeclaration(node) || context.ts.isExportDeclaration(node)) {
if (node.moduleSpecifier != null && context.ts.isStringLiteral(node.moduleSpecifier) && context.ts.isSourceFile(node.parent)) {
mode = tsModule.getModeForUsageLocation(fromSourceFile, node.moduleSpecifier);
}
}

if (cache != null) {
result = context.ts.resolveModuleNameFromCache(moduleSpecifier, node.getSourceFile().fileName, cache);
result = context.ts.resolveModuleNameFromCache(moduleSpecifier, node.getSourceFile().fileName, cache, mode);
}
}

Expand Down
23 changes: 13 additions & 10 deletions packages/lit-analyzer/src/lib/cli/compile.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { existsSync, readFileSync } from "fs";
import { basename } from "path";
import {
CompilerOptions,
convertCompilerOptionsFromJson,
parseJsonConfigFileContent,
sys,
createProgram,
findConfigFile,
ModuleKind,
Expand All @@ -20,9 +20,7 @@ const requiredCompilerOptions: CompilerOptions = {
allowJs: true,
//maxNodeModuleJsDepth: 3,
strictNullChecks: true, // Type checking will remove all "null" and "undefined" from types if "strictNullChecks" is false
moduleResolution: ModuleResolutionKind.NodeJs,
skipLibCheck: true,
lib: ["lib.esnext.d.ts", "lib.dom.d.ts"]
skipLibCheck: true
};

/**
Expand All @@ -38,7 +36,8 @@ const defaultCompilerOptions: CompilerOptions = {
esModuleInterop: true,
allowSyntheticDefaultImports: true,
allowUnreachableCode: true,
allowUnusedLabels: true
allowUnusedLabels: true,
lib: ["lib.esnext.d.ts", "lib.dom.d.ts"]
};

export interface CompileResult {
Expand All @@ -53,7 +52,6 @@ export interface CompileResult {
*/
export function compileTypescript(filePaths: string | string[]): CompileResult {
const options = getCompilerOptions();

filePaths = Array.isArray(filePaths) ? filePaths : [filePaths];
const program = createProgram(filePaths, options);
const files = program
Expand All @@ -73,10 +71,16 @@ export function getCompilerOptions(): CompilerOptions {

// If we found existing compiler options, merged "required compiler options" into it.
if (compilerOptions != null) {
return {
const options = {
...compilerOptions,
...requiredCompilerOptions
};
// set module resolution to nodejs if it is classic
// but if the user has set it to something else, don't override it
if (!options.moduleResolution || options.moduleResolution === ModuleResolutionKind.Classic) {
options.moduleResolution = ModuleResolutionKind.NodeJs;
}
return options;
}

// Return default compiler options if no compiler options were found
Expand All @@ -93,10 +97,9 @@ export function resolveTsConfigCompilerOptions(): CompilerOptions | undefined {
if (tsConfigFilePath != null) {
// Read the tsconfig.json file
const parsedConfig = readConfigFile(tsConfigFilePath, path => readFileSync(path, "utf8"));

if (parsedConfig != null && parsedConfig.config != null) {
// Parse the tsconfig.json file
const parsedJson = convertCompilerOptionsFromJson(parsedConfig.config.compilerOptions, basename(tsConfigFilePath), "tsconfig.json");
const parsedJson = parseJsonConfigFileContent(parsedConfig.config, sys, process.cwd());
return parsedJson?.options;
}
}
Expand Down
Loading

0 comments on commit 0b8c8ba

Please sign in to comment.