Skip to content

fix: multiple issue fixes #61

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions package.json
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,6 @@
"**/tests/index.ts"
]
},
"dependencies": {
"slash": "^3.0.0"
},
"devDependencies": {
"@types/fs-readdir-recursive": "^1.0.0",
"@types/jest": "^24.0.15",
Expand All @@ -59,6 +56,6 @@
"standard-version": "^6.0.1",
"ts-jest": "^24.0.2",
"ttypescript": "^1.5.6",
"typescript": "^3.4.5"
"typescript": "^3.9.7"
}
}
70 changes: 46 additions & 24 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
import { dirname, relative, resolve, extname } from "path";
import ts from "typescript";
import slash from "slash";
import { parse } from "url";
import { existsSync } from "fs";


/* ****************************************************************************************************************** *
* Helpers
* ****************************************************************************************************************** */

export const normalizePath = (p: string) =>
// Is extended length or has non-ascii chars (respectively)
(/^\\\\\?\\/.test(p) || /[^\u0000-\u0080]+/.test(p)) ? p :
// Normalize to forward slash and remove repeating slashes
p.replace(/[\\\/]+/g, '/');


/* ****************************************************************************************************************** *
* Transformer
* ****************************************************************************************************************** */

const transformer = (_: ts.Program) => (context: ts.TransformationContext) => (
sourceFile: ts.SourceFile
) => {
Expand Down Expand Up @@ -36,7 +51,7 @@ const transformer = (_: ts.Program) => (context: ts.TransformationContext) => (
.filter(key => paths[key].length)
.map(key => ({
regexp: new RegExp("^" + key.replace("*", "(.*)") + "$"),
path: paths[key][0]
paths: paths[key]
}));

if (!baseUrl || binds.length === 0) {
Expand Down Expand Up @@ -66,19 +81,24 @@ const transformer = (_: ts.Program) => (context: ts.TransformationContext) => (
return moduleName;
}

for (const { regexp, path } of binds) {
for (const { regexp, paths } of binds) {
const match = regexp.exec(moduleName);
if (match) {
const out = path.replace(/\*/g, match[1]);
if (isUrl(out)) {
return out;
for (const p of paths) {
const out = p.replace(/\*/g, match[1]);

if (isUrl(out)) return out;

const filepath = resolve(baseUrl, out);
if (!fileExists(`${filepath}/index`) && !fileExists(filepath)) continue;

const resolved = fixupImportPath(relative(sourceDir, filepath));

return isRelative(resolved) ? resolved : `./${resolved}`;
}
const filepath = resolve(baseUrl, out);
if (!fileExists(`${filepath}/index`) && !fileExists(filepath)) continue;
const resolved = slash(relative(sourceDir, filepath));
return isRelative(resolved) ? resolved : `./${resolved}`;
}
}

return undefined;
}

Expand All @@ -96,17 +116,6 @@ const transformer = (_: ts.Program) => (context: ts.TransformationContext) => (
node.arguments.length === 1;

function visit(node: ts.Node): ts.VisitResult<ts.Node> {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note on TS changes regarding type only exports

It seems that types only export behaviour has changed in TypeScript. For one, they added isTypeOnly, but they've also changed the standard behaviour of export * from 'module' and import 'module'

Even if module has only type exports, this line will still get compiled into the final result. This was causing the tests to fail, by default, after cloning the package. In order to match the prescribed TS behaviour , we've added the isTypeOnly param where needed as well as removed the following conditional.

In short, this means the TS compiler has taken over this logic.

if (
!isDeclarationFile &&
resolver &&
ts.isExportDeclaration(node) &&
!node.exportClause &&
!compilerOptions.isolatedModules &&
!resolver.moduleExportsSomeValue(node.moduleSpecifier)
) {
return undefined;
}

if (isRequire(node) || isAsyncImport(node)) {
return unpathRequireAndAsyncImport(node);
}
Expand Down Expand Up @@ -224,7 +233,7 @@ const transformer = (_: ts.Program) => (context: ts.TransformationContext) => (
ts.isNamedImports
);
return name || namedBindings
? ts.updateImportClause(node, name, namedBindings)
? ts.updateImportClause(node, name, namedBindings, node.isTypeOnly)
: undefined;
}
function visitNamedImportBindings(
Expand Down Expand Up @@ -273,7 +282,8 @@ const transformer = (_: ts.Program) => (context: ts.TransformationContext) => (
node.decorators,
node.modifiers,
node.exportClause,
fileLiteral
fileLiteral,
node.isTypeOnly
);
}

Expand All @@ -290,7 +300,8 @@ const transformer = (_: ts.Program) => (context: ts.TransformationContext) => (
node.decorators,
node.modifiers,
node.exportClause,
fileLiteral
fileLiteral,
node.isTypeOnly
)
: undefined;
}
Expand All @@ -312,6 +323,17 @@ const transformer = (_: ts.Program) => (context: ts.TransformationContext) => (
return resolver.isValueAliasDeclaration(node) ? node : undefined;
}

function fixupImportPath(p:string) {
let res = normalizePath(p);

/* Remove implicit extension */
const ext = extname(res);
if (ext && implicitExtensions.includes(ext.replace(/^\./, '')))
res = res.slice(0, -ext.length);

return res;
}

return ts.visitNode(sourceFile, visit);
};

Expand Down
4 changes: 3 additions & 1 deletion tests/__fixtures/with-path/core/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import sum = require("@utils/sum");
export { sum } from "@utils";
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Allows testing for secondary path routing

export { g } from "#utils/hello";
export { sum as sum2 } from "#utils/sum";
export { NoRuntimecodeHere } from "@utils/types-only";
import { subs, NoRuntimecodeHere } from "@utils";
import "@circular/b";
Expand Down Expand Up @@ -36,4 +38,4 @@ const a = new A("");

testerClass.test(12);
testerConst.test("12");
})();
})();
1 change: 1 addition & 0 deletions tests/__fixtures/with-path/secondary/hello.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const g = 'hello'
1 change: 1 addition & 0 deletions tests/__fixtures/with-path/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"paths": {
"path": ["https://external.url/path.js"],
"@*": ["*"],
"#utils/*": [ "./utils/*", "./secondary/*" ],
"*": ["*"]
},

Expand Down
2 changes: 1 addition & 1 deletion tests/__fixtures/with-path/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export * from "@utils/sum";
export * from "@utils/subs";
export * from "@utils/types-only";
export { NoRuntimecodeHere } from "@utils/types-only";
1 change: 1 addition & 0 deletions tests/__fixtures/without-path/dir2/other.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const g = 'hello'
13 changes: 11 additions & 2 deletions tests/index.ts
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { dirname, relative, join } from "path";
import slash = require("slash");
import { normalizePath } from '../src'
import read = require("fs-readdir-recursive");
import { readFileSync } from "fs";

Expand All @@ -14,6 +14,9 @@ describe("with-path", () => {
.replace(/"(@.*)"/g, (_, moduleName) => {
return `"${bindModuleToFile(moduleName, sourceDir)}"`;
})
.replace(/"(#utils\/.*)"/g, (_, moduleName) => {
return `"${bindModuleToFile(moduleName, sourceDir)}"`;
})
.replace('"path"', '"https://external.url/path.js"')
.replace('"circular/a"', '"../circular/a"');
const generatedFile = join(
Expand Down Expand Up @@ -56,7 +59,13 @@ function bindModuleToFile(moduleName: string, sourceDir: string) {
const match = /@(.*)/.exec(moduleName);
if (match) {
const out = match[1];
const file = slash(relative(sourceDir, out));
const file = normalizePath(relative(sourceDir, out));
return file[0] === "." ? file : `./${file}`;
}

let utilsModule = /^#utils\/(.+)/.exec(moduleName)?.[1];
if (utilsModule) {
const subDir = (utilsModule === 'hello') ? 'secondary' : 'utils';
return normalizePath(relative(sourceDir, join(subDir, utilsModule)));
}
}
8 changes: 8 additions & 0 deletions tests/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"exclude": [ "__fixtures" ],
"compilerOptions": {
"noEmit": true,
"strict": true,
"esModuleInterop": true
}
}