Created
December 2, 2023 13:38
-
-
Save ixahmedxi/972ebb37c06f2f81513e1834a9457593 to your computer and use it in GitHub Desktop.
tsup multi entrypoint
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | |
}, | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
you can just do
entry: ['./src/**/*.ts']
and specify yourexports
inpackage.json