@ts-bridge/cli
Version:
Bridge the gap between ES modules and CommonJS modules with an easy-to-use alternative to `tsc`.
115 lines • 4.06 kB
TypeScript
import type { Statement } from 'typescript';
import typescript from 'typescript';
/**
* Get the AST for the `fileURLToPath` function, i.e.:
*
* ```ts
* function fileURLToPath(fileUrl: string) {
* const url = new URL(fileUrl);
* return url.pathname.replace(/^\/([a-zA-Z]:)/u, '$1');
* }
* ```
*
* This function is a simplified version of the `fileURLToPath` function in
* Node.js and does not handle edge cases like file URLs that contain invalid
* characters. It is assumed that the input is always a valid file URL.
*
* This is used to avoid the need for polyfills in browser environment.
*
* @param functionName - The name of the function to create.
* @returns The AST for the `fileURLToPath` function.
*/
export declare function getFileUrlToPathHelperFunction(functionName: string): typescript.FunctionDeclaration;
/**
* Get the AST for the `dirname` function, i.e.:
*
* ```ts
* function dirname(path: string) {
* const sanitisedPath = path
* .toString()
* .replace(/\\/gu, '/')
* .replace(/\/$/u, '');
*
* const index = sanitisedPath.lastIndexOf('/');
* if (index === -1) {
* return path;
* }
*
* if (index === 0) {
* return '/';
* }
*
* return sanitisedPath.slice(0, index);
* }
* ```
*
* This function is a simplified version of the `dirname` function in Node.js.
* It does not handle edge cases like paths that end with multiple slashes or
* paths that contain invalid characters. It is assumed that the input is always
* a valid path.
*
* This is used to avoid the need for polyfills in browser environment.
*
* @param functionName - The name of the function to create.
* @returns The AST for the `dirname` function.
*/
export declare function getDirnameHelperFunction(functionName: string): typescript.FunctionDeclaration;
/**
* Get the AST for the `__dirname` global function, i.e.:
*
* ```ts
* function __dirname(url: string): string {
* return dirname(fileUrlToPath(url));
* }
* ```
*
* This function returns the directory name of the current module, i.e.,
* `__dirname`, but for ESM.
*
* @param functionName - The name of the function to create.
* @param fileUrlToPathFunctionName - The name of the function that converts a
* file URL to a path.
* @param dirnameFunctionName - The name of the function that gets the directory
* name of a path.
* @returns The AST for the `__dirname` global function.
*/
export declare function getDirnameGlobalFunction(functionName: string, fileUrlToPathFunctionName: string, dirnameFunctionName: string): typescript.FunctionDeclaration;
/**
* Get the AST for the `getImportMetaUrl` function, i.e.:
*
* ```ts
* function getImportMetaUrl(fileName: string): string {
* return typeof document === 'undefined'
* ? new URL(`file:${fileName}`).href
* : document.currentScript?.src ?? new URL('main.js', document.baseURI).href;
* }
* ```
*
* If the current environment is a browser, it will return the URL of the
* current script (if it's available). Otherwise, it will return the URL of the
* current file.
*
* @param functionName - The name of the function to create.
* @returns The AST for the `getImportMetaUrl` function.
*/
export declare function getImportMetaUrlFunction(functionName: string): typescript.FunctionDeclaration;
/**
* Get the AST for the `require` function, i.e.:
*
* ```ts
* import { createRequire as $createRequire } from 'module';
*
* const $require = $createRequire(import.meta.url);
* ```
*
* This is a shim for Node.js's `require` function, and is intended to be used
* in ESM modules. Note that this function cannot be used to import ESM modules,
* only CJS modules.
*
* @param functionName - The name of the require function to create.
* @param createRequireFunctionName - The name of the `createRequire` function
* to import.
* @returns The AST for the `require` function.
*/
export declare function getRequireHelperFunction(functionName: string, createRequireFunctionName: string): [Statement, Statement];
//# sourceMappingURL=shims.d.ts.map