Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: egoist/tsup
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v7.1.0
Choose a base ref
...
head repository: egoist/tsup
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v7.2.0
Choose a head ref
  • 6 commits
  • 10 files changed
  • 2 contributors

Commits on Jun 25, 2023

  1. Copy the full SHA
    612cabf View commit details
  2. chore: tweak cli help

    egoist committed Jun 25, 2023
    Copy the full SHA
    87b80ca View commit details
  3. chore(deps): update tsup

    egoist committed Jun 25, 2023
    Copy the full SHA
    742afab View commit details

Commits on Jul 17, 2023

  1. Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    692c112 View commit details

Commits on Aug 2, 2023

  1. Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    d870f4e View commit details
  2. 1

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    ec83015 View commit details
Showing with 109 additions and 351 deletions.
  1. +10 −6 docs/README.md
  2. +2 −2 package.json
  3. +26 −322 pnpm-lock.yaml
  4. +5 −0 src/cli-main.ts
  5. +10 −10 src/index.ts
  6. +9 −1 src/options.ts
  7. +26 −0 src/plugins/cjs-interop.ts
  8. +20 −8 src/rollup.ts
  9. +1 −1 tsup.config.ts
  10. +0 −1 types.d.ts
16 changes: 10 additions & 6 deletions docs/README.md
Original file line number Diff line number Diff line change
@@ -145,16 +145,12 @@ Provide the following configuration in your `.vscode/settings.json` (or global)
"json.schemas": [
{
"url": "https://cdn.jsdelivr.net/npm/tsup/schema.json",
"fileMatch": [
"package.json",
"tsup.config.json"
]
"fileMatch": ["package.json", "tsup.config.json"]
}
]
}
```


### Multiple entrypoints

Beside using positional arguments `tsup [...files]` to specify multiple entrypoints, you can also use the cli flag `--entry`:
@@ -164,7 +160,7 @@ Beside using positional arguments `tsup [...files]` to specify multiple entrypoi
tsup --entry src/a.ts --entry src/b.ts
```

The associated output file names can be defined as follows:
The associated output file names can be defined as follows:

```bash
# Outputs `dist/foo.js` and `dist/bar.js`.
@@ -350,6 +346,14 @@ tsup src/index.ts --env.NODE_ENV production
When an entry file like `src/cli.ts` contains hashbang like `#!/bin/env node` tsup will automatically make the output file executable, so you don't have to run `chmod +x dist/cli.js`.
### Interop with CommonJS
By default, esbuild will transform `export default x` to `module.exports.default = x` in CommonJS, but you can change this behavior by using the `--cjsInterop` flag: If there are only default exports and no named exports, it will be transformed to `module.exports = x` instead.
```bash
tsup src/index.ts --cjsInterop
```
### Watch mode
```bash
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -60,15 +60,15 @@
"prettier": "2.5.1",
"resolve": "1.20.0",
"rollup-plugin-dts": "5.3.0",
"rollup-plugin-hashbang": "2.2.2",
"rollup-plugin-hashbang": "3.0.0",
"sass": "1.62.1",
"strip-json-comments": "4.0.0",
"svelte": "3.46.4",
"svelte-preprocess": "5.0.3",
"terser": "^5.16.0",
"ts-essentials": "9.1.2",
"tsconfig-paths": "3.12.0",
"tsup": "6.6.1",
"tsup": "7.1.0",
"typescript": "5.0.2",
"vitest": "0.28.4",
"wait-for-expect": "3.0.2"
348 changes: 26 additions & 322 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions src/cli-main.ts
Original file line number Diff line number Diff line change
@@ -91,6 +91,11 @@ export async function main(options: Options = {}) {
'Using Rollup for treeshaking instead, "recommended" or "smallest" or "safest"'
)
.option('--publicDir [dir]', 'Copy public directory to output directory')
.option(
'--killSignal <signal>',
'Signal to kill child process, "SIGTERM" or "SIGKILL"'
)
.option('--cjsInterop', 'Enable cjs interop')
.action(async (files: string[], flags) => {
const { build } = await import('.')
Object.assign(options, {
20 changes: 10 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -11,7 +11,7 @@ import execa from 'execa'
import kill from 'tree-kill'
import { version } from '../package.json'
import { createLogger, setSilent } from './log'
import { NormalizedOptions, Format, Options } from './options'
import { NormalizedOptions, Format, Options, KILL_SIGNAL } from './options'
import { runEsbuild } from './esbuild'
import { shebang } from './plugins/shebang'
import { cjsSplitting } from './plugins/cjs-splitting'
@@ -21,6 +21,7 @@ import { sizeReporter } from './plugins/size-reporter'
import { treeShakingPlugin } from './plugins/tree-shaking'
import { copyPublicDir, isInPublicDir } from './lib/public-dir'
import { terserPlugin } from './plugins/terser'
import { cjsInterop } from './plugins/cjs-interop'

export type { Format, Options, NormalizedOptions }

@@ -34,15 +35,12 @@ export const defineConfig = (
) => MaybePromise<Options | Options[]>)
) => options

const killProcess = ({
pid,
signal = 'SIGTERM',
}: {
pid: number
signal?: string | number
}) =>
new Promise<unknown>((resolve) => {
kill(pid, signal, resolve)
const killProcess = ({ pid, signal }: { pid: number; signal: KILL_SIGNAL }) =>
new Promise<void>((resolve, reject) => {
kill(pid, signal, (err) => {
if (err) return reject(err)
resolve()
})
})

const normalizeOptions = async (
@@ -207,6 +205,7 @@ export async function build(_options: Options) {
if (onSuccessProcess) {
await killProcess({
pid: onSuccessProcess.pid,
signal: options.killSignal || 'SIGTERM',
})
} else if (onSuccessCleanup) {
await onSuccessCleanup()
@@ -256,6 +255,7 @@ export async function build(_options: Options) {
silent: options.silent,
}),
cjsSplitting(),
cjsInterop(),
es5(),
sizeReporter(),
terserPlugin({
10 changes: 9 additions & 1 deletion src/options.ts
Original file line number Diff line number Diff line change
@@ -5,6 +5,8 @@ import type { Plugin } from './plugin'
import type { TreeshakingStrategy } from './plugins/tree-shaking'
import type { MinifyOptions } from 'terser'

export type KILL_SIGNAL = 'SIGKILL' | 'SIGTERM'

export type Format = 'cjs' | 'esm' | 'iife'

export type ContextForOutPathGeneration = {
@@ -14,7 +16,7 @@ export type ContextForOutPathGeneration = {
pkgType?: string
}

export type OutExtensionObject = { js?: string, dts?: string }
export type OutExtensionObject = { js?: string; dts?: string }

export type OutExtensionFactory = (
ctx: ContextForOutPathGeneration
@@ -221,6 +223,12 @@ export type Options = {
* Copy the files inside `publicDir` to output directory
*/
publicDir?: string | boolean
killSignal?: KILL_SIGNAL
/**
* Interop default within `module.exports` in cjs
* @default false
*/
cjsInterop?: boolean
}

export type NormalizedOptions = Omit<
26 changes: 26 additions & 0 deletions src/plugins/cjs-interop.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Plugin } from '../plugin'

export const cjsInterop = (): Plugin => {
return {
name: 'cjs-interop',

async renderChunk(code, info) {
if (
!this.options.cjsInterop ||
this.format !== 'cjs' ||
info.type !== 'chunk' ||
!/\.(js|cjs)$/.test(info.path) ||
!info.entryPoint ||
info.exports?.length !== 1 ||
info.exports[0] !== 'default'
) {
return
}

return {
code: code + '\nmodule.exports = exports.default',
map: info.map,
}
},
}
}
28 changes: 20 additions & 8 deletions src/rollup.ts
Original file line number Diff line number Diff line change
@@ -132,12 +132,22 @@ const getRollupConfig = async (
},
}

const fixEnumDeclaration: Plugin = {
name: 'tsup:fix-enum-declaration',
renderChunk(code) {
// make sure enum declaration starts with `declare`
// #834
return code.replace(/^(\s*)enum\s/gm, '$1declare enum ')
const fixCjsExport: Plugin = {
name: 'tsup:fix-cjs-export',
renderChunk(code, info) {
if (
info.type !== 'chunk' ||
!/\.(ts|cts)$/.test(info.fileName) ||
!info.isEntry ||
info.exports?.length !== 1 ||
info.exports[0] !== 'default'
)
return

return code.replace(
/(?<=(?<=[;}]|^)\s*export\s*){\s*([\w$]+)\s*as\s+default\s*}/,
`= $1`
)
},
}

@@ -181,15 +191,14 @@ const getRollupConfig = async (
target: ts.ScriptTarget.ESNext,
},
}),
fixEnumDeclaration,
].filter(Boolean),
external: [
// Exclude dependencies, e.g. `lodash`, `lodash/get`
...deps.map((dep) => new RegExp(`^${dep}($|\\/|\\\\)`)),
...(options.external || []),
],
},
outputConfig: options.format.map((format) => {
outputConfig: options.format.map((format): OutputOptions => {
const outputExtension =
options.outExtension?.({ format, options, pkgType: pkg.type }).dts ||
defaultOutExtension({ format, pkgType: pkg.type }).dts
@@ -200,6 +209,9 @@ const getRollupConfig = async (
banner: dtsOptions.banner,
footer: dtsOptions.footer,
entryFileNames: `[name]${outputExtension}`,
plugins: [
format === 'cjs' && options.cjsInterop && fixCjsExport,
].filter(Boolean),
}
}),
}
2 changes: 1 addition & 1 deletion tsup.config.ts
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@ import { defineConfig } from 'tsup'

export default defineConfig({
name: 'tsup',
target: 'node14',
target: 'node16.14',
dts: {
resolve: true,
// build types for `src/index.ts` only
1 change: 0 additions & 1 deletion types.d.ts

This file was deleted.