Skip to content

Commit

Permalink
fix: handle tsconfig with out paths (#41)
Browse files Browse the repository at this point in the history
* test: tsconfig without paths should work

* fix: handle tsconfig with out paths

* ci: fix appveyor
  • Loading branch information
danielpza authored Nov 9, 2019
1 parent 8807465 commit 1e936b8
Show file tree
Hide file tree
Showing 15 changed files with 74 additions and 27 deletions.
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@
"prepare": "npm run build",
"release": "standard-version",
"pretest": "npm run build && rm -rf tests/__result",
"test-original": "tsc -p tests/__fixtures/tsconfig.json --outDir tests/__result/original",
"test-generated": "ttsc -p tests/__fixtures/tsconfig.json --outDir tests/__result/generated",
"test": "npm run test-original && npm run test-generated && jest"
"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"
},
"jest": {
"preset": "ts-jest",
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { dirname, relative, resolve, extname } from "path";
import ts from "typescript";
import slash from "slash";
import { parse } from "url";
import { existsSync, statSync } from "fs";
import { existsSync } from "fs";

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

const { isDeclarationFile } = sourceFile;

const { baseUrl = "", paths = {} } = compilerOptions;
const { baseUrl = "", paths = { "*": ["*"] } } = compilerOptions;

const binds = Object.keys(paths)
.filter(key => paths[key].length)
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
"target": "es5",
"module": "commonjs",

"strict": true,

"declaration": true,
"baseUrl": "./",
"paths": {
Expand All @@ -16,8 +14,8 @@
"esModuleInterop": true,

"plugins": [
{ "transform": "../../" },
{ "transform": "../../", "afterDeclarations": true }
{ "transform": "../../../" },
{ "transform": "../../../", "afterDeclarations": true }
]
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
5 changes: 5 additions & 0 deletions tests/__fixtures/without-path/core/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { log } from "utils/logger";

export function main() {
log("Hello World");
}
16 changes: 16 additions & 0 deletions tests/__fixtures/without-path/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",

"declaration": true,
"baseUrl": "./",

"esModuleInterop": true,

"plugins": [
{ "transform": "../../../" },
{ "transform": "../../../", "afterDeclarations": true }
]
}
}
3 changes: 3 additions & 0 deletions tests/__fixtures/without-path/utils/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function log(x) {
console.log(x);
}
63 changes: 45 additions & 18 deletions tests/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,54 @@ import slash = require("slash");
import read = require("fs-readdir-recursive");
import { readFileSync } from "fs";

const root = join(__dirname, "__result/original");
const files = read(root);
files.forEach(file => {
test(file, () => {
const originalFile = join(__dirname, "__result/original", file);
const sourceDir = dirname(relative(root, originalFile));
const original = update(readFileSync(originalFile, "utf8"), sourceDir);
const generatedFile = join(__dirname, "__result/generated", file);
const generated = readFileSync(generatedFile, "utf8");
expect(generated).toEqual(original);
describe("with-path", () => {
const root = join(__dirname, "__result/with-path/original");
const files = read(root);
files.forEach(file => {
test(file, () => {
const originalFile = join(__dirname, "__result/with-path/original", file);
const sourceDir = dirname(relative(root, originalFile));
const original = readFileSync(originalFile, "utf8")
.replace(/"(@.*)"/g, (_, moduleName) => {
return `"${bindModuleToFile(moduleName, sourceDir)}"`;
})
.replace('"path"', '"https://external.url/path.js"')
.replace('"circular/a"', '"../circular/a"');
const generatedFile = join(
__dirname,
"__result/with-path/generated",
file
);
const generated = readFileSync(generatedFile, "utf8");
expect(generated).toEqual(original);
});
});
});

function update(content: string, sourceDir: string) {
return content
.replace(/"(@.*)"/g, (_, moduleName) => {
return `"${bindModuleToFile(moduleName, sourceDir)}"`;
})
.replace('"path"', '"https://external.url/path.js"')
.replace('"circular/a"', '"../circular/a"');
}
describe("without-path", () => {
const root = join(__dirname, "__result/without-path/original");
const files = read(root);
files.forEach(file => {
test(file, () => {
const originalFile = join(
__dirname,
"__result/without-path/original",
file
);
const original = readFileSync(originalFile, "utf8").replace(
'"utils/logger"',
'"../utils/logger"'
);
const generatedFile = join(
__dirname,
"__result/without-path/generated",
file
);
const generated = readFileSync(generatedFile, "utf8");
expect(generated).toEqual(original);
});
});
});

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

0 comments on commit 1e936b8

Please sign in to comment.