Skip to content
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
28 changes: 25 additions & 3 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -425,10 +425,32 @@ const visitImportDeclaration = (node: ts.ImportDeclaration) => {
};

const visitImportCallExpression = (node: ts.CallExpression) => {
if (pathRewriting) {
if (ts.isStringLiteral(node.arguments[0])) {
const moduleSpecifier = node.arguments[0];
if (ts.isStringLiteral(node.arguments[0])) {
const moduleSpecifier = node.arguments[0];

if (imports) {
const match = imports.find(([alias]) =>
moduleSpecifier.text.startsWith(alias)
);

if (match) {
let replacement = match[1];

// relative path: non absolute path that's not an @alias
if (!isAbsolute(replacement) && replacement.startsWith(".")) {
replacement = relative(dirname(filePath), replacement) +
(replacement.endsWith("/") ? "/" : "");
}

transformSpecifiers.push({
pos: moduleSpecifier.pos,
alias: new RegExp("(['\"])" + RegExp.escape(match[0])),
replacement,
});
}
}

if (pathRewriting) {
sourceCode = sourceCode.slice(0, moduleSpecifier.pos) +
`"${moduleSpecifier.text.replace(/\.ts$/, ".js")}"` +
sourceCode.slice(moduleSpecifier.end);
Expand Down
9 changes: 8 additions & 1 deletion tests/supported/cases/option-imports/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,11 @@ import { c, type C } from "$baz/bar";

export { b } from "$fiz/bar.ts";
export { type B } from "$foo/bar.d.ts";
export { c, type C } from "$baz/bar";
export { c, type C } from "$baz/bar";

import("$foo/bar.ts");
import("$fiz/bar.ts");
import("$baz/bar");

// Expressions are not rewritten
import("$foo/bar" + ".ts");
9 changes: 8 additions & 1 deletion tests/supported/cases/option-imports/output.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,11 @@ import { b } from "../fiz/bar.js";
import { c,} from "@baz/bar";

export { b } from "../fiz/bar.js";
export { c, } from "@baz/bar";
export { c, } from "@baz/bar";

import("../foo/bar.js");
import("../fiz/bar.js");
import("@baz/bar");

// Expressions are not rewritten
import("$foo/bar" + ".ts");
Loading