Skip to content

Commit

Permalink
feat: resolve .tsx as well as .ts
Browse files Browse the repository at this point in the history
  • Loading branch information
djcsdy committed Apr 23, 2021
1 parent 2d8f595 commit 69f7163
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,29 @@ 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.split(/[\\/]/).indexOf("node_modules") >= 0) {
if (!request.path || request.path.match(/(^|[\\/])node_modules($|[\\/])/)) {
return callback();
}

const path = request.path.replace(/\.js$/, ".ts");
const path = request.path.replace(/\.js$/, "");
if (path === request.path) {
callback();
} else {
resolver.doResolve(
target,
{
...request,
path,
relativePath:
request.relativePath && request.relativePath.replace(/\.js$/, ".ts")
},
`using path: ${path}`,
resolveContext,
callback
);
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
);
}
}
});
}
Expand Down

0 comments on commit 69f7163

Please sign in to comment.