Skip to content

Commit

Permalink
fix: fix incorrect handling of multiple file extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
djcsdy committed Apr 23, 2021
1 parent 69f7163 commit 75b8149
Showing 1 changed file with 27 additions and 25 deletions.
52 changes: 27 additions & 25 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,33 @@ const pluginName = "ResolveTypescriptPlugin";
export default class ResolveTypescriptPlugin {
public apply(resolver: Resolver): void {
const target = resolver.ensureHook("file");
resolver.getHook("raw-file").tapAsync(pluginName, (request, resolveContext, callback) => {
if (!request.path || request.path.match(/(^|[\\/])node_modules($|[\\/])/)) {
return callback();
}
for (const extension of [".ts", ".tsx"]) {
resolver
.getHook("raw-file")
.tapAsync(pluginName, (request, resolveContext, callback) => {
if (!request.path || request.path.match(/(^|[\\/])node_modules($|[\\/])/)) {
return callback();
}

const path = request.path.replace(/\.js$/, "");
if (path === request.path) {
callback();
} else {
for (const extension of [".ts", ".tsx"]) {
resolver.doResolve(
target,
{
...request,
path: `${path}${extension}`,
relativePath:
request.relativePath &&
request.relativePath.replace(/\.js$/, extension)
},
`using path: ${path}`,
resolveContext,
callback
);
}
}
});
const path = request.path.replace(/\.js$/, extension);
if (path === request.path) {
callback();
} else {
resolver.doResolve(
target,
{
...request,
path,
relativePath:
request.relativePath &&
request.relativePath.replace(/\.js$/, extension)
},
`using path: ${path}`,
resolveContext,
callback
);
}
});
}
}
}

0 comments on commit 75b8149

Please sign in to comment.