Skip to content

Commit

Permalink
feat: add --metafile flag, closes #443
Browse files Browse the repository at this point in the history
  • Loading branch information
egoist committed Oct 25, 2021
1 parent 15c82f5 commit 1a0f69e
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
10 changes: 8 additions & 2 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ tsup src/index.ts src/cli.ts
```

This will output `dist/index.js` and `dist/cli.js`.

### Excluding packages

By default tsup bundles all `import`-ed modules but `dependencies` and `peerDependencies` in your `packages.json` are always excluded, you can also use `--external <module>` flag to mark other packages as external.
Expand Down Expand Up @@ -63,7 +64,7 @@ You can use any of these files:
- `tsup.config.json`
- `tsup` property in your `package.json`

> In all the custom files you can export the options either as `tsup`, `default` or `module.exports =`
> In all the custom files you can export the options either as `tsup`, `default` or `module.exports =`
#### TypeScript

Expand Down Expand Up @@ -109,7 +110,6 @@ module.exports = {
}
```


### Generate declaration file

```bash
Expand Down Expand Up @@ -277,6 +277,12 @@ module.exports = {
}
```

### Metafile

Passing `--metafile` flag to tell esbuild to produce some metadata about the build in JSON format. You can feed the output file to analysis tools like [bundle buddy](https://www.bundle-buddy.com/esbuild) to visualize the modules in your bundle and how much space each one takes up.

The file outputs as `metafile-{format}.json`, e.g. `tsup --format cjs,esm` will generate `metafile-cjs.json` and `metafile-esm.json`.

---

For more details:
Expand Down
1 change: 1 addition & 0 deletions src/cli-main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export async function main(options: Options = {}) {
'Supress non-error logs (excluding "onSuccess" process output)'
)
.option('--pure <express>', 'Mark specific expressions as pure')
.option('--metafile', 'Emit esbuild metafile (a JSON file)')
.action(async (files: string[], flags) => {
const { build } = await import('.')
Object.assign(options, {
Expand Down
16 changes: 16 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ export type Options = {
* @see https://esbuild.github.io/api/#inject
*/
inject?: string[]
/**
* Emit esbuild metafile
* @see https://esbuild.github.io/api/#metafile
*/
metafile?: boolean
}

export type NormalizedOptions = MarkRequired<
Expand Down Expand Up @@ -233,6 +238,7 @@ export async function runEsbuild(
keepNames: options.keepNames,
incremental: !!options.watch,
pure: typeof options.pure === 'string' ? [options.pure] : options.pure,
metafile: Boolean(options.metafile),
})
} catch (error) {
log(format, 'error', 'Build failed')
Expand Down Expand Up @@ -328,6 +334,16 @@ export async function runEsbuild(
)
}

if (options.metafile && result?.metafile) {
const outPath = path.resolve(outDir, `metafile-${format}.json`)
await fs.promises.mkdir(path.dirname(outPath), { recursive: true })
await fs.promises.writeFile(
outPath,
JSON.stringify(result.metafile),
'utf8'
)
}

return result
}

Expand Down
16 changes: 16 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1014,3 +1014,19 @@ test('declaration files with multiple entrypoints #316', async () => {
"
`)
})

test('esbuild metafile', async () => {
const { outFiles } = await run(
getTestName(),
{ 'input.ts': `export const foo = 1` },
{
flags: ['--metafile'],
}
)
expect(outFiles).toMatchInlineSnapshot(`
Array [
"input.js",
"metafile-cjs.json",
]
`)
})

1 comment on commit 1a0f69e

@vercel
Copy link

@vercel vercel bot commented on 1a0f69e Oct 25, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.