我正在使用 TypeScript 和 tsup 构建工具来进行打字稿捆绑。我需要将
components
文件夹内的所有文件夹和文件指定到根目录。
src/
components/
card/
card.tsx
index.ts
carousel/
carousel.tsx
index.ts
...other components...
index.ts
我得到的当前输出目录是这样的:
dist/
components
card
card.d.ts
card.js
index.d.ts
index.js
carousel
carousel.d.ts
carousel.js
index.d.ts
index.js
...
但是为了在导入(tree-shaking)时更加方便,我想将组件文件夹中的所有组件放入
dist
中,如下所示:
dist/
card
card.d.ts
card.js
index.d.ts
index.js
carousel
carousel.d.ts
carousel.js
index.d.ts
index.js
...
这是我的
tsup
配置文件:
export default defineConfig((options: Options) => ({
treeshake: true,
splitting: true,
entry: ['src/**/*.{ts,tsx}'],
format: ['cjs', 'esm'],
dts: true,
...options,
}));
对所有文件夹使用通用路径,例如下面的“glob”包。
检查所有具有 src 的文件夹并获取 'src' 目录及其子目录中的所有 .ts 文件**
import type { Options } from 'tsup'
import * as glob from 'glob'
const entries = glob.sync('**/src/*.ts')
console.log(entries)
const config: Options = {
entry: entries,
dts: true,
sourcemap: true,
minify: 'terser',
outDir: 'dist',
...
}