Skip to content

Commit

Permalink
use baseurl as a base of resolving
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanmourtada committed May 23, 2017
1 parent cd02be6 commit cb18815
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 28 deletions.
11 changes: 2 additions & 9 deletions src/config-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,26 +54,19 @@ export function configLoader({
};
}

if(!loadResult.baseUrl) {
if (!loadResult.baseUrl) {
return {
resultType: "failed",
message: "Missing baseUrl in compilerOptions"
};
}

if(!loadResult.paths) {
return {
resultType: "failed",
message: "Missing paths in compilerOptions"
};
}

const tsConfigDir = path.dirname(loadResult.tsConfigPath);
const absoluteBaseUrl = path.join(tsConfigDir, loadResult.baseUrl);

return {
resultType: "success",
absoluteBaseUrl,
paths: loadResult.paths
paths: loadResult.paths || {}
};
}
7 changes: 5 additions & 2 deletions src/match-path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,16 @@ export function createMatchPath(
absoluteBaseUrl: string,
paths: { [key: string]: Array<string> }): MatchPath {

// Resolve all paths to absolute form once here, this saves time on each request later
// Resolve all paths to absolute form once here, this saves time on each request later.
// We also add the baseUrl as a base which will be replace if specified in paths. This is how typescript works
const absolutePaths: { [key: string]: Array<string> } = Object.keys(paths)
.reduce((soFar, key) => ({
...soFar,
[key]: paths[key]
.map((pathToResolve) => path.join(absoluteBaseUrl, pathToResolve))
}), {});
}), {
"*": [`${absoluteBaseUrl.replace(/\/$/, "")}/*`],
});

return (sourceFileName: string, requestedModule: string, readPackageJson: (packageJsonPath: string) => any, fileExists: any, extensions?: Array<string>) =>
matchFromAbsolutePaths(absolutePaths, sourceFileName, requestedModule, readPackageJson, fileExists, extensions);
Expand Down
16 changes: 0 additions & 16 deletions tests/config-loader-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,20 +68,4 @@ describe('config-loader', function () {
assert.isTrue(failResult.message.indexOf("baseUrl") > -1);
});

it('should show an error message when paths is missing', () => {
const result = configLoader({
explicitParams: undefined,
cwd: "/baz",
tsConfigLoader: (_: any) => ({
tsConfigPath: "/baz/tsconfig.json",
baseUrl: "./src",
paths: undefined
})
});

const failResult = result as ConfigLoaderFailResult
assert.equal(failResult.resultType, "failed");
assert.isTrue(failResult.message.indexOf("paths") > -1);
});

});
14 changes: 13 additions & 1 deletion tests/match-path-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe('find-path', function () {

it('should resolve to correct path when many are specified', () => {

const matchPath = createMatchPath("/root/", { "lib/*": ["foo1/*", "foo2/*","location/*", "foo3/*"] });
const matchPath = createMatchPath("/root/", { "lib/*": ["foo1/*", "foo2/*", "location/*", "foo3/*"] });
const result = matchPath(
"/root/test.ts",
"lib/mylib",
Expand Down Expand Up @@ -113,6 +113,18 @@ describe('find-path', function () {
assert.equal(result2, "/root/location/mylibjs/kallejs");
});

it('should resolve to with the help of baseUrl when not explicitly set', () => {

const matchPath = createMatchPath("/root/", {});
const result = matchPath(
"/root/test.ts",
"mylib",
(_: string) => undefined,
(name: string) => name === "/root/mylib/index.ts"
);
assert.equal(result, "/root/mylib");
});

it('should not locate path that does not match', () => {

const matchPath = createMatchPath("/root/", { "lib/*": ["location/*"] });
Expand Down

0 comments on commit cb18815

Please sign in to comment.