Skip to content

Commit

Permalink
feat: Add includeNodeModules to opt into resolving in node_modules (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobmischka authored Jul 3, 2021
1 parent bf9520c commit 04e58b4
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,32 @@ type Resolver = NonNullable<ResolveOptions["resolver"]>;

const pluginName = "ResolveTypescriptPlugin";

export interface ResolveTypescriptPluginOptions {
includeNodeModules?: boolean;
}

export default class ResolveTypescriptPlugin {
private static defaultOptions: ResolveTypescriptPluginOptions = {
includeNodeModules: false
};

private options: ResolveTypescriptPluginOptions;

public constructor(options: ResolveTypescriptPluginOptions = {}) {
this.options = {...ResolveTypescriptPlugin.defaultOptions, ...options};
}

public apply(resolver: Resolver): void {
const target = resolver.ensureHook("file");
for (const extension of [".ts", ".tsx"]) {
resolver
.getHook("raw-file")
.tapAsync(pluginName, (request, resolveContext, callback) => {
if (!request.path || request.path.match(/(^|[\\/])node_modules($|[\\/])/)) {
if (
!request.path ||
(!this.options.includeNodeModules &&
request.path.match(/(^|[\\/])node_modules($|[\\/])/))
) {
return callback();
}

Expand Down

0 comments on commit 04e58b4

Please sign in to comment.