tsup: 打包工具以及遇到的坑

一个号称很快的打包工具
更新于: 2023-10-31 18:04:18

安装

yarn add tsup --dev

踩坑

  • 这个坑影响到了 tsup,但实际这个是 esbuild 的一个BUG

在使用 tsup 的时候,如果没有加 splitting:true 这个参数,默认导出的 export 里,会带一个.default,无法直接使用。

有些情况,如 vite-pluigin 我们导出的就是一个纯函数,遇到这个参数就很讨厌

我的解决过程,参数这里: https://github.com/afeiship/vite-packages/commit/c1ff1206403a5cbaecf8ac8bb34089f3157666f3

const abc =  () => {
  console.log("hello");
};

// for commonjs es5 require
// if (typeof module !== 'undefined' && module.exports) {
//   module.exports = abc;
// }

export default abc;
import { defineConfig } from 'tsup';

export default defineConfig({
  splitting: true,
  cjsInterop: true,
  entry: ['src/*.ts'],
  format: ['cjs', 'esm' /*'iife' */],
  external: ['clipboardy'],
  dts: true,
  clean: true,
  sourcemap: true,
  outExtension({ format }) {
    return {
      js: `.${format}.js`,
    };
  },
});

参考