Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding option matchAfterOriginal #78

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -148,6 +148,7 @@ export interface ExplicitParams {
paths: { [key: string]: Array<string> };
mainFields?: (string | string[])[];
addMatchAll?: boolean;
matchAfterOriginal?: boolean;
cwd?: string;
}

Expand Down
3 changes: 3 additions & 0 deletions src/config-loader.ts
Expand Up @@ -6,6 +6,7 @@ export interface ExplicitParams {
paths: { [key: string]: Array<string> };
mainFields?: (string | string[])[];
addMatchAll?: boolean;
matchAfterOriginal?: boolean;
}

export type TsConfigLoader = (
Expand All @@ -26,6 +27,7 @@ export interface ConfigLoaderSuccessResult {
paths: { [key: string]: Array<string> };
mainFields?: (string | string[])[];
addMatchAll?: boolean;
matchAfterOriginal?: boolean;
}

export interface ConfigLoaderFailResult {
Expand Down Expand Up @@ -59,6 +61,7 @@ export function configLoader({
paths: explicitParams.paths,
mainFields: explicitParams.mainFields,
addMatchAll: explicitParams.addMatchAll,
matchAfterOriginal: explicitParams.matchAfterOriginal
};
}

Expand Down
15 changes: 15 additions & 0 deletions src/register.ts
Expand Up @@ -106,6 +106,21 @@ export function register(params?: RegisterParams): () => void {
Module._resolveFilename = function (request: string, _parent: any): string {
const isCoreModule = coreModules.hasOwnProperty(request);
if (!isCoreModule) {
if (configLoaderResult.matchAfterOriginal) {
let originalFound;

try {
// tslint:disable-next-line:no-invalid-this
originalFound = originalResolveFilename.apply(this, arguments);
} catch (e) {
// noop
}

if (originalFound) {
return originalFound;
}
}

const found = matchPath(request);
if (found) {
const modifiedArguments = [found, ...[].slice.call(arguments, 1)]; // Passes all arguments. Even those that is not specified above.
Expand Down