Skip to content

Commit 1e936b8

Browse files
authored
fix: handle tsconfig with out paths (#41)
* test: tsconfig without paths should work * fix: handle tsconfig with out paths * ci: fix appveyor
1 parent 8807465 commit 1e936b8

File tree

15 files changed

+74
-27
lines changed

15 files changed

+74
-27
lines changed

package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,7 @@
3838
"prepare": "npm run build",
3939
"release": "standard-version",
4040
"pretest": "npm run build && rm -rf tests/__result",
41-
"test-original": "tsc -p tests/__fixtures/tsconfig.json --outDir tests/__result/original",
42-
"test-generated": "ttsc -p tests/__fixtures/tsconfig.json --outDir tests/__result/generated",
43-
"test": "npm run test-original && npm run test-generated && jest"
41+
"test": "tsc -p tests/__fixtures/with-path/tsconfig.json --outDir tests/__result/with-path/original && ttsc -p tests/__fixtures/with-path/tsconfig.json --outDir tests/__result/with-path/generated && tsc -p tests/__fixtures/without-path/tsconfig.json --outDir tests/__result/without-path/original && ttsc -p tests/__fixtures/without-path/tsconfig.json --outDir tests/__result/without-path/generated && jest"
4442
},
4543
"jest": {
4644
"preset": "ts-jest",

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { dirname, relative, resolve, extname } from "path";
22
import ts from "typescript";
33
import slash from "slash";
44
import { parse } from "url";
5-
import { existsSync, statSync } from "fs";
5+
import { existsSync } from "fs";
66

77
const transformer = (_: ts.Program) => (context: ts.TransformationContext) => (
88
sourceFile: ts.SourceFile
@@ -29,7 +29,7 @@ const transformer = (_: ts.Program) => (context: ts.TransformationContext) => (
2929

3030
const { isDeclarationFile } = sourceFile;
3131

32-
const { baseUrl = "", paths = {} } = compilerOptions;
32+
const { baseUrl = "", paths = { "*": ["*"] } } = compilerOptions;
3333

3434
const binds = Object.keys(paths)
3535
.filter(key => paths[key].length)

tests/__fixtures/tsconfig.json renamed to tests/__fixtures/with-path/tsconfig.json

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
"target": "es5",
44
"module": "commonjs",
55

6-
"strict": true,
7-
86
"declaration": true,
97
"baseUrl": "./",
108
"paths": {
@@ -16,8 +14,8 @@
1614
"esModuleInterop": true,
1715

1816
"plugins": [
19-
{ "transform": "../../" },
20-
{ "transform": "../../", "afterDeclarations": true }
17+
{ "transform": "../../../" },
18+
{ "transform": "../../../", "afterDeclarations": true }
2119
]
2220
}
2321
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { log } from "utils/logger";
2+
3+
export function main() {
4+
log("Hello World");
5+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es5",
4+
"module": "commonjs",
5+
6+
"declaration": true,
7+
"baseUrl": "./",
8+
9+
"esModuleInterop": true,
10+
11+
"plugins": [
12+
{ "transform": "../../../" },
13+
{ "transform": "../../../", "afterDeclarations": true }
14+
]
15+
}
16+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function log(x) {
2+
console.log(x);
3+
}

tests/index.ts

Lines changed: 45 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,54 @@ import slash = require("slash");
33
import read = require("fs-readdir-recursive");
44
import { readFileSync } from "fs";
55

6-
const root = join(__dirname, "__result/original");
7-
const files = read(root);
8-
files.forEach(file => {
9-
test(file, () => {
10-
const originalFile = join(__dirname, "__result/original", file);
11-
const sourceDir = dirname(relative(root, originalFile));
12-
const original = update(readFileSync(originalFile, "utf8"), sourceDir);
13-
const generatedFile = join(__dirname, "__result/generated", file);
14-
const generated = readFileSync(generatedFile, "utf8");
15-
expect(generated).toEqual(original);
6+
describe("with-path", () => {
7+
const root = join(__dirname, "__result/with-path/original");
8+
const files = read(root);
9+
files.forEach(file => {
10+
test(file, () => {
11+
const originalFile = join(__dirname, "__result/with-path/original", file);
12+
const sourceDir = dirname(relative(root, originalFile));
13+
const original = readFileSync(originalFile, "utf8")
14+
.replace(/"(@.*)"/g, (_, moduleName) => {
15+
return `"${bindModuleToFile(moduleName, sourceDir)}"`;
16+
})
17+
.replace('"path"', '"https://external.url/path.js"')
18+
.replace('"circular/a"', '"../circular/a"');
19+
const generatedFile = join(
20+
__dirname,
21+
"__result/with-path/generated",
22+
file
23+
);
24+
const generated = readFileSync(generatedFile, "utf8");
25+
expect(generated).toEqual(original);
26+
});
1627
});
1728
});
1829

19-
function update(content: string, sourceDir: string) {
20-
return content
21-
.replace(/"(@.*)"/g, (_, moduleName) => {
22-
return `"${bindModuleToFile(moduleName, sourceDir)}"`;
23-
})
24-
.replace('"path"', '"https://external.url/path.js"')
25-
.replace('"circular/a"', '"../circular/a"');
26-
}
30+
describe("without-path", () => {
31+
const root = join(__dirname, "__result/without-path/original");
32+
const files = read(root);
33+
files.forEach(file => {
34+
test(file, () => {
35+
const originalFile = join(
36+
__dirname,
37+
"__result/without-path/original",
38+
file
39+
);
40+
const original = readFileSync(originalFile, "utf8").replace(
41+
'"utils/logger"',
42+
'"../utils/logger"'
43+
);
44+
const generatedFile = join(
45+
__dirname,
46+
"__result/without-path/generated",
47+
file
48+
);
49+
const generated = readFileSync(generatedFile, "utf8");
50+
expect(generated).toEqual(original);
51+
});
52+
});
53+
});
2754

2855
function bindModuleToFile(moduleName: string, sourceDir: string) {
2956
const match = /@(.*)/.exec(moduleName);

0 commit comments

Comments
 (0)