UNPKG

@ts-bridge/cli

Version:

Bridge the gap between ES modules and CommonJS modules with an easy-to-use alternative to `tsc`.

52 lines (51 loc) 1.84 kB
import typescript from 'typescript'; import { getDefaultImportTransformer, getRemoveImportAttributeTransformer, getImportAttributeTransformer, getRequireTransformer, getGlobalsTransformer, getImportMetaTransformer, getNamedImportTransformer, getTargetTransformer, } from './transformers.js'; const { ModuleKind } = typescript; export const BUILD_TYPES = { module: { name: 'ES module', extension: '.mjs', declarationExtension: '.d.mts', sourceExtension: '.mts', target: ModuleKind.ESNext, getTransformers: (options) => [ getDefaultImportTransformer(options), getNamedImportTransformer(options), getImportAttributeTransformer({ moduleType: 'json', type: 'json', }, options), getTargetTransformer(ModuleKind.ESNext), ], getShimsTransformers: (options) => [ getGlobalsTransformer(options), getRequireTransformer(options), ], }, commonjs: { name: 'CommonJS module', extension: '.cjs', declarationExtension: '.d.cts', sourceExtension: '.cts', target: ModuleKind.CommonJS, getTransformers: (options) => [ getRemoveImportAttributeTransformer(options), getTargetTransformer(ModuleKind.CommonJS), ], getShimsTransformers: (options) => [getImportMetaTransformer(options)], }, }; /** * Get the options for a build type. * * @param type - The build type to get the options for. * @returns The options for the build type. * @throws If the build type is unknown. */ export function getBuildTypeOptions(type) { const options = BUILD_TYPES[type]; if (!options) { throw new Error(`Unknown build type: "${type}".`); } return options; }