From bc9913f24d17c14d3742ee0d690a9b19e63a3b78 Mon Sep 17 00:00:00 2001 From: jjangga0214 Date: Wed, 26 Jan 2022 17:01:38 +0900 Subject: [PATCH] test: for ts path mapping --- test/root3/README.md | 3 +++ test/root3/packages/bar/index.ts | 8 ++++++++ test/root3/packages/foo/hello.ts | 3 +++ test/root3/packages/foo/index.ts | 10 ++++++++++ test/root3/tsconfig.json | 27 +++++++++++++++++++++++++++ test/test.js | 24 ++++++++++++++++++++++++ 6 files changed, 75 insertions(+) create mode 100644 test/root3/README.md create mode 100644 test/root3/packages/bar/index.ts create mode 100644 test/root3/packages/foo/hello.ts create mode 100644 test/root3/packages/foo/index.ts create mode 100644 test/root3/tsconfig.json diff --git a/test/root3/README.md b/test/root3/README.md new file mode 100644 index 0000000..4735536 --- /dev/null +++ b/test/root3/README.md @@ -0,0 +1,3 @@ +# root3 + +This is a simple example for typescript monorepo, using [path mapping](https://www.typescriptlang.org/docs/handbook/module-resolution.html#path-mapping). diff --git a/test/root3/packages/bar/index.ts b/test/root3/packages/bar/index.ts new file mode 100644 index 0000000..750f16d --- /dev/null +++ b/test/root3/packages/bar/index.ts @@ -0,0 +1,8 @@ +import { doubleNumbers } from "@monorepo/foo"; + +export const run = () => { + const value = doubleNumbers([1, 2, 3]); + return value; +}; + +console.log(run()); diff --git a/test/root3/packages/foo/hello.ts b/test/root3/packages/foo/hello.ts new file mode 100644 index 0000000..026083d --- /dev/null +++ b/test/root3/packages/foo/hello.ts @@ -0,0 +1,3 @@ +export const hello = (to: string) => { + console.log(`hello ${to}`) +} diff --git a/test/root3/packages/foo/index.ts b/test/root3/packages/foo/index.ts new file mode 100644 index 0000000..f844474 --- /dev/null +++ b/test/root3/packages/foo/index.ts @@ -0,0 +1,10 @@ +/* The module name can be just './hello'. +But '#foo/hello' is demonstration of "Path Mapping" of `tsconfig` +and "Subpath imports"(defined in package.json's `imports` field) of node.js */ +import { hello } from "#foo/hello"; +// import { hello } from './hello' => this will work, too +// import { hello } from '@monorepo/foo/hello' // => this will not work for tsc, without additional configuration on tsconfig.json + +hello("world"); + +export const doubleNumbers = (data: number[]) => data.map((i) => i * 2); diff --git a/test/root3/tsconfig.json b/test/root3/tsconfig.json new file mode 100644 index 0000000..010d505 --- /dev/null +++ b/test/root3/tsconfig.json @@ -0,0 +1,27 @@ +{ + "compilerOptions": { + "target": "ESNext", + "module": "CommonJS", + "rootDir": ".", + "baseUrl": "packages", + "paths": { + "@monorepo/*": ["*"], + "#foo/*": ["foo/*"], + "#bar/*": ["bar/*"], + "#*": ["*"] + }, + "strict": true, + "noUnusedLocals": true, + "noUnusedParameters": true, + "noImplicitReturns": true, + "noFallthroughCasesInSwitch": true, + "removeComments": true, + "composite": true, + "declaration": true, + "declarationMap": true, + "sourceMap": true, + "esModuleInterop": true, + "incremental": true, + "resolveJsonModule": true + } +} diff --git a/test/test.js b/test/test.js index f97bcd1..ffa770d 100644 --- a/test/test.js +++ b/test/test.js @@ -518,6 +518,30 @@ describe('filing-cabinet', function() { assert.equal(result, path.join(directory, 'foo.ts')); }); }); + describe(`when the typescript's path mapping is configured`, function() { + it('should resolve the path', function() { + const result = cabinet({ + partial: '#foo/hello', + filename: path.resolve(__dirname, 'root3', 'packages', 'foo', 'index.ts'), + directory: path.resolve(__dirname, 'root3'), + tsConfig: { + 'compilerOptions': { + 'rootDir': '.', + 'baseUrl': 'packages', + 'paths': { + '@monorepo/*': ['*'], + '#foo/*': ['foo/*'], + '#bar/*': ['bar/*'], + '#*': ['*'] + }, + }, + }, + tsConfigPath: path.resolve(__dirname, 'root3', 'tsconfig.json'), + }); + const expected = path.resolve(__dirname, 'root3', 'packages', 'foo', 'hello.ts'); + assert.equal(result, expected); + }); + }); }); describe('when not given a tsconfig', function() {