Skip to content

Commit

Permalink
fixes for tsconfig.json "paths" support (fixes #60)
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed May 29, 2020
1 parent a982800 commit 0e0a9fb
Show file tree
Hide file tree
Showing 3 changed files with 313 additions and 86 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## Unreleased

* Add support for the `"paths"` field in `tsconfig.json` ([#60](https://github.com/evanw/esbuild/issues/60) and [#144](https://github.com/evanw/esbuild/issues/144))

This provides a way of remapping module paths to local file paths. It's relatively powerful because it supports wildcard patterns and multiple fallback locations. See [the documentation in the TypeScript handbook](https://www.typescriptlang.org/docs/handbook/module-resolution.html#path-mapping) for more information about how this feature works. This was contributed by [@viankakrisna](https://github.com/viankakrisna).

## 0.4.3

* Fix bug with optional chaining parentheses ([#156](https://github.com/evanw/esbuild/issues/156))
Expand Down
185 changes: 151 additions & 34 deletions internal/bundler/bundler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1389,37 +1389,115 @@ console.log(main_esm_default());
func TestTsConfigPaths(t *testing.T) {
expectBundled(t, bundled{
files: map[string]string{
"/Users/user/project/src/entry.js": `
import fn from 'core/test'
import fn2 from 'testing/test'
console.log(fn())
console.log(fn2())
"/Users/user/project/entry.ts": `
import baseurl_dot from './baseurl_dot'
import baseurl_nested from './baseurl_nested'
console.log(baseurl_dot, baseurl_nested)
`,

// Tests with "baseUrl": "."
"/Users/user/project/baseurl_dot/index.ts": `
import test0 from 'test0'
import test1 from 'test1/foo'
import test2 from 'test2/foo'
import test3 from 'test3/foo'
import test4 from 'test4/foo'
import test5 from 'test5/foo'
export default {
test0,
test1,
test2,
test3,
test4,
test5,
}
`,
"/Users/user/project/tsconfig.json": `
"/Users/user/project/baseurl_dot/tsconfig.json": `
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"core/*": ["./src/*"],
"testing": ["./someotherdir/*"]
"test0": ["./test0-success.ts"],
"test1/*": ["./test1-success.ts"],
"test2/*": ["./test2-success/*"],
"t*t3/foo": ["./test3-succ*s.ts"],
"test4/*": ["./test4-first/*", "./test4-second/*"],
"test5/*": ["./test5-first/*", "./test5-second/*"],
}
}
}
`,
"/Users/user/project/src/test.js": `
module.exports = function() {
return 123
"/Users/user/project/baseurl_dot/test0-success.ts": `
export default 'test0-success'
`,
"/Users/user/project/baseurl_dot/test1-success.ts": `
export default 'test1-success'
`,
"/Users/user/project/baseurl_dot/test2-success/foo.ts": `
export default 'test2-success'
`,
"/Users/user/project/baseurl_dot/test3-success.ts": `
export default 'test3-success'
`,
"/Users/user/project/baseurl_dot/test4-first/foo.ts": `
export default 'test4-success'
`,
"/Users/user/project/baseurl_dot/test5-second/foo.ts": `
export default 'test5-success'
`,

// Tests with "baseUrl": "nested"
"/Users/user/project/baseurl_nested/index.ts": `
import test0 from 'test0'
import test1 from 'test1/foo'
import test2 from 'test2/foo'
import test3 from 'test3/foo'
import test4 from 'test4/foo'
import test5 from 'test5/foo'
export default {
test0,
test1,
test2,
test3,
test4,
test5,
}
`,
"/Users/user/project/someotherdir/test.js": `
module.exports = function() {
return 123
"/Users/user/project/baseurl_nested/tsconfig.json": `
{
"compilerOptions": {
"baseUrl": "nested",
"paths": {
"test0": ["./test0-success.ts"],
"test1/*": ["./test1-success.ts"],
"test2/*": ["./test2-success/*"],
"t*t3/foo": ["./test3-succ*s.ts"],
"test4/*": ["./test4-first/*", "./test4-second/*"],
"test5/*": ["./test5-first/*", "./test5-second/*"],
}
}
}
`,
"/Users/user/project/baseurl_nested/nested/test0-success.ts": `
export default 'test0-success'
`,
"/Users/user/project/baseurl_nested/nested/test1-success.ts": `
export default 'test1-success'
`,
"/Users/user/project/baseurl_nested/nested/test2-success/foo.ts": `
export default 'test2-success'
`,
"/Users/user/project/baseurl_nested/nested/test3-success.ts": `
export default 'test3-success'
`,
"/Users/user/project/baseurl_nested/nested/test4-first/foo.ts": `
export default 'test4-success'
`,
"/Users/user/project/baseurl_nested/nested/test5-second/foo.ts": `
export default 'test5-success'
`,
},
entryPaths: []string{"/Users/user/project/src/entry.js"},
entryPaths: []string{"/Users/user/project/entry.ts"},
parseOptions: parser.ParseOptions{
IsBundling: true,
},
Expand All @@ -1428,25 +1506,64 @@ func TestTsConfigPaths(t *testing.T) {
AbsOutputFile: "/Users/user/project/out.js",
},
expected: map[string]string{
"/Users/user/project/out.js": `// /Users/user/project/src/test.js
var require_test2 = __commonJS((exports, module) => {
module.exports = function() {
return 123;
};
});
"/Users/user/project/out.js": `// /Users/user/project/baseurl_dot/test0-success.ts
const test0_success_default = "test0-success";
// /Users/user/project/someotherdir/test.js
var require_test = __commonJS((exports, module) => {
module.exports = function() {
return 123;
};
});
// /Users/user/project/baseurl_dot/test1-success.ts
const test1_success_default = "test1-success";
// /Users/user/project/src/entry.js
const test = __toModule(require_test2());
const test2 = __toModule(require_test());
console.log(test.default());
console.log(test2.default());
// /Users/user/project/baseurl_dot/test2-success/foo.ts
const foo_default = "test2-success";
// /Users/user/project/baseurl_dot/test3-success.ts
const test3_success_default = "test3-success";
// /Users/user/project/baseurl_dot/test4-first/foo.ts
const foo_default2 = "test4-success";
// /Users/user/project/baseurl_dot/test5-second/foo.ts
const foo_default3 = "test5-success";
// /Users/user/project/baseurl_dot/index.ts
const index_default = {
test0: test0_success_default,
test1: test1_success_default,
test2: foo_default,
test3: test3_success_default,
test4: foo_default2,
test5: foo_default3
};
// /Users/user/project/baseurl_nested/nested/test0-success.ts
const test0_success_default2 = "test0-success";
// /Users/user/project/baseurl_nested/nested/test1-success.ts
const test1_success_default2 = "test1-success";
// /Users/user/project/baseurl_nested/nested/test2-success/foo.ts
const foo_default4 = "test2-success";
// /Users/user/project/baseurl_nested/nested/test3-success.ts
const test3_success_default2 = "test3-success";
// /Users/user/project/baseurl_nested/nested/test4-first/foo.ts
const foo_default5 = "test4-success";
// /Users/user/project/baseurl_nested/nested/test5-second/foo.ts
const foo_default6 = "test5-success";
// /Users/user/project/baseurl_nested/index.ts
const index_default2 = {
test0: test0_success_default2,
test1: test1_success_default2,
test2: foo_default4,
test3: test3_success_default2,
test4: foo_default5,
test5: foo_default6
};
// /Users/user/project/entry.ts
console.log(index_default, index_default2);
`,
},
})
Expand Down
Loading

0 comments on commit 0e0a9fb

Please sign in to comment.