From d2163b48caa1c1ca622810926f615df36ac30d7c Mon Sep 17 00:00:00 2001 From: Arpad Borsos Date: Thu, 24 Jan 2019 14:45:53 +0100 Subject: [PATCH] add option to avoid adding a match-all rule this solves part of #72 --- src/config-loader.ts | 8 +++++++- src/mapping-entry.ts | 9 +++++---- src/match-path-async.ts | 6 ++++-- src/match-path-sync.ts | 6 ++++-- src/register.ts | 4 +++- test/data/match-path-data.ts | 10 ++++++++++ test/mapping-entry-test.ts | 27 ++++++++++++++++++++++----- test/match-path-async-tests.ts | 3 ++- test/match-path-sync-tests.ts | 3 ++- 9 files changed, 59 insertions(+), 17 deletions(-) diff --git a/src/config-loader.ts b/src/config-loader.ts index 7d2489e..124c769 100644 --- a/src/config-loader.ts +++ b/src/config-loader.ts @@ -5,6 +5,8 @@ import { options } from "./options"; export interface ExplicitParams { baseUrl: string; paths: { [key: string]: Array }; + mainFields?: Array; + addMatchAll?: boolean; } export type TsConfigLoader = ( @@ -23,6 +25,8 @@ export interface ConfigLoaderSuccessResult { baseUrl: string; absoluteBaseUrl: string; paths: { [key: string]: Array }; + mainFields?: Array; + addMatchAll?: boolean; } export interface ConfigLoaderFailResult { @@ -54,7 +58,9 @@ export function configLoader({ configFileAbsolutePath: "", baseUrl: explicitParams.baseUrl, absoluteBaseUrl, - paths: explicitParams.paths + paths: explicitParams.paths, + mainFields: explicitParams.mainFields, + addMatchAll: explicitParams.addMatchAll }; } diff --git a/src/mapping-entry.ts b/src/mapping-entry.ts index 78ff16f..bc8d418 100644 --- a/src/mapping-entry.ts +++ b/src/mapping-entry.ts @@ -19,7 +19,8 @@ export interface Paths { */ export function getAbsoluteMappingEntries( absoluteBaseUrl: string, - paths: Paths + paths: Paths, + addMatchAll: boolean ): ReadonlyArray { // Resolve all paths to absolute form once here, and sort them by // longest prefix once here, this saves time on each request later. @@ -34,9 +35,9 @@ export function getAbsoluteMappingEntries( ) }); } - // If there is no match-all path specified in the paths section of tsconfig, then try to match all - // all relative to baseUrl, this is how typescript works. - if (!paths["*"]) { + // If there is no match-all path specified in the paths section of tsconfig, then try to match + // all paths relative to baseUrl, this is how typescript works. + if (!paths["*"] && addMatchAll) { absolutePaths.push({ pattern: "*", paths: [`${absoluteBaseUrl.replace(/\/$/, "")}/*`] diff --git a/src/match-path-async.ts b/src/match-path-async.ts index aaf96b8..f3e94c2 100644 --- a/src/match-path-async.ts +++ b/src/match-path-async.ts @@ -26,11 +26,13 @@ export interface MatchPathAsyncCallback { export function createMatchPathAsync( absoluteBaseUrl: string, paths: { [key: string]: Array }, - mainFields: string[] = ["main"] + mainFields: string[] = ["main"], + addMatchAll: boolean = true ): MatchPathAsync { const absolutePaths = MappingEntry.getAbsoluteMappingEntries( absoluteBaseUrl, - paths + paths, + addMatchAll ); return ( diff --git a/src/match-path-sync.ts b/src/match-path-sync.ts index e43e74b..21246dd 100644 --- a/src/match-path-sync.ts +++ b/src/match-path-sync.ts @@ -25,11 +25,13 @@ export interface MatchPath { export function createMatchPath( absoluteBaseUrl: string, paths: { [key: string]: Array }, - mainFields: string[] = ["main"] + mainFields: string[] = ["main"], + addMatchAll: boolean = true ): MatchPath { const absolutePaths = MappingEntry.getAbsoluteMappingEntries( absoluteBaseUrl, - paths + paths, + addMatchAll ); return ( diff --git a/src/register.ts b/src/register.ts index 232c346..8bb80ab 100644 --- a/src/register.ts +++ b/src/register.ts @@ -65,7 +65,9 @@ export function register(explicitParams: ExplicitParams): () => void { const matchPath = createMatchPath( configLoaderResult.absoluteBaseUrl, - configLoaderResult.paths + configLoaderResult.paths, + configLoaderResult.mainFields, + configLoaderResult.addMatchAll ); // Patch node's module loading diff --git a/test/data/match-path-data.ts b/test/data/match-path-data.ts index d4699ef..d0c60a1 100644 --- a/test/data/match-path-data.ts +++ b/test/data/match-path-data.ts @@ -8,6 +8,7 @@ export interface OneTest { readonly absoluteBaseUrl: string; readonly paths: { [key: string]: Array }; readonly mainFields?: string[]; + readonly addMatchAll?: boolean; readonly existingFiles: ReadonlyArray; readonly requestedModule: string; readonly extensions?: ReadonlyArray; @@ -181,6 +182,15 @@ export const tests: ReadonlyArray = [ requestedModule: "mylib", expectedPath: dirname(join("/root", "mylib", "index.ts")) }, + { + name: "should not resolve with the help of baseUrl when asked not to", + absoluteBaseUrl: "/root/", + paths: {}, + addMatchAll: false, + existingFiles: [join("/root", "mylib", "index.ts")], + requestedModule: "mylib", + expectedPath: undefined + }, { name: "should not locate path that does not match", absoluteBaseUrl: "/root/", diff --git a/test/mapping-entry-test.ts b/test/mapping-entry-test.ts index 876ddb8..004888f 100644 --- a/test/mapping-entry-test.ts +++ b/test/mapping-entry-test.ts @@ -4,11 +4,15 @@ import { join } from "path"; describe("mapping-entry", () => { it("should change to absolute paths and sort in longest prefix order", () => { - const result = getAbsoluteMappingEntries("/absolute/base/url", { - "*": ["/foo1", "/foo2"], - "longest/pre/fix/*": ["/foo2/bar"], - "pre/fix/*": ["/foo3"] - }); + const result = getAbsoluteMappingEntries( + "/absolute/base/url", + { + "*": ["/foo1", "/foo2"], + "longest/pre/fix/*": ["/foo2/bar"], + "pre/fix/*": ["/foo3"] + }, + true + ); assert.deepEqual(result, [ { pattern: "longest/pre/fix/*", @@ -27,4 +31,17 @@ describe("mapping-entry", () => { } ]); }); + + it("should should add a match-all pattern when requested", () => { + let result = getAbsoluteMappingEntries("/absolute/base/url", {}, true); + assert.deepEqual(result, [ + { + pattern: "*", + paths: [join("/absolute", "base", "url", "*")] + } + ]); + + result = getAbsoluteMappingEntries("/absolute/base/url", {}, false); + assert.deepEqual(result, []); + }); }); diff --git a/test/match-path-async-tests.ts b/test/match-path-async-tests.ts index e3249d0..e55bcc6 100644 --- a/test/match-path-async-tests.ts +++ b/test/match-path-async-tests.ts @@ -8,7 +8,8 @@ describe("match-path-async", () => { const matchPath = createMatchPathAsync( t.absoluteBaseUrl, t.paths, - t.mainFields + t.mainFields, + t.addMatchAll ); matchPath( t.requestedModule, diff --git a/test/match-path-sync-tests.ts b/test/match-path-sync-tests.ts index d66cc0e..668bc4a 100644 --- a/test/match-path-sync-tests.ts +++ b/test/match-path-sync-tests.ts @@ -8,7 +8,8 @@ describe("match-path-sync", () => { const matchPath = createMatchPath( t.absoluteBaseUrl, t.paths, - t.mainFields + t.mainFields, + t.addMatchAll ); const result = matchPath( t.requestedModule,