这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 21 additions & 20 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,34 +68,35 @@ You can use any of these files:

[Check out all available options](https://github.com/egoist/tsup/blob/master/src/options.ts).

#### TypeScript
#### TypeScript / JavaScript

```ts
// tsup.config.ts
import type { Options } from 'tsup'
export const tsup: Options = {
import { defineConfig } from 'tsup'

export default defineConfig({
splitting: false,
sourcemap: true,
clean: true,
entryPoints: ['src/index.ts'],
}
})
```

#### JavaScript
#### Conditional config

```js
// tsup.config.cjs
/**
* @type {import("tsup").Options}
*/
module.exports = {
splitting: false,
sourcemap: true,
clean: true,
entryPoints: ['src/index.ts'],
}
If the config needs to be conditionally determined based on CLI flags, it can export a function instead:

```ts
import { defineConfig } from 'tsup'

export default defineConfig((options) => {
return {
minify: !options.watch,
}
})
```

The `options` here is derived from CLI flags.

#### package.json

```json
Expand Down Expand Up @@ -290,14 +291,14 @@ The file outputs as `metafile-{format}.json`, e.g. `tsup --format cjs,esm` will
Use `esbuildPlugins` and `esbuildOptions` respectively in `tsup.config.ts`:

```ts
import { Options } from 'tsup'
import { defineConfig } from 'tsup'

export const tsup: Options = {
export default defineConfig({
esbuildPlugins: [YourPlugin],
esbuildOptions(options, context) {
options.define.foo = '"bar"'
},
}
})
```

The `context` argument for `esbuildOptions`:
Expand Down
2 changes: 1 addition & 1 deletion src/cli-main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export async function main(options: Options = {}) {
.option('--clean', 'Clean output directory')
.option(
'--silent',
'Supress non-error logs (excluding "onSuccess" process output)'
'Suppress non-error logs (excluding "onSuccess" process output)'
)
.option('--pure <express>', 'Mark specific expressions as pure')
.option('--metafile', 'Emit esbuild metafile (a JSON file)')
Expand Down
2 changes: 1 addition & 1 deletion src/esbuild/postcss.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export const postcssPlugin = ({
}
}

// Tranform CSS
// Transform CSS
const result = await postcss
?.default(plugins)
.process(contents, { ...options, from: args.path })
Expand Down
18 changes: 14 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,14 @@ const getOutputExtensionMap = (
return map
}

export const defineConfig = (options: Options) => options
export const defineConfig = (
options:
| Options
| ((
/** The options derived from CLI flags */
overrideOptions: Options
) => Options)
) => options

export async function runEsbuild(
options: NormalizedOptions,
Expand Down Expand Up @@ -239,7 +246,7 @@ export async function runEsbuild(
)
}
}
// Workaound to enable code splitting for cjs format
// Workaround to enable code splitting for cjs format
// Manually transform esm to cjs
// TODO: remove this once esbuild supports code splitting for cjs natively
if (splitting && format === 'cjs') {
Expand Down Expand Up @@ -282,7 +289,7 @@ const killProcess = ({
})

const normalizeOptions = async (
optionsFromConfigFile: Options,
optionsFromConfigFile: Options | undefined,
optionsOverride: Options
) => {
const options: Buildable<NormalizedOptions> = {
Expand Down Expand Up @@ -344,7 +351,10 @@ const normalizeOptions = async (
export async function build(_options: Options) {
const config = await loadTsupConfig(process.cwd())

const options = await normalizeOptions(config.data, _options)
const configData =
typeof config.data === 'function' ? config.data(_options) : config.data

const options = await normalizeOptions(configData, _options)

log('CLI', 'info', `tsup v${version}`)

Expand Down
2 changes: 1 addition & 1 deletion src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export type Options = {
esbuildPlugins?: EsbuildPlugin[]
esbuildOptions?: (options: BuildOptions, context: { format: Format }) => void
/**
* Supress non-error logs (excluding "onSuccess" process output)
* Suppress non-error logs (excluding "onSuccess" process output)
*/
silent?: boolean
/**
Expand Down
4 changes: 2 additions & 2 deletions src/rollup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ const findLowestCommonAncestor = (filepaths: string[]) => {
// See #316
const toObjectEntry = (entry: string[]) => {
entry = entry.map((e) => e.replace(/\\/g, '/'))
const ancester = findLowestCommonAncestor(entry)
const ancestor = findLowestCommonAncestor(entry)
return entry.reduce((result, item) => {
const key = item
.replace(ancester, '')
.replace(ancestor, '')
.replace(/^\//, '')
.replace(/\.[a-z]+$/, '')
return {
Expand Down
2 changes: 1 addition & 1 deletion src/rollup/ts-resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export const tsResolvePlugin: PluginImpl<TsResolveOptions> = ({
if (!shouldResolve) return null
}

// Skip absolut path
// Skip absolute path
if (path.isAbsolute(source)) return null

const basedir = importer ? path.dirname(importer) : process.cwd()
Expand Down