Skip to content

FIXED: dynamic type imports #46

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 6 commits into from
Dec 27, 2019
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
81 changes: 65 additions & 16 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ const transformer = (_: ts.Program) => (context: ts.TransformationContext) => (
// if it's relative path do not transform
return moduleName;
}

for (const { regexp, path } of binds) {
const match = regexp.exec(moduleName);
if (match) {
Expand All @@ -81,6 +82,19 @@ const transformer = (_: ts.Program) => (context: ts.TransformationContext) => (
return undefined;
}

const isRequire = (node: ts.Node): node is ts.CallExpression =>
ts.isCallExpression(node) &&
ts.isIdentifier(node.expression) &&
node.expression.text === "require" &&
ts.isStringLiteral(node.arguments[0]) &&
node.arguments.length === 1;

const isAsyncImport = (node: ts.Node): node is ts.CallExpression =>
ts.isCallExpression(node) &&
node.expression.kind === ts.SyntaxKind.ImportKeyword &&
ts.isStringLiteral(node.arguments[0]) &&
node.arguments.length === 1;

function visit(node: ts.Node): ts.VisitResult<ts.Node> {
if (
!isDeclarationFile &&
Expand All @@ -92,36 +106,71 @@ const transformer = (_: ts.Program) => (context: ts.TransformationContext) => (
) {
return undefined;
}
if (
ts.isCallExpression(node) &&
ts.isIdentifier(node.expression) &&
node.expression.text === "require" &&
ts.isStringLiteral(node.arguments[0]) &&
node.arguments.length === 1
) {
const firstArg = node.arguments[0] as ts.StringLiteral;
const file = bindModuleToFile(firstArg.text);
if (!file) {
return node;
}
const fileLiteral = ts.createLiteral(file);
return ts.updateCall(node, node.expression, node.typeArguments, [
fileLiteral
]);

if (isRequire(node) || isAsyncImport(node)) {
return unpathRequireAndAsyncImport(node);
}

if (ts.isExternalModuleReference(node)) {
return unpathImportEqualsDeclaration(node);
}

if (ts.isImportDeclaration(node)) {
return unpathImportDeclaration(node);
}

if (ts.isExportDeclaration(node)) {
return unpathExportDeclaration(node);
}

if (ts.isImportTypeNode(node)) {
return unpathImportTypeNode(node);
}

return ts.visitEachChild(node, visit, context);
}

function unpathRequireAndAsyncImport(node: ts.CallExpression) {
const firstArg = node.arguments[0] as ts.StringLiteral;
const file = bindModuleToFile(firstArg.text);

if (!file) {
return node;
}

const fileLiteral = ts.createLiteral(file);

return ts.updateCall(node, node.expression, node.typeArguments, [
fileLiteral
]);
}

function unpathImportTypeNode(node: ts.ImportTypeNode) {
const argument = node.argument as ts.LiteralTypeNode;
const literal = argument.literal;

if (!ts.isStringLiteral(literal)) {
return node;
}

const file = bindModuleToFile(literal.text);

if (!file) {
return node;
}

const fileLiteral = ts.createLiteral(file);
const fileArgument = ts.updateLiteralTypeNode(argument, fileLiteral);

return ts.updateImportTypeNode(
node,
fileArgument,
node.qualifier,
node.typeArguments,
node.isTypeOf
);
}

function unpathImportEqualsDeclaration(node: ts.ExternalModuleReference) {
if (!ts.isStringLiteral(node.expression)) {
return node;
Expand Down
17 changes: 17 additions & 0 deletions tests/__fixtures/with-path/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,20 @@ const n: NoRuntimecodeHere = null as any;

subs(2, 3);
const a = new A("");

(async function() {
const Logger = await (await import("@dynamic/logger")).Logger;
const logger = new Logger();

logger.log('hi');
})();

(async function() {
const Tester = (await import("@dynamic/tester")).Tester;

const testerConst = (await import("@dynamic/tester")).tester;
const testerClass = new Tester();

testerClass.test(12);
testerConst.test("12");
})();
10 changes: 10 additions & 0 deletions tests/__fixtures/with-path/dynamic/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
type Tester = import("@dynamic/tester").Tester;

export class Logger {
level: string = 'hi';
tester: Tester;

public log (x: string): void {
console.log(x);
};
}
9 changes: 9 additions & 0 deletions tests/__fixtures/with-path/dynamic/tester.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export class Tester {
public test (x: number): void {
console.log(x);
};
}

export const tester = {
test: (x: string) => console.log(x),
};