diff --git a/package.json b/package.json index 2a3c9861..23379037 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/index.ts b/src/index.ts index 3a331f8f..429869e5 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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 @@ -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) diff --git a/tests/__fixtures/circular/a.ts b/tests/__fixtures/with-path/circular/a.ts similarity index 100% rename from tests/__fixtures/circular/a.ts rename to tests/__fixtures/with-path/circular/a.ts diff --git a/tests/__fixtures/circular/b.ts b/tests/__fixtures/with-path/circular/b.ts similarity index 100% rename from tests/__fixtures/circular/b.ts rename to tests/__fixtures/with-path/circular/b.ts diff --git a/tests/__fixtures/core/index.ts b/tests/__fixtures/with-path/core/index.ts similarity index 100% rename from tests/__fixtures/core/index.ts rename to tests/__fixtures/with-path/core/index.ts diff --git a/tests/__fixtures/tsconfig.json b/tests/__fixtures/with-path/tsconfig.json similarity index 71% rename from tests/__fixtures/tsconfig.json rename to tests/__fixtures/with-path/tsconfig.json index 2204e454..7038a78f 100644 --- a/tests/__fixtures/tsconfig.json +++ b/tests/__fixtures/with-path/tsconfig.json @@ -3,8 +3,6 @@ "target": "es5", "module": "commonjs", - "strict": true, - "declaration": true, "baseUrl": "./", "paths": { @@ -16,8 +14,8 @@ "esModuleInterop": true, "plugins": [ - { "transform": "../../" }, - { "transform": "../../", "afterDeclarations": true } + { "transform": "../../../" }, + { "transform": "../../../", "afterDeclarations": true } ] } } diff --git a/tests/__fixtures/utils/index.ts b/tests/__fixtures/with-path/utils/index.ts similarity index 100% rename from tests/__fixtures/utils/index.ts rename to tests/__fixtures/with-path/utils/index.ts diff --git a/tests/__fixtures/utils/subs.ts b/tests/__fixtures/with-path/utils/subs.ts similarity index 100% rename from tests/__fixtures/utils/subs.ts rename to tests/__fixtures/with-path/utils/subs.ts diff --git a/tests/__fixtures/utils/sum.ts b/tests/__fixtures/with-path/utils/sum.ts similarity index 100% rename from tests/__fixtures/utils/sum.ts rename to tests/__fixtures/with-path/utils/sum.ts diff --git a/tests/__fixtures/utils/types-only.ts b/tests/__fixtures/with-path/utils/types-only.ts similarity index 100% rename from tests/__fixtures/utils/types-only.ts rename to tests/__fixtures/with-path/utils/types-only.ts diff --git a/tests/__fixtures/utils/utils.native.ts b/tests/__fixtures/with-path/utils/utils.native.ts similarity index 100% rename from tests/__fixtures/utils/utils.native.ts rename to tests/__fixtures/with-path/utils/utils.native.ts diff --git a/tests/__fixtures/without-path/core/index.ts b/tests/__fixtures/without-path/core/index.ts new file mode 100644 index 00000000..59ebeac5 --- /dev/null +++ b/tests/__fixtures/without-path/core/index.ts @@ -0,0 +1,5 @@ +import { log } from "utils/logger"; + +export function main() { + log("Hello World"); +} diff --git a/tests/__fixtures/without-path/tsconfig.json b/tests/__fixtures/without-path/tsconfig.json new file mode 100644 index 00000000..2b1ea6fc --- /dev/null +++ b/tests/__fixtures/without-path/tsconfig.json @@ -0,0 +1,16 @@ +{ + "compilerOptions": { + "target": "es5", + "module": "commonjs", + + "declaration": true, + "baseUrl": "./", + + "esModuleInterop": true, + + "plugins": [ + { "transform": "../../../" }, + { "transform": "../../../", "afterDeclarations": true } + ] + } +} diff --git a/tests/__fixtures/without-path/utils/logger.ts b/tests/__fixtures/without-path/utils/logger.ts new file mode 100644 index 00000000..5b583277 --- /dev/null +++ b/tests/__fixtures/without-path/utils/logger.ts @@ -0,0 +1,3 @@ +export function log(x) { + console.log(x); +} diff --git a/tests/index.ts b/tests/index.ts index 6f51d5a9..8e169add 100644 --- a/tests/index.ts +++ b/tests/index.ts @@ -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);