Skip to content

Commit

Permalink
add option to avoid adding a match-all rule
Browse files Browse the repository at this point in the history
this solves part of dividab#72
  • Loading branch information
Swatinem committed Jan 24, 2019
1 parent 6beaeaf commit 623cd1f
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 18 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ The public API consists of these functions:
export interface ExplicitParams {
baseUrl: string;
paths: { [key: string]: Array<string> };
mainFields?: Array<string>;
addMatchAll?: boolean;
}

/**
Expand Down Expand Up @@ -163,12 +165,14 @@ export interface MatchPath {
* @param absoluteBaseUrl Absolute version of baseUrl as specified in tsconfig.
* @param paths The paths as specified in tsconfig.
* @param mainFields A list of package.json field names to try when resolving module files.
* @param addMatchAll Add a match-all "*" rule if none is present
* @returns a function that can resolve paths.
*/
export function createMatchPath(
absoluteBaseUrl: string,
paths: { [key: string]: Array<string> },
mainFields: string[] = ["main"]
mainFields: string[] = ["main"],
addMatchAll: boolean = true
): MatchPath {
```
Expand Down
8 changes: 7 additions & 1 deletion src/config-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { options } from "./options";
export interface ExplicitParams {
baseUrl: string;
paths: { [key: string]: Array<string> };
mainFields?: Array<string>;
addMatchAll?: boolean;
}

export type TsConfigLoader = (
Expand All @@ -23,6 +25,8 @@ export interface ConfigLoaderSuccessResult {
baseUrl: string;
absoluteBaseUrl: string;
paths: { [key: string]: Array<string> };
mainFields?: Array<string>;
addMatchAll?: boolean;
}

export interface ConfigLoaderFailResult {
Expand Down Expand Up @@ -54,7 +58,9 @@ export function configLoader({
configFileAbsolutePath: "",
baseUrl: explicitParams.baseUrl,
absoluteBaseUrl,
paths: explicitParams.paths
paths: explicitParams.paths,
mainFields: explicitParams.mainFields,
addMatchAll: explicitParams.addMatchAll
};
}

Expand Down
10 changes: 6 additions & 4 deletions src/mapping-entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ export interface Paths {
* sort by keys each time we use the mappings.
* @param absoluteBaseUrl
* @param paths
* @param addMatchAll
*/
export function getAbsoluteMappingEntries(
absoluteBaseUrl: string,
paths: Paths
paths: Paths,
addMatchAll: boolean
): ReadonlyArray<MappingEntry> {
// Resolve all paths to absolute form once here, and sort them by
// longest prefix once here, this saves time on each request later.
Expand All @@ -34,9 +36,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(/\/$/, "")}/*`]
Expand Down
6 changes: 4 additions & 2 deletions src/match-path-async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ export interface MatchPathAsyncCallback {
export function createMatchPathAsync(
absoluteBaseUrl: string,
paths: { [key: string]: Array<string> },
mainFields: string[] = ["main"]
mainFields: string[] = ["main"],
addMatchAll: boolean = true
): MatchPathAsync {
const absolutePaths = MappingEntry.getAbsoluteMappingEntries(
absoluteBaseUrl,
paths
paths,
addMatchAll
);

return (
Expand Down
7 changes: 5 additions & 2 deletions src/match-path-sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,19 @@ export interface MatchPath {
* @param absoluteBaseUrl Absolute version of baseUrl as specified in tsconfig.
* @param paths The paths as specified in tsconfig.
* @param mainFields A list of package.json field names to try when resolving module files.
* @param addMatchAll Add a match-all "*" rule if none is present
* @returns a function that can resolve paths.
*/
export function createMatchPath(
absoluteBaseUrl: string,
paths: { [key: string]: Array<string> },
mainFields: string[] = ["main"]
mainFields: string[] = ["main"],
addMatchAll: boolean = true
): MatchPath {
const absolutePaths = MappingEntry.getAbsoluteMappingEntries(
absoluteBaseUrl,
paths
paths,
addMatchAll
);

return (
Expand Down
4 changes: 3 additions & 1 deletion src/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions test/data/match-path-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export interface OneTest {
readonly absoluteBaseUrl: string;
readonly paths: { [key: string]: Array<string> };
readonly mainFields?: string[];
readonly addMatchAll?: boolean;
readonly existingFiles: ReadonlyArray<string>;
readonly requestedModule: string;
readonly extensions?: ReadonlyArray<string>;
Expand Down Expand Up @@ -181,6 +182,15 @@ export const tests: ReadonlyArray<OneTest> = [
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/",
Expand Down
27 changes: 22 additions & 5 deletions test/mapping-entry-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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/*",
Expand All @@ -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, []);
});
});
3 changes: 2 additions & 1 deletion test/match-path-async-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ describe("match-path-async", () => {
const matchPath = createMatchPathAsync(
t.absoluteBaseUrl,
t.paths,
t.mainFields
t.mainFields,
t.addMatchAll
);
matchPath(
t.requestedModule,
Expand Down
3 changes: 2 additions & 1 deletion test/match-path-sync-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 623cd1f

Please sign in to comment.