All files / src/components tsup.ts

100% Statements 97/97
90% Branches 9/10
100% Functions 8/8
100% Lines 97/97

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 981x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 4x 4x 4x 4x 4x 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 1x 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x  
import { type Options } from 'tsup';
import { Component } from './component.js';
import { SourceCode } from './source-code.js';
import type { TypeScriptNpmPackage } from '../projects/npm-package.js';
import { merge } from '../utils/merge.js';
 
export type TsUpOptions = Options;
 
export function legacyEntryPoints() {
  return {
    // CJS fall-back for older versions of Node.js
    entrypoint: 'lib/index.cjs',
    // Fall-back for older versions of TypeScript
    entrypointTypes: 'lib/index.d.cts',
  };
}
 
function defaultTsUpOptions(): TsUpOptions {
  return {
    bundle: false,
    clean: true,
    dts: true,
    entry: ['src/**/*.ts'],
    format: ['cjs', 'esm'],
    minify: false,
    outDir: 'lib',
    shims: true,
    sourcemap: true,
    splitting: false,
  };
}
 
export class TsUp extends Component {
  options: TsUpOptions;
 
  constructor(project: TypeScriptNpmPackage, options?: TsUpOptions) {
    super(project);
    this.options = merge(defaultTsUpOptions(), options ?? {});
    this.addNpmPackages();
    this.configurePackageJson();
    this.setCompileTask();
    this.project.package.addField('type', 'module');
    this.project.npmignore?.addPatterns('tsup.config.ts');
  }
 
  private addNpmPackages() {
    this.project.addDevDeps('tsup');
  }
 
  private setCompileTask() {
    const compileTask = this.project.tasks.tryFind('compile');
    if (compileTask) {
      // Reset the default compile task
      compileTask.reset(`npx tsup`);
    }
  }
 
  private configurePackageJson() {
    this.project.package.addField('type', 'module');
    this.project.package.addField('module', 'lib/index.js');
    this.project.package.addField('exports', {
      '.': {
        // ES Modules entrypoint
        import: {
          // Where typescript will look for the types
          types: './lib/index.d.ts',
          // Where Node.js will look
          default: './lib/index.js',
        },
        // CommonJS entrypoint
        require: {
          // Where typescript will look for the types
          types: './lib/index.d.cts',
          // Where Node.js will look
          default: './lib/index.cjs',
        },
      },
    });
  }
 
  preSynthesize(): void {
    new SourceCode(this.project, 'tsup.config.ts', {
      codeBlock: `
        import { defineConfig } from 'tsup';
 
        export function tsup() {
          return defineConfig(
            ${JSON.stringify(this.options, undefined, 2)
              .split('\n')
              .map((line) => `  ${line}`)
              .join('\n')}
          );
        }
      `,
    });
  }
}