@wessberg/cjs-to-esm-transformer
Version:
A Custom Transformer for Typescript that transforms Node-style CommonJS to tree-shakeable ES Modules
233 lines • 12.1 kB
TypeScript
import { CjsToEsmOptions } from "./cjs-to-esm-options-e8306e53";
import { TS } from "./type-8c3839f0"; /**
* A TransformerFactory that converts CommonJS to tree-shakeable ESM
*
* @param [options]
* @returns
*/
import { TaskOptions } from "./task-options-702cf0be";
/**
* A TransformerFactory that converts CommonJS to tree-shakeable ESM
*
* @param [options]
* @returns
*/
declare function cjsToEsmTransformerFactory({ fileExists, readFile, debug, typescript, ...rest }?: CjsToEsmOptions): TS.TransformerFactory<TS.SourceFile>;
interface VisitorContext extends Required<CjsToEsmOptions> {
onlyExports: boolean;
printer?: TS.Printer;
}
interface BeforeTransformerOptions {
baseVisitorContext: VisitorContext;
}
/**
* @param options
* @return
*/
declare function beforeTransformer(options: BeforeTransformerOptions): TS.TransformerFactory<TS.SourceFile>;
interface ModuleExports {
namedExports: Set<string>;
hasDefaultExport: boolean;
}
interface BeforeTransformerSourceFileStepResult {
sourceFile: TS.SourceFile;
exports: ModuleExports;
}
declare function transformSourceFile(sourceFile: TS.SourceFile, { baseVisitorContext }: BeforeTransformerOptions, context: TS.TransformationContext): BeforeTransformerSourceFileStepResult;
type VisitorContinuation<T extends TS.Node> = (node: T) => TS.VisitResult<T>;
interface VisitorOptions<T extends TS.Node> {
node: T;
sourceFile: TS.SourceFile;
context: VisitorContext;
continuation: VisitorContinuation<TS.Node>;
childContinuation: VisitorContinuation<TS.Node>;
}
interface BeforeVisitorContext extends VisitorContext {
transformationContext: TS.TransformationContext;
exportsName: string | undefined;
getModuleExportsForPath(path: string): ModuleExports | undefined;
addModuleExportsForPath(path: string, exports: ModuleExports): void;
markLocalAsExported(local: string): void;
markDefaultAsExported(): void;
isLocalExported(local: string): boolean;
addImport(declaration: TS.ImportDeclaration, noEmit?: boolean): void;
isModuleSpecifierImportedWithoutLocals(moduleSpecifier: string): boolean;
getImportDeclarationWithModuleSpecifier(moduleSpecifier: string): TS.ImportDeclaration | undefined;
getLocalForDefaultImportFromModule(moduleSpecifier: string): string | undefined;
hasLocalForDefaultImportFromModule(moduleSpecifier: string): boolean;
getLocalForNamespaceImportFromModule(moduleSpecifier: string): string | undefined;
hasLocalForNamespaceImportFromModule(moduleSpecifier: string): boolean;
getLocalForNamedImportPropertyNameFromModule(propertyName: string, moduleSpecifier: string): string | undefined;
hasLocalForNamedImportPropertyNameFromModule(propertyName: string, moduleSpecifier: string): boolean;
addLeadingStatements(...statements: TS.Statement[]): void;
addTrailingStatements(...statements: TS.Statement[]): void;
getFreeIdentifier(candidate: string, force?: boolean): string;
ignoreIdentifier(identifier: string): boolean;
isIdentifierFree(identifier: string): boolean;
readonly imports: TS.ImportDeclaration[];
readonly leadingStatements: TS.Statement[];
readonly trailingStatements: TS.Statement[];
readonly isDefaultExported: boolean;
readonly exportedLocals: Set<string>;
}
interface BeforeVisitorOptions<T extends TS.Node> extends VisitorOptions<T> {
context: BeforeVisitorContext;
}
/**
* Visits the given Node
*
* @param options
* @returns
*/
declare function visitImportAndExportDeclarations<T extends TS.Node>(options: BeforeVisitorOptions<T>): TS.VisitResult<TS.Node>;
declare function hasDefaultExportModifier(node: TS.Node, typescript: typeof TS): boolean;
/**
* Visits the given ExportAssignment
*
* @param options
* @returns
*/
declare function visitExportAssignment({ context }: BeforeVisitorOptions<TS.ExportAssignment>): TS.VisitResult<TS.Node>;
/**
* Visits the given ExportDeclaration
*/
declare function visitExportDeclaration({ node, context }: BeforeVisitorOptions<TS.ExportDeclaration>): TS.VisitResult<TS.Node>;
/**
* Visits the given ImportDeclaration
*
* @param options
* @returns
*/
declare function visitImportDeclaration({ node, context }: BeforeVisitorOptions<TS.ImportDeclaration>): TS.VisitResult<TS.Node>;
/**
* Returns true if the given Node contains an empty child
*/
declare function shouldSkipEmit(node: TS.VisitResult<TS.Node>, typescript: typeof TS): boolean;
/**
* Visits the given Node
*/
declare function visitNode<T extends TS.Node>(options: BeforeVisitorOptions<T>): TS.VisitResult<TS.Node>;
declare function getBestBodyInScope({ node, context }: BeforeVisitorOptions<TS.Node>): TS.Node | undefined;
/**
* Visits the given VariableDeclarationList
*/
declare function visitVariableDeclarationList({ node, childContinuation, context }: BeforeVisitorOptions<TS.VariableDeclarationList>): TS.VisitResult<TS.Node>;
declare function isNotEmittedStatement(node: TS.Node, typescript: typeof TS): node is TS.NotEmittedStatement;
/**
* Visits the given VariableDeclaration
*/
declare function visitVariableDeclaration({ node, childContinuation, sourceFile, context }: BeforeVisitorOptions<TS.VariableDeclaration>): TS.VisitResult<TS.Node>;
declare function hasExportModifier(node: TS.Node, typescript: typeof TS): boolean;
declare function willReassignIdentifier(identifier: string, node: TS.Node, typescript: typeof TS): boolean;
/**
* Visits the given BinaryExpression
*/
declare function visitBinaryExpression({ node, sourceFile, context, continuation }: BeforeVisitorOptions<TS.BinaryExpression>): TS.VisitResult<TS.Node>;
declare function getLocalsForBindingName(name: TS.BindingName, typescript: typeof TS): string[];
/**
* Returns true if the given Node is an Expression.
* Uses an internal non-exposed Typescript helper to decide whether or not the Node is an Expression
*/
declare function isExpression(node: TS.Node, typescript: typeof TS): node is TS.Expression;
declare function addExportModifier<T extends TS.ModifiersArray>(modifiers: T | undefined, typescript: typeof TS): T extends TS.ModifiersArray ? TS.ModifiersArray : undefined;
declare function nodeContainsSuper<T extends TS.Node>(node: T, typescript: typeof TS): boolean;
declare function ensureNodeHasExportModifier<T extends TS.NamedDeclaration>(node: T, context: BeforeVisitorContext): T;
declare function isNamedDeclaration(node: TS.Node | TS.NamedDeclaration, typescript: typeof TS): node is TS.NamedDeclaration;
interface ExportsData {
property: string;
}
declare function getExportsData(expression: TS.Expression, exportsName: string | undefined, typescript: typeof TS): Partial<ExportsData> | undefined;
/**
* Visits the given CallExpression
*
* @param options
* @returns
*/
declare function visitCallExpression({ node, childContinuation, sourceFile, context }: BeforeVisitorOptions<TS.CallExpression>): TS.VisitResult<TS.Node>;
declare function shouldDebug(debug: CjsToEsmOptions["debug"], sourceFile?: TS.SourceFile): boolean;
interface IsRequireCallNoMatchResult {
match: false;
}
interface IsRequireCallMatchResultWithNoResolvedModuleSpecifier {
match: true;
moduleSpecifier: string | undefined;
resolvedModuleSpecifier: undefined;
resolvedModuleSpecifierText: undefined;
}
interface IsRequireCallMatchResultWithResolvedModuleSpecifier {
match: true;
moduleSpecifier: string;
resolvedModuleSpecifier: string;
resolvedModuleSpecifierText: string;
}
type IsRequireCallResult = IsRequireCallNoMatchResult | IsRequireCallMatchResultWithNoResolvedModuleSpecifier | IsRequireCallMatchResultWithResolvedModuleSpecifier; /**
* Checks if the CallExpression represents a require call (e.g.: 'require(...)')
*/
/**
* Checks if the CallExpression represents a require call (e.g.: 'require(...)')
*/
/**
* Checks if the CallExpression represents a require call (e.g.: 'require(...)')
*/
/**
* Tries to get or potentially parse module exports based on the given data in the given context
* @param data
* @param context
*/
declare function getModuleExportsFromRequireDataInContext(data: IsRequireCallResult, context: BeforeVisitorContext): ModuleExports | undefined;
/**
* Generates a proper name based on the given module specifier
*
* @param moduleSpecifier
* @return
*/
declare function generateNameFromModuleSpecifier(moduleSpecifier: string): string;
/**
* Returns true if the given Node is a Statement is a Declaration
*/
declare function isStatementOrDeclaration(node: TS.Node, typescript: typeof TS): node is TS.Statement | TS.Declaration;
/**
* Returns true if the given Node is a Declaration
* Uses an internal non-exposed Typescript helper to decide whether or not the Node is an Expression
*/
declare function isDeclaration(node: TS.Node, typescript: typeof TS): node is TS.Declaration;
/**
* Returns true if the given Node is a Statement
* Uses an internal non-exposed Typescript helper to decide whether or not the Node is an Expression
*/
declare function isStatement(node: TS.Node, typescript: typeof TS): node is TS.Statement;
declare function findNodeUp<T extends TS.Node>(from: TS.Node, nodeCb: (node: TS.Node) => node is T, breakWhen?: (node: TS.Node) => boolean): T | undefined;
declare function findNodeUp<T extends TS.Node>(from: TS.Node, nodeCb: (node: TS.Node) => boolean, breakWhen?: (node: TS.Node) => boolean): T | undefined;
type ElementOf<ArrayType> = ArrayType extends (infer ElementType)[] ? ElementType : ArrayType extends readonly (infer ReadonlyElementType)[] ? ReadonlyElementType : ArrayType extends Set<infer SetElementType> ? SetElementType : never;
declare const BUILT_IN_MODULE: Set<"module" | "assert" | "async_hooks" | "buffer" | "child_process" | "cluster" | "console" | "constants" | "crypto" | "dgram" | "dns" | "domain" | "events" | "fs" | "fs/promises" | "http" | "http2" | "https" | "inspector" | "net" | "os" | "path" | "perf_hooks" | "process" | "punycode" | "querystring" | "readline" | "repl" | "stream" | "string_decoder" | "timers" | "tls" | "trace_events" | "tty" | "url" | "util" | "v8" | "vm" | "worker_threads" | "zlib">;
type BuiltInModule = ElementOf<typeof BUILT_IN_MODULE>;
type BuiltInModuleMap = {
[Key in BuiltInModule]: ModuleExports;
};
declare function isBuiltInModule(moduleName: string): moduleName is BuiltInModule;
declare const BUILT_IN_MODULE_MAP: BuiltInModuleMap;
declare function walkThroughFillerNodes(expression: TS.Expression, typescript: typeof TS): TS.Expression;
/**
* Returns true if the given path represents an external library
*
* @param path
* @returns
*/
declare function isExternalLibrary(path: string): boolean;
declare const CONSTANT: {
inspectOptions: {
colors: boolean;
depth: number;
maxArrayLength: number;
};
};
interface TransformTaskOptions extends TaskOptions {
input: string;
outDir: string | undefined;
}
/**
* Executes the 'generate' task
*/
declare function transformTask({ logger, input, outDir, root, fs, typescript }: TransformTaskOptions): Promise<void>;
export { cjsToEsmTransformerFactory, beforeTransformer, BeforeTransformerSourceFileStepResult, transformSourceFile, visitImportAndExportDeclarations, hasDefaultExportModifier, visitExportAssignment, visitExportDeclaration, visitImportDeclaration, shouldSkipEmit, visitNode, getBestBodyInScope, visitVariableDeclarationList, isNotEmittedStatement, visitVariableDeclaration, hasExportModifier, willReassignIdentifier, visitBinaryExpression, getLocalsForBindingName, isExpression, addExportModifier, nodeContainsSuper, ensureNodeHasExportModifier, isNamedDeclaration, ExportsData, getExportsData, visitCallExpression, shouldDebug, getModuleExportsFromRequireDataInContext, generateNameFromModuleSpecifier, isStatementOrDeclaration, isDeclaration, isStatement, findNodeUp, BUILT_IN_MODULE, BuiltInModule, BuiltInModuleMap, isBuiltInModule, BUILT_IN_MODULE_MAP, walkThroughFillerNodes, isExternalLibrary, CONSTANT, TransformTaskOptions, transformTask };
//# sourceMappingURL=transform-task-5c7b7086.d.ts.map