Skip to content

Instantly share code, notes, and snippets.

@ixahmedxi
Created December 2, 2023 13:38
Show Gist options
  • Save ixahmedxi/972ebb37c06f2f81513e1834a9457593 to your computer and use it in GitHub Desktop.
Save ixahmedxi/972ebb37c06f2f81513e1834a9457593 to your computer and use it in GitHub Desktop.
tsup multi entrypoint
import fs from 'fs';
import path from 'path';
import { defineConfig } from 'tsup';
// INFO: This is the only place you need to update when adding new entry folders
const entryFolders = ['primitives', 'ui'];
function getAllFilesInDirectory(dirPath: string): string[] {
return fs.readdirSync(dirPath).reduce<string[]>((allFiles, file) => {
const fullPath = path.join(dirPath, file);
if (fs.statSync(fullPath).isDirectory()) {
return allFiles.concat(getAllFilesInDirectory(fullPath));
} else {
return allFiles.concat('./' + fullPath);
}
}, []);
}
export default defineConfig({
entry: entryFolders
.map((folder) => getAllFilesInDirectory(`./src/${folder}`))
.flat(),
external: ['@noodle/styled-system'],
format: ['esm'],
splitting: true,
sourcemap: true,
clean: true,
dts: true,
outDir: 'dist',
onSuccess: async () => {
const packageJsonPath = './package.json';
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8'));
const distFiles = getAllFilesInDirectory('./dist').filter((file) =>
file.endsWith('.js'),
);
packageJson.exports = distFiles.reduce<
Record<string, { import: string; types: string }>
>((exports, file) => {
const key = file.replace('dist/', '').replace('.js', '');
exports[key] = { import: file, types: file.replace('.js', '.d.ts') };
return exports;
}, {});
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2));
},
});
@o-az
Copy link

o-az commented Apr 9, 2024

you can just do entry: ['./src/**/*.ts'] and specify your exports in package.json

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment