Skip to content

Commit ac8ee8f

Browse files
committed
refactor!: esm only (#442)
1 parent 85b01e2 commit ac8ee8f

22 files changed

+150
-119
lines changed

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "typescript-transform-paths",
33
"version": "3.5.5",
44
"description": "Transforms module resolution paths using TypeScript path mapping and/or custom paths",
5-
"type": "commonjs",
5+
"type": "module",
66
"exports": {
77
".": {
88
"types": "./dist/index.d.ts",
@@ -54,7 +54,7 @@
5454
],
5555
"devDependencies": {
5656
"@eslint/js": "^9.9.1",
57-
"@tsconfig/node18": "^18.2.4",
57+
"@tsconfig/node22": "^22.0.2",
5858
"@tsconfig/strictest": "^2.0.5",
5959
"@types/eslint": "^9",
6060
"@types/minimatch": "^5.1.2",
@@ -67,7 +67,7 @@
6767
"prettier": "^3.6.2",
6868
"prettier-plugin-jsdoc": "^1.3.3",
6969
"ts-patch": "^3.2.1",
70-
"typescript": "^5.5.4",
70+
"typescript": "^5.8.3",
7171
"typescript-eslint": "^8.3.0"
7272
},
7373
"peerDependencies": {

src/index.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
1-
export type { TsTransformPathsConfig } from "./types";
2-
3-
export { default } from "./transformer";
1+
export { default } from "./transformer.ts";
2+
export type { TsTransformPathsConfig } from "./types.ts";

src/transformer.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
import path from "node:path";
2-
import ts, { CompilerOptions } from "typescript";
3-
import { RunMode, TsNodeState, TsTransformPathsConfig, TsTransformPathsContext, VisitorContext } from "./types";
4-
import { nodeVisitor } from "./visitor";
2+
53
import { Minimatch } from "minimatch";
6-
import { createSyntheticEmitHost, getTsNodeRegistrationProperties } from "./utils/ts-helpers";
7-
import { TransformerExtras } from "ts-patch";
4+
import type { TransformerExtras } from "ts-patch";
5+
import ts, { type CompilerOptions } from "typescript";
6+
7+
import {
8+
RunMode,
9+
TsNodeState,
10+
type TsTransformPathsConfig,
11+
type TsTransformPathsContext,
12+
type VisitorContext,
13+
} from "./types.ts";
14+
import { createSyntheticEmitHost, getTsNodeRegistrationProperties } from "./utils/ts-helpers.ts";
15+
import { nodeVisitor } from "./visitor.ts";
816

917
function getTsProperties(args: Parameters<typeof transformer>) {
1018
let fileNames: readonly string[] | undefined;

src/types.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import ts, { CompilerOptions, EmitHost, Pattern, SourceFile } from "typescript";
2-
import { PluginConfig } from "ts-patch";
31
import { Minimatch } from "minimatch";
2+
import { type PluginConfig } from "ts-patch";
3+
import ts, { type CompilerOptions, type EmitHost, type Pattern, type SourceFile } from "typescript";
44

55
/* ****************************************************************************************************************** */
66
// region: TS Types
@@ -60,15 +60,19 @@ export interface VisitorContext extends TsTransformPathsContext {
6060
// region: General
6161
/* ****************************************************************************************************************** */
6262

63-
export enum RunMode {
64-
TsNode = "ts-node",
65-
Manual = "manual",
66-
Program = "program",
67-
}
63+
export const RunMode = {
64+
TsNode: "ts-node",
65+
Manual: "manual",
66+
Program: "program",
67+
};
6868

69-
export enum TsNodeState {
70-
Full,
71-
Stripped,
72-
}
69+
export type RunMode = (typeof RunMode)[keyof typeof RunMode];
70+
71+
export const TsNodeState = {
72+
Full: 0,
73+
Stripped: 1,
74+
};
75+
76+
export type TsNodeState = (typeof TsNodeState)[keyof typeof TsNodeState];
7377

7478
// endregion

src/utils/elide-import-export.ts

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -35,25 +35,26 @@
3535
* import { A, B } from "./b";
3636
* export { A } from "./b";
3737
*/
38-
import { ImportOrExportDeclaration, VisitorContext } from "../types";
3938
import {
4039
Debug,
41-
EmitResolver,
42-
ExportSpecifier,
43-
ImportClause,
4440
ImportsNotUsedAsValues,
45-
ImportSpecifier,
4641
isInJSFile,
47-
NamedExportBindings,
48-
NamedExports,
49-
NamedImportBindings,
50-
NamespaceExport,
51-
Node,
52-
StringLiteral,
53-
Visitor,
54-
VisitResult,
42+
type EmitResolver,
43+
type ExportSpecifier,
44+
type ImportClause,
45+
type ImportSpecifier,
46+
type NamedExportBindings,
47+
type NamedExports,
48+
type NamedImportBindings,
49+
type NamespaceExport,
50+
type Node,
51+
type StringLiteral,
52+
type Visitor,
53+
type VisitResult,
5554
} from "typescript";
5655

56+
import type { ImportOrExportDeclaration, VisitorContext } from "../types.ts";
57+
5758
/* ****************************************************************************************************************** */
5859
// region: Utilities
5960
/* ****************************************************************************************************************** */
@@ -104,7 +105,7 @@ export function elideImportOrExportDeclaration(
104105
// Always elide type-only imports
105106
if (node.importClause.isTypeOnly) return undefined;
106107

107-
const importClause = visitNode(node.importClause, <Visitor>visitImportClause);
108+
const importClause = visitNode(node.importClause, visitImportClause as Visitor);
108109

109110
if (
110111
importClause ||
@@ -139,7 +140,7 @@ export function elideImportOrExportDeclaration(
139140

140141
const exportClause = visitNode(
141142
node.exportClause,
142-
<Visitor>((bindings: NamedExportBindings) => visitNamedExportBindings(bindings, allowEmpty)),
143+
((bindings: NamedExportBindings) => visitNamedExportBindings(bindings, allowEmpty)) as Visitor,
143144
isNamedExportBindings,
144145
);
145146

@@ -171,7 +172,7 @@ export function elideImportOrExportDeclaration(
171172
function visitImportClause(node: ImportClause): VisitResult<ImportClause> {
172173
// Elide the import clause if we elide both its name and its named bindings.
173174
const name = shouldEmitAliasDeclaration(node) ? node.name : undefined;
174-
const namedBindings = visitNode(node.namedBindings, <Visitor>visitNamedImportBindings, isNamedImportBindings);
175+
const namedBindings = visitNode(node.namedBindings, visitNamedImportBindings as Visitor, isNamedImportBindings);
175176
return name || namedBindings
176177
? factory.updateImportClause(node, /*isTypeOnly*/ false, name, namedBindings)
177178
: undefined;
@@ -194,7 +195,7 @@ export function elideImportOrExportDeclaration(
194195
(compilerOptions.importsNotUsedAsValues === ImportsNotUsedAsValues.Preserve ||
195196
compilerOptions.importsNotUsedAsValues === ImportsNotUsedAsValues.Error));
196197

197-
const elements = visitNodes(node.elements, <Visitor>visitImportSpecifier, isImportSpecifier);
198+
const elements = visitNodes(node.elements, visitImportSpecifier as Visitor, isImportSpecifier);
198199
return allowEmpty || tsInstance.some(elements) ? factory.updateNamedImports(node, elements) : undefined;
199200
}
200201
}
@@ -212,7 +213,7 @@ export function elideImportOrExportDeclaration(
212213
/** Visits named exports, eliding it if it does not contain an export specifier that resolves to a value. */
213214
function visitNamedExports(node: NamedExports, allowEmpty: boolean): VisitResult<NamedExports> | undefined {
214215
// Elide the named exports if all of its export specifiers were elided.
215-
const elements = visitNodes(node.elements, <Visitor>visitExportSpecifier, isExportSpecifier);
216+
const elements = visitNodes(node.elements, visitExportSpecifier as Visitor, isExportSpecifier);
216217
return allowEmpty || tsInstance.some(elements) ? factory.updateNamedExports(node, elements) : undefined;
217218
}
218219

src/utils/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
export * from "./general-utils";
2-
export * from "./elide-import-export";
3-
export * from "./resolve-path-update-node";
1+
export * from "./general-utils.ts";
2+
export * from "./elide-import-export.ts";
3+
export * from "./resolve-path-update-node.ts";

src/utils/resolve-module-name.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
import { VisitorContext } from "../types";
2-
import { isBaseDir, isURL, maybeAddRelativeLocalPrefix } from "./general-utils";
31
import * as path from "node:path";
4-
import { removeFileExtension, removeSuffix, ResolvedModuleFull, SourceFile } from "typescript";
5-
import { getOutputDirForSourceFile } from "./ts-helpers";
6-
import { getRelativePath } from "./get-relative-path";
2+
3+
import { removeFileExtension, removeSuffix, type ResolvedModuleFull, type SourceFile } from "typescript";
4+
5+
import { type VisitorContext } from "../types.ts";
6+
import { isBaseDir, isURL, maybeAddRelativeLocalPrefix } from "./general-utils.ts";
7+
import { getRelativePath } from "./get-relative-path.ts";
8+
import { getOutputDirForSourceFile } from "./ts-helpers.ts";
79

810
export interface ResolvedModule {
911
/** Absolute path to resolved module */
@@ -14,12 +16,12 @@ export interface ResolvedModule {
1416
isURL: boolean;
1517
}
1618

17-
enum IndexType {
18-
NonIndex,
19-
Explicit,
20-
Implicit,
21-
ImplicitPackage,
22-
}
19+
const IndexType = {
20+
NonIndex: 0,
21+
Explicit: 1,
22+
Implicit: 2,
23+
ImplicitPackage: 3,
24+
};
2325

2426
function getPathDetail(moduleName: string, resolvedModule: ResolvedModuleFull) {
2527
const resolvedFileName = resolvedModule.originalPath ?? resolvedModule.resolvedFileName;

src/utils/resolve-path-update-node.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import ts from "typescript";
2-
import { VisitorContext } from "../types";
3-
import { isURL, maybeAddRelativeLocalPrefix } from "./general-utils";
4-
import { isModulePathsMatch } from "./ts-helpers";
5-
import { resolveModuleName } from "./resolve-module-name";
2+
3+
import { type VisitorContext } from "../types.ts";
4+
import { isURL, maybeAddRelativeLocalPrefix } from "./general-utils.ts";
5+
import { resolveModuleName } from "./resolve-module-name.ts";
6+
import { isModulePathsMatch } from "./ts-helpers.ts";
67

78
/** Gets proper path and calls updaterFn to get the new node if it should be updated */
89
export function resolvePathAndUpdateNode(

src/utils/ts-helpers.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
import ts, { GetCanonicalFileName, SourceFile } from "typescript";
21
import path from "node:path";
3-
import { VisitorContext } from "../types";
2+
43
import type { REGISTER_INSTANCE } from "ts-node";
4+
import ts, { type GetCanonicalFileName, type SourceFile } from "typescript";
5+
6+
import { type VisitorContext } from "../types.ts";
57

68
/* ****************************************************************************************************************** */
79
// region: TS Helpers

src/visitor.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import ts from "typescript";
2-
import { VisitorContext } from "./types";
3-
import { elideImportOrExportDeclaration, resolvePathAndUpdateNode } from "./utils";
2+
3+
import { type VisitorContext } from "./types.ts";
4+
import { elideImportOrExportDeclaration, resolvePathAndUpdateNode } from "./utils/index.ts";
45

56
const isAsyncImport = ({ tsInstance }: VisitorContext, node: ts.Node): node is ts.CallExpression =>
67
tsInstance.isCallExpression(node) &&
@@ -29,7 +30,7 @@ export function nodeVisitor(this: VisitorContext, node: ts.Node): ts.Node | unde
2930
* import("module");
3031
*/
3132
if (isRequire(this, node) || isAsyncImport(this, node))
32-
return resolvePathAndUpdateNode(this, node, (<ts.StringLiteral>node.arguments[0]).text, (p) => {
33+
return resolvePathAndUpdateNode(this, node, (node.arguments[0] as ts.StringLiteral).text, (p) => {
3334
const res = factory.updateCallExpression(node, node.expression, node.typeArguments, [p]);
3435

3536
/* Handle comments */

0 commit comments

Comments
 (0)