@chialab/cjs-to-esm
Version:
A commonjs to esm converter.
68 lines (67 loc) • 4.86 kB
TypeScript
/**
* Create an ESM module that exports the helper with an empty sourcemap.
*/
export function createRequireHelperModule(): string;
/**
* Check if there are chanches that the provided code is a commonjs module.
* @param {string} code
*/
export function maybeCommonjsModule(code: string): Promise<boolean>;
/**
* Check if there are chanches that the provided code is both a esm and commonjs module.
* @param {string} code
*/
export function maybeMixedModule(code: string): Promise<boolean>;
/**
* @typedef {(specifier: string) => boolean|Promise<boolean>} IgnoreCallback
*/
/**
* @typedef {Object} TransformOptions
* @property {boolean} [sourcemap]
* @property {string} [source]
* @property {boolean} [sourcesContent]
* @property {IgnoreCallback} [ignore]
* @property {boolean} [helperModule]
* @property {boolean} [ignoreTryCatch]
*/
/**
* @param {string} code
* @param {TransformOptions|undefined} options
*/
export function transform(code: string, { sourcemap, source, sourcesContent, ignore, helperModule, ignoreTryCatch }?: TransformOptions | undefined): Promise<{
code: string;
map: any;
} | undefined>;
/**
* Wrap with a try catch block any require call.
* @param {string} code
* @param {{ sourcemap?: boolean, source?: string; sourcesContent?: boolean }} options
*/
export function wrapDynamicRequire(code: string, { sourcemap, source, sourcesContent }?: {
sourcemap?: boolean;
source?: string;
sourcesContent?: boolean;
}): Promise<{
code: string;
map: any;
} | undefined>;
export const REQUIRE_REGEX: RegExp;
export const UMD_REGEXES: RegExp[];
export const UMD_GLOBALS: string[];
export const ESM_KEYWORDS: RegExp;
export const EXPORTS_KEYWORDS: RegExp;
export const CJS_KEYWORDS: RegExp;
export const THIS_PARAM: RegExp;
export const REQUIRE_FUNCTION: "__cjs_default__";
export const HELPER_MODULE: "__cjs_helper__.js";
export const GLOBAL_HELPER: "((typeof window !== 'undefined' && window) ||\n(typeof self !== 'undefined' && self) ||\n(typeof global !== 'undefined' && global) ||\n(typeof globalThis !== 'undefined' && globalThis) ||\n{})";
export const REQUIRE_HELPER: "function __cjs_default__(requiredModule) {\n var Object = ((typeof window !== 'undefined' && window) ||\n(typeof self !== 'undefined' && self) ||\n(typeof global !== 'undefined' && global) ||\n(typeof globalThis !== 'undefined' && globalThis) ||\n{}).Object;\n var isEsModule = false;\n var specifiers = Object.create(null);\n var hasNamedExports = false;\n var hasDefaultExport = false;\n\n Object.defineProperty(specifiers, '__esModule', {\n value: true,\n enumerable: false,\n configurable: true,\n });\n\n if (requiredModule) {\n var names = Object.getOwnPropertyNames(requiredModule);;\n names.forEach(function(k) {\n if (k === 'default') {\n hasDefaultExport = true;\n } else if (!hasNamedExports && k != '__esModule') {\n try {\n hasNamedExports = requiredModule[k] != null;\n } catch (err) {\n //\n }\n }\n Object.defineProperty(specifiers, k, {\n get: function () {\n return requiredModule[k];\n },\n enumerable: true,\n configurable: false,\n });\n });\n if (Object.getOwnPropertySymbols) {\n var symbols = Object.getOwnPropertySymbols(requiredModule);\n symbols.forEach(function(k) {\n Object.defineProperty(specifiers, k, {\n get: function () {\n return requiredModule[k];\n },\n enumerable: false,\n configurable: false,\n });\n });\n }\n\n Object.preventExtensions(specifiers);\n Object.seal(specifiers);\n if (Object.freeze) {\n Object.freeze(specifiers);\n }\n }\n\n if (hasNamedExports) {\n return specifiers;\n }\n\n if (hasDefaultExport) {\n if (Object.isExtensible(specifiers.default) && !('default' in specifiers.default)) {\n Object.defineProperty(specifiers.default, 'default', {\n value: specifiers.default,\n configurable: false,\n enumerable: false,\n })\n }\n return specifiers.default;\n }\n\n return specifiers;\n}\n";
export type IgnoreCallback = (specifier: string) => boolean | Promise<boolean>;
export type TransformOptions = {
sourcemap?: boolean | undefined;
source?: string | undefined;
sourcesContent?: boolean | undefined;
ignore?: IgnoreCallback | undefined;
helperModule?: boolean | undefined;
ignoreTryCatch?: boolean | undefined;
};