@tsbb/babel
Version:
TSBB is a zero-config CLI that helps you develop, test, and publish modern TypeScript project.
36 lines (35 loc) • 1.83 kB
JavaScript
import path from 'node:path';
import { getRootsFolderName } from '@tsbb/typescript';
/**
* Convert suffix
*/
export const convertExtname = (str = '') => {
return str.replace(/\.(m?ts|m?js|jsx?|tsx?|c?js)(?<!\.d\.ts)$/i, '.js');
};
/**
* @param fileName `/examples/react-component/src/demo.tsx`
* @param options
* @returns {OutputPathResult}
*/
export function getOutputPath(fileName, options = {}) {
const { entry = [], cjs = 'lib', esm = 'esm' } = options;
const result = { esm: {}, cjs: {} };
result.folderList = [...new Set(getRootsFolderName(entry))];
result.projectDirectory = process.cwd();
result.folderFilePath = path.relative(result.projectDirectory, fileName);
result.filePathInProjectDirectory = result.folderFilePath
.replace(result.folderList.find((m) => result.folderFilePath.startsWith(m)) || '', '')
.replace(path.sep, '');
const filePath = result.folderList.length === 1 ? result.filePathInProjectDirectory : result.folderFilePath;
result.cjs.path = path.resolve(result.projectDirectory, typeof cjs !== 'boolean' ? cjs : '', filePath);
result.esm.path = path.resolve(result.projectDirectory, typeof esm !== 'boolean' ? esm : '', filePath);
result.cjs.fileName = convertExtname(path.join(cjs || '', result.filePathInProjectDirectory));
result.esm.fileName = convertExtname(path.join(esm || '', result.filePathInProjectDirectory));
result.cjs.path = convertExtname(result.cjs.path);
result.esm.path = convertExtname(result.esm.path);
result.esm.ts = result.esm.path.replace(/\.js$/i, '.d.ts');
result.cjs.ts = result.cjs.path.replace(/\.js$/i, '.d.ts');
result.esm.tsFileName = result.esm.fileName.replace(/\.js$/i, '.d.ts');
result.cjs.tsFileName = result.cjs.fileName.replace(/\.js$/i, '.d.ts');
return result;
}