Skip to content

Commit

Permalink
fix: update module resolution for non-relative paths (#229)
Browse files Browse the repository at this point in the history
  • Loading branch information
smoores-dev authored Sep 22, 2023
1 parent 2e50207 commit e21ae36
Showing 1 changed file with 22 additions and 13 deletions.
35 changes: 22 additions & 13 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,12 @@ const init: tsModule.server.PluginModuleFactory = ({ typescript: ts }) => {

const createModuleResolver =
(containingFile: string) =>
(moduleName: string): tsModule.ResolvedModuleFull | undefined => {
(
moduleName: string,
resolveModule: () =>
| tsModule.ResolvedModuleWithFailedLookupLocations
| undefined,
): tsModule.ResolvedModuleFull | undefined => {
if (isRelativeCSS(moduleName)) {
return {
extension: ts.Extension.Dts,
Expand All @@ -152,25 +157,21 @@ const init: tsModule.server.PluginModuleFactory = ({ typescript: ts }) => {
moduleName,
),
};
} else if (
isCSS(moduleName) &&
languageServiceHost.getResolvedModuleWithFailedLookupLocationsFromCache
) {
}
if (isCSS(moduleName)) {
// TODO: Move this section to a separate file and add basic tests.
// Attempts to locate the module using TypeScript's previous search paths. These include "baseUrl" and "paths".
const failedModule =
languageServiceHost.getResolvedModuleWithFailedLookupLocationsFromCache(
moduleName,
containingFile,
);
const resolvedModule = resolveModule();
if (!resolvedModule) return;

const baseUrl = info.project.getCompilerOptions().baseUrl;
const match = '/index.ts';

// An array of paths TypeScript searched for the module. All include .ts, .tsx, .d.ts, or .json extensions.
// NOTE: TypeScript doesn't expose this in their interfaces, which is why the type is unknown.
// https://github.com/microsoft/TypeScript/issues/28770
const failedLocations: readonly string[] = (
failedModule as unknown as {
resolvedModule as unknown as {
failedLookupLocations: readonly string[];
}
).failedLookupLocations;
Expand Down Expand Up @@ -227,7 +228,10 @@ const init: tsModule.server.PluginModuleFactory = ({ typescript: ts }) => {

return moduleNames.map(({ text: moduleName }, index) => {
try {
const resolvedModule = moduleResolver(moduleName);
const resolvedModule = moduleResolver(
moduleName,
() => resolvedModules[index],
);
if (resolvedModule) return { resolvedModule };
} catch (e) {
logger.error(e);
Expand Down Expand Up @@ -259,7 +263,12 @@ const init: tsModule.server.PluginModuleFactory = ({ typescript: ts }) => {

return moduleNames.map((moduleName, index) => {
try {
const resolvedModule = moduleResolver(moduleName);
const resolvedModule = moduleResolver(moduleName, () =>
languageServiceHost.getResolvedModuleWithFailedLookupLocationsFromCache?.(
moduleName,
containingFile,
),
);
if (resolvedModule) return resolvedModule;
} catch (e) {
logger.error(e);
Expand Down

0 comments on commit e21ae36

Please sign in to comment.